本人新手,有一段代码看不懂,求大神注解(这是关于avr单片机所设的电压监测系统)
#include <iom16v.h>#include <macros.h>
#include <math.h>
#define uchar unsigned char
#define uint unsigned int
#define LCD_EN BIT(PC3)
#define LCD_RS BIT(PC2)
uchar title[]={"hello!"};
uchar title1[]={"key:"};
uchar key_flag;
unsigned data_temp;
uchar xians[]="0123456789";
void delay(unsigned int i)
{
unsigned int a, b;
for (a = 1; a < i; a++)
{
for (b = 1; b<2000; b++);
}
}
void write_com(uchar com)
{
PORTD&=~BIT(3);
PORTD&=~BIT(4);
PORTB=com;
PORTD|=BIT(6);
delay(3);
PORTD&=~BIT(6);
}
void write_dat(uchar dat)
{
PORTD|=BIT(3);
PORTD&=~BIT(4);
PORTB=dat;
PORTD|=BIT(6);
delay(3);
PORTD&=~BIT(6);
}
void Uart_Init(void)
{ UCSRA = 0x02;
UCSRB = 0x18;
UCSRC = 0x06;
UBRRH = 0x00;
UBRRL = 12;
}
void Uart_Transmit(unsigned char i)
{ while (!(UCSRA & (1<<UDRE)));
UDR = i;
}
uint mega16_ad()
{
uint addata;
DDRA&=~BIT(PA0);
PORTA&=~BIT(PA0);
ADMUX=0xC0;
ADCSRA=0X80;
ADCSRA|=BIT(ADSC);
while(!(ADCSRA&(BIT(ADIF))));
addata=ADCL;
addata=addata+ADCH*256;
return addata;
}
int main(void)
{
uint ad;
float adc0;
uchar k,j,k1,k2,k3;
DDRD = 0x22;
PORTD = 0xFF;
Uart_Init();
while(1)
{
ad=mega16_ad();
adc0=ad*5.0/1024;
k1=(int)adc0;
k2=(int)((adc0-k1)*10);
k3=(int)((adc0-k1-k2*0.1)*100);
DDRB=0XFF;
DDRD|=BIT(3)|BIT(4)|BIT(6);
PORTD&=~BIT(6);
write_com(0X38);
delay(9);
write_com(0X01);
delay(9);
write_com(0X0C);
delay(9);
write_com(0X06);
delay(9);
if (k1>=4)
PORTD&=~BIT(5);
else
PORTD|=BIT(5);
while(1)
{
write_com(0X80+0X00);
delay(5);
write_dat(xians[k1]);
delay(5);
write_dat(0x2e);
delay(5);
write_dat(xians[k2]);
delay(5);
write_dat(xians[k3]);
delay(5);
write_dat('V');
delay(5);
Uart_Transmit(xians[k1]);
Uart_Transmit(0x2e);
Uart_Transmit(xians[k2]);
Uart_Transmit(xians[k3]);
Uart_Transmit('V');
delay(2);
}
return 0;}
}