標(biāo)題: STC12C單片機(jī)串口通信波特率怎么設(shè)置9600 [打印本頁]

作者: environmentx    時(shí)間: 2016-4-1 17:52
標(biāo)題: STC12C單片機(jī)串口通信波特率怎么設(shè)置9600
選工作方式2,波特率怎么設(shè)置?晶振11.0592的

作者: liuyimao485812    時(shí)間: 2016-4-5 13:52
SCON = 0x50;                //8位數(shù)據(jù),可變波特率
        AUXR |= 0x40;                //定時(shí)器1時(shí)鐘為Fosc,即1T
        AUXR &= 0xFE;                //串口1選擇定時(shí)器1為波特率發(fā)生器
        TMOD &= 0x0F;                //設(shè)定定時(shí)器1為16位自動(dòng)重裝方式
        TL1 = 0xE0;                //設(shè)定定時(shí)初值
        TH1 = 0xFE;                //設(shè)定定時(shí)初值
        ET1 = 0;                //禁止定時(shí)器1中斷
        TR1 = 1;                //啟動(dòng)定時(shí)器1
作者: liuyimao485812    時(shí)間: 2016-4-5 13:53
SCON = 0x50;                //8位數(shù)據(jù),可變波特率
        AUXR |= 0x40;                //定時(shí)器1時(shí)鐘為Fosc,即1T
        AUXR &= 0xFE;                //串口1選擇定時(shí)器1為波特率發(fā)生器
        TMOD &= 0x0F;                //設(shè)定定時(shí)器1為16位自動(dòng)重裝方式
        TL1 = 0xE0;                //設(shè)定定時(shí)初值
        TH1 = 0xFE;                //設(shè)定定時(shí)初值
        ET1 = 0;                //禁止定時(shí)器1中斷
        TR1 = 1;                //啟動(dòng)定時(shí)器1
作者: 用戶21111688    時(shí)間: 2016-4-5 20:32
/*------------------------------------------------------------------*/ /* --- STC MCU Limited ---------------------------------------------*/ /* --- STC12C5Axx Series MCU UART (8-bit/9-bit)Demo ----------------*/ /* --- Mobile: (86)13922805190 -------------------------------------*/ /* --- Fax: 86-755-82905966 ----------------------------------------*/ /* --- Tel: 86-755-82948412 ----------------------------------------*/ /* --- Web: www.STCMCU.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"  typedef unsigned char BYTE; typedef unsigned int WORD;  #define FOSC 18432000L      //System frequency #define BAUD 9600           //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      TMOD = 0x20;            //Set Timer1 as 8-bit auto reload mode     TH1 = TL1 = -(FOSC/12/32/BAUD); //Set auto-reload vaule     TR1 = 1;                //Timer1 start run     ES = 1;                 //Enable UART interrupt     EA = 1;                 //Open master interrupt switch      SendString("STC12C5A60S2\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     } }
作者: 1330085384    時(shí)間: 2018-5-6 21:11

    TMOD=0x20;    //定時(shí)器1工作在方式2  8位自動(dòng)重裝
    SCON=0x50;    //串口1工作在方式1  10位異步收發(fā) REN=1允許接收
    TH1=0xFD;           //定時(shí)器1初值  9600BIT
    TL1=0xFD;
    TR1=1;           //定時(shí)器1開始計(jì)數(shù)
        RI=0;   
    EA=1;        //開總中斷
    ES=1;        //開串口1中斷
作者: daisir    時(shí)間: 2020-4-24 17:31
SCON = 0x50;                //8位數(shù)據(jù),可變波特率
        AUXR |= 0x40;                //定時(shí)器1時(shí)鐘為Fosc,即1T
        AUXR &= 0xFE;                //串口1選擇定時(shí)器1為波特率發(fā)生器
        TMOD &= 0x0F;                //設(shè)定定時(shí)器1為16位自動(dòng)重裝方式
        TL1 = 0xE0;                //設(shè)定定時(shí)初值
        TH1 = 0xFE;                //設(shè)定定時(shí)初值
        ET1 = 0;                //禁止定時(shí)器1中斷
        TR1 = 1;                //啟動(dòng)定時(shí)器1

作者: 指尖螺    時(shí)間: 2020-5-16 23:09
void UART_INIT()
{
        SM0 = 0;
        SM1 = 1;
        REN = 1;                                                                                                         
        EA = 1;
        ES = 1;
        TMOD = 0x20;
        TH1 = 0xfd;
        TL1 = 0xfd;
        TR1 = 1;
}
作者: TTQ001    時(shí)間: 2020-5-17 01:35
試一下使用以下配置:

void init_com( void )
{
    SCON = 0x50 ; //串口工作方式1,8位UART,波特率可變
    TMOD |= 0x20 ; //定時(shí)器1,工作方式2,自動(dòng)再裝入8位定時(shí)器
    PCON |= 0x80 ; //SMOD=1; 波特率加倍
    TH1 = 0xfa ; //波特率:9600 晶振=11.0592MHz
    IE |= 0x90 ; //使能串口中斷
    TR1 = 1 ; // 定時(shí)器1開始
}
作者: TTQ001    時(shí)間: 2020-5-17 01:35
試一下使用以下配置:

void init_com( void )
{
    SCON = 0x50 ; //串口工作方式1,8位UART,波特率可變
    TMOD |= 0x20 ; //定時(shí)器1,工作方式2,自動(dòng)再裝入8位定時(shí)器
    PCON |= 0x80 ; //SMOD=1; 波特率加倍
    TH1 = 0xfa ; //波特率:9600 晶振=11.0592MHz
    IE |= 0x90 ; //使能串口中斷
    TR1 = 1 ; // 定時(shí)器1開始
}
作者: 2983606955    時(shí)間: 2020-9-3 09:56
        SCON=0X50;                        //設(shè)置為工作方式1 ,既然是方式一,自然要確定波特率,設(shè)置定時(shí)器1
        TMOD|=0X20;//8位重裝載
        PCON|=0x00;
        TH1=0xfa;//波特率9600
        TL1=0xfa;
        RI=1;
        TI=1;
        ES=1;                                                //打開通信中斷
        EA=1;                                                //打開總中斷
        TR1=1;       
作者: pcf2000    時(shí)間: 2020-9-3 12:24
學(xué)會看資料,一般資料里都有介紹使用,串口波特率一般都會指定一個(gè)定時(shí)器來產(chǎn)生,設(shè)置好定時(shí)器的計(jì)數(shù)周期,也就是波特率就可以了
作者: 梁廷明    時(shí)間: 2020-9-3 17:25
SCON = 0x50;            //8-bit variable UART




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