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.

How to send SMS using keypad attached to 8051

Status
Not open for further replies.

Isuru Senarath

Member level 2
Joined
Apr 10, 2012
Messages
42
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,580
Hi all
Can anyone help me to write C program for following requirement

4x4 keypad is attached to AT89s52
16x2 LCD and GSM modem is also attached

I need to send sms using keypad (ex:- If I press "A" from the keypad, send sms as "Arrived" to a defined cell number) and at the same time, massage must be shown in LCD
 

I will send you... the code for 4x4 when i reach home...

by the way,.. you can modify the same code... if u have knowledge of key pad (Matrix) Interfacing
 

If you can send me the code, it is a great help for me, because my C programming is very poor
Thank you
 

circuit_diagram.jpg

Key Press will we displayed on LCD

Code:
#include <REGX51.H>
#include<string.h>
#define COL P2
#define ROW P0
sfr LcdData = 0x90;							//LCD Data Pins
sbit RS =  P2^5;
sbit RW =  P2^6;
sbit EN =  P2^7;

//Function Prototype Declaration
void lcdcmd(unsigned char value);
void lcddata(unsigned char value);
void Delay(unsigned int itime);
void SerTX(unsigned char);
unsigned char msg[] = "Pressed Key:-";
unsigned char keypad[4][4] =	{'1','2','3','4',
								 '5','6','7','8',
								 '9','0','!','@',
								 '#','$','%','^'};
void main()
{
	
	unsigned char colLoc,rowLoc;
	unsigned int len,i;
	LcdData = 0x00;		 //Port-1 as Output Port
	TMOD = 0x20;	//For Baud Rate Generation
	TH1 = -3;		//Baud Rate of 9600bps
	SCON = 0x50;
	TR1 = 1;		//Start Timer-1

	//LCD Initialization Commands

	lcdcmd(0x38);	//Initialize LCD Commands
	Delay(1);
	lcdcmd(0x0E);	//Display on Cursor Blinking
	Delay(1);
	lcdcmd(0x01);	//Clear LCD
	Delay(1);
	lcdcmd(0x80);	//Row 1 and First Column
	len = strlen(msg);
	for(i=0;i<len;i++)
	{
		lcddata(msg[i]);
		Delay(1);
	}
	lcdcmd(0x0C0);						//Move to Second Line of the LCD
	COL =  0x0FF;						//Make Port-2 as Input Port
	while(1)								//Infinite Loop
	{
		do
		{
			ROW = 0x00;						//Groud all Row's
			colLoc = COL;					//Read all the Columns
			colLoc = colLoc & 0x0F;			//Mask the Unused Bits.
		}while(colLoc != 0x0F); 			//Check until all the Keys are released
		do
		{
			do
			{
				Delay(2);					//20msec delay
				colLoc = COL;				//Read all the Columns
				colLoc = colLoc & 0x0F;		//Mask the Unused Bits.
			}while(colLoc == 0x0F); 			//Keep Checking for Key-press
			//It may be possible that this key-press may be due to some noise-spikes in the circuit
			//thats why we will check once again whether the same key is pressed or not.
			//If not the whole loop will be executed once again
			Delay(2);
			colLoc = COL;					//read all columns
			colLoc = colLoc & 0x0F;			//Mask the Unused Bits.
		}while(colLoc == 0x0F);				//Wait for the Key-Press

		while(1)
		{
			ROW = 0x0FE;					//Ground First Row
			colLoc = COL;
			colLoc = colLoc & 0x0F;			//Mask the Unused Bits.
			if(colLoc != 0x0F)				//Column Detected
			{
				rowLoc = 0;					//1st Row Selected
				break;
			}

			ROW = 0x0FD;					//Ground Second Row
			colLoc = COL;
			colLoc = colLoc & 0x0F;			//Mask the Unused Bits.
			if(colLoc != 0x0F)				//Column Detected
			{
				rowLoc = 1;					//2nd Row Selected				
				break;
			}

			ROW = 0x0FB;					//Ground Third Row
			colLoc = COL;
			colLoc = colLoc & 0x0F;			//Mask the Unused Bits.
			if(colLoc != 0x0F)				//Column Detected
			{
				rowLoc = 2;					//3rd Row Selected
				break;
			}

			ROW = 0x0F7;					//Ground Fourth Row
			colLoc = COL;
			colLoc = colLoc & 0x0F;			//Mask the Unused Bits.
			if(colLoc!= 0x0F)
			{
				rowLoc = 3;					//4th Row Selected
				break;
			}
		}

		if(colLoc == 0x0E)			  	//This means that First column is Selected
		{
			SerTX(keypad[rowLoc][0]);
		}
		else if(colLoc == 0x0D)
		{
			SerTX(keypad[rowLoc][1]);
		}
		else if(colLoc == 0x0B)
		{
			SerTX(keypad[rowLoc][2]);
		}
		else if(colLoc == 0x07)
		{
			SerTX(keypad[rowLoc][3]);
		}

		//lcdcmd(0xC0);	//Second Row
		
			//Check Columns and send the Data to the LCD Screen:-
			if(colLoc == 0x0E)			  	//This means that First column is Selected
			{
				lcddata(keypad[rowLoc][0]);
			}
			else if(colLoc == 0x0D)
			{
				lcddata(keypad[rowLoc][1]);
			}
			else if(colLoc == 0x0B)
			{
				lcddata(keypad[rowLoc][2]);
			}
			else if(colLoc == 0x07)
			{
				lcddata(keypad[rowLoc][3]);
			}
	}
}
void lcdcmd(unsigned char value)
{
	LcdData = value;
	RS = 0;
	RW = 0;
	EN = 1;
	Delay(1);
	EN = 0;
}
void lcddata(unsigned char value)
{
	LcdData = value;
	RS = 1;
	RW = 0;
	EN = 1;
	Delay(1);
	EN = 0;
}
void Delay(unsigned int itime)
{
	unsigned int i,j;
	for(i=0;i<1275;i++)
	for(j=0;j<itime;j++);
}
void SerTX(unsigned char x)
{
	SBUF = x;
	while(TI == 0);
	TI = 0;
}

