2426 views|0 replies

1662

Posts

0

Resources
The OP
 

Smart Light Design Based on MSP430F5529 [Copy link]

#include <msp430F5529.h>

void IO_Init();
void InitADC();
unsigned int GetAD(char channel);
void light_Compare(int templight);
int counter=0; //Interrupt counter parameters, unit second

/**
* main.c
*/

int main(void)
{
IO_Init(); //IO initialization
InitADC(); //Initialize ADC module

int voice = 0; //Cache sound loudness
int light = 0; //Cache ambient brightness

WDTCTL = WDT_ADLY_1000; //Set interrupt timing to 1s
SFRIE1 |= WDTIE; //Enable WDT interrupt WDTIE=(0x0001)/* WDT Interrupt Enable */
_EINT(); //Enable global interrupt

while(1){
voice = GetAD(0); //Read voice cache
while(voice>3000){ //Compare sound with threshold
counter=0; //Set timer to zero
do{
light = GetAD(1); //Brightness
readinglight_Compare(light);
voice = GetAD(0); //Read voice cacheif
(voice>3000){
break;
}
if(counter >=10) //Judge the counting time, if it is greater than 20s, turn off the LED and exit
{
P1OUT &= ~BIT5;
P8OUT &= ~BIT1;
P3OUT &= ~BIT7;
P7OUT &= ~BIT4;
P6OUT &= ~BIT3;
P6OUT &= ~BIT4;
P3OUT &= ~BIT5;
break;
}
}while(counter<10);
}
}
}
void light_Compare(int templight){ //The number of LEDs that light up is inversely proportional to the brightness measured by the photoresistorP2OUT
|= BIT5; //2.5 sets highP2OUT
&= ~BIT4; //2.4 sets lowP1OUT
|= BIT5; //Turn on Nsleep and turn on the LED lightif
(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(){ //Initialize the six lights and set them all to zero
P1DIR|= BIT0; //Initialize the six lights and set them all to zero
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 initialization
P2DIR|= BIT5 ; P2OUT
|= BIT5;
P2OUT&= ~BIT4;
P1DIR|= BIT5; //The switching direction of the driving circuit of the large LED lamp
}
#pragma vector= WDT_VECTOR
__interrupt void watchdog_timer (void)
{
counter ++; //Timer plus one
P1OUT^=BIT0; //Signal light
}
unsigned int GetAD(char channel)
{
unsigned int temp = 0; //Set variable
ADC12CTL0 |= ADC12SC; //Start sampling conversion
temp= ADC12MEM0; //Assign the result to the variable
switch (channel)
{
case 0:temp= ADC12MEM0; break; //Select channel 0, P6.0
case 1:temp= ADC12MEM1; break; //Select channel 1, P6.1
case 2:temp= ADC12MEM2; break; //Select channel 2, P6.2
case 3:temp= ADC12MEM3; break; //Select channel 3, P6.3
case 4:temp= ADC12MEM4; break; //Select channel 4, P6.4
case 5:temp= ADC12MEM5; break; //Select channel 5, P6.5 to connect the dip potentiometer
case 6:temp= ADC12MEM6; break; //Select channel 6, P6.6
case 12:temp= ADC12MEM12; break; //Select channel 12, P7.0
default: ;break;
}
return temp;
}
void InitADC()
{
ADC12CTL0 |= ADC12MSC; //Automatic cycle sampling and conversion
ADC12CTL0 |= ADC12ON; //Start ADC12 module
ADC12CTL1 |= ADC12CONSEQ_3 ; //Select sequence channel for multiple cycle sampling and conversion
ADC12CTL1 |= ADC12SHP; //Sample and hold mode
ADC12CTL1 |= ADC12CSTARTADD_0;

ADC12MCTL0 |=ADC12INCH_0; //Channel selection
ADC12MCTL1 |=ADC12INCH_1;
ADC12MCTL2 |=ADC12INCH_2;
ADC12MCTL3 |=ADC12INCH_3;
ADC12MCTL4 |=ADC12INCH_4;
ADC12MCTL5 |=ADC12INCH_5+ADC12EOS;

ADC12CTL0 |= ADC12ENC;

}

This post is from Microcontroller MCU
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list