/*
当光照强的时候关闭LED灯 手捂着传感器跑马灯
*/
#include<iocc2530.h>
int count=0;
char output[8];
void Delay(unsigned char m)
{
int i=0,j=0;
for(i=0;i<1000;i++)
for(j=0;j<m;j++);
}
//定义一个函数用来进行adc转换
unsigned short get_adc()
{
APCFG|=1;
P0SEL |=0x01;
P0DIR &=~0x01;
ADCCON3 = 0X90;
ADCCON3=(0X80|0X10|0X00);
ADCIF=0;
while(!ADCIF);
unsigned long value;
value = ADCH;
value = value<<8;
value |=ADCL;
value = value*330;
value = value>>15;
return value;
}
void initUART0()
{
PERCFG|= 0x00;
P0SEL |=0X0C;
U0CSR |=0X80;
U0GCR = 10;
U0BAUD = 216;
U0UCR |= 0X80;
U0CSR |=0X40;
UTX0IF = 0;
URX0IF = 0;
EA = 1;
URX0IE = 1;
}
void initT1()
{
CLKCONCMD &=~0x40;
while(CLKCONSTA &0X40);
CLKCONCMD &=~0X47;
T1CTL |= 0X05;
IEN1 |=0X02;
EA=1;
}
void led()
{
P1 = 0x02;
Delay(100);
P1 =0x01;
Delay(100);
P1=0x10;
Delay(100);
P1=0x08;
Delay(100);
P1=0x10;
Delay(100);
P1 =0x01;
Delay(100);
P1 = 0x02;
Delay(100);
P1 = 0x00;
Delay(100);
}
void UART0SendString(char *str)
{
while(1)
{
if(*str=='\0')
break;
U0DBUF = *str++;
while(!UTX0IF);
UTX0IF = 0;
}
}
int isRun = 0;
#pragma vector = T1_VECTOR
__interrupt void t1()
{
count++;
unsigned short gz_val = get_adc();
if(gz_val<300)
isRun=1;
else
isRun=0;
if(count==30)
{
count = 0;
output[0] = gz_val/100+'0';
output[1] = '.';
output[2] = gz_val/10%10+'0';
output[3] = gz_val%10+'0';
output[4] = 'V';
output[5] = '\r';
output[6] = '\n';
output[7] ='\0';
UART0SendString("光照值:");
UART0SendString(output);
}
T1IF = 0;
}
void main()
{
P1SEL = 0x00;
P1DIR = 0x1B;
P1=0;
unsigned short gz_val = get_adc();
initUART0();
initT1();
while(1)
{
if(isRun==1)
{
led();
isRun=0;
}
}
}