Hope this helps..
 
HI
Thank you very much. Highly appreciate your help. I need to modify the keypad as,
If I press "A", LCD should display -Arrived
If I press "B", LCD should display -Breakdown
If I press "C", LCD should display -Next bus stop number
If I press "D", LCD should display -Approximate time
If I press "*", LCD should display -Send Info
If I press "#", LCD should display -Other

How can I do this and how can I combine AT commands with keypad?

Code:
unsigned char keypad[4][4] =	{'1','2','3','A',
								 '4','4','6','B',
								 '7','8','9','C',
								 '*','0','#','D'};
 

It's not at all difficult, just have patience and try it your self first..

And also read this tutorial, so that you can learn how to interface keypad and after that try it yourself..

You have to complete this...
Not me..
Try it yourself if face any problem then contact back here..
 

Hi
I edit my program. I need to get the enter key number for following FOR loops. How can I filter that. How can I get the pressed key instead of

if(SBUF = 1)
{
Task 1
}

else if(SBUF = 2)
{
Task 2
}

else if (SBUF = '*')
{
Task 3
}

How can I clear the SBUF ? I want to clear the SBUF before I enter the message which I need to send through the GSM module. How can I do that in Task 3. Following is my full program.
Thanks


Code:
#include <REGX51.H>
#include<string.h>
#define COL P2
#define ROW P0
sfr LcdData = 0x90;							//LCD Data Pins
sbit RS =  P2^5;
sbit RW =  P2^6;
sbit EN =  P2^7;

//Function Prototype Declaration


void lcd_data_string(unsigned char *str);
void lcdcmd(unsigned char value);
void lcddata(unsigned char value);
void Delay(unsigned int itime);
void SerTX(unsigned char);
void SMSString(char*text) ;
unsigned char msg[] = "Enter Info:-";
unsigned char keypad[4][4] =	{'1','2','3','A',
								 '4','4','6','B',
								 '7','8','9','C',
								 '*','0','#',':'};
