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

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

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

關于89C52單片機串行通信的數據定時發送要怎么實現呢?

[復制鏈接]
跳轉到指定樓層
樓主
ID:296121 發表于 2018-3-23 21:02 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
    我是進行了一點基礎學習的單片機的小白一枚,但是現在有項目急需使用到單片機的串行通信。想請教各位數據定時發送要怎樣在單片機中實現呢?
    是使用串口通信中斷實現還是直接用定時器中斷就能實現呢?定時的要求比較高,每10ms能從串口發送一次數據出去。各位大佬支支招!
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復

使用道具 舉報

沙發
ID:287575 發表于 2018-3-23 22:41 | 只看該作者
每10MS發一次數據,當然用定時器中斷了
回復

使用道具 舉報

板凳
ID:237354 發表于 2018-3-23 23:10 | 只看該作者
既然定時要求高當然要用定時器,在中斷代碼中發串口數據

#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;

#define FOSC 18432000L      //System frequency
#define BAUD 115200       //UART baudrate

/*Define UART parity mode*/
#define NONE_PARITY     0   //None parity
#define ODD_PARITY      1   //Odd parity
#define EVEN_PARITY     2   //Even parity
#define MARK_PARITY     3   //Mark parity
#define SPACE_PARITY    4   //Space parity

#define PARITYBIT EVEN_PARITY   //Testing even parity

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

void SendData(BYTE dat);
void SendString(char *s);

void main()
{
#if (PARITYBIT == NONE_PARITY)
    SCON = 0x50;            //8-bit variable UART
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
    SCON = 0xda;            //9-bit variable UART, parity bit initial to 1
#elif (PARITYBIT == SPACE_PARITY)
    SCON = 0xd2;            //9-bit variable UART, parity bit initial to 0
#endif

    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("STC89-90xx\r\nUart Test !\r\n");
    while(1);
}

/*----------------------------
UART interrupt service routine
----------------------------*/
void Uart_Isr() interrupt 4 using 1
{
    if (RI)
    {
        RI = 0;             //Clear receive interrupt flag
        P0 = SBUF;          //P0 show UART data
        bit9 = RB8;         //P2.2 show parity bit
    }
    if (TI)
    {
        TI = 0;             //Clear transmit interrupt flag
        busy = 0;           //Clear transmit busy flag
    }
}

/*----------------------------
Send a byte data to UART
Input: dat (data to be sent)
Output:None
----------------------------*/
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)
    if (P)                  //Set the parity bit according to P
    {
#if (PARITYBIT == ODD_PARITY)
        TB8 = 0;            //Set parity bit to 0
#elif (PARITYBIT == EVEN_PARITY)
        TB8 = 1;            //Set parity bit to 1
#endif
    }
    else
    {
#if (PARITYBIT == ODD_PARITY)
        TB8 = 1;            //Set parity bit to 1
#elif (PARITYBIT == EVEN_PARITY)
        TB8 = 0;            //Set parity bit to 0
#endif
    }
    busy = 1;
    SBUF = ACC;             //Send data to UART buffer
}

/*----------------------------
Send a string to UART
Input: s (address of string)
Output:None
----------------------------*/
void SendString(char *s)
{
    while (*s)              //Check the end of the string
    {
        SendData(*s++);     //Send current char and increment string ptr
    }
}
回復

使用道具 舉報

地板
ID:296121 發表于 2018-3-25 15:05 | 只看該作者
zhangxiaozi 發表于 2018-3-23 22:41
每10MS發一次數據,當然用定時器中斷了

那數據發送要怎么實現呢?
回復

使用道具 舉報

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

本版積分規則

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

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 日韩一区二区三区在线播放 | 夜夜骑首页 | 妹子干综合 | 在线一区 | 99久久婷婷 | 欧美成年人网站 | 国产精品久久久久一区二区三区 | 精品中文字幕视频 | 亚洲网站在线观看 | 中文字幕精品一区久久久久 | 国产三级国产精品 | 婷婷在线视频 | 波多野吉衣在线播放 | 国精日本亚洲欧州国产中文久久 | 午夜成人免费视频 | 久久久国产精品视频 | 色综久久 | 久久精品一区二区三区四区 | 国产a区 | 国产精品日韩在线观看 | 一区二区日韩 | 日韩av网址在线观看 | 免费亚洲一区二区 | 日韩av网址在线观看 | 亚洲欧美视频一区 | 91影院| 午夜成人在线视频 | 欧美在线视频一区二区 | 欧美日韩在线观看视频 | 久草综合在线视频 | 国产成人精品一区二区在线 | 国产精品一区二区久久 | 成人精品久久日伦片大全免费 | 成人亚洲 | 国产高清免费 | 午夜男人天堂 | 色综合久 | 亚洲精品女优 | 免费一级欧美在线观看视频 | h视频在线播放 | 国产一区二区三区免费观看视频 |