Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

HI-TECH code example for send and receive sms with gsm module

Status
Not open for further replies.

samnang39

Full Member level 2
Full Member level 2
Joined
Oct 26, 2006
Messages
130
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,296
Location
Cambodia
Activity points
2,246
Dear all

could you please send me about code Hi-tech example that can send and receive SMS with GSM module.

Thanks in advance:smile:
 


Dear all
this is code in Hi-tech c compliler
#include <pic.h>
#include <htc.h>
__CONFIG(0x3F32);

#define _XTAL_FREQ 20000000
#define BAUD 9600

void pic_init(void);
void uart_init(void);

void uart_transmit(char data);
void uart_string(const char *s);

char gsm[90],rec[]={"+CMTI"};
unsigned int counter,z;

static void interrupt isr(void)
{
if(RCIF==1)
{
counter=0;
gsm[z]=RCREG;
if(z<88) z++;
} //*
if(TMR0IF==1) //Overflow interrupt flag
{
TMR0IF=0;
if(counter<20000) counter++;
if(counter==5000) z=0;
} //*/
}

main()
{
int i;
pic_init(); //initialize PIC
uart_init(); //initialize UART

for(i=0;i<=89;i++) gsm=0x20;

uart_string("AT+CMGS="); //Send message
uart_transmit(0x22); //"
uart_string("+601xxxxxxxx"); //Phone no
uart_transmit(0x22); //"
uart_transmit(0x0D); //Enter
uart_string("Hi, I'm PIC16F877A"); //Text message
uart_transmit(0x0D); //Enter
uart_string("Reply, if you receive this message");
uart_transmit(0x1A); //Ctrl+Z

}

void pic_init(void)
{
TRISA= 0b00000000;
TRISB= 0b00000000;
TRISC= 0b10000000;
OPTION= 0b00000000;
ADCON1= 0b00000110;
INTCON= 0b11100000;
PIE1= 0b00100000; //Enable RX interrupt
PORTA= 0b00000000;
PORTB= 0b00000000;
PORTC= 0b00000000;
}

void uart_init(void)
{
TXSTA=0b10100000;
RCSTA=0b10010000;
SPBRG=(int)(_XTAL_FREQ/(64.0*BAUD)-1);
}

void uart_transmit(char data)
{
while(TXIF==0) continue;
TXREG=data;
}

void uart_string(const char *s)
{
// while(*s)
// uart_transmit(*s++);
//*
while((*s)!='\0')
{
//Wait for TXREG Buffer to become available
while(!TXIF);

//Write data
TXREG=(*s);

//Next goto char
s++;
}
//*/
}


if i omit Timer0 interrupt in code above
if(TMR0IF==1) //Overflow interrupt flag
{
TMR0IF=0;
if(counter<20000) counter++;
if(counter==5000) z=0;
}
Usart transmit cannot work i don't know why?
 

I should mention, when using recent versions of the Hi-Tech C Compiler only use the following:


#include <htc.h>

not

#include <pic.h>

The pic.h header is included automatically by the htc.h header.



What model PIC are you using?

BigDog
 

When you removed the following from the ISR:

Code:
if(TMR0IF==1) //Overflow interrupt flag
{
   TMR0IF=0;
    if(counter<20000) counter++;
    if(counter==5000) z=0;
}

Did you disable the Timer Interrupt?

Code:
INTCON= 0b11[COLOR="#FF0000"]0[/COLOR]00000;

instead of this

Code:
INTCON= 0b11[COLOR="#FF0000"]1[/COLOR]00000;

BigDog
 
The next problem that
In my main program below don't have loop statement but why it work all statement in main again and again
Thank you in advance:smile:
main()
{
int i;
pic_init(); //initialize PIC
uart_init(); //initialize UART

for(i=0;i<=89;i++) gsm=0x20;

uart_string("AT+CMGS="); //Send message
uart_transmit(0x22); //"
uart_string("+601xxxxxxxx"); //Phone no
uart_transmit(0x22); //"
uart_transmit(0x0D); //Enter
uart_string("Hi, I'm PIC16F877A"); //Text message
uart_transmit(0x0D); //Enter
uart_string("Reply, if you receive this message");
uart_transmit(0x1A); //Ctrl+Z

}
 

