Livingston
Full Member level 2
- Joined
- Nov 26, 2007
- Messages
- 129
- Helped
- 3
- Reputation
- 6
- Reaction score
- 1
- Trophy points
- 1,298
- Location
- Chennai-India
- Activity points
- 2,104
hello all!
I am using PIC18F46K22. I need to communicate with gprs module and an rf receiver .
I am receiving the rf data using interrupts.This is my first priority. Once i receive the data it needs to be sent to the server using a gprs module. I am communicating with the gprs module without any interrupts.
I first initialize the gprs module and then ping the server and then i send the data.I have checked my code implementation in proteus . I can see that the mcu is sending the necessary AT commands to initialize the gprs module but i fail to get any data from the other serial port. Eventhough any data comes to the other serial port which receives rf data, my MCU doesnt go into the interrupt service routine.
I also have a function which needs to get time from the second uart,i am unable to do it.
i get reentrancy errors saying that same function cant be used in two threads.I am calling the gettime function from ISR.
the code is below
THANK YOU.
I am using PIC18F46K22. I need to communicate with gprs module and an rf receiver .
I am receiving the rf data using interrupts.This is my first priority. Once i receive the data it needs to be sent to the server using a gprs module. I am communicating with the gprs module without any interrupts.
I first initialize the gprs module and then ping the server and then i send the data.I have checked my code implementation in proteus . I can see that the mcu is sending the necessary AT commands to initialize the gprs module but i fail to get any data from the other serial port. Eventhough any data comes to the other serial port which receives rf data, my MCU doesnt go into the interrupt service routine.
I also have a function which needs to get time from the second uart,i am unable to do it.
i get reentrancy errors saying that same function cant be used in two threads.I am calling the gettime function from ISR.
the code is below
Code:
/*******************************************************************************/
//AT command set
char response[] = "OK";
char response2[] = "CONNECT OK";
char at1[] = "AT\r\n";
char csq[] = "AT+CSQ\r\n";
char creg[] = "AT+CREG?\r\n";
char cgatt[] = "AT+CGATT?\r\n";
char cstt[] = "AT+CSTT=\"AIRTELGPRS.COM\",\"\",\"\"\r\n";
char ciicr[] = "AT+CIICR\r\n";
char cifsr[] = "AT+CIFSR\r\n";
char cipstart[] = "AT+CIPSTART=\"TCP\",\"203.197.114.67\",\"6900\"\r\n";
char cipsend[] = "AT+CIPSEND\r\n";
char clts[12] = "AT+CLTS=1\r\n";
char cclk[11] = "AT+CCLK?\r\n";
char cgsn[10] = "AT+CGSN\r\n";
char prompt[] = "> ";
char err[] = "ERROR";
/*******************************************************************************/
char rxchar; //variable for storing data from UART1
int i = 0,j=0,count=0,msgs=0; // array counter and flag to write once the desired pattern is read
char buf[1009]; // array to store the received charaters from UART1
char hexstrn[3],out[9]; //strings to convert hextostring
char *rc_ptr,*tx_ptr; //pointers to point to the received data and transmitting data
enum{WAIT_FOR_1C,WAIT_FOR_3bytes,RECEIVING} state=WAIT_FOR_1C; //state to check
//for the required chars
/*********************************************************************************/
char gsmbuf[200]; //storing data from gprs module
char timebuf[19]="14/05/29;15:24:45;"; //manual time buf
unsigned char imei[8]; //stores imei number
char latlng[22] = "+24.345656;+80.878383" ; //lat and lng manually entered
char chartosend; //check for >
char databuf[70]="ERF;01;"; //data string
unsigned int packet=0;
/*********************************************************************************/
void Send_AT_cmd(char command[],char gsmbuffer[],char response[],char err[]){
int k=0;
UART2_Write_Text(command);
while(1){
if(strstr(gsmbuffer,response)== 0){
while(UART2_Data_Ready()==0);
gsmbuffer[k]=UART2_Read();
k++;
}
else break;
if (strstr(gsmbuffer,err)!=0){
LATA0_bit=1;
}
}
}
//get imei number of the device
void getIMEI(unsigned char imei[]){
int k=0;
UART2_Write_Text(cgsn);
while(strstr(gsmbuf,response)==0){
while(UART2_Data_Ready()==0);
gsmbuf[k]=UART2_Read();
k++;
}
for(k=0;k<6;k++){
imei[k]= *(strstr(gsmbuf,"OK") -11 + k); //store the last 6 but 1 bytes
}
memset(gsmbuf,'\0',200);
}
/********************************************************************************/
//char *getTIME(char timebuf[],char gsmbuf[]){
//unsigned int k=0;
//memset(timebuf,'\0',50);
//UART2_Write_Text(cclk); //cclk command
//while(strstr(timebuf,"OK")==0){
//gsmbuf[k]=UART2_Read();
//k++;
//}
//*(strstr(gsmbuf,":")+20) = ';';
//*(strstr(gsmbuf,":")+21) = '\0';
//strcpy(timebuf,strstr(gsmbuf,"\"")+1) ;
//*(strstr(timebuf,",")) = ';';
//return(timebuf);
//}
//send data to server
void Send_Data(char databuf[]){
unsigned int k=0;
UART2_Write_Text(cipstart); //ping the server
while(1){
if(strstr(gsmbuf,response2)!= 0){
while(UART2_Data_Ready()==0);
gsmbuf[k]=UART2_Read();
k++;
}
else if(strstr(gsmbuf,"ALREADY CONNECTED")==0){
break;
}
else if(strstr(gsmbuf,"CONNECT FAIL")==0){
LATA2_bit=0;
}
else break;
}
memset(gsmbuf,'\0',200);
UART2_Write_Text(cipsend); //command to send data
while(1){
while(UART2_Data_Ready()==0);
chartosend = UART2_Read();
if(chartosend=='>'){
while(UART2_Data_Ready()==0); //wait for the prompt >
chartosend = UART2_Read();
if(chartosend==' ') break;
}
}
UART2_Write_Text(databuf); //send the data to gprs
UART2_Write(0x1A);
UART2_Read_Text(gsmbuf,"CLOSED",255); //close the connection
memset(gsmbuf,'\0',200);
}
/*******************************************************************************/
//interrupt service routine to read data from uart1
void interrupt () {
if (RC1IF_bit == 1) {
LATC0_bit=1; // test the interrupt for uart rx
rxchar = UART1_Read(); //read a byte from UART1
switch(state){
case WAIT_FOR_1C: //wait till 31 is arrived
if(rxchar == 0x31) {
state = WAIT_FOR_3bytes;
}
break;
case WAIT_FOR_3bytes:
i++;
if(i>=3){
for(i=0;i<18;i++){
*rc_ptr=timebuf[i]; //I am attaching a manual time string
//please find the getTime function above which i thought to use
//for getting date and time
//getTime func is giving reentrancy errors
rc_ptr++;
}
i=0;
state=RECEIVING;
}
break;
case RECEIVING:
if(count==168){
count=0;
rc_ptr=buf;
}
*rc_ptr=rxchar;
rc_ptr++;
count++;
if(count%4==0){
rc_ptr=rc_ptr+2;
msgs=msgs+1;
state=WAIT_FOR_1C ;
}
break;
} // end interrupt
}
if(OERR1_bit){
LATA=0xFF;
}
}
/******************************************************************************/
/*******************************************************************************/
void main() {
ANSELA = 0x00;
ANSELB = 0x00;
ANSELC = 0x00;
ANSELD = 0x00;
TRISA = 0xFF;
PORTA = 0x00;
LATA = 0xFF;
TRISB = 0x00;
PORTB = 0x00;
LATB = 0x00;
TRISC = 0xC0;
PORTC = 0x00;
LATC = 0x00;
TRISD = 0xC0;
PORTD = 0x00;
LATD = 0x00;
UART2_Init(9600);
Delay_ms(2000);
//RC2IE_bit = 1;
UART1_Init(19200);
Delay_ms(100);
//INTCON.GIE = 1; //enable global interrupt
//INTCON.PEIE = 1;
//PIE1.RCIE = 1; //enable receive interrupt.
RC1IE_bit = 1; // turn ON interrupt on UART1 receive
RC1IF_bit = 0; // Clear interrupt flag
PEIE_bit = 1; // Enable peripheral interrupts
GIE_bit = 1; // Enable GLOBAL interrupts
rc_ptr=buf;
tx_ptr=buf;
UART1_Write_Text("start");
Send_AT_cmd(at1,gsmbuf,response,err); //send at
memset(gsmbuf,'\0',200);
getIMEI(imei); //get imei
Send_AT_cmd(creg,gsmbuf,response,err); //send at+creg
if(*(strstr(gsmbuf,":")+4) != ('1'||'5')) {
LATA1_bit =0;
}
memset(gsmbuf,'\0',200);
Send_AT_cmd(cgatt,gsmbuf,response,err);
if(*(strstr(gsmbuf,":")+2) == '1'){
LATB=0xFF;
}
memset(gsmbuf,'\0',200);
Send_AT_cmd(cstt,gsmbuf,response,err);
memset(gsmbuf,'\0',200);
Send_AT_cmd(clts,gsmbuf,response,err);
memset(gsmbuf,'\0',200);
Send_AT_cmd(ciicr,gsmbuf,response,err);
memset(gsmbuf,'\0',200);
UART2_Write_Text(cifsr);
Delay_ms(2000);
while(1) { // Begin endless loop
// This section is where you tell the program what to do with the data once it
// has all arrived
Delay_ms(100);
while(msgs!=0){ //read all the messages
//when i get a string i store 24 bbytes including date,time and some data from the rf receiver
for(j=0;j<4;j++){
ByteToHex(*(tx_ptr+19),hexstrn); //convert input data to string
strcat(out,hexstrn);
tx_ptr++;
}
tx_ptr=tx_ptr+2; //increment the tx_ptr to avoid errors
out[9]=';';
strcpy(&databuf[7],out);
strcpy(&databuf[17],tx_ptr);
sprintf(&databuf[34],"%s;%s;%4u#",latlng,imei,packet); //create the string to be sent
packet++;
if(packet == 10000){
packet=0; //if 10000 packets sent reset
}
UART1_Write_Text(databuf);
memset(out,'\0',10);
UART1_Write(13); //new line char
Send_Data(databuf); //send the data
if( tx_ptr - buf ==1008){ //if tx_ptr reaches the end of buffer loop back to beginning
tx_ptr = buf;
}
msgs=msgs-1;
}
} // end while
}
THANK YOU.