基于MSP430F5529的智能灯设计

#include 

void IO_Init();
void InitADC();
unsigned int  GetAD(char channel);
void light_Compare(int templight);
int counter=0;                          //中断计数器参数,单位秒

/**
 * main.c
 */

int main(void)
{
    IO_Init();                          //IO初始化
    InitADC();                          //初始化ADC模块

    int voice = 0;                      //缓存声音响度
    int light = 0;                      //缓存环境亮度

    WDTCTL = WDT_ADLY_1000;             //设置中断定时1s
    SFRIE1 |= WDTIE;                    //使能WDT中断          WDTIE=(0x0001)/* WDT Interrupt Enable */
    _EINT();                            // 使能全局中断

    while(1){
        voice = GetAD(0);               //读取声音缓存
        while(voice>3000){               //声音与阈值比较
            counter=0;                  //定时器置零
            do{
                light = GetAD(1);       //亮度读取
                light_Compare(light);
                voice = GetAD(0);       //读取声音缓存
                if(voice>3000){
                    break;
                }
                if(counter >=10)        //判断计数时间,如果大于20s,关闭LED,并退出
                {
                    P1OUT &= ~BIT5;
                    P8OUT &= ~BIT1;
                    P3OUT &= ~BIT7;
                    P7OUT &= ~BIT4;
                    P6OUT &= ~BIT3;
                    P6OUT &= ~BIT4;
                    P3OUT &= ~BIT5;
                    break;
                }
            }while(counter<10);
        }
    }
}
void light_Compare(int templight){      //LED发亮的个数与光敏电阻测出亮度成反比
    P2OUT |= BIT5;                      //2.5置高
    P2OUT &= ~BIT4;                     //2.4置低
    P1OUT |= BIT5;                      //打开Nsleep,开大LED灯
    if(templight >=700)
        P8OUT |= BIT1;
    else
        P8OUT &= ~BIT1;
    if(templight >= 1200)
        P3OUT |= BIT7;
    else
        P3OUT &= ~BIT7;
    if(templight >= 1500)
        P7OUT |= BIT4;
    else
        P7OUT &= ~BIT4;
    if(templight >= 2000)
        P6OUT |= BIT3;
    else
        P6OUT &= ~BIT3;
    if(templight >= 2500)
        P6OUT |= BIT4;
    else
        P6OUT &= ~BIT4;
    if(templight >= 3000)
        P3OUT |= BIT5;
    else
        P3OUT &= ~BIT5;
}
void IO_Init(){             //六盏灯初始化,并全置零
    P1DIR|= BIT0;           //六盏灯初始化,并全置零
    P8DIR|= BIT1;
    P3DIR|= BIT7;
    P7DIR|= BIT4;
    P6DIR|= BIT3;
    P6DIR|= BIT4;
    P3DIR|= BIT5;
    P8OUT&=~BIT1;
    P3OUT&=~BIT7;
    P7OUT&=~BIT4;
    P6OUT&=~BIT3;
    P6OUT&=~BIT4;
    P3OUT&=~BIT5;

    P2DIR|= BIT4;           //2.4,2.5初始化
    P2DIR|= BIT5;
    P2OUT|= BIT5;
    P2OUT&= ~BIT4;
    P1DIR|= BIT5;           //大LED灯的驱动的电路开关方向
}
#pragma vector= WDT_VECTOR
__interrupt void watchdog_timer (void)
{
     counter ++;            //计时器加一
     P1OUT^=BIT0;           //信号灯
}
unsigned int  GetAD(char channel)
{
    unsigned int temp = 0;                              //设置变量
    ADC12CTL0 |= ADC12SC;                               //开始采样转换
    temp= ADC12MEM0;                                    //把结果赋给变量
      switch (channel)
      {
          case 0:temp= ADC12MEM0;       break;          //选择通道0,P6.0
            case 1:temp= ADC12MEM1;     break;          //选择通道1,P6.1
            case 2:temp= ADC12MEM2;     break;          //选择通道2,P6.2
            case 3:temp= ADC12MEM3;     break;          //选择通道3,P6.3
            case 4:temp= ADC12MEM4;     break;          //选择通道4,P6.4
            case 5:temp= ADC12MEM5;     break;          //选择通道5,P6.5  连接拨码电位器
            case 6:temp= ADC12MEM6;     break;          //选择通道6,P6.6
            case 12:temp= ADC12MEM12;    break;         //选择通道12,P7.0
            default: ;break;
        }
    return temp;
}
void InitADC()
{
    ADC12CTL0 |= ADC12MSC;                              //自动循环采样转换
    ADC12CTL0 |= ADC12ON;                               //启动ADC12模块
    ADC12CTL1 |= ADC12CONSEQ_3 ;                        //选择序列通道多次循环采样转换
    ADC12CTL1 |= ADC12SHP;                              //采样保持模式
    ADC12CTL1  |= ADC12CSTARTADD_0;

    ADC12MCTL0 |=ADC12INCH_0;                           //通道选择
    ADC12MCTL1 |=ADC12INCH_1;
    ADC12MCTL2 |=ADC12INCH_2;
    ADC12MCTL3 |=ADC12INCH_3;
    ADC12MCTL4 |=ADC12INCH_4;
    ADC12MCTL5 |=ADC12INCH_5+ADC12EOS;

    ADC12CTL0 |= ADC12ENC;
}

 

你可能感兴趣的:(基于MSP430F5529的智能灯设计)