void main()
{
	unsigned char colLoc,rowLoc;
	unsigned int len,i;
	LcdData = 0x00;		 //Port-1 as Output Port
	TMOD = 0x20;	//For Baud Rate Generation
	TH1 = -3;		//Baud Rate of 9600bps
	SCON = 0x50;
	TR1 = 1;		//Start Timer-1

	//LCD Initialization Commands

	lcdcmd(0x38);	//Initialize LCD Commands
	Delay(1);
	lcdcmd(0x0E);	//Display on Cursor Blinking
	Delay(1);
	lcdcmd(0x01);	//Clear LCD
	Delay(1);
	lcdcmd(0x80);	//Row 1 and First Column
	lcd_data_string("Bus Route Info.");		
	Delay(10);
	lcdcmd(0x0C0);
	lcd_data_string("System Project");
	Delay(10);
	lcdcmd(0x01);  //clear screen
	Delay(1);
	len = strlen(msg);
	for(i=0;i<len;i++)
	{
		lcddata(msg[i]);
		Delay(1);
	}
	lcdcmd(0x0C0);						//Move to Second Line of the LCD
	COL =  0x0FF;						//Make Port-2 as Input Port
	while(1)								//Infinite Loop
	{
		do
		{
			ROW = 0x00;						//Groud all Row's
			colLoc = COL;					//Read all the Columns
			colLoc = colLoc & 0x0F;			//Mask the Unused Bits.
		}while(colLoc != 0x0F); 			//Check until all the Keys are released
		do
		{
			do
			{
				Delay(2);					//20msec delay
				colLoc = COL;				//Read all the Columns
				colLoc = colLoc & 0x0F;		//Mask the Unused Bits.
			}while(colLoc == 0x0F); 			//Keep Checking for Key-press
			//It may be possible that this key-press may be due to some noise-spikes in the circuit
			//thats why we will check once again whether the same key is pressed or not.
			//If not the whole loop will be executed once again
			Delay(2);
			colLoc = COL;					//read all columns
			colLoc = colLoc & 0x0F;			//Mask the Unused Bits.
		}while(colLoc == 0x0F);				//Wait for the Key-Press

		while(1)
		{
			ROW = 0x0FE;					//Ground First Row
			colLoc = COL;
			colLoc = colLoc & 0x0F;			//Mask the Unused Bits.
			if(colLoc != 0x0F)				//Column Detected
			{
				rowLoc = 0;					//1st Row Selected
				break;
			}

			ROW = 0x0FD;					//Ground Second Row
			colLoc = COL;
			colLoc = colLoc & 0x0F;			//Mask the Unused Bits.
			if(colLoc != 0x0F)				//Column Detected
			{
				rowLoc = 1;					//2nd Row Selected				
				break;
			}

			ROW = 0x0FB;					//Ground Third Row
			colLoc = COL;
			colLoc = colLoc & 0x0F;			//Mask the Unused Bits.
			if(colLoc != 0x0F)				//Column Detected
			{
				rowLoc = 2;					//3rd Row Selected
				break;
			}

			ROW = 0x0F7;					//Ground Fourth Row
			colLoc = COL;
			colLoc = colLoc & 0x0F;			//Mask the Unused Bits.
			if(colLoc!= 0x0F)
			{
				rowLoc = 3;					//4th Row Selected
				break;
			}
		}

		if(colLoc == 0x0E)			  	//1110 This means that First column is Selected
		{
			SerTX(keypad[rowLoc][0]);
		}
		else if(colLoc == 0x0D)			// 1101
		{
			SerTX(keypad[rowLoc][1]);
		}
		else if(colLoc == 0x0B)			 // 1011
		{
			SerTX(keypad[rowLoc][2]);
		}
		else if(colLoc == 0x07)			  // 0111
		{
			SerTX(keypad[rowLoc][3]);
		}

		//lcdcmd(0xC0);	//Second Row
		
			//Check Columns and send the Data to the LCD Screen:-
			if(colLoc == 0x0E)			  	//1110 This means that First column is Selected
			{
				lcddata(keypad[rowLoc][0]);
			}
			else if(colLoc == 0x0D)					 // 1101
			{
				lcddata(keypad[rowLoc][1]);
			}
			else if(colLoc == 0x0B)					 //  1011
			{
				lcddata(keypad[rowLoc][2]);
			}
			else if(colLoc == 0x07)					 //  0111
			{
				lcddata(keypad[rowLoc][3]);
			}
	
	if(SBUF = 1)
			{
				lcdcmd(0x01);  //clear screen
				lcdcmd(0x80);	//Row 1 and First Column
				lcd_data_string("Go to Bus Stand");		
				Delay(10);
				lcdcmd(0x0C0);
				lcd_data_string("Loc:A1 7.00 PM");
				Delay(100);
				lcdcmd(0x01);  //clear screen
				lcdcmd(0x80);	//Row 1 and First Column
				lcd_data_string("Enter Next Stop");		
				Delay(10);
				lcdcmd(0x0C0);
				lcd_data_string("Number");
				Delay(100);
			}
	
	else if(SBUF = 2)
			{
				lcdcmd(0x80);	//Row 1 and First Column
				lcd_data_string("BS 2 at 7.10PM");		
				Delay(10);
				lcdcmd(0x0C0);
				lcd_data_string("ACHIVE? Y/N?");
				Delay(10);
			}

	else if (SBUF = '*')	 //* is for Y (YES)
				{
					SMSString("AT\r"); // AT commands to initialize gsm modem
					Delay(50);

					SMSString( "ATe0\r"); // turn off echo
					Delay(50);

					SMSString( "AT&W\r"); // save settings
					Delay(50);

					SMSString( "AT+CMGS=\"0718021336\"\r");//my mob number here
					Delay(50);
					SMSString("7.10");
					Delay(500);
					SMSString("0x1A\r");
					Delay(50);
				}
			}
		}
		



void lcdcmd(unsigned char value)
{
	LcdData = value;
	RS = 0;
	RW = 0;
	EN = 1;
	Delay(1);
	EN = 0;
}
void lcddata(unsigned char value)
{
	LcdData = value;
	RS = 1;
	RW = 0;
	EN = 1;
	Delay(1);
	EN = 0;
}
void Delay(unsigned int itime)
{
	unsigned int i,j;
	for(i=0;i<1275;i++)
	for(j=0;j<itime;j++);
}
void SerTX(unsigned char x)
{
	SBUF = x;
	while(TI == 0);
	TI = 0;
}

void lcd_data(unsigned char item)		// Function to send one byte data to LCD
{
	LcdData = item;
	RS= 1;
	RW=0;
	EN=1;
	Delay(1);
	EN=0;
	return;
}

void lcd_data_string(unsigned char *str)	// Function to send string to LCD
{
	int i=0;
	while(str[i]!='\0')
	{
		lcd_data(str[i]);
		i++;
		Delay(10);
	}
	return; 
}


void SMSString(unsigned char* text) //function to send SMS using GSM modem
{
        while (*text)
        {
                SerTX(*text++);
        }
}
 

Hi
When I send SMS using AT command (AT+CMGS="tel no" message and ctrl+z) it will successfully send. but I cant read the received sms. It will show as <unreadable format>. What is wrong with my AT commands?
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top