利用串行A/D轉(zhuǎn)換器TLC549對輸入信號電壓源進行采集轉(zhuǎn)換成數(shù)字信號給51單片機,經(jīng)單片機進行數(shù)據(jù)處理后給1602液晶顯示。
誤差小于1%.
#include
#include
#define uint unsigned int
#define uchar unsigned char
uchar LCD[6]; //電壓數(shù)據(jù)轉(zhuǎn)換成LCD1602液晶字符顯示
uint Volt;
sbit DO= P1^0; //時鐘
sbit CS= P1^1; //片選
sbit CLK = P1^2; //數(shù)據(jù)輸出
sbit RS = P2^0; //1602液晶數(shù)據(jù)/命令選擇端
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);
}
}
//****************************************************//
//函數(shù)名:delay_ms(uint xms)
//函數(shù)功能:ms級延時函數(shù)
//***************************************************//
void delay_ms(uint xms)
{
uint x,y;
for(x=xms;x》0;x--)
for(y=110;y》0;y--);
}
//***************************************************//
//函數(shù)名稱:TLC549_ADC()
//函數(shù)功能:讀取上一次A/D轉(zhuǎn)換的數(shù)據(jù),啟動下一次A/D轉(zhuǎn)換
//**************************************************//
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++) //串行數(shù)據(jù)移位輸入
{
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);
}
//****************************************//
//函數(shù)名稱ata_Conversion()
//函數(shù)功能:電壓換算
//*****************************************//
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] = ‘?!? //小數(shù)點
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
評論
查看更多