51單片機(jī)模擬串口C語(yǔ)言程序代碼
#include "string.h"
#include"stdio.h"
#include
sfr16 DPTR = 0x82;
typedef unsigned char INT8U;
typedef unsigned int INT16U;
typedef unsigned char uint8_t;
typedef unsigned int uint16_t;
typedef unsigned int uchar;
typedef unsigned char byte;
typedef unsigned int word;
#define YES 1
#define NO 0
//定義使用哪個(gè)定時(shí)器, 只可定義一個(gè)
#define TIMER_0
//#define TIMER_1
//定義串口收、發(fā)送管腳。
sbit rs_TXD = P2^1;
sbit rs_RXD = P2^0;
//根據(jù)定時(shí)器確定參數(shù)
#ifdef TIMER_0
#endif
#ifdef TIMER_1
#endif
INT8U bdata rs_BUF; //串行收、發(fā)時(shí)用的移位暫存器。
sbit rs_BUF_bit7 = rs_BUF^7; //移位暫存器的最高位。
INT8U rs_shift_count; //移位計(jì)數(shù)器。
INT8U bdata rsFlags;
sbit rs_f_TI = rsFlags^0; //0:正在發(fā)送; 1: 一個(gè)字符完畢
sbit rs_f_RI_enable = rsFlags^1; //0:禁止接收; 1:允許接收
sbit rs_f_TI_enable = rsFlags^2; //0:禁止發(fā)送; 1:允許發(fā)送
//選擇以下一個(gè)晶體頻率
//#define Fosc 6000000 //6MHz
#define Fosc 11059200 //11.059MHz
//#define Fosc 12000000
//#define Fosc 18432000
//#define Fosc 20000000
//#define Fosc 24000000
//#define Fosc 30000000
//#define Fosc 40000000
//選擇以下一個(gè)波特率:
//#efine Baud 300 //11.059MHz時(shí),baud 最低為 300
//#define Baud 1200
//#define Baud 2400
//#define Baud 4800
#define Baud 9600
//#define Baud 14400
//#define Baud 19200
//#define Baud 28800
//#define Baud 38400
//#define Baud 57600
//收、發(fā)一位所需定時(shí)器計(jì)數(shù)
#define rs_FULL_BIT0 ((Fosc/12) / Baud)
#define rs_FULL_BIT (65536 - rs_FULL_BIT0)
#define rs_FULL_BIT_H rs_FULL_BIT >> 8 //收、發(fā)一位所需定時(shí)器計(jì)數(shù)高位
#define rs_FULL_BIT_L (rs_FULL_BIT & 0x00FF) //收、發(fā)一位所需定時(shí)器計(jì)數(shù)低位
//檢測(cè)起始位的時(shí)間間隔所需定時(shí)器計(jì)數(shù)
#define rs_TEST0 rs_FULL_BIT0 / 4 //波特率較低時(shí)可以除以 3 或除以 2
#define rs_TEST ((~rs_TEST0))
#define rs_TEST_H rs_TEST >> 8 //高位
#define rs_TEST_L rs_TEST & 0x00FF //低位
//發(fā)送起始位所需定時(shí)器總計(jì)數(shù)
#define rs_START_BIT 0xFFFF - (Fosc/12/Baud) + 0x28
#define rs_START_BIT_H rs_START_BIT >> 8 //發(fā)送起始位所需定時(shí)器計(jì)數(shù)高位
#define rs_START_BIT_L rs_START_BIT & 0x00FF //發(fā)送起始位所需定時(shí)器計(jì)數(shù)低位
#define rs_RECEIVE_MAX 32 //最大接收長(zhǎng)度
INT8U idata rs232buffer[rs_RECEIVE_MAX]; //收、發(fā)緩沖區(qū)
INT16U ReceivePoint; //接收數(shù)據(jù)存儲(chǔ)指針
void soft_rs232_interrupt( void );
#ifdef TIMER_0
#endif
#ifdef TIMER_1
#endif
void soft_rs232_init (void) //串口初始化
{
}
void soft_receive_init() //監(jiān)測(cè)起始位
{
}
void soft_receive_enable() //允許接收
{
}
void soft_send_enable (void) //允許發(fā)送
{
}
評(píng)論