??? 08/31/05 07:39 Read: times |
#100318 - *Groan* Here is my inital code Responding to: ???'s previous message |
Okay okay guy. I understand your point. I have developed my bit-banging code in C...
I intend to use 4200 Baud therefore 1/baud which yields 416uS. When the start bit occurs, the first delay will be generated bitwidth/2 (208uS). The Interrupt Service Routine activates which repeats at 8 times for every 416us in order to capture the data bits because i want to detect the data bit in the middle of data bit'speriod. Finally the delay is generated at twice time (208us + 208us = 416us) to check if the stop bit is 1 then the data bits (buffer_G) is placed in the P1... #include <AT89X52.H> #define INTERRUPT_Timer_2_Overflow 5 #define OUTPUT P1 void Timer_2_Init(void); void hardware_delay_208us(); /* --------------------------------------------------------------- */ char buffer_G; int i_G=0; sbit DATA_RX = P3^2; void main(void) { Timer_2_Init(); // Set up Timer 2 EA = 1; // Globally enable interrupts while(1) { // Wait for Start bit while(DATA_RX == 1) { } //Delay (208uS) hardware_delay_208us(); // Is it logic zero if (DATA_RX == 0) { // Activate Interrupt Service Routine ET2=1; TR2=1; // Wait for Interrupt Service Routine completes 8 times while(i_G != 8) { } // set i_G to be zero i_G=0; // Hardware delay 208 (Repeat twice to get 416us) hardware_delay_208us(); hardware_delay_208us(); // Stop bit if (DATA_RX != 1) { // Remove the content in buffer_G buffer_G = buffer_G & 0x00; } else { OUTPUT = OUTPUT & buffer_G; } } } } /* --------------------------------------------------------------- */ void Timer_2_Init(void) { T2CON = 0x04; // Load Timer 2 control register TH2 = 0xFE; // Load Timer 2 high byte RCAP2H = 0xFE; // Load Timer 2 reload capt. reg. high byte TL2 = 0x6F; // Load Timer 2 low byte RCAP2L = 0x6F; // Load Timer 2 reload capt. reg. low byte ET2 = 0; TR2 = 0; } /* --------------------------------------------------------------- */ void X(void) interrupt INTERRUPT_Timer_2_Overflow { i_G++; if ( DATA_RX ==1) { buffer_G=(buffer_G & 0x80); } else { buffer_G=(buffer_G & 0x00); } // Shift the data to right at seven times if (i_G<7) { buffer_G=buffer_G>>1; } if (i_G == 8) { //Disable Interrupt Service Routine ET2=0; TR2=0; } } /* --------------------------------------------------------------- */ void hardware_delay_208us() { // Configure Timer 0 as a 16-bit timer TMOD &= 0xF0; // Clear all TO bits (T1 left unchanged) TMOD |= 0x01; // Set required TO bits(T1 left unchanged) ET0=0; // Value for 50ms delay TH0=0xFF; // Timer 0 initial value (High Byte) TL0=0x3F; // Timer 0 initial value (LOW Byte) TF0=0; // Clear overflow flag TR0=1; // Start timer 0 while (TF0==0); //Loop until Timer 0 overflows (TF0==1) TR0=0; // Stop Timer 0; } Any feedback? Max |