Saturday, September 28, 2013

SIM300 Module

SIM 300:
       When accessing GSM networks comes in picture in embedded system ,one of the best way is to use SIMCOM modules which provides RS-232 interfacing and supports for AT command sets (Commands used for calling,Messaging etc) and usually easy to integrate with embedded modules.In this post i have tried to explain how to Interface the SIM300 module to LPC2148 using UART and send message to a no.

Hardware Connection :
      The SIM module is connected to the LPC2148 by using the UART port.Simple 2 wire connection is made between the board and the module.The Rx and Tx pins are connected accordingly.


CODE:

#include <LPC214X.H>
#include "Serial.h"
#include "Utility.h"
#include "LCD4.h"
int main (void)
{
unsigned char x[200];
int PosAck,y=0;
//hex code of "at enter" command for checking comm status of uart-reply should be ok
 unsigned char AckComm[3]={0x61,0x74,0x0d};
//hex code - for checking balance  -here sim-airtel so,atd*123# 
 unsigned char BalChkComm[9]={0x61 ,0x74 ,0x64 ,0x2A ,0x31 ,0x32 ,0x33 ,0x23,0x0d};
//hex code for selecting text mode-
 unsigned char SmsConfigComm[10]={0x41 ,0x54 ,0x2B ,0x43  ,0x4D ,0x47 ,0x46 ,0x3D ,0x31,0x0d};
// Setting No-9439421905   Bikash
unsigned char SendMsgComm[21]={0x61,0x74,0x2B,0x63,0x6D,0x67,0x73,0x3D,0x22,0x39,0x34 ,0x33 ,0x39 ,0x34 ,0x32 ,0x31 ,0x39,0x30 ,0x35,0x22,0x0D};
 //message body NISt (change it to have diff content )
unsigned char SendMsgBody[5]={0x4E,0x49,0x53,0x54,0x1A};
//lcd data is used temoporarily to store data for lcd display
 unsigned char LcdData[16];
//data used to clear the display
unsigned char LcdClear[16]={0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20};
int i;
LCD4_init (&IOPIN0, 20/*D4*/, 12/*RS*/, 14/*E*/);//initialization of LCD in 4 bit mode
DelayProc(2*CCLOCK);
PINSEL0 = 0;
PINSEL1 = 0;
PINSEL2 &= 0x0000000C;
PINSEL2 |= 0x00000030;
DelayProc(0.2 * CCLOCK);


 UART0_init(9600, 15000, length_8_bit, stop_bit_1, parity_disable, parity_odd);//UART initilization connected to PC
 UART1_init(9600, 15000, length_8_bit, stop_bit_1, parity_disable, parity_odd);//UART initilizaton connected to MODEM

LCD4_sendstr (0,2,"Modem Status :");
      for(i=0;i<3;i++)
                    {
                  UART1_sendchar(AckComm[i]);//Sending At command to check Status
                      }
                      for(i=0;i<7;i++)
                              {
                              x[i]=UART1_getchar();//receiving Status
                              UART0_sendchar(x[i]);
                                }
LcdData[0]=x[5];
LcdData[1]=x[6];

  LCD4_sendstr (1,1,LcdData);
DelayProc(2*CCLOCK);


LCD4_sendstr (1,1,LcdClear);
LCD4_sendstr (0,2,LcdClear);


LCD4_sendstr (0,2,"Account Balance:");
DelayProc(2*CCLOCK);
       for(i=0;i<9;i++)
                    {
                  UART1_sendchar(BalChkComm[i]);//Sending Command to Check balance

                      }
          
                    for(i=0;i<100;i++)
                              {
                              x[i]=UART1_getchar();//getting reply
                              UART0_sendchar(x[i]);
if (x[i]==0x3A && y<2)
{
PosAck=i;
//LCD4_sendstr (1,1,"reached");
y++;
                                    }
                                }

LcdData[0]=x[PosAck];
LcdData[1]=x[PosAck+1];
LcdData[2]=x[PosAck+2];
LcdData[3]=x[PosAck+3];
LcdData[4]=x[PosAck+4];
LcdData[5]=x[PosAck+5];
LCD4_sendstr (1,1,LcdData);


            DelayProc(2*CCLOCK);

                                LCD4_sendstr (1,1,LcdClear);
LCD4_sendstr (0,2,LcdClear);

LCD4_sendstr(0,2,"TEXT mode:");
   DelayProc(2*CCLOCK);
      for(i=0;i<10;i++)
                    {
                  UART1_sendchar(SmsConfigComm[i]);//cONfiguring MODEM in TEXT Mode

                      }
                    for(i=0;i<15;i++)
                              {
                              x[i]=UART1_getchar();
                              UART0_sendchar(x[i]);
if (x[i]==0x31)
{
PosAck=i;
//LCD4_sendstr (1,1,"reached");
//y++;
                                    }
                                }

LCD4_sendstr(1,1,"ok");

DelayProc(2*CCLOCK);
LCD4_sendstr (1,1,LcdClear);
LCD4_sendstr (0,2,LcdClear);



LCD4_sendstr(0,2,"Sending Message");

DelayProc(2*CCLOCK);
for(i=0;i<21;i++)
                    {
                  UART1_sendchar(SendMsgComm[i]);//Adding no to send Message

                      }
                   for(i=0;i<25;i++)
                              {
                              x[i]=UART1_getchar();
                              UART0_sendchar(x[i]);
                                }
     for(i=0;i<5;i++)
                    {
                  UART1_sendchar(SendMsgBody[i]);//Sending Message Body To Receipient. ctrl+z hex code 1A is used at last.

                      }
               for(i=0;i<170;i++)
                              {
                              x[i]=UART1_getchar();
                              UART0_sendchar(x[i]);
                                }

                 LCD4_sendstr(1,1,"ok");
DelayProc(2*CCLOCK);
 LCD4_sendstr (1,1,LcdClear);
LCD4_sendstr (0,2,LcdClear);

}

