一、芯片簡介
STC15F100系列單片機(jī)是宏晶科技生產(chǎn)的單時(shí)鐘/機(jī)器周期(1T)的單片機(jī),新一代8051單片機(jī),指令代碼完全兼容傳統(tǒng)8051,但是速度快6-12倍。
內(nèi)部集成R/C時(shí)鐘,5MHz~35MHz寬范圍可設(shè)置,可以省掉外部晶振。
內(nèi)部集成復(fù)位電路,可省掉外部復(fù)位電路。
6個(gè)通用I/O口,支持四種模式:準(zhǔn)雙向口/弱上拉、強(qiáng)推挽/強(qiáng)上拉、輸入/高阻、開漏
芯片管腳圖
二、開發(fā)環(huán)境
在Keil中添加STC芯片
-
下載STC-ISP軟件
https://www.stcmcudata.com/ -
打開STC-ISP軟件,選擇右側(cè)"Keil仿真設(shè)置"欄
-
選擇"添加型號(hào)和頭文件到Keil中,添加STC仿真器驅(qū)動(dòng)到Keil中", 選擇Keil C51的安裝目錄
例如:D:/software/keil4 -
系統(tǒng)會(huì)自動(dòng)添加"STC"文件夾( C51/INC/ )
-
Keil新建工程選擇芯片型號(hào)時(shí)就會(huì)有新添加的STC芯片
三、軟件模擬串口
STC15F100E芯片內(nèi)部是不直接支持串口通信的,但是可以用I/O口+定時(shí)器來實(shí)現(xiàn)串口功能。
這里實(shí)現(xiàn)的是半雙工通信
。
系統(tǒng)工作時(shí)鐘為5.5296Mhz,波特率為9600bps。引腳說明
- P3.0為Uart的RX引腳
- P3.1為Uart的TX引腳
原理說明
文章來源:http://www.zghlxwxcb.cn/news/detail-829682.html
- 發(fā)送原理
設(shè)置定時(shí)器的定時(shí)時(shí)間為一個(gè)bit的的發(fā)送時(shí)間,在定時(shí)器中斷服務(wù)函數(shù)中,設(shè)置Uart_TX引腳的值。 - 接收原理
將Uart_RX引腳設(shè)置為外部中斷(下降沿觸發(fā))模式,捕獲Uart接收的開始信號(hào)。
設(shè)置第一次定時(shí)時(shí)間為 3 2 \frac{3}{2} 23?個(gè)bit持續(xù)的時(shí)間,其余的定時(shí)時(shí)間為一個(gè)bit的持續(xù)時(shí)間,在定時(shí)器中斷服務(wù)函數(shù)中對Uart_RX引腳進(jìn)行采樣。
程序
文章來源地址http://www.zghlxwxcb.cn/news/detail-829682.html
- uart.c
/*
module name : uart
author : wkk
create time : 2023/12/5
*/
#include "uart.h"
/*
BaudRate -> timer
SysClk : 5.5296Mhz
Baudrate : 115200
5_529_600 / 115200 = 48 ( x -> error )
5_529_600 / 9600 = 576
65536 - 48 = 65488 0xffd0
65536 - 48-24 = 65464 0xffb8
65536 - 576 = 64960 0xfdc0
65536 - 576-288 = 64672 0xfca0
*/
// define type u8
typedef unsigned char u8;
// ext 2 3 4 register
// 7 6 5 4 3 2 1 0
// - ext4 ext3 ext2 - - tlclkO t0clkO
sfr int_clkO = 0x8f;
// 1T mode or /12
// 7 6 5 4 3 2 1 0
// t0x12 t1x12 - T2R T2_C/T T2x12 - -
sfr auxr = 0x8e;
// IE2
// 7 6 5 4 3 2 1 9
// - ET4 ET3 ES4 ES3 ET2 ESP1 ES2
sfr ie2 = 0xaf;
// timer
sfr TH2 = 0xd6;
sfr TL2 = 0xd7;
//define uart tx/rx port
sbit uart_rx = P3^0; // P3.0 rx
sbit uart_tx = P3^1; // P3.1 tx
u8 TEND,TING,tcnt,tbuf;
u8 REND,RING,rcnt,rbuf;
void Enable_Ext4(){
int_clkO = int_clkO | 0x40;
}
void Disable_Ext4(){
int_clkO = int_clkO & 0xbf;
}
void Timer2_config(){
// 1T mode
auxr = auxr & 0xf0;
auxr = auxr | 0x04;
// enable timer1 interrupt
ie2 = ie2 | 0x04;
// need to enable EA
}
void Timer2_Disable(){
auxr = auxr & 0xef;
}
void Timer2_Enable(u8 th1,u8 tl1){
TH2 = th1;
TL2 = tl1;
auxr = auxr | 0x10;
}
void Uart_Init(){
//P3.0 work in interrupt mode ( int4 falling )
Enable_Ext4();
// timer2 config
Timer2_config();
Timer2_Disable();
// need to enable EA
TEND = 1;
REND = 0;
TING = 0;
RING = 0;
uart_tx = 1;
}
void Ext4_Interrupt() interrupt 16 {
RING = 1;
REND = 0;
rcnt = 0;
rbuf = 0;
Timer2_Enable(0xfc,0xa0);
Disable_Ext4();
}
void Uart_SendByte( u8 tx_data ) {
tbuf = tx_data;
tcnt = 0;
TEND = 0;
TING = 1;
uart_tx = 0;
Timer2_Enable(0xfd,0xc0);
while( TEND != 1); // wait for tx complete!!
}
void Uart_LoopTest( ){
if( TEND && REND == 1 ) {
Uart_SendByte(rbuf);
REND = 0;
}
}
void Timer2_Interrupt() interrupt 12 {
if( RING ) {
if( rcnt == 0 ) {
Timer2_Disable();
rbuf = rbuf >> 1;
if( uart_rx ) rbuf = rbuf | 0x80;
Timer2_Enable(0xfd,0xc0);
}else if( rcnt == 8 ){
Timer2_Disable();
Enable_Ext4();
RING = 0;
REND = 1;
// recv done !!!
}else {
rbuf = rbuf >> 1;
if( uart_rx ) rbuf = rbuf | 0x80;
}
rcnt ++;
}else if( TING ) {
if( tcnt == 8) {
uart_tx = 1;
}else if( tcnt == 9) {
Timer2_Disable();
TING = 0;
TEND = 1;
// tx done !!!
}else{
uart_tx = tbuf & 0x01;
tbuf = tbuf >> 1;
}
tcnt ++;
}else{
// other things
}
}
- uart.h
#ifndef _UART_H_
#define _UART_H_
#include <reg51.h>
void Uart_Init(void);
void Uart_SendByte( unsigned char tx_data );
void Uart_LoopTest( void );
#endif
- main.c
#include <reg51.h>
#include "uart.h"
void Sys_Init(){
Uart_Init();
EA = 1;
}
void main(){
Sys_Init();
while(1) {
//Uart_LoopTest();
}
}
參考
- 定時(shí)器相關(guān)
https://blog.csdn.net/weixin_52853526/article/details/125180813 - 模擬串口程序
https://www.cnblogs.com/zjutlitao/archive/2018/03/31/8681049.html - 右移/左移和標(biāo)志位CY的關(guān)系
到了這里,關(guān)于STC15F100E單片機(jī)模擬串口的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!