找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6698|回复: 9
收起左侧

stc89c52单片机串口通信如何用定时器2,请大侠指教一二

[复制链接]
ID:420214 发表于 2019-1-10 18:01 | 显示全部楼层 |阅读模式
采用定时器1时,单片机程序可以进行通信。改用定时器2时,就不行了。请大侠指教一二!!!

        uchar i;

//        TMOD = 0x20;//设置定时器1位工作方式2  ,8位自动重装载
//        TH1 = 0xfd;        //装入初值
//        TL1 = 0xfd;
//        TR1 = 1;
// 用以上定时器1是可以的

        T2CON = 0x30; //TF2 EXF2 RCLK = 1;TCLK = 1;        EXEN2 TR2; C/T2 CP/RL2

        RCAP2H =  (65536-main_fosc/ (32 * baudmy)) / 256 ;
        RCAP2L =  (65536-main_fosc/ (32 * baudmy)) % 256 ;
        TH2 = RCAP2H;
        TL2 = RCAP2L;
        T2MOD =0x00;
        TR2 =1;           //启动定时器2
//用定时器2,按上述设置,就不行了

//SCON SM0 SM1 SM2 REN TB8 RB8 TI RI
        SM0 = 0;
        SM1 = 1;
        REN = 1;
        ES = 1;
        EA = 1;


回复

使用道具 举报

ID:155507 发表于 2019-1-10 20:15 | 显示全部楼层
我给你来个程序试试

  1. /*------------------------------------------------------------------*/
  2. /* --- STC MCU Limited ---------------------------------------------*/
  3. /* --- STC89-90xx Series MCU UART (8-bit/9-bit)Demo ----------------*/
  4. /* --- Mobile: (86)13922805190 -------------------------------------*/
  5. /* --- Fax: 86-0513-55012956,55012947,55012969 ---------------------*/
  6. /* --- Tel: 86-0513-55012928,55012929,55012966----------------------*/
  7. /* --- Web: www.STCMCU.com -----------------------------------------*/
  8. /* --- Web: www.GXWMCU.com -----------------------------------------*/
  9. /* If you want to use the program or the program referenced in the  */
  10. /* article, please specify in which data and procedures from STC    */
  11. /*------------------------------------------------------------------*/

  12. #include "reg51.h"
  13. #include "intrins.h"

  14. sfr T2CON  = 0xC8;          //timer2 control register
  15. sfr RCAP2L = 0xCA;
  16. sfr RCAP2H = 0xCB;
  17. sfr TL2    = 0xCC;
  18. sfr TH2    = 0xCD;

  19. typedef unsigned char BYTE;
  20. typedef unsigned int WORD;

  21. #define FOSC 11059200L      //System frequency
  22. #define BAUD 115200       //UART baudrate

  23. /*Define UART parity mode*/
  24. #define NONE_PARITY     0   //None parity
  25. #define ODD_PARITY      1   //Odd parity
  26. #define EVEN_PARITY     2   //Even parity
  27. #define MARK_PARITY     3   //Mark parity
  28. #define SPACE_PARITY    4   //Space parity

  29. #define PARITYBIT EVEN_PARITY   //Testing even parity

  30. sbit bit9 = P2^2;           //P2.2 show UART data bit9
  31. bit busy;

  32. void SendData(BYTE dat);
  33. void SendString(char *s);

  34. void main()
  35. {
  36. #if (PARITYBIT == NONE_PARITY)
  37.     SCON = 0x50;            //8-bit variable UART
  38. #elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
  39.     SCON = 0xda;            //9-bit variable UART, parity bit initial to 1
  40. #elif (PARITYBIT == SPACE_PARITY)
  41.     SCON = 0xd2;            //9-bit variable UART, parity bit initial to 0
  42. #endif

  43.     TL2 = RCAP2L = (65536-(FOSC/32/BAUD)); //Set auto-reload vaule
  44.     TH2 = RCAP2H = (65536-(FOSC/32/BAUD)) >> 8;
  45.     T2CON = 0x34;           //Timer2 start run
  46.     ES = 1;                 //Enable UART interrupt
  47.     EA = 1;                 //Open master interrupt switch

  48.     SendString("STC89-90xx\r\nUart Test !\r\n");
  49.     while(1);
  50. }

  51. /*----------------------------
  52. UART interrupt service routine
  53. ----------------------------*/
  54. void Uart_Isr() interrupt 4
  55. {
  56.     if (RI)
  57.     {
  58.         RI = 0;             //Clear receive interrupt flag
  59.         P0 = SBUF;          //P0 show UART data
  60.         bit9 = RB8;         //P2.2 show parity bit
  61.     }
  62.     if (TI)
  63.     {
  64.         TI = 0;             //Clear transmit interrupt flag
  65.         busy = 0;           //Clear transmit busy flag
  66.     }
  67. }

  68. /*----------------------------
  69. Send a byte data to UART
  70. Input: dat (data to be sent)
  71. Output:None
  72. ----------------------------*/
  73. void SendData(BYTE dat)
  74. {
  75.     while (busy);           //Wait for the completion of the previous data is sent
  76.     ACC = dat;              //Calculate the even parity bit P (PSW.0)
  77.     if (P)                  //Set the parity bit according to P
  78.     {
  79. #if (PARITYBIT == ODD_PARITY)
  80.         TB8 = 0;            //Set parity bit to 0
  81. #elif (PARITYBIT == EVEN_PARITY)
  82.         TB8 = 1;            //Set parity bit to 1
  83. #endif
  84.     }
  85.     else
  86.     {
  87. #if (PARITYBIT == ODD_PARITY)
  88.         TB8 = 1;            //Set parity bit to 1
  89. #elif (PARITYBIT == EVEN_PARITY)
  90.         TB8 = 0;            //Set parity bit to 0
  91. #endif
  92.     }
  93.     busy = 1;
  94.     SBUF = ACC;             //Send data to UART buffer
  95. }

  96. /*----------------------------
  97. Send a string to UART
  98. Input: s (address of string)
  99. Output:None
  100. ----------------------------*/
  101. void SendString(char *s)
  102. {
  103.     while (*s)              //Check the end of the string
  104.     {
  105.         SendData(*s++);     //Send current char and increment string ptr
  106.     }
  107. }


