Pages

About Lan Ahmad

My photo
Pendang, Kedah, Malaysia
It's all about my real life!!!

1 Ogos 2012 - Wednesday


Deleting SMS Messages from Message Storage (AT+CMGD)

The AT command +CMGD (command name in text: Delete Message) is used to delete SMS message(s) from message storage. The message storage area from which SMS messages are deleted is specified by the +CPMS AT command (command name in text: Preferred Message Storage).


AT+CMGD=index[,flag]


The SMS specification has defined these flag values: 0, 1, 2, 3 and 4.
  • 0. Meaning: Delete only the SMS message stored at the location index from the message storage area. This is the default value.
  • 1. Meaning: Ignore the value of index and delete all SMS messages whose status is "received read" from the message storage area.
  • 2. Meaning: Ignore the value of index and delete all SMS messages whose status is "received read" or "stored sent" from the message storage area.
  • 3. Meaning: Ignore the value of index and delete all SMS messages whose status is "received read", "stored unsent" or "stored sent" from the message storage area.
  • 4. Meaning: Ignore the value of index and delete all SMS messages from the message storage area

Sending SMS using PIC Micro through GSM Modem

Program code for sending sms using PIC Microcontroller.

#include <16f876a.h>
#fuse hs, nowdt, noput, noprotect, nodebug, brownout, nowrt
#use delay(clock=20000000)
#use rs232(baud=9600, parity=N, xmit=PIN_C6, rcv=PIN_C7, bits=8, stream=GSM)

#int_rda
void serial_isr()
{
   char c;
   c=fgetc(GSM);
}

void main()
{
    enable_interrupts(INT_RDA);
    enable_interrupts(GLOBAL);
    
    fprintf(GSM, "AT\n\r");
    delay_ms(7000);
    fprintf(GSM, "AT+CMGF=1\n\r");
    delay_ms(3000);
    fprintf(GSM, "AT+CMGS=\"+6**********\"\n\r");
    delay_ms(3000);
    fprintf(GSM, "hello FROM mcu");
    putc(26);
    
    while(true)
    {
      delay_ms(1000);
    }
}

30 July 2012 - Monday

Serial Communication with Hyper Terminal. 



#include <pic.h>
#use delay(clock=20000000)
#fuses hs,nowdt,nolvp,noprotect
#byte portb=6
#byte portc=7
#use rs232(baud=9600,xmit=pin_c6,rcv=pin_c7, parity=n)


int mystatus=0; //here can't declare byte coz not detected by compiler
char mydata;


void main()
{
int count=0;


set_tris_c(0b10001000); //here c6 must be 0, c7 must be 1 (input)
printf("Enter your character : A or S or Z or X");


while(true)
{
if(kbhit()) //monitor incoming charactor from RS232

   mydata=toupper(getch()); //receive charactor dan save in mydata
   printf("\n\rYou entered: %c",mydata); //display on hyper terminal
   count++;
   mystatus=1;
}
if (mystatus==1)
{
if(mydata=='A')
{ output_high(pin_c4);} //led on
if (mydata=='S')
{ output_low(pin_c4);} //led off
if (mydata=='Z')
{ output_high(pin_c1);} //buzzer on
if (mydata=='X')
{ output_low(pin_c1);} //buzzer off
mystatus=0;
}//end if
}//end while
}//end void main

Explanation some Program Code:-

  •         Toupper ()

Toupper () is special function to convert input lowercase letter to uppercase letter. If the input data is ‘a’ so that the toupper () will convert the character to uppercase ‘A’.


  •         Kbhit ()
Kbhit () is use to check or monitor the incoming character from hardware RS232. When a key is pressed, this function will returns to true or non-zero. When nothings pressed, kbhit () will returns false or zero.

  •          Getch ()
Getch () is returns an 8-bit character. This function is to gets a single-byte (8-bit) character from the terminal. This functions only allow a single character as an input from hardware RS232 to the PIC 16F877A.

27 July 2012 - Friday

Troubleshooting Serial Communication. Cannot connect PIC with Hyper Terminal.

Serial Communication

Serial Communication


Introduction
Serial communication is a way enables different equipments to communicate with their outside world. It is called serial because the data bits will be sent in a serial way over a single line.

A personal computer has a serial port known as communication port or COM Port used to connect a modem for example or any other device, there could be more than one COM Port in a PC.

Advantages of Serial Communication

Serial communication has some advantages over the parallel communication. One of the advantages is transmission distance, serial link can send data to a remote device more far then parallel link. Also the cable connection of serial link is simpler then parallel link and uses less number of wires.

Serial link is used also for Infrared communication, now many devices such as laptops & printers can communicate via inferred link.


