We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi all. I am using two Nordic boards to communicate between two PCs using the serial ports. The program I have written works fine if I am using Hyper Terminal to input the characters one at a time. However, my main goal was to be able to send complete strings wirelessly from one PC to another and this is where the problem is. If I try sending a string, some of the characters go missing and never arrive at the receiver. I have included my code below:
#include <Nordic\reg24e1.h> struct RFConfig { unsigned char n; unsigned char buf[15]; }; typedef struct RFConfig RFConfig; #define ADDR_INDEX 8 // Index to address bytes in RFConfig.buf #define ADDR_COUNT 4 // Number of address bytes const RFConfig tconf = //configures the 16 tx bits //tconf rconf { // 0x04 0x05 - 2402MHz Channel 15, // 0x76 0x77 - 2459Mhz Channel 0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, 0xbb, 0x12, 0x34, 0x83, 0x6F, 0x76 }; const RFConfig rconf = { 15, 0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, 0xbb, 0x12, 0x34, 0x83, 0x6F, 0x05 }; volatile unsigned char rxbyte, rxcount,txcount, txbyte,check; void Delay100us(volatile unsigned char n) { unsigned char i; while(n--) for(i=0;i<35;i++) ; } void Init(void) { P0_ALT = 0x06; // Select alternate functions on pins P0.1 and P0.2 P0_DIR= 0x42; // P0.1 input, rest is output PWR_UP = 1; // Turn on Radio Delay100us(30); // Wait > 3ms SPICLK = 0; // Max SPI clock (XTAL/8) SPI_CTRL = 0x02; // Connect internal SPI controller to Radio } void InitRS(void) { TH1 = 230; // 9600@16MHz (when T1M=1 and SMOD=1) (ref page 95 of Datasheet) CKCON |= 0x10; // T1M=1 (/4 timer clock) PCON = 0x80; // SMOD=1 (double baud rate) SCON = 0x77; // Serial mode1, enable receiver(1010010) TMOD = 0x20; // Timer1 8bit auto reload (Page 84 of Datasheet TCON = 0x40; // Start timer1 (01000000) (Page 85 of Datasheet) } void PutChar(char c)//puts a char on the serial port { while (!TI) ; TI = 0; SBUF = c; } unsigned char SpiReadWrite(unsigned char b) //(wirelessly) transmits char b { EXIF &= ~0x20; // Clear SPI interrupt SPI_DATA = b; // Move byte to send to SPI data register while((EXIF & 0x20) == 0x00) // Wait until SPI hs finished transmitting ; return SPI_DATA; } void TransmitPacket(unsigned char b) { unsigned char i; CE = 1; //chip enabled (Activate TX mode) Delay100us(0); for(i=0;i<ADDR_COUNT;i++) SpiReadWrite(tconf.buf[ADDR_INDEX+i]); SpiReadWrite(b); //transmit CE = 0; //chip disabled (disable TX mode) Delay100us(3); // Wait ~300us } void Transmitter(unsigned char c) { unsigned char b; CS = 1; Delay100us(0); for(b=0;b<tconf.n;b++) { SpiReadWrite(tconf.buf[b]); } CS = 0; TransmitPacket(c); // Transmit data } void Receiver(void) { unsigned char b; CS = 1; Delay100us(0); for(b=0;b<rconf.n;b++) { SpiReadWrite(rconf.buf[b]); } CS = 0; CE = 1; Delay100us(100); { if (rxcount > 0) { EX4 = 0; //disable DR1 interupt b = rxbyte; //place recieved data into b rxcount = 0; PutChar(b); EX4 = 1; //enable DR1 interupt } } CE = 0; } void drirq(void) interrupt 10 { EXIF &= ~0x40; // Clear DR1 interrupt rxbyte = SpiReadWrite(0); rxcount = 1; } void main(void) { unsigned char RxChar; unsigned char TxChar; Init(); InitRS(); rxcount = 0; EX4 = 1; // Enable DR1 interrupt EA = 1; // Global interrupt enable for (;;) { if (RI) { TxChar = SBUF; Transmitter(TxChar); RI=0; } Receiver(); } }
I think the problem is due to the 10ms delay I have placed in the receiver. However, if I take this delay out, the receiver never works.
The problem could could possibly be somewhere else but I am not sure.
I appreciate any help.
- Thanks.
Hi Kunal, Instead of polling for serial data in main loop use interrupt based algorithm. Also I see error in using SpiReadWrite function
All the best