16X2 LCD Interfacing With Microcntrollers

What is 16x2 LCD:
              LCD is an output display device abbreviation for Liquid Crystal Display .Many varieties and types of LCD comes into market and out of them 16x2 LCD is the simplest one and is easy to be interfaced.As the name suggest it is a 2 line diplay and each line supports up-to 16 characters.

Pin Diagram & Connections:
            The 16x2 LCD is a  16 pin device having a PCB mounted controller and a display.The controller listen to the data ports and generate the pattern to be displayed according to the i/p data.Out of 16b pins 8 pins are data pins,2 pins for power supply,1 for contrast setting of the display,1 is Register Select,1 is Read/Write select,1 enable and 2 for the background lightening LED.


Data Pins: The device has 8 data pins which receives data to be displayed from the interfaced device.All the 8 pins  carries data in 8 bit mode but in case of 4 bit (Explained later) only  MSB(D5 to D7) pins are used and rest 4 pins are grounded.One more job is done by the data lines ,that is these transfers the control signals also from the interfaced circuit to the LCD.

  Power Supply : The device needs an external power supply to work.About required voltage can be found from the LCD datasheet.

   Contrast Setting : This pins takes input from outside interfaced circuit or voltage source in the
range 'Gnd' to 'Vcc'.And according to the voltage level the contrast of the LCD is set.This is simply done by using one Potentiometer(Variable Resistance) between 'Vcc' and 'Gnd' and putting the third pin to the LCD  'Vee' pin'.

    Register Select: Two types of data comes to the LCD Controller one is the 'Control' data  and the second type of data are the data to be displayed on the screen.But their arrives the confusion which data is what ,as all are send to the same data lines.To get rid of the confusion and complexity of differentiation the LCD is provided with a Register Select(R/S) pin which tells the LCD controller the available data is actually what ?If '0'is set on R/S pin the controller treats the available data as DATA.If it is set to '1' it treats it as Control Signal .

      Enable:This pin enables the LCD to communicate with other devices.Only when their is a pulse at this pin the values given to the LCD data pins received by the LCD.So to establish any type of communication with the LCD you need to supply a pulse at the Enable pin.

     R/W: The R/W pin is responsible for all read and write operations.When the pin is LOW it performs the WRITE operations and when the pin is HIGH it performs the READ operations.During Write the data available at the data pins are written to the respective registers depending upon the RS value.And when Read operation is carried out the ,the only reading operation is carried out that is reading the Busy Flag.The red data is available on pin D7.


LCD Controller Memories:
             Different types of memory are present inside the LCD controller namely
                               DDRAM  ( 80 Bytes ):
                                        
                               • CGROM :

                               • CGRAM :
                                        These are used for Special character generation.

                               • BF - Busy Flag :
                                        This shows that a process is currently going on or not inside the controller.The BF is set to '1'  if  a process   is going on or else to '0' if nothing is going on. the BF can be red through the D7 pin of the LCD module.
                               • Instruction Register (IR) : 
                                        Instruction register temporarily holds the instructions coming to the module from the data bus (D0-D7).
                               • Data Register (DR) :
                                        Data register temporarily holds the data coming to the module from the data bus (D0-D7). 

Operating Modes of the Controller:
           
        The module is operated in 2 modes.First one is 4 bit mode and second one is 8 bit modes.Both modes are different in the sense that in case of 4 bit mode only 4 data pins are used whereas in 8 bit mode 8 data pins are used.The 4 bit mode saves 4 pins of the interfacing circuits which becomes important when LCD is to be interfaced to a circuit whose output pins are less.But the 4 bit mode is slower as compared to the 4 bit mode as data has to be transmitted in 4 bit blocks and are transmitted separately as MSB and LSB.
    4bit mode;
          In 4 bit mode the MSB(D5-D7) data pins are only connected and other  connections are same as the 8 bit mode or usual configuration.The remaining data pins are grounded.

    




    8bit mode:
          In 8 bit mode all data pins are  connected and other connections are  like the enable pin,Register Select Pin,R/W pin are connected.

