標(biāo)題:
有人遇到過STC12C5A60S2串口2無法使用的問題嗎?
[打印本頁]
作者:
jakcypater
時(shí)間:
2019-4-30 11:22
標(biāo)題:
有人遇到過STC12C5A60S2串口2無法使用的問題嗎?
有人遇到過STC12C5A60S2串口2無法使用的問題嗎?串口1正常能收發(fā)數(shù)據(jù),用的官網(wǎng)提供的例程,百度發(fā)現(xiàn)很多人都遇到這個(gè)問題,目前還沒找到解決辦法。下載過
http://www.torrancerestoration.com/bbs/dpj-133507-1.html
也無法使用。
/*------------------------------------------------------------------*/
/* --- STC MCU Limited ---------------------------------------------*/
/* --- STC12C5Axx Series MCU UART2 (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"
typedef unsigned char BYTE;
typedef unsigned int WORD;
#define FOSC 11059200L //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
/*Declare SFR associated with the UART2 */
sfr AUXR = 0x8e; //Auxiliary register
sfr S2CON = 0x9a; //UART2 control register
sfr S2BUF = 0x9b; //UART2 data buffer
sfr BRT = 0x9c; //Baudrate generator
sfr IE2 = 0xaf; //Interrupt control 2
#define S2RI 0x01 //S2CON.0
#define S2TI 0x02 //S2CON.1
#define S2RB8 0x04 //S2CON.2
#define S2TB8 0x08 //S2CON.3
bit busy;
void SendData(BYTE dat);
void SendString(char *s);
void main()
{
#if (PARITYBIT == NONE_PARITY)
S2CON = 0x50; //8-bit variable UART
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
S2CON = 0xda; //9-bit variable UART, parity bit initial to 1
#elif (PARITYBIT == SPACE_PARITY)
S2CON = 0xd2; //9-bit variable UART, parity bit initial to 0
#endif
BRT = -(FOSC/32/BAUD); //Set auto-reload vaule of baudrate generator
AUXR = 0x14; //Baudrate generator work in 1T mode
IE2 = 0x01; //Enable UART2 interrupt
EA = 1; //Open master interrupt switch
SendString("STC12C5A60S2\r\nUart2 Test !\r\n");
while(1);
}
/*----------------------------
UART2 interrupt service routine
----------------------------*/
void Uart2() interrupt 8
{
if (S2CON & S2RI)
{
S2CON &= ~S2RI; //Clear receive interrupt flag
P0 = S2BUF; //P0 show UART data
P2 = (S2CON & S2RB8);//P2.2 show parity bit
}
if (S2CON & S2TI)
{
S2CON &= ~S2TI; //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)
S2CON &= ~S2TB8; //Set parity bit to 0
#elif (PARITYBIT == EVEN_PARITY)
S2CON |= S2TB8; //Set parity bit to 1
#endif
}
else
{
#if (PARITYBIT == ODD_PARITY)
S2CON |= S2TB8; //Set parity bit to 1
#elif (PARITYBIT == EVEN_PARITY)
S2CON &= ~S2TB8; //Set parity bit to 0
#endif
}
busy = 1;
S2BUF = ACC; //Send data to UART2 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
}
}
復(fù)制代碼
作者:
青龍書生
時(shí)間:
2019-4-30 16:32
我之前用官網(wǎng)的歷程發(fā)現(xiàn)有問題,后來就不用了
作者:
angmall
時(shí)間:
2019-5-1 10:51
你可以試試這個(gè)
#include "reg51.h"
#define FOSC 11059200L
#define BAUD 115200
#define NONE_PARITY 0 //無校驗(yàn)位
#define ODD_PARITY 1 //奇校驗(yàn)
#define EVEN_PARITY 2 //偶校驗(yàn)
#define MARK_PARITY 3 //標(biāo)記校驗(yàn)
#define SPACE_PARITY 4 //空校驗(yàn)
#define PARITYBIT NONE_PARITY
#define S2RI 0x01
#define S2TI 0x02
#define S2RB8 0x04
#define S2TB8 0x08
sfr AUXR = 0x8e;
sfr S2CON = 0x9a;
sfr S2BUF = 0x9b;
sfr BRT = 0x9c;
sfr IE2 = 0xaf;
bit busy;
void SendData(char dat);
void SendString(char *s);
void main()
{
#if (PARITYBIT == NONE_PARITY)
S2CON = 0x5a; //8位可變波特率 (無校驗(yàn)位)
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
S2CON = 0xda; //9位可變波特率,校驗(yàn)位初始為1
#elif (PARITYBIT == SPACE_PARITY)
S2CON = 0xd5; //9位可變波特率,校驗(yàn)位初始為0
#endif
BRT = -(FOSC/32/BAUD); //設(shè)置獨(dú)立波特率發(fā)生器的重載初值
AUXR = 0x14; //獨(dú)立波特率發(fā)生器工作在1T模式
IE2 = 0x01; //使能串口2中斷
EA = 1; //開總中斷
SendString("STC12C5A60S2\r\nUart2 Test !\r\n");
while(1);
}
void Uart2() interrupt 8 using 1
{
if (S2CON & S2RI)
{
S2CON &= ~S2RI; //清除接收完成標(biāo)志
P0 = S2BUF; //P0顯示串口數(shù)據(jù)
P2 = (S2CON & S2RB8); //P2.2顯示校驗(yàn)位
}
if (S2CON & S2TI)
{
S2CON &= ~S2TI; //清除發(fā)送完成標(biāo)志
busy = 0;
}
}
void SendData(char dat)
{
while (busy); //等待上個(gè)數(shù)據(jù)發(fā)送完成
ACC = dat; //取得偶校驗(yàn)位P
if (P) //根據(jù)P來設(shè)置串口數(shù)據(jù)的校驗(yàn)位
{
#if (PARITYBIT == ODD_PARITY)
S2CON &= ~S2TB8; //置校驗(yàn)位為0
#elif (PARITYBIT == EVEN_PARITY)
S2CON |= S2TB8; //置校驗(yàn)位為1
#endif
}
else
{
#if (PARITYBIT == ODD_PARITY)
S2CON |= S2TB8; //置校驗(yàn)位為1
#elif (PARITYBIT == EVEN_PARITY)
S2CON &= ~S2TB8; //置校驗(yàn)位為0
#endif
}
busy = 1;
S2BUF = ACC; //發(fā)送數(shù)據(jù)
}
void SendString(char *s)
{
while (*s) //判斷字符串結(jié)束標(biāo)志
{
SendData(*s++); //發(fā)送字符
}
}
復(fù)制代碼
作者:
baddd
時(shí)間:
2020-5-14 17:05
angmall 發(fā)表于 2019-5-1 10:51
你可以試試這個(gè)
對我很有用。十分感謝。!
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1