📄 auto.c
字号:
//Target : M48
//Crystal : 1.0000Mhz
//Authror : 侯进振
//Date : 2008.11.4
#include <iom48v.h>
#include <macros.h>
////////////////////////////////////////////////////////////
unsigned char digit[11]={0xC0,0xF9,0x92,0xB0,0xA9,0xA4,0x84,0xF1,0x80,0xA0,0xFF};
float V=0;
unsigned long lux;
////////////////////////////////////////////////////////////
void port_init(void)
{
PORTB = 0x00;
DDRB = 0xFF;
PORTC = 0xE0;
DDRC = 0x17;
PORTD = 0x02;
DDRD = 0xFE;
}
////////////////////////////////////////////////////////////
//ADC initialize
void adc_init(void)
{
ADCSRA = 0x00; //disable adc
ADMUX = 0x00; //select adc ref
ACSR = 0x80;
ADCSRB = 0x00;
}
////////////////////////////////////////////////////////////
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
adc_init();
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
///////////////////////////////////////////////////////////////
void delay(unsigned int n)
{
while(n--);
}
////////////////////////////////////////////////////////////
void adc(void)
{
unsigned char temp1;
unsigned int temp2=0,i;
unsigned long temp3=0;
ADMUX = 0xC3;//选择1.1V基准,通道5
for(i=0;i<64;i++)//数字滤波处理
{
ADCSRA |= 0xC7;//开始转换
delay(5);
temp1 = ADCL;
temp2 = ADCH;
temp2 <<=8;
temp2+= temp1;
temp3=temp2+temp3;
}
temp3=temp3>>6;
V = ((float)(temp3))*1.1/1023.0;
if(V>=1.0)
{
ADMUX = 0x43;//选择5.0V基准,通道5
for(i=0;i<64;i++)//数字滤波处理
{
ADCSRA |= 0xC7;//开始转换
delay(5);
temp1 = ADCL;
temp2 = ADCH;
temp2 <<=8;
temp2+= temp1;
temp3=temp2+temp3;
}
temp3=temp3>>6;
V = ((float)(temp3))*5.0/1023.0;
}
}
///////////////////////////////////////////////////////////////
void display_below(void)
{
unsigned char wei1,wei2,wei3,wei4;
wei1 = lux/1000;
wei2 = lux%1000/100;
wei3 = lux%100/10;
wei4 = lux%10;
if(lux<=10) {wei1 = 10;wei2 = 10;wei3 = 10;}
else if(lux<=100) {wei1 = 10;wei2 = 10;}
else if(lux<=1000) wei1 = 10;
PORTB = digit[wei1];
PORTC&= 0xE8;PORTC|= 0x01;
delay(100);PORTC&= 0xE8;
PORTB = digit[wei2];
PORTC&= 0xE8;PORTC|= 0x02;
delay(100);PORTC&= 0xE8;
PORTB = digit[wei3];
PORTC&= 0xE8;PORTC|= 0x04;
delay(100);PORTC&= 0xE8;
PORTB = digit[wei4];
PORTC&= 0xE8;PORTC|= 0x10;
delay(100);PORTC&= 0xE8;
}
///////////////////////////////////////////////////////////////
void display_high(void)
{
unsigned char wei1,wei2,wei3,wei4;
PORTD&=0xF7;//LED_1 ON
wei1 = lux/10000;
wei2 = lux%10000/1000;
wei3 = lux%1000/100;
wei4 = lux%100/10;
if(lux<=1000) {wei1 = 10;wei2 = 0;wei3 = 0;}
else if(lux<=10000) {wei1 = 10;wei2 = 0;}
else if(lux<=100000) wei1 = 10;
PORTB = digit[wei1];
PORTC&= 0xE8;PORTC|= 0x01;
delay(100);PORTC&= 0xE8;
PORTB = digit[wei2];
PORTB&=0x7F;//加小数点
PORTC&= 0xE8;PORTC|= 0x02;
delay(100);PORTC&= 0xE8;
PORTB = digit[wei3];
PORTC&= 0xE8;PORTC|= 0x04;
delay(100);PORTC&= 0xE8;
PORTB = digit[wei4];
PORTC&= 0xE8;PORTC|= 0x10;
delay(100);PORTC&= 0xE8;
}
////////////////////////////////////////////////////////
void main(void)
{
unsigned char i;
init_devices();
//------------------------------------
//PORTD|=0x18;//LED OFF
//PORTD&=0xF7;//LED_1 ON
//PORTD&=0xEF;//LED_2 ON
//PORTD&=0xFD;//BUZZER ON
//PORTD|=0x02;//BUZZER OFF
while(1)
{
PORTD|=0x18;//LED OFF
adc();
lux = V*12500;
for(i=0;i<100;i++)
{
if(lux<=9999)
display_below();
else
display_high();
}
if((PIND&0x01)==0x01)
{
PORTD&=0xEF;//LED_2 ON
PORTD&=0xFD;//BUZZER ON
delay(10000);
PORTD|=0x02;//BUZZER OFF
while(1)//*冻结显示
{
if(lux<=9999)
display_below();
else
display_high();
if((PIND&0x01)==0x00)
{
PORTD&=0xFD;//BUZZER ON
delay(10000);
PORTD|=0x02;//BUZZER OFF
break;
}
}
}
}
}
///////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -