|
/*****************************************************************
串行通迅 51單片機LCD1602顯示條形碼掃碼槍發來的串行數據
*************************************************************************
* 描述:(用串口調試助手軟件觀察)
* LCD1602顯示接收數據的ASCII碼。 波特率9600
************************************************************************/
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit LCD_RS = P3^5;
sbit LCD_EN = P3^4;
sbit LCD_WR = P3^6;
#define delayNOP(); {_nop_();_nop_();_nop_();_nop_();};
uchar data RXDdata[ ] = {0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20 };
uchar temp,buf,m,count;
bit playflag=0;
uchar code cdis1[ ] = {" Serial Transfer "};
uchar code cdis2[ ] = {"xxxxxxxxxxxxxxx "};
/**********************************************************
延時子程序
**********************************************************/
void delay1(uint ms)
{
uchar k;
while(ms--)
{
for(k = 0; k < 120; k++);
}
}
/******************************************************************/
/* */
/*檢查LCD忙狀態 */
/*lcd_busy為1時,忙,等待。lcd-busy為0時,閑,可寫指令與數據。 */
/* */
/******************************************************************/
bit lcd_busy()
{
bit result;
LCD_RS = 0;
// LCD_RW = 1;
LCD_EN = 1;
delayNOP();
result = (bit)(P0&0x80);
LCD_EN = 0;
return(result);
}
/*******************************************************************/
/* */
/*寫指令數據到LCD */
/*RS=L,RW=L,E=高脈沖,D0-D7=指令碼。 */
/* */
/*******************************************************************/
void lcd_wcmd(uchar cmd)
{
// while(lcd_busy());
LCD_RS = 0;
LCD_WR = 0;
LCD_EN = 0;
_nop_();
_nop_();
P0 = cmd;
delayNOP();
LCD_EN = 1;
delayNOP();
LCD_EN = 0;
}
/*******************************************************************/
/* */
/*寫顯示數據到LCD */
/*RS=H,RW=L,E=高脈沖,D0-D7=數據。 */
/* */
/*******************************************************************/
void lcd_wdat(uchar dat)
{
// while(lcd_busy());
LCD_RS = 1;
LCD_WR = 0;
LCD_EN = 0;
P0 = dat;
delayNOP();
LCD_EN = 1;
delayNOP();
LCD_EN = 0;
}
/*******************************************************************/
/* */
/* LCD初始化設定 */
/* */
/*******************************************************************/
void lcd_init()
{
delay1(15);
lcd_wcmd(0x01); //清除LCD的顯示內容
lcd_wcmd(0x38); //16*2顯示,5*7點陣,8位數據
delay1(5);
lcd_wcmd(0x38);
delay1(5);
lcd_wcmd(0x38);
delay1(5);
lcd_wcmd(0x0c); //開顯示,顯示光標,光標閃爍
delay1(5);
lcd_wcmd(0x01); //清除LCD的顯示內容
delay1(5);
}
/*******************************************************************/
/* */
/* 設定顯示位置 */
/* */
/*******************************************************************/
void lcd_pos(uchar pos)
{
lcd_wcmd(pos | 0x80); //數據指針=80+地址變量
}
/*********************************************************
發送數據函數
*********************************************************/
void senddata(uchar dat)
{
SBUF =dat;
while(!TI);
TI = 0;
}
/*********************************************************
串行中斷服務函數
*********************************************************/
void serial() interrupt 4
{
ES = 0; //關閉串行中斷
RI = 0; //清除串行接受標志位
buf = SBUF; //從串口緩沖區取得數據
switch(buf)
{
case 0x31: senddata('T');break; //接受到1,發送字符'T'給計算機
case 0x32: senddata('X');break; //接受到2,發送字符'X'給計算機
case 0x33: senddata('-');break; //接受到3,發送字符'-'給計算機
case 0x34: senddata('M');break; //接受到4,發送字符'M'給計算機
case 0x35: senddata('C');break; //接受到5,發送字符'C'給計算機
case 0x36: senddata('U');break; //接受到6,發送字符'U'給計算機
default: senddata(buf);break; //接受到其它數據,將其發送給計算機
}
if(buf!=0x0D)
{
if(buf!=0x0A)
{
temp =buf;
if(count<16)
{
RXDdata[count]=temp;
count++;
if(count==16)
playflag=1;
}
}
}
ES = 1; //允許串口中斷
}
/*********************************************************
數據顯示函數
*********************************************************/
void play()
{
if(playflag)
{
lcd_pos(0x40); //設置位置為第二行
for(m=0;m<16;m++)
{
lcd_wdat(cdis2[m]); //清LCD1602第二行
delay1(5);
}
lcd_pos(0x40); //設置位置為第二行
for(m=0;m<16;++m)
{
// lcd_pos(0x40+m); //設置顯示位置為第二行
lcd_wdat(RXDdata[m]); //顯示字符
delay1(5);
}
playflag=0;
count=0x00;
for(m=0;m<16;++m)
{
RXDdata[m]=0x20; //清顯存單元
delay1(5);
}
}
}
/*********************************************************
主函數
*********************************************************/
void main(void)
{
P0 = 0xff;
P2 = 0xff;
SCON=0x50; //設定串口工作方式
PCON=0x00; //波特率不倍增
TMOD=0x20; //定時器1工作于8位自動重載模式, 用于產生波特率
EA=1;
ES = 1; //允許串口中斷
TL1=0xfd;
TH1=0xfd; //波特率9600
TR1=1;
lcd_init();
lcd_pos(0x00); //設置顯示位置為第一行
for(m=0;m<16;m++)
{
lcd_wdat(cdis1[m]); //顯示字符
delay1(5);
}
lcd_pos(0x40); //設置顯示位置為第二行
for(m=0;m<16;m++)
{
lcd_wdat(cdis2[m]); //顯示字符
delay1(5);
}
while(1)
{
play();
}
}
/*********************************************************/
|
評分
-
查看全部評分
|