复制代码
回复

使用道具 举报

ID:458411 发表于 2019-1-10 20:27 | 显示全部楼层
/*------------------------------------------------------------------*/
/* --- STC MCU Limited ---------------------------------------------*/
/* --- STC89-90xx Series MCU UART (8-bit/9-bit)Demo ----------------*/
/* --- Mobile: (86)13922805190 -------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ---------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966----------------------*/
/* --- Web: www.STCMCU.com -----------------------------------------*/
/* --- Web: www.GXWMCU.com -----------------------------------------*/
/* If you want to use the program or the program referenced in the  */
/* article, please specify in which data and procedures from STC    */
/*------------------------------------------------------------------*/

#include "reg51.h"
#include "intrins.h"

sfr T2CON  = 0xC8;          //timer2 control register
sfr RCAP2L = 0xCA;
sfr RCAP2H = 0xCB;
sfr TL2    = 0xCC;
sfr TH2    = 0xCD;

typedef unsigned char BYTE;
typedef unsigned int WORD;
unsigned char num;
#define FOSC 11059200L      //System frequency
#define BAUD 9600       //UART baudrate
bit busy;
void main()
{
    SCON = 0x50;            //8-bit variable UART
    TL2 = RCAP2L = (65536-(FOSC/32/BAUD)); //Set auto-reload vaule
    TH2 = RCAP2H = (65536-(FOSC/32/BAUD)) >> 8;
    T2CON = 0x34;           //Timer2 start run
    ES = 1;                 //Enable UART interrupt
    EA = 1;                 //Open master interrupt switch
    SendString("66666 !\r\n");
    while(1);
}

void Uart_Isr() interrupt 4 using 1
{
    if (RI)
    {
    num = SBUF;          //num è¡3ö½óêÕ»o′æÆ÷μÄÖμ
                RI = 0;
                SBUF = num;       
    }
    if (TI)
    {
        TI = 0;             //Clear transmit interrupt flag
        busy = 0;           //Clear transmit busy flag
    }
}

void SendData(BYTE dat)
{
    while (busy);           //Wait for the completion of the previous data is sent
    ACC = dat;              //Calculate the even parity bit P (PSW.0)
    busy = 1;
    SBUF = ACC;             //Send data to UART buffer
}
void SendString(char *s)
{
    while (*s)              //Check the end of the string
    {
        SendData(*s++);     //Send current char and increment string ptr
    }
}

回复

使用道具 举报

ID:213173 发表于 2019-1-10 20:33 | 显示全部楼层
无标题.jpg
回复

使用道具 举报

ID:420214 发表于 2019-1-10 21:55 | 显示全部楼层
angmall 发表于 2019-1-10 20:15
我给你来个程序试试

谢谢!!
我试了一下,还不太好。我好好学习一下先。
多谢了!!
回复

使用道具 举报

ID:420214 发表于 2019-1-10 21:57 | 显示全部楼层
xiaob123 发表于 2019-1-10 20:27
/*------------------------------------------------------------------*/
/* --- STC MCU Limited ----- ...

嗯,可以运行了!
感谢!!
我好好研究一下,是啥原因。
回复

使用道具 举报

ID:420214 发表于 2019-1-10 22:00 | 显示全部楼层

找到这个了,再次感谢!!
回复

使用道具 举报

ID:420214 发表于 2019-1-10 22:02 | 显示全部楼层
解决了,谢谢两位。
原因在查。
回复

使用道具 举报

ID:607652 发表于 2019-9-17 11:09 | 显示全部楼层
楼主。你是什么问题a?
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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