MEGA128单片机实现ADC电压测量液晶显示
- 格式:doc
- 大小:60.50 KB
- 文档页数:4
电路图
熔丝位
#include "iom128v.h"
#define uchar unsigned char
#define uint unsigned int
#define BIT(x) (1 << x)
uint ad;
#define RS_H PORTE |= BIT(0);
#define RS_L PORTE &= ~BIT(0);
#define LCDEN_H PORTE |= BIT(2);
#define LCDEN_L PORTE &= ~BIT(2);
uchar tab[]="0123456789";
void delay(uint x)
{
uint a,b;
for(a=x;a>0;a--)
for(b=80;b>0;b--);
}
void write_com(uchar com)//根据时序图写出来的写指令函数{
//P0=com;
PORTD = com;
//rs=0;
RS_L;
//lcden=0;
LCDEN_L;
delay(10);
//lcden=1;
LCDEN_H;
delay(10);
//lcden=0;
LCDEN_L;
}
void write_date(uchar date)//根据时序图写出来的写数据函数{
//P0=date;
PORTD = date;
//rs=1;
RS_H;
//lcden=0;
LCDEN_L;
delay(10);
//lcden=1;
LCDEN_H;
delay(10);
//lcden=0;
LCDEN_L;
}
void init(void)
{
DDRE |= BIT(0) + BIT(2) +BIT(1);
PORTE &= ~BIT(1);
DDRD = 0XFF;
write_com(0x38);//显示模式指令
delay(10);
write_com(0x0c);//开显示显示光标光标闪烁
delay(10);
write_com(0x06);//读写一个字符地址指针加一
delay(10);
write_com(0x01);//清屏指令
delay(10);
}
uint adc()
{
uint addata;
DDRF&=~BIT(0);
PORTF&=~BIT(0);
ADMUX |= 0x40; //ADC0 进行测量
ADCSRA |= BIT(7);
ADCSRA |= BIT(6); //ADSC 开始转换
ADCSRA |= BIT(5); //ADFR //连续转换模式
while(!(ADCSRA&(BIT(4)))); //ADIF 判断是否转换完成addata=ADCL; //
addata=addata+ADCH*256;//计算最终的数据
return addata;
}
void main()
{
uint value;
init();
DDRD = 0XFF; //PD口输出
DDRE = 0XFF; //PE口输出
DDRF = 0X00; //PF口输入
while(1)
{
ad = adc(); //AD采集数据
value = 5000.0 * ad / 1023; //将采集的具体数值转换为电压值write_com(0x80);
write_date(tab[value/1000]);
write_date('.');
write_date(tab[value%1000/100]);
write_date(tab[value%100/10]);
write_date(tab[value%10]);
write_date('V');
}
}。