In your case the system resets everytime as there is no end to the program... after the last line of the code, the controller seaches for the end of the program and since it does not find the end even after it reaches the last memory location , it again comes to first location and keeps on executing the same code again and again...

if you use while loop only a certain part of code gets executed.. without while loop and proper termination, the whole code gets executed repeadtedly...
if you want to stop it after one execution then use while(1); at the end of main ()...........
 
thanks for you reply

i used to write program in another compiler if in main don't have loop statement the process is not work again and again
but why Hi-tech compiler work like This :!:
 

it is there in all compilers, as there should be end to program. but it depends on the controller you are using and the memory it has.. it takes time to reach the end of memory and return again to main... the time factor varies... you can check in simulator window of all compilers, they will repeat the program at some point of time...

it is common in all compilers where program for specific controllers are written.....

---------- Post added at 12:07 ---------- Previous post was at 12:06 ----------

did it run properly with while (1);
 
Thanks so much
the problem is that the statement below i want it work only the first time
pic_init(); //initialize PIC
uart_init(); //initialize UART
And
the statement below i want it work in loop
for(i=0;i<=89;i++) gsm=0x20;

uart_string("AT+CMGS="); //Send message
uart_transmit(0x22); //"
uart_string("+601xxxxxxxx"); //Phone no
uart_transmit(0x22); //"
uart_transmit(0x0D); //Enter
uart_string("Hi, I'm PIC16F877A"); //Text message
uart_transmit(0x0D); //Enter
uart_string("Reply, if you receive this message");
uart_transmit(0x1A); //Ctrl+Z

how i should write it?

thanks in advance

---------- Post added at 07:50 ---------- Previous post was at 07:50 ----------

Thanks so much
the problem is that the statement below i want it work only the first time
pic_init(); //initialize PIC
uart_init(); //initialize UART
And
the statement below i want it work in loop
for(i=0;i<=89;i++) gsm=0x20;

uart_string("AT+CMGS="); //Send message
uart_transmit(0x22); //"
uart_string("+601xxxxxxxx"); //Phone no
uart_transmit(0x22); //"
uart_transmit(0x0D); //Enter
uart_string("Hi, I'm PIC16F877A"); //Text message
uart_transmit(0x0D); //Enter
uart_string("Reply, if you receive this message");
uart_transmit(0x1A); //Ctrl+Z

how i should write it?

thanks in advance
 

your code should be like this

Code:
pic_init(); //initialize PIC
uart_init(); //initialize UART 


while(1)
{
for(i=0;i<=89;i++) gsm[i]=0x20;

uart_string("AT+CMGS="); //Send message
uart_transmit(0x22); //"
uart_string("+601xxxxxxxx"); //Phone no
uart_transmit(0x22); //"
uart_transmit(0x0D); //Enter
uart_string("Hi, I'm PIC16F877A"); //Text message
uart_transmit(0x0D); //Enter
uart_string("Reply, if you receive this message");
uart_transmit(0x1A); //Ctrl+Z 

}
 
but all statement in main work again and again
so all statement work again and again
 

it will never work again and again... only the code within the while(1) loop will work infinetely..

if you are getting the output as you have told then there may be some problem with your compiler..........

---------- Post added at 12:30 ---------- Previous post was at 12:29 ----------

check by putting break points at every functions and run the code in simulator.. also check on hardware and tell us...
 
aoa dear...u r using PIC microcontroller...u hav to set the configuration bits so taht ur controller work properly according to waht u want....u hav to disable watchdog timer that sets automatically wen ur code ends....so the code repeats again...just disable WDT
cheer :)
if u need configuaration bits do elt me knw i lhav u by mail
regards jahanzaib
 

Thank for your reply. this is my configuration word
__CONFIG(0x3F32);
i think WDT already disabled
 

where is the code for receive sms and store to pic controller
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top