标题:
STC12C单片机串口通信波特率怎么设置9600
[打印本页]
作者:
environmentx
时间:
2016-4-1 17:52
标题:
STC12C单片机串口通信波特率怎么设置9600
选工作方式2,波特率怎么设置?晶振11.0592的
作者:
liuyimao485812
时间:
2016-4-5 13:52
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x40; //定时器1时钟为Fosc,即1T
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设定定时器1为16位自动重装方式
TL1 = 0xE0; //设定定时初值
TH1 = 0xFE; //设定定时初值
ET1 = 0; //禁止定时器1中断
TR1 = 1; //启动定时器1
作者:
liuyimao485812
时间:
2016-4-5 13:53
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x40; //定时器1时钟为Fosc,即1T
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设定定时器1为16位自动重装方式
TL1 = 0xE0; //设定定时初值
TH1 = 0xFE; //设定定时初值
ET1 = 0; //禁止定时器1中断
TR1 = 1; //启动定时器1
作者:
用户21111688
时间:
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
时间:
2018-5-6 21:11
TMOD=0x20; //定时器1工作在方式2 8位自动重装
SCON=0x50; //串口1工作在方式1 10位异步收发 REN=1允许接收
TH1=0xFD; //定时器1初值 9600BIT
TL1=0xFD;
TR1=1; //定时器1开始计数
RI=0;
EA=1; //开总中断
ES=1; //开串口1中断
作者:
daisir
时间:
2020-4-24 17:31
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x40; //定时器1时钟为Fosc,即1T
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设定定时器1为16位自动重装方式
TL1 = 0xE0; //设定定时初值
TH1 = 0xFE; //设定定时初值
ET1 = 0; //禁止定时器1中断
TR1 = 1; //启动定时器1
作者:
指尖螺
时间:
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
时间:
2020-5-17 01:35
试一下使用以下配置:
void init_com( void )
{
SCON = 0x50 ; //串口工作方式1,8位UART,波特率可变
TMOD |= 0x20 ; //定时器1,工作方式2,自动再装入8位定时器
PCON |= 0x80 ; //SMOD=1; 波特率加倍
TH1 = 0xfa ; //波特率:9600 晶振=11.0592MHz
IE |= 0x90 ; //使能串口中断
TR1 = 1 ; // 定时器1开始
}
作者:
TTQ001
时间:
2020-5-17 01:35
试一下使用以下配置:
void init_com( void )
{
SCON = 0x50 ; //串口工作方式1,8位UART,波特率可变
TMOD |= 0x20 ; //定时器1,工作方式2,自动再装入8位定时器
PCON |= 0x80 ; //SMOD=1; 波特率加倍
TH1 = 0xfa ; //波特率:9600 晶振=11.0592MHz
IE |= 0x90 ; //使能串口中断
TR1 = 1 ; // 定时器1开始
}
作者:
2983606955
时间:
2020-9-3 09:56
SCON=0X50; //设置为工作方式1 ,既然是方式一,自然要确定波特率,设置定时器1
TMOD|=0X20;//8位重装载
PCON|=0x00;
TH1=0xfa;//波特率9600
TL1=0xfa;
RI=1;
TI=1;
ES=1; //打开通信中断
EA=1; //打开总中断
TR1=1;
作者:
pcf2000
时间:
2020-9-3 12:24
学会看资料,一般资料里都有介绍使用,串口波特率一般都会指定一个定时器来产生,设置好定时器的计数周期,也就是波特率就可以了
作者:
梁廷明
时间:
2020-9-3 17:25
SCON = 0x50; //8-bit variable UART
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1