新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > MSP430單片機AD轉(zhuǎn)換LCD1602&TUBE顯示

MSP430單片機AD轉(zhuǎn)換LCD1602&TUBE顯示

作者: 時間:2016-11-23 來源:網(wǎng)絡(luò) 收藏
#include<msp430x14x.h>
#define uint unsigned int
#define uchar unsigned char
#define ulint unsigned long int
#define RS BIT0;
#define RW BIT1;
#define EN BIT2;
uint Volt0; //設(shè)置電壓變量
ulint Volttem0;
unsigned data0=0,data1=0;
uint ADresult0; //設(shè)置A/D轉(zhuǎn)換結(jié)果
uint a[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//數(shù)碼管不帶小數(shù)點譯碼
uint b[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};//數(shù)碼管帶小數(shù)點譯碼
uchar c[]={"voltage:"};
uchar *fu="V",*dian=".";
uchar num[]={"0123456789"}; //電壓值尋址
int value[4]={0,0,0,0}; //存放電壓 (v/1000)
int counter=0; //計數(shù)分時顯示
void lcd_init(); //初始化LCD
void lcd_wcmd(uchar cmd); //寫LCD指令
void lcd_wdata(uchar data); //寫LCD數(shù)據(jù)
void lcd_pos(uchar pos); //設(shè)置LCD顯示位置
void LED_SH();
void LCD_SH();
void Adcvolt(void) //進行電壓轉(zhuǎn)換時ADC12的初始化
{
ADC12CTL0 &=~ENC; //ENC為低電平,設(shè)置AD控制寄存器
ADC12CTL0 |=ADC12ON+MSC; //打開ADC12,可以進行AD轉(zhuǎn)換,參考電壓3.3V
ADC12CTL1=CSTARTADD_0+CONSEQ_1+SHP;//單通道單次轉(zhuǎn)換,采樣頻率源自采樣定時器
ADC12MCTL0=EOS+INCH_0; //選擇模擬輸入通道1
ADC12IE |=BIT0; //AD轉(zhuǎn)換中斷允許
ADC12CTL0 |=ENC; //轉(zhuǎn)換允許
ADC12CTL0 |=ADC12SC; //開始AD轉(zhuǎn)換
}
void Delay(uint n) //延時函數(shù)
{
uint i,j;
for(i=n;i>0;i--)
for(j=100;j>0;j--) ;
}
void lcd_init() //初始化LCD
{
lcd_wcmd(0x38);//16*2顯示、5*7顯示、8位數(shù)據(jù)顯示
Delay(1);
lcd_wcmd(0x0c);//顯示開,關(guān)光標
Delay(1);
lcd_wcmd(0x06);//移動光標
Delay(1);
lcd_wcmd(0x01);//清除LCD現(xiàn)實的內(nèi)容
Delay(1);
}
void lcd_wcmd(uchar cmd) //LCD寫指令
{
P3OUT &=~RS;
P3OUT &=~RW;
P3OUT &=~EN;
P4OUT=cmd;
P3OUT |=EN;
Delay(1);
P3OUT &=~EN;
}
void lcd_pos(uchar pos) //設(shè)置顯示位置
{
lcd_wcmd(pos | 0x80);
}
void lcd_wdata(uchar data)//寫入數(shù)據(jù)到LCD
{
P3OUT |=RS;
P3OUT &=~RW;
P3OUT &=~EN;
P4OUT = data;
P3OUT |=EN;
P3OUT &=~EN;
}
void LED_SH() //LED_show
{
P2OUT |=BIT1+BIT3+BIT4;
P4OUT=0xff;
P5OUT=0x01; //選擇第一位數(shù)碼管
P4OUT=b[value[0]]; //P4口顯示采樣值的個位
Delay(3);
P4OUT=0xff;
P5OUT=0x02; //選擇第二位數(shù)碼管
P4OUT=a[value[1]];//P4口顯示采樣值的第一位小數(shù)
Delay(3);
P4OUT=0xff;
P5OUT=0x04; //選擇第三位數(shù)碼管
P4OUT=a[value[2]];//P4顯示采樣值的第二位小數(shù)
Delay(3);
P4OUT=0xff;
P5OUT=0x08; //選擇第四位數(shù)碼管
P4OUT=a[value[3]];//P4顯示采樣值的第三位小數(shù)
Delay(3);
P4OUT=0xff;
}
void LCD_SH() //LCD_show
{
uint i;
P2OUT &=~(BIT1+BIT3+BIT4);//turn off 74HC573
lcd_init(); //初始化LCD,以下為LCD顯示部分
Delay(1);
lcd_wcmd(0x06); //光標右移
lcd_pos(0); //設(shè)置顯示位置為第一行的第一個字符
for(i=0;c[i]!=