⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serial.lst

📁 1602字符液晶在89C52单片机上的应用实例和在proteus环境下的仿真实例!
💻 LST
字号:
C51 COMPILER V8.02   SERIAL                                                                07/20/2007 17:08:04 PAGE 1   


C51 COMPILER V8.02, COMPILATION OF MODULE SERIAL
OBJECT MODULE PLACED IN serial.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE serial.c BROWSE DEBUG OBJECTEXTEND

line level    source

   1          //**********************************************************************************
   2          //杨屹    2002/08/20    第一版
   3          //基于中断的串口驱动及显示程序
   4          //联系方法:gdtyy@ri.gdt.com.cn(2003/07/31以前有效)
   5          //**********************************************************************************
   6          //程序特点:
   7          //        1.基于中断,可并发执行
   8          //        2.参数可配置(收发缓冲区大小,最大字符串长度,TAB键移动距离)
   9          //**********************************************************************************
  10          //使用方法:(此范例自包含,独立于其他程序。)
  11          //        先配制收发缓冲区大小等可变参数(在serial.h中的宏定义)
  12          //        1.开头加入#include <reg51.h>语句,一定要有。
  13          //        2.初始化串口        InitSerial();//本例中为20MHz晶体,300波特率,模式2初始化
  14          //        3.初始化串口缓冲区  InitSerialBuffer();
  15          //        4.使用显示字节,字,长字,字符,字符串,清屏函数。
  16          //自包含
  17          //**********************************************************************************
  18          //波特率计算公式:
  19          //        TH1=256-(2^SMOD/32*Fosc/12*1/Bound)
  20          //    其中:SMOD=0,1;Fosc=晶体或晶振频率;Bound=波特率
  21          //    本例中,SMOD=0;Fosc=20*10E6;Bound=300,所以TH1=0x52。
  22          //Baud rate(20Mhz)
  23          //300(52);1200(D5);2400(EA);4800(F5);9600(FB);19200(FD);38400(FF);
  24          //**********************************************************************************
  25          //书写风格:
  26          //        1.带yy前缀标志的函数为杨屹改写的等效C库函数。
  27          //        2.单个单词用小写,yy定义为前缀,不算一个单词。
  28          //        3.多个单词(2个及以上),每个单词首字母大写。(有时变量名第一个单词首字母小写)
  29          //        4.采用内缩风格,每次缩进一个TAB。
  30          //**********************************************************************************
  31          //应用举例:(可在KEIL仿真环境下运行)
  32          //源程序文件:serial.h/serial.c/main.c
  33          //main.c内容:
  34          //#include <reg51.h>//Note:It must be added.必须在serial.c之前
  35          //#include <serial.c>//Note:It's ".c",not ".h"
  36          //main()
  37          //{
  38          //      unsigned char ch;
  39          //
  40          //      InitSerial();
  41          //      InitSerialBuffer();
  42          //      while(1){
  43          //              PrintStr("\n");
  44          //              PrintByte(90);PrintStr(" ");
  45          //              PrintWord(90);PrintStr(" ");
  46          //              PrintLong(90);PrintStr(" ");
  47          //              PrintChar('y');PrintChar(' ');//千万别写成双引号,否则打印0(乱字符)。
  48          //              PrintStr("\nHello!\nI'm YangYi.\n");
  49          //              PrintStr("Press any key to continue...");
  50          //              while(!yygetch(&ch));
  51          //      }
  52          //}
  53          //**********************************************************************************
  54          //建议:
  55          //    你完全可以把该子程序当作函数库使用,以便减少重复劳动,提高代码质量。
C51 COMPILER V8.02   SERIAL                                                                07/20/2007 17:08:04 PAGE 2   

  56          //**********************************************************************************
  57          //#include <general.h>
  58          #include "serial.h"
  59          #include <reg51.h>
  60          
  61          unsigned char TxBuf[LenTxBuf],RxBuf[LenRxBuf];//收发缓冲区实体
  62          unsigned char *inTxBuf,*outTxBuf,*inRxBuf,*outRxBuf;//收发缓冲区读写指针
  63          bit TIflag=1;//Note:It must be 1.
  64          
  65          void InitSerial()//串口初始化
  66          {
  67   1       TMOD=0x20;              
  68   1       TH1=0xE6;
  69   1       TL1=0xE6;    //1200 , 12MHz 
  70   1       PCON=0x00;
  71   1       TR1=1;                
  72   1       SCON=0x50; 
  73   1      }
  74          
  75          
  76          void InitSerialBuffer(void)//串口缓冲区初始化
  77          {
  78   1              inTxBuf=TxBuf;outTxBuf=TxBuf;
  79   1              inRxBuf=RxBuf;outRxBuf=RxBuf;
  80   1              EA=1;ES=1;
  81   1      }
  82          
  83          void serial(void) interrupt 4{//串口中断服务子程序
  84   1              unsigned char *t;
  85   1      
  86   1              if(TI){
  87   2                      TI=0;
  88   2                      if(inTxBuf==outTxBuf) {TIflag=1;return;}//TxBuf Empty
  89   2                      SBUF=*outTxBuf; outTxBuf++;
  90   2                      if(outTxBuf==TxBuf+LenTxBuf) outTxBuf=TxBuf;    
  91   2              }
  92   1              if(RI){
  93   2                      RI=0;
  94   2                      t=inRxBuf;t++;
  95   2                      if(t==RxBuf+LenRxBuf) t=RxBuf;
  96   2                      if(t==outRxBuf) return;                 //RxBuf Full
  97   2                      *inRxBuf=SBUF;
  98   2                      inRxBuf=t;
  99   2              }
 100   1      }
 101          // 查询方式从串口接收数据
 102          uchar getchar(void)
