运行在AVR上,把变量封装在结构体,在用结构体指针来访问,还可以无限扩展UART数量呢
#include <iom32v.h>
#include <macros.h>
#include <string.h>
#include "Vga_drv.h"
#include "uart.h"
#include "Key_Scan.h"
#include "bmp.h"
#include "Isr.h"
#include "ps2_driver.h"
#include "eeprom.h"
#include "global.h"
#include "CRC.h"
#define uint32 unsigned long
#define SC_RXD 6
#define SC_RXD_SET_INPUT() {DDRD&=(~BIT(SC_RXD));}
#define SC_RXD_GET() ((PIND&(BIT(SC_RXD))))
#define SC_TXD 7
#define SC_TXD_0() {PORTD&=(~BIT(SC_TXD));}
#define SC_TXD_1() {PORTD|=BIT(SC_TXD);}
#define SC_TXD_SET_OUTPUT() {DDRD|=BIT(SC_TXD);}
uint8 sc1_rxd_scan_ct=0;
uint8 sc1_rxd_scan_next_time=0;
uint8 sc1_rxd_scan_step=0;
uint8 sc1_rxd_dat;
uint8 sc1_rxd_ready=0;
uint8 sc1_rxd_tmpdat;
uint8 sc_txd_ready=0;//模拟串口变量
uint8 sc_txd_bit_pt=0;
uint8 sc_txd_data=0;
uint8 sc_txd_ct=0;
void SC_Recv_Pro(uint8 dat){
}
void SC_RxdSrv(void){
if(sc1_rxd_scan_step==0){
if(!SC_RXD_GET()){
sc1_rxd_scan_step=1;
return;
}
}
if(sc1_rxd_scan_step==1){
if(!SC_RXD_GET()){
sc1_rxd_scan_step=2; //rxd start bit ok,goto next step
sc1_rxd_scan_ct=0;
sc1_rxd_scan_next_time=3;
sc1_rxd_tmpdat=0;
return;
}
else{
sc1_rxd_scan_step=0; //rxd start bit is avalid
return;
}
}
if(sc1_rxd_scan_step>=2){
sc1_rxd_scan_ct++;
if(sc1_rxd_scan_ct<sc1_rxd_scan_next_time) return;
sc1_rxd_scan_ct=0;
if(sc1_rxd_scan_step<10){
sc1_rxd_tmpdat>>=1;
if(SC_RXD_GET()){
sc1_rxd_tmpdat|=0x80;
}
sc1_rxd_scan_step++;
return;
}
if(sc1_rxd_scan_step==10){
if(SC_RXD_GET()){
sc1_rxd_dat=sc1_rxd_tmpdat;
sc1_rxd_ready=1;
//Receive a byte OK
#if 0
sc_txd_data=sc1_rxd_dat;
sc_txd_ready=1;
#endif
#if 0
Test_Uart1(sc1_rxd_dat);
#endif
SC_Recv_Pro(sc1_rxd_dat);
}
sc1_rxd_scan_step=0;
return;
}
}
}
void SC_TxdSrv(void){
sc_txd_ct++;
if(sc_txd_ct<3) return;
sc_txd_ct=0;
if(sc_txd_ready){ //Data Ready
if(sc_txd_bit_pt<10){
if(sc_txd_bit_pt==0){
SC_TXD_0(); //Start BIT
}
else{
if(sc_txd_bit_pt>=9){
SC_TXD_1(); //End BIT
}
else{ //数据位
if((sc_txd_data>>(sc_txd_bit_pt-1))&0x01){
SC_TXD_1();
}
else{
SC_TXD_0();
}
}
}
}
if(sc_txd_bit_pt>10){
sc_txd_bit_pt=0; //发送完后延时两个时钟,复位各标志
sc_txd_ready=0;
}
else{
sc_txd_bit_pt++; //位指针自加
}
}
}
void SC_send_char(uint8 b){
sc_txd_data=b;
sc_txd_ready=1;
while(sc_txd_ready==1);
}
void SC_send_str(uint8 *str){
while((*str)!=0){
SC_send_char(*str);
str++;
}
}
void init_SimComIO(void){
SC_TXD_SET_OUTPUT();
SC_TXD_1();
SC_RXD_SET_INPUT();
}
#pragma interrupt_handler ISR_T1:8
void ISR_T1(void)
{
//compare occured TCNT1=OCR1A
SC_RxdSrv();
SC_TxdSrv();
}
结构体封装多串口
typedef sim_uart_def{
uint8 sc1_rxd_scan_ct;
uint8 sc1_rxd_scan_next_time;
uint8 sc1_rxd_scan_step;
uint8 sc1_rxd_dat;
uint8 sc1_rxd_ready;
uint8 sc1_rxd_tmpdat;
uint8 sc_txd_ready;//模拟串口变量
uint8 sc_txd_bit_pt;
uint8 sc_txd_data;
uint8 sc_txd_ct;
}TYPE_SIM_UART_DEF;
TYPE_SIM_UART_DEF su1,su2,su3,su4,su5,su6;
void SC_RxdSrv(TYPE_SIM_UART_DEF *pSU);
void SC_TxdSrv(TYPE_SIM_UART_DEF *pSU);