C8051F單片機(jī)和PC機(jī)串口通訊程序
//------------------------------------------------------------------
//串口編程--接收PC發(fā)過來的字符串,回發(fā)字符串.發(fā)送期間中斷控制LED燈閃爍
//采用外部晶振22.1184MHz 使用定時器1,方式2產(chǎn)生波特率,SMOD = 0或者1
//定時器初值X=256-SYSCLK*(SMOD+1)/(BAUDRATE*384)
//
//程序編寫: 郭郭
//編程時間: 2005,12,30
//最后修改: 2005,12,31
//------------------------------------------------------------------
#include <C8051F020.h>
//串口編程--接收PC發(fā)過來的字符串,回發(fā)字符串.發(fā)送期間中斷控制LED燈閃爍
//采用外部晶振22.1184MHz 使用定時器1,方式2產(chǎn)生波特率,SMOD = 0或者1
//定時器初值X=256-SYSCLK*(SMOD+1)/(BAUDRATE*384)
//
//程序編寫: 郭郭
//編程時間: 2005,12,30
//最后修改: 2005,12,31
//------------------------------------------------------------------
#include <C8051F020.h>
sfr16 TMR3RL = 0x92; //定時器3重裝載寄存器
sfr16 TMR3 = 0x94; //定時器3計數(shù)器
sfr16 TMR3 = 0x94;
#define uchar unsigned char
#define uint unsigned int
#define uint unsigned int
//------------------------------------------------------------------
//參數(shù)設(shè)置區(qū)
//------------------------------------------------------------------
#define BAUDRATE 4800 //波特率bps
#define CLKOUT 22118400 //外部晶振,修改也要修改OSCXCN
#define SMODVAL 0 //SMOD的值,修改請也修改PCONVAL
#define PCONVAL 0x00 //PCON的值,=0x00時SMOD0=0; =0x80時SMOD0=1
#define TXVAL (256-CLKOUT*(SMODVAL+1)/BAUDRATE/384) //定時器初值
#define MAX_LEN 10 //每次接收/發(fā)送字符串的長度
//參數(shù)設(shè)置區(qū)
//------------------------------------------------------------------
#define BAUDRATE 4800
#define CLKOUT 22118400
#define SMODVAL 0
#define PCONVAL 0x00
#define TXVAL (256-CLKOUT*(SMODVAL+1)/BAUDRATE/384)
#define MAX_LEN 10
//------------------------------------------------------------------
//全局變量
//------------------------------------------------------------------
sbit LED = P1^6; //LED 1亮 0滅
bit readFlag = 0; //讀標(biāo)志
uchar readCounts = 0; //已經(jīng)讀取的字符個數(shù),與MAX_LEN比較
uchar idata trdata[MAX_LEN]; //要接收/發(fā)送的字符串
//全局變量
//------------------------------------------------------------------
sbit LED = P1^6;
bit
uchar readCounts = 0;
uchar idata trdata[MAX_LEN];
//------------------------------------------------------------------
//子函數(shù)聲明
//------------------------------------------------------------------
void SYSCLK_Init(void); //系統(tǒng)時鐘初始化
void PORT_Init(void); //端口初始化
void UART0_Init(void); //串口UART0初始化
void Send_Char(uchar ch); //發(fā)送單個字符
void Send_String(uchar * str, uint len); //發(fā)送一個字符串
void UART0_ISR(); //串口中斷服務(wù)程序,接收字符
void Timer3_Init(uint counts); //定時器3初始化
void Timer3_ISR(void); //定時器3中斷服務(wù)程序
//子函數(shù)聲明
//------------------------------------------------------------------
void SYSCLK_Init(void);
void PORT_Init(void);
void UART0_Init(void);
void Send_Char(uchar ch);
void Send_String(uchar * str, uint len);
void UART0_ISR();
void Timer3_Init(uint counts);
void Timer3_ISR(void);
//------------------------------------------------------------------
//主函數(shù)
//------------------------------------------------------------------
void main(void)
{
WDTCN = 0xde; //禁止看門狗
WDTCN = 0xad;
//主函數(shù)
//------------------------------------------------------------------
void main(void)
{
}
//------------------------------------------------------------------
//子函數(shù)具體實(shí)現(xiàn)
//------------------------------------------------------------------
//子函數(shù)具體實(shí)現(xiàn)
//------------------------------------------------------------------
//系統(tǒng)時鐘初始化
void SYSCLK_Init(void)
{
uint i;
OSCXCN = 0x67; /采用外部晶振22.1184MHz,不分頻. 選型OSCXCN=0110,0111
for(i=0;i<256;i++); //等待>1ms
while(!(OSCXCN&0x80)); //查詢直到XTLVLD=1,晶振穩(wěn)定
OSCICN = 0x88;//切換到外部振蕩器,允許時鐘失效監(jiān)測器. OSCICN=1000,1000
}
void SYSCLK_Init(void)
{
}
//端口初始化
void PORT_Init(void)
{
XBR0 = 0x04; //允許UART0,RX,TX連到2個端口引腳. XBR0=0000,0100
XBR1 = 0x00;
XBR2 = 0x40; //交叉開關(guān)使能
P0MDOUT |= 0x03; //P0.0為推拉方式輸出,即TX0,RX0所在的端口 0000,0011
P1MDOUT |=0x40; //P1.6為推拉方式輸出,即LED所在的端口 0100,0000
}
void PORT_Init(void)
{
}
//串口初始化
void UART0_Init(void)
{
SCON0 = 0x50; //選擇串口方式1,波特率可變 SCON0=0101,0000
TMOD = 0x20; //選擇T1,方式2,自動再裝入8位計數(shù)器
TH1 = (int)TXVAL;//T1初值,根據(jù)波特率,時鐘等計算. 0xF4, bps=4800bps
TL1 = (int)TXVAL;
ES0 = 1; //UART0中斷開啟
TR1 = 1; //啟動定時器T1
PCON |= PCONVAL; //PCON=0x00,SMOD = 0 ; PCON=0x80,SMOD=1
TI0 = 1; //聲明TX0就緒,可以發(fā)送
TR0 = 1;
}
void UART0_Init(void)
{
}
//定時器初始化
void Timer3_Init(uint counts)
{
TMR3CN = 0x00; //禁止定時器T3,清TF3,采用SYSCLK/12為時基
TMR3RL = -counts; //初始化重裝載值
TMR3 = 0xffff; //設(shè)置為立即重裝載
EIE2 |= 0x01; //T3中斷開啟
TMR3CN |= 0x04; //啟動T3
}
void Timer3_Init(uint counts)
{
}
//發(fā)送單個字符
void Send_Char(uchar ch)
{
SBUF0 = ch; //送入緩沖區(qū)
while(TI0 == 0); //等待發(fā)送完畢
TI0 = 0; //軟件清零
}
void Send_Char(uchar ch)
{
}
//發(fā)送字符串,調(diào)用Send_Char() len字符串長度
void Send_String(uchar * str,uint len)
{
uint k = 0;
do
{
Send_Char(*(str + k));
k++;
} while(k < len);
void Send_String(uchar * str,uint len)
{
}
//定時器3中斷服務(wù)程序
void Timer3_ISR(void) interrupt 14 using 0
{
TMR3CN &= ~(0x80); //清TF3
LED = ~LED;
}
void Timer3_ISR(void) interrupt 14 using 0
{
}
//UART0中斷服務(wù)程序. 接收字符
void UART0_ISR(void) interrupt 4 using 1
{
uchar rxch;
if(RI0) //中斷標(biāo)志 RI0=1 數(shù)據(jù)完整接收
{
RI0 = 0; //軟件清零
rxch = SBUF0; //讀緩沖
if(readCounts>=MAX_LEN)
{
readCounts = 0;
readFlag = 1;
}
trdata[readCounts] = rxch; //存入數(shù)組,供發(fā)送
readCounts++;
}
}
void UART0_ISR(void) interrupt 4 using 1
{
}
//-------------------------------------------------------------
//程序結(jié)束
//--------------------------------------------------------------
//程序結(jié)束
//--------------------------------------------------------------
評論