*** ERROR C129 IN LINE 102 OF SERIAL.C: missing ';' before 'getchar'
 103          {
 104             while(!RI);// 
 105             RI=0;
 106             return SBUF;
 107          }
 108          // 查询方式向串口发送数据
 109          void putchar(uchar ch)
 110          {
 111             SBUF=ch;
 112             while(!TI);
 113             TI=0;
 114          }
 115          bit yygetch(unsigned char *ch)//从串口缓冲区读1字节数据
 116          {
C51 COMPILER V8.02   SERIAL                                                                07/20/2007 17:08:04 PAGE 3   

 117                  ES=0;
 118                  if(inRxBuf==outRxBuf) {ES=1;return 0;}          //RxBuf Empty
 119                  *ch=*outRxBuf;  outRxBuf++;
 120                  if(outRxBuf==RxBuf+LenRxBuf) outRxBuf=RxBuf;
 121                  ES=1;
 122                  return 1;
 123          }
 124          
 125          void PrintChar(unsigned char ch)//显示字符
 126          {
 127                  unsigned char *t;
 128          
 129                  ES=0;
 130                  if(TIflag){             
 131                          TIflag=0;
 132                          TI=1;
 133                  }
 134                  t=inTxBuf;t++;
 135                  if(t==TxBuf+LenTxBuf) t=TxBuf;
 136                  if(t==outTxBuf) {ES=1;return;}                  //TxBuf Full
 137                  *inTxBuf=ch;
 138                  inTxBuf=t;
 139                  ES=1;
 140          }
 141          
 142          void PrintCh(unsigned char ch)//内部使用,不建议用户看到。
 143          {
 144                  if(ch>=0&&ch<=9) ch=ch+'0';
 145                  else if(ch>=10&&ch<=15) ch=ch+'A'-10;
 146                  PrintChar(ch);
 147          }
 148          
 149          void PrintByte(unsigned char Byte)//以十六进制格式显示1个字节数据
 150          {
 151                  unsigned char c;
 152                  c=Byte;
 153                  c=c>>4;PrintCh(c);
 154                  c=Byte;c=c&0x0F;PrintCh(c);
 155          }
 156          
 157          void PrintWord(unsigned int Word)//以十六进制格式显示1个字数据
 158          {
 159                  PrintByte(Word>>8);
 160                  PrintByte(Word&0xFF);
 161          }
 162          
 163          void PrintLong(unsigned long LongWord)//以十六进制格式显示1个长字数据
 164          {
 165                  PrintWord(LongWord>>16);
 166                  PrintWord(LongWord&0xFFFF);
 167          }
 168          
 169          void PrintStr(unsigned char *str)//显示字符串
 170          {
 171                  int i;
 172                  unsigned char j;
 173                  unsigned char ch;
 174                  for(i=0;i<MaxLenStr;i++){
 175                          ch=*(str+i);
 176                          if(ch=='\0') break;
 177                          else if(ch=='\n'){PrintChar(10);PrintChar(13);} 
 178                          else if(ch=='\t'){
C51 COMPILER V8.02   SERIAL                                                                07/20/2007 17:08:04 PAGE 4   

 179                                  for(j=0;j<TABNum;j++)
 180                                          PrintChar(' ');
 181                          }
 182                          else PrintChar(ch);
 183                  }
 184          }
 185          
 186          
 187          void GetString(char *string)
 188          {
 189              char *string2=string;
 190              char c;
 191              while((c=Uart_Getch())!='\r')               //?????????
 192              {
 193                          if(c=='\b')             //????????
 194                          {
 195                              if( (int)string2 < (int)string )
 196                              {
 197                                          Uart_Printf("\b \b");
 198                                          string--;
 199                              }
 200                          }
 201                          else 
 202                          {
 203                              *string++=c;
 204                              Uart_SendByte(c);
 205                          }
 206              }
 207              *string='\0';               //????
 208              Uart_SendByte('\n');                //??
 209          }

C51 COMPILATION COMPLETE.  0 WARNING(S),  1 ERROR(S)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -