找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 6358|回復(fù): 3
收起左側(cè)

基于51單片機(jī)條形碼識(shí)別系統(tǒng)程序

[復(fù)制鏈接]
ID:151220 發(fā)表于 2016-11-30 14:31 | 顯示全部樓層 |閱讀模式
/*****************************************************************
串行通迅     51單片機(jī)LCD1602顯示條形碼掃碼槍發(fā)來(lái)的串行數(shù)據(jù)                                                  
*************************************************************************
* 描述:(用串口調(diào)試助手軟件觀察)                                                           
*   LCD1602顯示接收數(shù)據(jù)的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 "};
/**********************************************************
延時(shí)子程序
**********************************************************/
void delay1(uint ms)
{
    uchar k;
    while(ms--)
    {
      for(k = 0; k < 120; k++);
    }
}
/******************************************************************/
/*                                                                */
/*檢查L(zhǎng)CD忙狀態(tài)                                                   */
/*lcd_busy為1時(shí),忙,等待。lcd-busy為0時(shí),閑,可寫(xiě)指令與數(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);
  }
/*******************************************************************/
/*                                                                 */
/*寫(xiě)指令數(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;  
}
/*******************************************************************/
/*                                                                 */
/*寫(xiě)顯示數(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);      //開(kāi)顯示,顯示光標(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;  //接受到6,發(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();      
     }
}
/*********************************************************/

評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

相關(guān)帖子

回復(fù)

使用道具 舉報(bào)

ID:208781 發(fā)表于 2017-6-8 19:23 | 顯示全部樓層
有沒(méi)有圖呀
回復(fù)

使用道具 舉報(bào)

ID:292882 發(fā)表于 2018-8-21 16:08 | 顯示全部樓層
這個(gè)需要電腦嗎
回復(fù)

使用道具 舉報(bào)

ID:292882 發(fā)表于 2018-8-21 16:08 | 顯示全部樓層
這個(gè)需要連電腦嗎
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表