Example coding for Serial Communication:-
#include <pic.h>
#use delay(clock=20000000)
#fuses hs,nowdt,nolvp,noprotect, nobrownout
#use rs232(baud=9600,xmit=pin_c6,rcv=pin_c7, parity=n)


void main()
{
int count=0;
while(true)
{
printf("\f**Welcome to Malaysia**\n\r");
printf("**counter value is %u **", count);
count+=1;
delay_ms(1000);
}
}


Compiler: PIC C Compiler

Get 'OK' from GSM Modem


#include <pic.h>
#device adc=8
#use delay (clock=20000000)
#fuses hs,nolvp,nowdt,noprotect,nobrownout
#use rs232(baud=9600, bits=8, xmit=pin_C6, rcv=pin_C7, parity=n, stream=GSM)
#byte porta=5
#byte portb=6
#byte portc=7
#include "flex_lcd.c"

volatile int  counter_search=0;            //counter to traverse the array
volatile int  counter_read=0;             //counter to fill the array
volatile Char Recieve_String[70];   //buffer for incoming data
int GET_OK(int Stop_Search);             // Just searches for an OK.
int TEST_AT();

void main()
{
       
         SET_TRIS_A( 0x00 );     // to avoid floating pins
         SET_TRIS_B( 0x00 );
         OUTPUT_B(0X00);
         SET_TRIS_C( 0x80 );
       
         enable_interrupts(INT_RDA);  // enable RDA interrupts
         enable_interrupts(GLOBAL);   // enable global ints
       
         delay_ms(500);
         lcd_init();
         lcd_gotoxy(3,1);
         lcd_putc("GET RESPONSE");
         delay_ms(1000);
       
         delay_ms(1000);
         TEST_AT();

}

int TEST_AT()
{
         counter_read=0;

         lcd_gotoxy(8,2);
         lcd_putc("AT");
         printf("AT\n\r");                //send command

         While(counter_read<=8)             //"A T \R \R \N O K \R \N" characters returned by modem
         {
         }

         counter_read=0;
         Delay_ms(500);

         if(GET_OK(0X0A))
         {  lcd_gotoxy(8,2);
            lcd_putc("OK");
            Return(1); }
         else return(0);

}

int GET_OK(Stop_Search)
{
         counter_search=0;

         while((Recieve_String[counter_search]!='O')&&(counter_search<Stop_Search))
         {
            counter_search++;     //obvious
         }

         if((Recieve_String[counter_search]=='O')&&(Recieve_String[counter_search+1]=='K'))
         {
            counter_search=0;
            Return(1);
         }
         Return(0);
         counter_search=0;
}


#int_RDA
void RDA_isr()
{
   Recieve_String[counter_read]=getchar(GSM);
   counter_read++;
   if(counter_read==69)counter_read=0;
}

This program code have some problem, where the #int_RDA can't work properly. The interrupts can be recall and the response get stuck.

GSM Modul Model

GSM Modem
GSM Modem

1. Sending SMS
Flow of AT Command that must be use to communicate with the GSM Modem. The following AT command will be use in Hyper Terminal or in PIC program code.



Hyper Terminal: Send SMS using AT Command


Source: Cytron Website

UART Modul

UART stands for Universal Asynchronous Receiver Transmitter. Address involved in the UART Module is TXSTA, RCSTA and SPBRG.





UART signals to send and receive data over the three wires TX, RX and GND. UART signals in Synchronous or Asynchronous type. Synchronous transmission requires the sender and receiver share the same clock signal. Asynchronous transmission allows the data is sent without a clock signal sent but the sender and receiver must operate at the same timing parameters known Baud Rate.


Baud Rate Formula



Setting Baud Rate BRGH will determine Formula. For example, using a 20MHz crystal, Low Speed ​​Asynchronous Baud Rate can be produced from 1.221 Hz-312, 500Hz, High Speed ​​Asynchronous Baud Rate can be produced from 4.883 Hz-1, 250kHz and can produce Synchronous Baud Rate from 19.53kHz-5MHz.

RS232 Data Interface Cable


Electronic data communications between elements will generally fall into two broad categories: single-ended and differential. RS232 (single-ended) was introduced in 1962 and widely used through the industry nowadays.

RS232 Pin Out Male


RS232 Data Interface is use to interface PIC28P board with GSM modem. In this application, RS232 only use 3 out of 9 pin of RS232 cable. Pin 2 is for transmitted (TXD), pin 3 is for received (RXD) and pin 5 for the ground (GND). RS232 is connected using SUB-D 9 Pins Male to connect with GSM modem. 

Sub-D 9 Pins Male

Circuit Diagram of PIC16F876A

Circuit Diagram for PIC16F876A