久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 7549|回復: 9
打印 上一主題 下一主題
收起左側

stc89c52單片機串口通信如何用定時器2,請大俠指教一二

[復制鏈接]
跳轉到指定樓層
樓主
ID:420214 發(fā)表于 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;


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

沙發(fā)
ID:155507 發(fā)表于 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 發(fā)表于 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 發(fā)表于 2019-1-10 20:33 | 只看該作者

回復

使用道具 舉報

5#
ID:420214 發(fā)表于 2019-1-10 21:55 | 只看該作者
angmall 發(fā)表于 2019-1-10 20:15
我給你來個程序試試

謝謝!!
我試了一下,還不太好。我好好學習一下先。
多謝了!!
回復

使用道具 舉報

6#
ID:420214 發(fā)表于 2019-1-10 21:57 | 只看該作者
xiaob123 發(fā)表于 2019-1-10 20:27
/*------------------------------------------------------------------*/
/* --- STC MCU Limited ----- ...

嗯,可以運行了!
感謝!!
我好好研究一下,是啥原因。
回復

使用道具 舉報

7#
ID:420214 發(fā)表于 2019-1-10 22:00 | 只看該作者

找到這個了,再次感謝!!
回復

使用道具 舉報

8#
ID:420214 發(fā)表于 2019-1-10 22:02 | 只看該作者
解決了,謝謝兩位。
原因在查。
回復

使用道具 舉報

9#
ID:607652 發(fā)表于 2019-9-17 11:09 | 只看該作者
樓主。你是什么問題a?
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

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

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

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 欧美精品导航 | 毛片大全 | 中文字幕一区二区在线观看 | 美女天天操 | 91精品国产乱码久久久久久久久 | 欧美一区二区在线 | 亚洲精品久久久久久宅男 | 在线中文视频 | 国产一区二区久久 | 国产精品视频免费看 | 中文字幕丁香5月 | 一区二区中文 | 日本免费视频 | 国产人久久人人人人爽 | 日本一区二区三区在线观看 | 99精品视频在线观看 | 一区二区高清在线观看 | 欧美午夜精品 | aaaaaaa片毛片免费观看 | 欧美成人hd| 婷婷国产一区 | 国产福利视频 | 99re在线播放 | 91精品一区| 亚洲精品播放 | 综合婷婷| 嫩草视频在线免费观看 | 天堂久久天堂综合色 | 欧美日本高清 | 一区二区三区国产在线观看 | 欧美激情在线观看一区二区三区 | 精品1区| 中文字幕第十一页 | 欧美久久久久久久久中文字幕 | 精品欧美一区二区三区久久久 | 欧美啪啪 | 久久久www| 亚洲一区二区中文字幕 | 久久久精品久久久 | 91视频大全 | 国产精品视频一区二区三区 |