欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

標(biāo)題: 關(guān)于89C52單片機(jī)串行通信的數(shù)據(jù)定時發(fā)送要怎么實(shí)現(xiàn)呢? [打印本頁]

作者: 文子路    時間: 2018-3-23 21:02
標(biāo)題: 關(guān)于89C52單片機(jī)串行通信的數(shù)據(jù)定時發(fā)送要怎么實(shí)現(xiàn)呢?
    我是進(jìn)行了一點(diǎn)基礎(chǔ)學(xué)習(xí)的單片機(jī)的小白一枚,但是現(xiàn)在有項目急需使用到單片機(jī)的串行通信。想請教各位數(shù)據(jù)定時發(fā)送要怎樣在單片機(jī)中實(shí)現(xiàn)呢?
    是使用串口通信中斷實(shí)現(xiàn)還是直接用定時器中斷就能實(shí)現(xiàn)呢?定時的要求比較高,每10ms能從串口發(fā)送一次數(shù)據(jù)出去。各位大佬支支招!

作者: zhangxiaozi    時間: 2018-3-23 22:41
每10MS發(fā)一次數(shù)據(jù),當(dāng)然用定時器中斷了
作者: oohoh    時間: 2018-3-23 23:10
既然定時要求高當(dāng)然要用定時器,在中斷代碼中發(fā)串口數(shù)據(jù)

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

作者: 文子路    時間: 2018-3-25 15:05
zhangxiaozi 發(fā)表于 2018-3-23 22:41
每10MS發(fā)一次數(shù)據(jù),當(dāng)然用定時器中斷了

那數(shù)據(jù)發(fā)送要怎么實(shí)現(xiàn)呢?




歡迎光臨 (http://www.raoushi.com/bbs/) Powered by Discuz! X3.1