+ Post New Thread
Results 1 to 7 of 7
  1. #1
    Newbie level 5
    Points: 621, Level: 5

    Join Date
    Oct 2009
    Posts
    8
    Helped
    1 / 1
    Points
    621
    Level
    5

    sending SMS from PIC to GSM modem

    Can you please help find the mistake on this code because it cn't send SMS after programming it into PIC16f877

    please help me. Submission is due tomorrow



    #include <pic.h>
    #include <conio.h>
    #include <stdio.h>

    //#include "always.h"
    //#include "delay.h"

    __CONFIG(XT & WDTDIS & PWRTDIS & BORDIS & LVPEN & WRTEN &
    DEBUGEN & DUNPROT & UNPROTECT);

    #ifndef _SERIAL_H_
    #define _SERIAL_H_

    #define BAUD 9600
    #define FOSC 4000000L
    #define NINE 0 /* Use 9bit communication? FALSE=8bit */

    #define DIVIDER ((int)(FOSC/(16UL * BAUD) -1))
    #define HIGH_SPEED 1

    #if NINE == 1
    #define NINE_BITS 0x40
    #else
    #define NINE_BITS 0
    #endif

    #if HIGH_SPEED == 1
    #define SPEED 0x4
    #else
    #define SPEED 0
    #endif




    #define RX_PIN TRISC7
    #define TX_PIN TRISC6



    void serial_setup(void)
    {

    SPBRG=DIVIDER;
    BRGH=HIGH_SPEED; //data rate for sending
    SYNC=0; //asynchronous
    SPEN=1; //enable serial port pins
    CREN=1; //enable reception
    SREN=0; //no effect
    TXIE=0; //disable tx interrupts
    RCIE=0; //disable rx interrupts
    TX9=0; //8-bit transmission
    RX9=0; //8-bit reception
    TXEN=0; //reset transmitter
    TXEN=1; //enable the transmitter
    }

    unsigned char dummy;

    #define clear_usart_errors_inline \
    if (OERR) \
    { \
    TXEN=0; \
    TXEN=1; \
    CREN=0; \
    CREN=1; \
    } \
    if (FERR) \
    { \
    dummy=RCREG; \
    TXEN=0; \
    TXEN=1; \
    }

    //writes a character to the serial port
    void putch(unsigned char c)
    {
    while(!TXIF) //set when register is empty
    {
    clear_usart_errors_inline;
    CLRWDT();
    }
    TXREG=c;
    _delay(60);
    }

    //gets a character from the serial port without timeout
    unsigned char getch(void)
    {
    while(!RCIF)
    {
    CLRWDT();
    clear_usart_errors_inline;
    }
    return RCREG;
    }

    unsigned char getch_timeout(void)
    {
    unsigned char i;
    unsigned int timeout_int;

    // retrieve one byte with a timeout
    for (i=2;i!=0;i--)
    {
    timeout_int=timeout_int_us(240000);

    while (hibyte(timeout_int)!=0) //only check the msb of the int for being 0, it saves space, see always.h for macro
    {
    CLRWDT();
    timeout_int--;
    if (RCIF)
    {
    return RCREG;
    }
    }
    }
    return 0;
    }


    void clear_usart_errors(void)
    {
    clear_usart_errors_inline;
    }



    /*
    writes a character to the serial port in hex
    if serial lines are disconnected, there are no errors
    */

    void putchhex(unsigned char c)
    {
    unsigned char temp;

    // transmits in hex

    temp=c;

    c=(c >> 4);
    if (c<10) c+=48; else c+=55;
    putch(c);

    c=temp;

    c=(c & 0x0F);
    if (c<10) c+=48; else c+=55;
    putch(c);
    }

    void putinthex(unsigned int c)
    {
    #define ramuint(x) (*((unsigned int *) (x)))
    #define ramuint_hibyte(x) (*(((unsigned char *)&x)+1))
    #define ramuint_lobyte(x) (*(((unsigned char *)&x)+0))
    #define ramuchar(x) (*((unsigned char *) (x)))

    putchhex(ramuint_hibyte(c));
    putchhex(ramuint_lobyte(c));

    #undef ramuint(x)
    #undef ramuint_hibyte(x)
    #undef ramuint_lobyte(x)
    #undef ramuchar(x)
    }

    //if there has been a previous timeout error from getch_timeout, this returns TRUE
    unsigned char usart_timeout(void)
    {
    // return usart_timeout_error;
    return 0;
    }

    /*
    writes a character to the serial port in decimal
    if serial lines are disconnected, there are no errors
    */
    void putchdec(unsigned char c)
    {
    unsigned char temp;

    temp=c;
    //hundreds
    if ((c/100)>0) putch((c/100)+'0');
    c-=(c/100)*100;

    //tens
    if (((temp/10)>0) || ((temp/100)>0)) putch((c/10)+'0');
    c-=(c/10)*10;

    //ones
    putch((c/1)+'0');
    }

    void putst(register const char *str)
    {
    while((*str)!=0)
    {
    putch(*str);
    if (*str==13) putch(10);
    if (*str==10) putch(13);
    str++;
    }
    }




    /* Serial initialization */
    #define init_comms()\
    RX_PIN = 1; \
    TX_PIN = 1; \
    SPBRG = DIVIDER; \
    RCSTA = (NINE_BITS|0x90); \
    TXSTA = (SPEED|NINE_BITS|0x20)

    /*void putch(unsigned char);
    unsigned char getch(void);
    unsigned char getche(void);*/

    void serial_setup(void);
    void putch(unsigned char c);
    unsigned char getch(void);
    void putst(register const char * str);
    unsigned char usart_timeout(void);
    void putchdec(unsigned char c);
    void putchhex(unsigned char c);
    void putinthex(unsigned int c);
    void Read_AD_Input(void);

    #endif



    /* This function initializes the A/D converter so that analog data can be
    received from channel AN0 of the microcontroller */
    void Initialize_AD(void)
    {
    ADCON1 = 0x8E; /* Configure AN0 for +5V reference */
    ADCON0 = 0x41; /* Select A/D converter clock */
    }


    /* This function reads data from the A/D converter and stores it in
    variable yk */
    float Yk;
    void Read_AD_Input(void)
    {
    unsigned int adc_value;
    GODONE=1; /* Start A/D conversion */
    while(GODONE)continue; /* Wait until convertion completes */
    adc_value = (ADRESH<<2) + (ADRESL>>6);
    Yk= 5 * (float)adc_value / 1023;
    }



    unsigned char;



    void init(void)
    {

    TRISA = 1;
    TRISB = 0;
    TRISC = 0;
    //TRISD = 0;
    }



    char High;
    char counter;

    const char GSM_0 = "AT$TT";
    const char GSM_1 = "AT+CLCK='SC',0,00000"; //disable PIN
    const char GSM_2 = "AT+CMGF=0";
    const char GSM_3 = "AT+CMGS=37<CR>069162860280F801000B916256597941F30 000114927555A9D269F4E10B1482D0EA94522<Ctrl+z>";




    void main(void)
    {
    init();
    counter = 0;
    init_comms();
    High = 1;
    PORTB = 0;
    PORTA = 0;
    PORTC = 0;

    #define putlf putst("\n")





    putch(GSM_0);
    _delay(2000); // wait half second


    putch(GSM_1);
    _delay(2000);

    putch(GSM_2);
    _delay(2000);


    if(PORTA = High )
    {

    putch(GSM_3);


    }



    }

    •   Alt26th January 2010, 01:28

      advertising

        
       

  2. #2
    Banned
    Points: 7,016, Level: 19

    Join Date
    Apr 2009
    Location
    New Jersey, USA
    Posts
    671
    Helped
    170 / 170
    Points
    7,016
    Level
    19

    Re: sending SMS from PIC to GSM modem

    Quote Originally Posted by ATcom
    //writes a character to the serial port
    void putch(unsigned char c)
    {
    while(!TXIF) //set when register is empty
    {
    clear_usart_errors_inline;
    CLRWDT();
    }
    TXREG=c;
    _delay(60);
    }

    void main(void)
    {
    .
    .
    .
    putch(GSM_0);
    _delay(2000); // wait half second

    putch(GSM_1);
    _delay(2000);

    putch(GSM_2);
    _delay(2000);
    }
    First of all, your code would be more readable if you used the "code" tags
    to format it

    One of your problems is that you are using your 'putch' function to try to send a string - it will only send the first character - you should be using your 'putst' fucntion.



  3. #3
    Newbie level 5
    Points: 621, Level: 5

    Join Date
    Oct 2009
    Posts
    8
    Helped
    1 / 1
    Points
    621
    Level
    5

    Re: sending SMS from PIC to GSM modem

    thanks for your reply i did that change and used putst() function. When I simulate this code using PIC16F877, MAX232, and Virtual Terminal in place of modem, it reflects that data is being trnsmitted but when I perfom it pratically it fails.

    I was wondering if there is anything I left out in the code. Can you please help me to find what might be the source.

    please help



    •   Alt26th January 2010, 16:57

      advertising

        
       

  4. #4
    Banned
    Points: 7,016, Level: 19

    Join Date
    Apr 2009
    Location
    New Jersey, USA
    Posts
    671
    Helped
    170 / 170
    Points
    7,016
    Level
    19

    Re: sending SMS from PIC to GSM modem

    Quote Originally Posted by ATcom
    ...but when I perfom it pratically it fails.
    How is it failing, exactly what is the problem you are having?



  5. #5
    Advanced Member level 2
    Points: 4,409, Level: 15
    Achievements:
    7 years registered
    ZASto's Avatar
    Join Date
    Jan 2008
    Location
    Messy Corner, 44°47'31.56"N, 20°28'9.16"E
    Posts
    533
    Helped
    109 / 109
    Points
    4,409
    Level
    15

    sending SMS from PIC to GSM modem

    Well, simulating a circuit is one thing, real work is another. Never trust the simulator.

    You should "sniff" the communication between GSM module and your uC, that will give you the real situation where things are going wrong.

    I did a project with 16F877A and Telit GM862-GPS in 4 days (first two just spent in reading Telit doccus), using my second computer as sniffer.
    Make no mistake between my personality and my attitude.
    My personality is who I am.
    My attitude depends on who you are.



  6. #6
    Newbie level 5
    Points: 621, Level: 5

    Join Date
    Oct 2009
    Posts
    8
    Helped
    1 / 1
    Points
    621
    Level
    5

    Re: sending SMS from PIC to GSM modem

    The problem is there is no output at the seiral ports. Especially RC6 for transmission. I was hoping i would get some output voltage at that pin because im pushing out some character with the function putst() but there is non.

    Is there anything else i have left out in the code. please help and thanks in advance



    •   Alt26th January 2010, 23:05

      advertising

        
       

  7. #7
    Newbie level 1
    Points: 454, Level: 4

    Join Date
    May 2010
    Location
    Sg
    Posts
    1
    Helped
    0 / 0
    Points
    454
    Level
    4

    Re: sending SMS from PIC to GSM modem

    i guess im a little too late.

    in ur coding, u set ur TRIS for ur output port - as input. and therefore u cant send thru it.

    TX_PIN = 1; <- ur pin is an input, not an output



--[[ ]]--