#AP NEW GSM Interfacing with LPC2148 (Two Files)

// Save this file as main.c

#include "lpc214x.h"

#include "stdio.h"
#include "string.h"


void delay(unsigned int time)
{
unsigned int i,j;
for(i=0;i<time;i++)
for(j=0;j<5000;j++);
}


void GSM_cmd(unsigned char *cmd)
{
printf("\r\ncommand:  %s",cmd);
printf("response: ");
UART1_PutS(cmd);
delay(100);
}

int main()
{
unsigned char *msg = "This is message from GSM Modem..\r";

Uart0Init(9600);
Uart1Init(9600);
printf("GSM program\r\n");

GSM_cmd("ATE0\r\n"); //Turn OFF echo
delay(3000);

GSM_cmd("ATD9226094924;\r\n"); //Call   (CHANGE THIS NUMBER)
delay(20000);
GSM_cmd("ATH0\r");     //disconnect call
delay(3000);

GSM_cmd("AT+CMGF=1\r\n"); //Send SMS: Select Text mode
delay(1000);
GSM_cmd("AT+CSCS=\"GSM\"\r\n"); //GSM Character Set
delay(1000);
GSM_cmd("AT+CMGS=\"9226094924\"\r\n"); //Number   (CHANGE THIS NUMBER)
delay(1000);
UART1_PutS(msg); //Send string
delay(1000);
UART1_PutChar(0x1A); //SUBSTITUTE character <CTRL+Z>

while(1);
return 0;
}




// *************************************************************************
//  Save this file as UART.c

#include "stdio.h"
#include "LPC214x.h"
// #include "UART.h"

void Uart0Init(unsigned int);
void Uart1Init(unsigned int);
unsigned char UART0_GetChar(void);
unsigned char UART0_PutChar(unsigned char);
unsigned char UART1_GetChar(void);
unsigned char UART1_PutChar(unsigned char);
void UART1_PutS(unsigned char*);

void UART1_isr(void) __irq
{
  UART0_PutChar(UART1_GetChar());
 VICVectAddr = 0;
}


void Uart0Init(unsigned int baudrate)   
{
 unsigned int FDiv;               
   PINSEL0 |= 0x00000005;           //Enable RxD0 and TxD0                 
    U0LCR = 0x83;                   // 8 bits, no Parity, 1 Stop bit
 FDiv = (15000000 / 16 ) / baudrate ; //
 U0DLM = FDiv /256;     //0x00;
 U0DLL = FDiv %256;     //0x97;     
    U0LCR = 0x03;      // DLAB = 0 
}


void Uart1Init(unsigned int baudrate)   
{
 unsigned int FDiv;               
   PINSEL0 |= 0x00050000;           //Enable RxD1 and TxD1                 
    U1LCR = 0x83;                   // 8 bits, no Parity, 1 Stop bit 
 FDiv = (15000000 / 16 ) / baudrate ; //
 U1DLM = FDiv /256;     //0x00;
 U1DLL = FDiv %256;     //0x97;     
    U1LCR = 0x03;      // DLAB = 0

 U1IER = 0x01;
 VICVectCntl1 = 0x20 | 7;
 VICVectAddr1 = (unsigned int)UART1_isr;
 VICIntEnable |= 1<<7;
}

                         
unsigned char UART0_GetChar(void)
{
 while(!(U0LSR & 0x01));
 return(U0RBR);
}
                         
unsigned char UART1_GetChar(void)
{
 while(!(U1LSR & 0x01));
 return(U1RBR);
}

unsigned char UART0_PutChar(unsigned char Ch)
{
   while(!(U0LSR & 0x20));
// if(Ch == '\n')
//  Ch = 0x0D;
 U0THR = Ch;
 return Ch;
}


unsigned char UART1_PutChar(unsigned char Ch)
{
   while(!(U1LSR & 0x20));
 U1THR = Ch;
 return Ch;
}

void UART1_PutS(unsigned char *Ch)
{
 while(*Ch)
  UART1_PutChar(*Ch++);
}


int fputc(int ch, FILE *f) {
  return (UART0_PutChar(ch));
}

struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;

Comments