#include <REG2051.H> #include "SimUART.h"
sbit SimPortA_TX =P1^1; sbit SimPortA_RX =P1^2; sbit SimPortB_TX =P3^3; sbit SimPortB_RX =P3^4;
static unsigned char guSimPortA_Count =1; static unsigned char guSimPortB_Count =1;
static unsigned char bdata gSimUART_Status =0xcc; sbit gbSimPortA_Sending =gSimUART_Status^0; sbit gbSimPortA_Receiving =gSimUART_Status^1; sbit gbSimPortA_Txd =gSimUART_Status^2; sbit gbSimPortA_Rxd =gSimUART_Status^3; sbit gbSimPortB_Sending =gSimUART_Status^4; sbit gbSimPortB_Receiving =gSimUART_Status^5; sbit gbSimPortB_Txd =gSimUART_Status^6; sbit gbSimPortB_Rxd =gSimUART_Status^7;
static bit fTimeOverflow_A =FALSE; static bit fTimeOverflow_B =FALSE;
unsigned char code BitCtrl[] ={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
unsigned char SimUART_BaudSet(unsigned char PortID,unsigned char BaudRate) { unsigned char divrate,state;
state =!0; switch(BaudRate) { case BAUD9600: divrate =1; break; case BAUD4800: divrate =2; break; case BAUD2400: divrate =4; break; case BAUD1200: divrate =8; break; default: state =0; //0 for fail break; }
if(state) { switch(PortID) { case SIMPORTA: guSimPortA_Count =(divrate *COUNT_MULTIPLE); state =0x01; break; case SIMPORTB: guSimPortB_Count =(divrate *COUNT_MULTIPLE); state =0x10; break; case SIMPORTAB: guSimPortA_Count =(divrate *COUNT_MULTIPLE); guSimPortB_Count =(divrate *COUNT_MULTIPLE); state =0x11; break; default: state =0; } }
return(state); }
void SimUARTs_Send(unsigned char CtrlWord) { unsigned char count;
if((gSimUART_Status &0x33) ==0) { fTimeOverflow_A =FALSE; fTimeOverflow_B =FALSE; gbSimPortA_Txd =0; gbSimPortB_Txd =0; gbSimPortA_Sending =TRUE; gbSimPortB_Sending =TRUE;
while(!fTimeOverflow_A) //waiting for sending start-bit ; fTimeOverflow_A =FALSE; fTimeOverflow_B =FALSE;
for(count =0;count <8;count++) { gbSimPortA_Txd =gbSimPortB_Txd =(bit)(CtrlWord &BitCtrl[count]); while(!fTimeOverflow_A) //waiting for sending a data-bit ; fTimeOverflow_A =FALSE; fTimeOverflow_B =FALSE; }
gbSimPortA_Txd =1; gbSimPortB_Txd =1; while(!fTimeOverflow_A) //waiting for sending stop-bit ; fTimeOverflow_A =FALSE; fTimeOverflow_B =FALSE;
gbSimPortA_Sending =FALSE; gbSimPortB_Sending =FALSE; } }
bit SimUARTA_Receive(unsigned char *ptr) { static unsigned char BitCount =0; static unsigned char ReceivedByte =0;
if(gbSimPortA_Receiving) { if(fTimeOverflow_A) { fTimeOverflow_A =FALSE; if(BitCount >=8) { BitCount =0; gbSimPortA_Receiving =FALSE; if(gbSimPortA_Rxd) //Check for stop-bit { *ptr =ReceivedByte; return(TRUE); } } else { if(gbSimPortA_Rxd) ReceivedByte |=BitCtrl[BitCount]; else ReceivedByte &=(~(BitCtrl[BitCount])); BitCount++; } } } else BitCount =0; return(FALSE); }
bit SimUARTB_Receive(unsigned char *ptr) { static unsigned char BitCount =0; static unsigned char ReceivedByte =0;
if(gbSimPortB_Receiving) { if(fTimeOverflow_B) { fTimeOverflow_B =FALSE; if(BitCount >=8) { BitCount =0; gbSimPortB_Receiving =FALSE; if(gbSimPortB_Rxd) //Check for stop-bit { *ptr =ReceivedByte; return(TRUE); } } else { if(gbSimPortB_Rxd) ReceivedByte |=BitCtrl[BitCount]; else ReceivedByte &=(~(BitCtrl[BitCount])); BitCount++; } } } else BitCount =0; return(FALSE); }
//Timer0 interrupt routine void Timer0(void) interrupt TF0_VECTOR using 1 { static unsigned char data SimPortA_SampleCount =0; static unsigned char data SimPortB_SampleCount =0; bit RxdPin_A,RxdPin_B;
RxdPin_A =SimPortA_RX; RxdPin_B =SimPortB_RX; // SimPortA_TX =gbSimPortA_Txd; // SimPortB_TX =gbSimPortB_Txd; //Port A if((gSimUART_Status &0x03) ==0) { if(RxdPin_A ==0) //find out receive start-bit { SimPortA_SampleCount++; if(SimPortA_SampleCount >=2) { if(!gbSimPortA_Sending) //working as simplex-system gbSimPortA_Receiving =TRUE; //start receive SimPortA_SampleCount =0; } } else SimPortA_SampleCount =0; } else { SimPortA_SampleCount++; if(SimPortA_SampleCount >=guSimPortA_Count) { fTimeOverflow_A =TRUE; if(gSimUART_Status &0x01) //Sending SimPortA_TX =gbSimPortA_Txd; if(gSimUART_Status &0x02) //Receiving gbSimPortA_Rxd =RxdPin_A; SimPortA_SampleCount =0; } }
//Port B if((gSimUART_Status &0x30) ==0) { if(RxdPin_B ==0) //find out receive start-bit { SimPortB_SampleCount++; if(SimPortB_SampleCount >=2) { if(!gbSimPortB_Sending) //working as simplex-system gbSimPortB_Receiving =TRUE; //start receive SimPortB_SampleCount =0; } } else SimPortB_SampleCount =0; } else { SimPortB_SampleCount++; if(SimPortB_SampleCount >=guSimPortB_Count) { fTimeOverflow_B =TRUE; if(gSimUART_Status &0x10) //Sending SimPortB_TX =gbSimPortB_Txd; if(gSimUART_Status &0x20) //Receiving gbSimPortB_Rxd =RxdPin_B; SimPortB_SampleCount =0; } } } |