利用串行A/D转换器TLC549对输入信号电压源进行采集转换成数字信号给51单片机,经单片机进行数据处理后给1602液晶显示。
误差小于1%.
#include
#include
#define uint unsigned int
#define uchar unsigned char
uchar LCD[6]; //电压数据转换成LCD1602液晶字符显示
uint Volt;
sbit DO= P1^0; //时钟
sbit CS= P1^1; //片选
sbit CLK = P1^2; //数据输出
sbit RS = P2^0; //1602液晶数据/命令选择端
sbit RW = P2^1; //1602液晶读写端
sbit E= P2^2; //1602液晶使能端
// Function Prototypes
void check_busy(void);
void write_command(uchar com);
void write_date(uchar date);
void LCD_init(void);
void string(uchar ad ,uchar *s);
//void lcd_test(void);
void delay(uint);
void Volt_To_LCD1602(void);
void delay_ms(uint xms);
void Data_Conversion(void);
uchar TLC549_ADC(void);
/*******************************************
LCD1602 Driver mapped as IO peripheral
*******************************************/
// Delay
// Delay
void delay(uint j)
{
uchar i = 60;
for(; j》0; j--)
{
while(--i);
i = 59;
while(--i);
i = 60;
}
}
// Test the Busy bit
void check_busy(void)
{
do
{
P0 = 0xff;
E = 0;
RS = 0;
RW = 1;
E = 1;
_nop_();
} while(P0 & 0x80);
E = 0;
}
// Write a command
void write_command(uchar com)
{
check_busy();
E = 0;
RS = 0;
RW = 0;
P0 = com;
E = 1;
_nop_();
E = 0;
delay(1);
}
// Write Data
void write_date(uchar date)
{
check_busy();
E = 0;
RS = 1;
RW = 0;
P0 = date;
E = 1;
_nop_();
E = 0;
delay(1);
}
// Initialize LCD controller
void LCD_init(void)
{
write_command(0x38); // 8-bits, 2 lines, 7x5 dots
write_command(0x0C); // no cursor, no blink, enable display
write_command(0x06); // auto-increment on
write_command(0x01); // clear screen
delay(1);
}
// Display a string
void string(uchar ad, uchar *s)
{
write_command(ad);
while(*s》0)
{
write_date(*s++);
delay(100);
}
}
//****************************************************//
//函数名:delay_ms(uint xms)
//函数功能:ms级延时函数
//***************************************************//
void delay_ms(uint xms)
{
uint x,y;
for(x=xms;x》0;x--)
for(y=110;y》0;y--);
}
//***************************************************//
//函数名称:TLC549_ADC()
//函数功能:读取上一次A/D转换的数据,启动下一次A/D转换
//**************************************************//
uchar TLC549_ADC(void)
{
uchar n, tmp;
CS= 1; //CS置高,片选无效
CLK = 0;
CS= 0; //CS置低,片选有效,同时DO输出高位
_nop_();
_nop_(); //适当延迟时间1.4us Setup Time
for(n = 0; n 《 8; n++) //串行数据移位输入
{
tmp 《《= 1;
tmp |=DO;
CLK = 1; //0.4us
_nop_(); //CLK transition time Max 0.1us
CLK = 0; //0.4us
}
CS = 1; //CS置高,片选无效
for(n = 17; n != 0; n--) _nop_();//Next Coversion需要延迟17us
return (tmp);
}
//****************************************//
//函数名称ata_Conversion()
//函数功能:电压换算
//*****************************************//
void Data_Conversion(void)
{
uchar AD_Data;
AD_Data = TLC549_ADC();
delay_ms(1000);
Volt = 5.0 / 256 * AD_Data*1000;
}
void Volt_To_LCD1602(void)
{
Data_Conversion(); //电压换算并放大1000倍
LCD[0] = Volt/1000; //千位
LCD[1] = ‘。’; //小数点
LCD[2] = Volt/100%10;//百位
LCD[3] = Volt/10%10; //十位
LCD[4] = Volt%10; //个位
LCD[5] = ‘V’; //字符V
write_command(0x80+8);
write_date(0x30+LCD[0]);
write_date(LCD[1]);
write_date(0x30+LCD[2]);
write_date(0x30+LCD[3]);
write_date(0x30+LCD[4]);
write_date(LCD[5]);
}
int main()
{
//uint m;
LCD_init();
delay(100);
write_command(0x01);
write_command(0x80);
string(0x80,“Voltage:”);
delay_ms(2000);
while(1)
{
Volt_To_LCD1602();
}
}
来源;21ic
评论
查看更多