找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2211|回复: 1
收起左侧

关于STC12C5A60S2单片机双串口程序问题

[复制链接]
ID:487464 发表于 2019-3-11 14:32 | 显示全部楼层 |阅读模式
为什么我的程序编译成功了,但是串口2却不会发送数据呢?谢谢!

  1. #include<STC12C5A60S2.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include<intrins.h>
  5. #include "UART2.h"
  6. #define uchar unsigned char
  7. #define uint unsigned int

  8. uchar code dis_tab1[]={"    识别结果    "};
  9. uchar code dis_tab4[]={"------------------------"};

  10. uchar RecDate[14]={0};  //串口接收 数据

  11. uint count_ms=0; //计时
  12. uchar recnum2=0; //串口接收长度

  13. uchar disflag=0;//显示模式

  14. bit recfinish=0; //串口接收完成为1


  15. void Timer0Init(void)                //10m秒@11.0592MHz
  16. {

  17. TMOD &= 0xF0;                //设置定时器模式
  18.         TMOD |= 0x01;                //设置定时器模式
  19.         TL0 = 0x00;                //设置定时初值
  20.         TH0 = 0xDC;                //设置定时初值
  21.         TF0 = 0;                //清除TF0标志
  22.         TR0 = 0;                //定时器0关闭计时
  23.   ET0=1;
  24.         EA=1;
  25. }


  26. /* 串行通信初始化*/
  27. void UartInit(void)                //9600bps@11.0592MHz
  28. {
  29.         PCON &= 0x7F;                //波特率不倍速
  30.         SCON = 0x50;                //8位数据,可变波特率

  31.         TMOD &= 0x0F;                //清除定时器1模式位
  32.         TMOD |= 0x20;                //设定定时器1为8位自动重装方式
  33.         TL1 = 0xfd;                //设定定时初值
  34.         TH1 = 0xfd;                //设定定时器重装值
  35.         ET1 = 0;                //禁止定时器1中断
  36.         TR1 = 1;                //启动定时器1
  37.         ES = 1;                 //Enable UART1 interrupt
  38.         EA=1;                                        //开总中断
  39.         

  40. }

  41. /************************************************/
  42. void Delay_1ms(unsigned int xms)
  43. {
  44.         unsigned int i,j;
  45.         for(i=xms;i>0;i--)
  46.                 for(j=122;j>0;j--);
  47. }
复制代码

  1. #include"12864.h"

  2. void display()///液晶显示函数
  3. {
  4.         uchar i=0;
  5.   uchar temp[14]={0};
  6.          
  7.           LCD_pos(3,0);
  8.      for(i=0;i<16;i++)                        
  9.      write_dat(' ');
  10.      LCD_pos(3,0);
  11.      sprintf((char*)temp,"%13s",RecDate);        
  12.          
  13.                         i=0;
  14.                         while(temp[i] != '\0')
  15.                         {                        
  16.                                  write_dat(temp[i]);
  17.                                  i++;
  18.                         }
  19.                
  20.                  LCD_pos(1,3);
  21.                  for(i=0;i<10;i++)                        
  22.      write_dat(' ');        
  23.                  LCD_pos(1,3);   
  24.          

  25.         }


  26. void send_char_com(unsigned char ch)  //串口1发送字节
  27. {
  28.     SBUF=ch;
  29.     while(TI==0); //若果TI=0,循环等待
  30.     TI=0;                  //发送完,清零TI
  31. }


  32. void main() //主函数
  33. {
  34.         uint count=0;
  35.         uchar i=0;

  36.         Timer0Init();//初始化T0
  37.         LCD_init();//初始化led        
  38.         UartInit();         //初始化串口1
  39.         Uart2Init();  //初始化串口2
  40.         while(1)
  41.         {
  42.          count++;
  43.   if(count>10)
  44.         {
  45.          count=0;
  46.         }               

  47.          
  48.         if(recfinish==1)//串口接收到数据,得到识别码
  49.         {
  50.           recfinish=0;
  51.                 TR0=0;//关闭定时器0
  52.                 count_ms=0;
  53.                 display();
  54.         
  55.         
  56.                          Delay_1ms(30);
  57.                          recnum2=0;// 接收数据长度清零
  58.                 for(i=0;i<14;i++)   
  59.                 RecDate[i]=0;
  60.         }

  61.                  Delay_1ms(30);
  62.         }            


  63. }
  64. /***********************************************
  65. 函数名称:Timer0_ISR
  66. 功    能:定时器0中断处理函数
  67. 入口参数:无
  68. 返 回 值:无        
  69. 备    注:无
  70. ************************************************/
  71. void Timer0_ISR() interrupt 1  //10ms
  72. {  

  73.         TL0 = 0x00;                //设置定时初值
  74.         TH0 = 0xDC;                //设置定时初值
  75.   count_ms++;
  76. if(count_ms>100)//超过1s
  77. {
  78.          count_ms=0;
  79.    recfinish=1; //串口接收完成
  80. }
  81. }




  82.   /*----------------------------
  83. UART interrupt service routine 串口接收条形码数据
  84. ----------------------------*/
  85. void Uart_Isr() interrupt 4 using 1
  86. {

  87.     if (RI)
  88.     {
  89.      RI = 0;             //Clear receive interrupt flag

  90.               
  91.     TR0=1;//打开定时器0                        
  92.                 if(recnum2<13)
  93.                 {
  94.      RecDate[recnum2]=SBUF;
  95.                  recnum2++;
  96.                 }   
  97.            }
  98.            }
  99. #ifndef _UART2_H_
  100. #define _UART2_H_



  101. void Uart2Init(void);
  102. void Uart2_send_byte(unsigned char date);
  103. void Uart2_send_string(unsigned char *p);

  104. void Uart2Init(void)                //9600bps@11.0592MHz      
  105. {
  106.     AUXR |= 0x08;                //使能波特率倍速位S2SMOD:(波特率加倍)
  107.         S2CON = 0x50;                //8位数据,可变波特率
  108.         AUXR &= 0xFB;                //独立波特率发生器时钟为Fosc/12,即12T
  109.         
  110.         BRT = 0xFA;                //设定独立波特率发生器重装值
  111.         
  112.         AUXR |= 0x10;                //启动独立波特率发生器
  113.                IE2 =0x01;        //开串口2中断  ES2=1
  114. }


  115. void Uart2_send_byte(unsigned char date)//串口2发送一个字节
  116. {        
  117.                 IE=0 ;
  118.         S2BUF=date;
  119.         while((S2CON&0X02)==0);
  120.         {
  121.         S2CON&=~0X02;              //清除发送标志位
  122.           IE=1;
  123.         }         
  124. }

  125. void Uart2_send_string(unsigned char *p)   //串口2发送一个字符串
  126. {
  127.         while(*p!='\0')
  128.         {
  129.                 Uart2_send_byte(*p);
  130.                 p++;
  131.         }
  132. }


  133. #endif
复制代码
回复

使用道具 举报

ID:229821 发表于 2019-4-6 08:35 | 显示全部楼层
我是串口2无法接受数据
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表