Programming:
   Sending Command :
       1.Move Command to port or D0 to D7 pins.
         2.Select the Command register by setting R/S pin 0.
         3.Select Write Operation by selecting R/W pin to 0.
         4.Send a pulse to the enable pin.
   Sending Data :
       1.Move Data to port or D0 to D7 pins.
         2.Select the Data register by setting R/S pin 1.
         3.Select Write Operation by selecting R/W pin to 0.
         4.Send a pulse to the enable pin.
   Initialization of LCD  in 8 bit mode:

       1.Delay for 20 millisecond.
         2.Send command 0x03
         3. Delay for 10 millisecond.
         4.Send command 0x03.
         5.Delay for 10 millisecond.
         6.Send command 0x03.
         7.Delay for 10 millisecond.
         8.Send command   0x02     //Cursor Home
         9.Send command   0x38 //8 Bit 2 Line Interface
       10.Send command   0x08   //Display Blank
       11.Send command   0x0c  // Display on Cursor off
       12.Send command   0x01 // Clear Screen
       13.Send command   0x06   // Entry Mode
Initialization of LCD in 8 bit mode:

       1.Delay for 20 millisecond.
         2.Send command 0x03
         3. Delay for 10 millisecond.
          
         4.Send command 0x03.
         5.Delay for 10 millisecond.
         6.Send command 0x03.
         7.Delay for 10 millisecond.
         8.Send command   0x02     //Cursor Home
         9.Send command   0x
28 //4 Bit 2 Line Interface
       10.Send command   0x08   //Display Blank
       11.Send command   0x0c  // Display on Cursor off
       12.Send command   0x01 // Clear Screen
       13.Send command   0x06   // Entry Mode
In 4 bit mode the data and commands are send in two parts each having 4 bits so, the Command and Data send functions are different for 4 bit and 8 bit modes.
   4-Bit mode: 
          1.Mask lower 4-bits(MSB is exposed)
          2.Send to the LCD port
          3.Send enable signal
          4.Mask higher 4-bits(LSB is exposed)
          5.Send to LCD port
          6.Send enable signal
.
Example Code using uVision on LPC2148 board :

#include <LPC214X.H>
#include "Serial.h"
#include "Utility.h"
 void cw()
   {
      IOCLR0=0x00001000;  //  Clear RS  pin
                                            // Write Grounded
      IOSET0=0x00004000; //    Set Enable
      DelayProc(CCLOCK * 0.05);
      IOCLR0=0x00004000;  // Clear Enable
      DelayProc(CCLOCK * 0.05);
    }
void dw()
    {
       IOSET0=0x00001000;    //  Set RS  pin
                                               // Write Grounded
       IOSET0=0x00004000;
       DelayProc(CCLOCK * 0.05);
       IOCLR0=0x00004000;
       DelayProc(CCLOCK * 0.05);
     }
SendComm(unsigned int Comm)
     {
   Comm = Comm << 16;
   IO0SET = Comm;
   cw();
   IO0CLR= Comm;
   Comm = Comm << 4;    IO0SET = Comm;
    cw();
   IO0CLR = Comm;
}
SendData( unsigned int Data)
{
   Data = Data << 16;
   IO0SET = Data;
   dw();
   IO0CLR = Data;
   Data = Data << 4;
   IO0SET= Data;
   dw();
   IO0CLR = Data;
}  
char writestring(char *s)
{
   int i=0;
   char c;
   c=*s;
    while(c!='\0')
      {
        SendData(c);
         i=i+1;
         c=*(s+i);
      }
}
void initialization()
{
    DelayProc(CCLOCK * 0.15);
SendComm(0x03);
DelayProc(CCLOCK * 0.05);
SendComm(0x03);
DelayProc(CCLOCK *0.05);
SendComm(0x03);
DelayProc(CCLOCK * 0.05);

  SendComm(0x02);//Cursor Home


//SendComm(0x28); //4 bit 2 rows 5x7 font

//SendComm(0x2c); //4 Bit 2 Rows 5x10 font
SendComm(0x2c);

//SendComm(0x08);//display off cursor off blink off

//SendComm(0x0c);//display on cursor off blink off
SendComm(0x0E);//display on cursor on blink off
//SendComm(0x0F);//display on cursor off blink  on
//SendComm(0x0E);

SendComm(0x01);//Clear screen

//SendComm(0x06);//increment shift 0
SendComm(0x07);//increment shift 1
//SendComm(0x04);//Decrement shift 0
//SendComm(0x05);//Decrement shift 1
//SendComm(0x06);
}
int main()
{
PINSEL0=0x00000000;
PINSEL1=0x00000000;
IODIR0=0xffffffff;
initialization();

while(1)

{
 // SendComm(0x00000001);
  SendComm(0xc4);
  //SendData('A');
  //DelayProc(CCLOCK * 5);
writestring("CHANDAN PATRA");

//DelayProc(CCLOCK * 0.05);

//SendComm(0x01);//Clear screen
//SendComm(0x07);//increment shift 1
//writestring("NIST");
  }
}