?? 1602232.c
字號:
/*******************************************************************************
*232通信程序2400速率,12m晶體, 燒寫后關掉XLISP軟件,編程仿真切換到仿真狀態(按下*
*使用配套光盤的工具軟件:串口調試助手,設置波特率2400, 連接232電纜到串口, *
*連接1602液晶到開發板, *
*用串口調試助手發送字符, 開發板的1602液晶顯示相應字符 *
********************************************************************************
* 日期: 2007-3-25 *
* 版本: 3.0 *
* 作者: 深圳學林電子有限公司 專業8051單片機教學站 *
* 郵箱: sxj1974@163.com *
* 網站: http://www.51c51.com http://www.8951.com 更多例程請登陸網站 *
********************************************************************************
* 硬件:此程序在本公司xlisp系列單片機實驗儀運行通過 *
********************************************************************************
* 【版權】 Copyright(C)深圳學林電子有限公司 www.51c51.com All Rights Reserved *
* 【聲明】 此程序僅用于學習與參考,引用請注明版權和作者信息! *
********************************************************************************/
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit LCD_RS = P2^0;
sbit LCD_RW = P2^1;
sbit LCD_EN = P2^2;
#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[ ] = {" SERILA TRANFER "};
uchar code cdis2[ ] = {" "};
/**********************************************************
延時子程序
**********************************************************/
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_RW = 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_RW = 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; //從串口緩沖區取得數據
playflag=1;
switch(buf)
{
case 0x31: senddata('X');break; //接受到1,發送字符'W'給計算機
case 0x32: senddata('L');break; //接受到2,發送字符'I'給計算機
case 0x33: senddata('1');break; //接受到3,發送字符'L'給計算機
case 0x34: senddata('0');break; //接受到4,發送字符'L'給計算機
case 0x35: senddata('0');break; //接受到5,發送字符'A'給計算機
case 0x36: senddata('0');break; //接受到5,發送字符'R'給計算機
default: senddata(buf);break; //接受到其它數據,將其發送給計算機
}
if(buf!=0x0D)
{
if(buf!=0x0A)
{
temp =buf;
if(count<16)
{
RXDdata[count]=temp;
count++;
}
}
}
ES = 1; //允許串口中斷
}
/*********************************************************
數據顯示函數
*********************************************************/
void play()
{
if(playflag)
{
lcd_pos(0x40); //設置位置為第二行
for(m=0;m<16;m++)
lcd_wdat(cdis2[m]); //清LCD1602第二行
for(m=0;m<16;m++)
{
lcd_pos(0x40+m); //設置顯示位置為第二行
lcd_wdat(RXDdata[m]); //顯示字符
}
playflag=0;
count=0x00;
for(m=0;m<16;m++)
RXDdata[m]=0x20; //清顯存單元
}
}
/*********************************************************
主函數
*********************************************************/
void main(void)
{
P0 = 0xff;
P2 = 0xff;
SCON=0x50; //設定串口工作方式
PCON=0x00; //波特率不倍增
TMOD=0x20; //定時器1工作于8位自動重載模式, 用于產生波特率
EA=1;
ES = 1; //允許串口中斷
TL1=0xf3;
TH1=0xf3; //波特率2400
TR1=1;
lcd_init();
lcd_pos(0x00); //設置顯示位置為第一行
for(m=0;m<16;m++)
lcd_wdat(cdis1[m]); //顯示字符
lcd_pos(0x40); //設置顯示位置為第二行
for(m=0;m<16;m++)
lcd_wdat(cdis2[m]); //顯示字符
while(1)
{
play();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -