專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

上位機(jī)串口發(fā)送至單片機(jī)1602液晶

作者:huqin   來源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2014年03月17日   【字體:

/*****************************************************************
串行通迅     LCD1602顯示                                                   
*************************************************************************
* 描述:(用串口調(diào)試助手軟件觀察)                                                           
*   主機(jī)發(fā)送123456給單片機(jī),單片機(jī)接收到數(shù)據(jù)后發(fā)送TX-MCU給主機(jī)。   *
*   主機(jī)發(fā)送其它數(shù)據(jù)給單片機(jī),單片機(jī)接收到數(shù)據(jù)后,再發(fā)送給主機(jī)。   *
*   LCD1602顯示接收數(shù)據(jù)的ASCII碼。  波特率9600           *
*   注:當(dāng)單片機(jī)收到數(shù)據(jù)只有湊夠16個(gè)時(shí)才會(huì)一次在液晶上顯示出來 *
************************************************************************/

#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[ ] = {" SERILA TRANFER "};
uchar code  cdis2[ ] = {"                "};

/**********************************************************

 延時(shí)子程序

**********************************************************/
void delay1(uint ms)

{
   uchar k;
   while(ms--)
   {
     for(k = 0; k < 120; k++);
   }
}

/******************************************************************/
/*                                                                */
/*檢查LCD忙狀態(tài)                                                   */
/*lcd_busy為1時(shí),忙,等待。lcd-busy為0時(shí),閑,可寫指令與數(shù)據(jù)。     */
/*                                                                */
/******************************************************************/
bit lcd_busy()
 {                         
    bit result;
    LCD_RS = 0;
//    LCD_RW = 1;
    LCD_EN = 1;
    delayNOP();
    result = (bit)(P0&0x80);
    LCD_EN = 0;
    return(result);
 }

/*******************************************************************/
/*                                                                 */
/*寫指令數(shù)據(jù)到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; 
}

/*******************************************************************/
/*                                                                 */
/*寫顯示數(shù)據(jù)到LCD                                                  */
/*RS=H,RW=L,E=高脈沖,D0-D7=數(shù)據(jù)。                               */
/*                                                                 */
/*******************************************************************/
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初始化設(shè)定                                                  */
/*                                                                 */
/*******************************************************************/
void lcd_init()
{
    delay1(15);  
    lcd_wcmd(0x01);      //清除LCD的顯示內(nèi)容           
    lcd_wcmd(0x38);      //16*2顯示,5*7點(diǎn)陣,8位數(shù)據(jù)
    delay1(5);
    lcd_wcmd(0x38);        
    delay1(5);
    lcd_wcmd(0x38);        
    delay1(5);

    lcd_wcmd(0x0c);      //開顯示,顯示光標(biāo),光標(biāo)閃爍
    delay1(5);

    lcd_wcmd(0x01);      //清除LCD的顯示內(nèi)容
    delay1(5);
}

/*******************************************************************/
/*                                                                 */
/*  設(shè)定顯示位置                                                   */
/*                                                                 */
/*******************************************************************/

void lcd_pos(uchar pos)
{                         
  lcd_wcmd(pos | 0x80);  //數(shù)據(jù)指針=80+地址變量
}

/*********************************************************

  發(fā)送數(shù)據(jù)函數(shù)

*********************************************************/
void senddata(uchar dat)
{
     SBUF =dat;
     while(!TI);
     TI = 0;
}

/*********************************************************

  串行中斷服務(wù)函數(shù)

*********************************************************/
void  serial() interrupt 4
{
   ES = 0;                //關(guān)閉串行中斷
   RI = 0;                //清除串行接受標(biāo)志位
   buf = SBUF;            //從串口緩沖區(qū)取得數(shù)據(jù)
    
   switch(buf)
   {
      case 0x31:  senddata('T');break;  //接受到1,發(fā)送字符'T'給計(jì)算機(jī)        
      case 0x32:  senddata('X');break;  //接受到2,發(fā)送字符'X'給計(jì)算機(jī)      
      case 0x33:  senddata('-');break;  //接受到3,發(fā)送字符'-'給計(jì)算機(jī)      
      case 0x34:  senddata('M');break;  //接受到4,發(fā)送字符'M'給計(jì)算機(jī)      
      case 0x35:  senddata('C');break;  //接受到5,發(fā)送字符'C'給計(jì)算機(jī)          
      case 0x36:  senddata('U');break;  //接受到5,發(fā)送字符'U'給計(jì)算機(jī)                 
      default:    senddata(buf);break;  //接受到其它數(shù)據(jù),將其發(fā)送給計(jì)算機(jī)        
   }

   if(buf!=0x0D)
   {  
     if(buf!=0x0A)
     {
       temp =buf;
       if(count<16)
       {        
         RXDdata[count]=temp;
         count++;
if(count==16)
playflag=1;
       }
     }
   }
   ES = 1;    //允許串口中斷
}

/*********************************************************

  數(shù)據(jù)顯示函數(shù)

*********************************************************/
void  play()
{
   if(playflag)
   {
     lcd_pos(0x40);           //設(shè)置位置為第二行
     for(m=0;m<16;m++)
     {
lcd_wdat(cdis2[m]);      //清LCD1602第二行
delay1(5);
}
lcd_pos(0x40);           //設(shè)置位置為第二行
     for(m=0;m<16;m++)
     {
    //   lcd_pos(0x40+m);       //設(shè)置顯示位置為第二行
       lcd_wdat(RXDdata[m]);  //顯示字符
  delay1(5);
     }

     playflag=0;             
     count=0x00;

     for(m=0;m<16;m++)     
     {
RXDdata[m]=0x20;         //清顯存單元   
delay1(5);
}
   }              
}

/*********************************************************

  主函數(shù)

*********************************************************/
void main(void)
{
    P0 = 0xff;
    P2 = 0xff;        
   
    SCON=0x50;           //設(shè)定串口工作方式
    PCON=0x00;           //波特率不倍增
    TMOD=0x20;           //定時(shí)器1工作于8位自動(dòng)重載模式, 用于產(chǎn)生波特率
    EA=1;
    ES = 1;              //允許串口中斷
    TL1=0xfd;
    TH1=0xfd;             //波特率9600
    TR1=1;

    lcd_init();
    lcd_pos(0x00);        //設(shè)置顯示位置為第一行
    for(m=0;m<16;m++)
    {
lcd_wdat(cdis1[m]);   //顯示字符
delay1(5);
}
    lcd_pos(0x40);        //設(shè)置顯示位置為第二行
    for(m=0;m<16;m++)
    {
lcd_wdat(cdis2[m]);   //顯示字符           
  delay1(5);
}
    while(1)       
    {
      play();      
    }
}

/*********************************************************/

關(guān)閉窗口

相關(guān)文章