any ideas on how i can write a code to transmit an AT command from 8051 to zigbee serially? thanks in advance
What problems do you see?
Does the 8051 need to know what device is connected to the serial port when it's about to send serial data? Isn't it enough that the 8051 and the external device have electrically compatible signals and that you know what baud rate, number of data bits and stop bits and parity settings to use?
Thanks for the reply. this is the code that i currently got, it is to turn on a port which i connected to an LED, when i receive a raw data !A@ from the zigbee. they are connected serially. next i want to do is when i receive a raw data from the zigbee serially, i want to send out an AT command instead to the zigbee for it to send out.
#include <DS89C4xx.h> //8051 header file
#define CR 0x0D
sbit mybit=P1^5;
unsigned char code start_header='!'; //to check where is the start of the data unsigned char code end_header='@'; //to check where is the end of the data unsigned char idata zigbeerxd[50]; //to store data from SBUF0 in serial interrupt unsigned char idata read[50]; //to read data from zigbeerxd[] in the main unsigned char idata rawdata[14]; //to store the data transmitted from zigbee unsigned char idata mynodeID[3]; //to check received data whether it is my ID unsigned char x,i,j,k,z,u,h; //declare variables for checking use unsigned char count; //to read serial data in the serial interrupt unsigned char read_count; //to read data from serial buffer unsigned char get_raw;
void delay (unsigned int value) { unsigned int x, y; for (x=0;x<value;x++) for (y=0;y<500;y++); } void serial_int (void) interrupt 4 { if (RI_0==1) { if(count==50) //check if array is full count=0; //restart from 0 zigbeerxd[count]=SBUF0; //storing the data from serial buffer into array count++; //increase the count RI_0=0; } }
void Read_Zigbee (void) { if(read_count!=count) //check for any unread data? { if(read_count==50) //go back to start position of array read_count=0; read[read_count]=zigbeerxd[read_count]; //read the data if (i==1) { if(read[read_count]==end_header) //check if data is '@'(end header) { k=0; //reset the values
i=0; get_raw=1; //raise the get_raw flag } else if (k<=13) //to prevent the array from overflow { rawdata[k]=read[read_count]; //store the data in rawdata[] array k++; } } else if(read[read_count]==start_header) //check the data is '!'(start header) i++; read_count++; } }
void check_data (void) { z=0; if(x==1); { if(rawdata[x]==mynodeID[1]) //check data if it's 'A' z++; }
if(z==1) { mybit=1; //toggle LED delay(2000); mybit=0; } }
void main (void) { delay(500); //delay to wait for zigbee to warm up count=0; //make count zero at first
read_count=0; get_raw=0; mynodeID[1]='A'; mybit=0; TMOD=0x20; //Use Timer1 TH1=0xfd; //9600 BAUD RATE, 8 BIT DATA, NO PARITY TR1=1; //Start Timer1 SCON0=0x50; //SERIAL MODE1, 8 BIT DATA, 1 STOP BIT, 1 START BIT, NO PARITY ES0=1; //enable serial interrupt for port 0 EA=1; //enable global interrupt RI_0=0; //reset receive interrupt flag delay(500); //wait for PPI to reset
while(1) { Read_Zigbee(); //function to read the new data from serial interrupt array
if(get_raw==1) //check if there is new data to check? { check_data(); //check the data get_raw=0; } } }
Your source code is illegible because you didn't follow the instructions on how to post source code - they are quite clearly stated, as this picture shows: www.danlhenry.com/.../keil_code.png
View all questions in Keil forum