#include <stdio.h>
#include <stdlib.h>
#include "config.c"
//LCD module connections
#define LCD_RS RD0
#define LCD_EN RD1
#define LCD_D4 RD2
#define LCD_D5 RD3
#define LCD_D6 RD4
#define LCD_D7 RD5
#define LCD_RS_DIR TRISD0
#define LCD_EN_DIR TRISD1
#define LCD_D4_DIR TRISD2
#define LCD_D5_DIR TRISD3
#define LCD_D6_DIR TRISD4
#define LCD_D7_DIR TRISD5
//End LCD module connections
//matrix keypad connections
#define X_1 RB0
#define X_2 RB1
#define X_3 RB2
#define X_4 RB3
#define Y_1 RB4
#define Y_2 RB5
#define Y_3 RB6
#define Y_4 RB7
#define Keypad_PORT PORTB
#define Keypad_PORT_Direction TRISB
void InitKeypad(void);
char switch_press_scan(void);
#include <xc.h>
#define _XTAL_FREQ 8000000
#include <stdint.h> // include stdint header
#include "LCD_Lib.c" // include LCD driver source file
/******************* Matrix keypad Functions *******************/
// Function declarations
void InitKeypad(void);
char switch_press_scan(void);
char switch_press_scan_num(void);
void InitKeypad(void)
{
Keypad_PORT = 0x00; // Set Keypad port pin values zero
ANSELH = 0;
Keypad_PORT_Direction = 0xF0; // Last 4 pins input, First 4 pins output
nRBPU = 0; // clear RBPU bit (OPTION_REG.7)
OPTION_REG &= 0X7F;
WPUB = 0xF0; // WPUB register = 0b11110000
}
char keypad_scanner(void)
{
PORTB = 0X0E; // PORTBbits -> 0000001, D0 is high
if ((Y_1 == 0)&&(X_1 == 0)) {return '1';} // S1 is pressed
else if ((Y_2 == 0)&&(X_1 == 0)) {return '2';} // S2 is pressed
else if ((Y_3 == 0)&&(X_1 == 0)) {return '3';} // S3 is pressed
else if ((Y_4 == 0)&&(X_1 == 0)) {return 'A';} // S4 is pressed
PORTB = 0X0D; // PORTBbits -> 00000010, D1 is high
if ((Y_1 == 0)&&(X_2 == 0)) {return '4';} // S5 is pressed
else if ((Y_2 == 0)&&(X_2 == 0)) {return '5';} // S6 is pressed
else if ((Y_3 == 0)&&(X_2 == 0)) {return '6';} // S7 is pressed
else if ((Y_4 == 0)&&(X_2 == 0)) {return 'B';} // S8 is pressed
PORTB = 0X0B; // PORTBbits -> 00000100, D2 is high
if ((Y_1 == 0)&&(X_3 == 0)) {return '7';} // S9 is pressed
else if ((Y_2 == 0)&&(X_3 == 0)) {return '8';} // S10 is pressed
else if ((Y_3 == 0)&&(X_3 == 0)) {return '9';} // S11 is pressed
else if ((Y_4 == 0)&&(X_3 == 0)) {return 'C';} // S12 is pressed
PORTB = 0X07; // PORTBbits -> 00001000, D3 is high
if ((Y_1 == 0)&&(X_4 == 0)) {return '*';} // S13 is pressed
else if ((Y_2 == 0)&&(X_4 == 0)) {return '0';} // S14 is pressed
else if ((Y_3 == 0)&&(X_4 == 0)) {return '#';} // S15 is pressed
else if ((Y_4 == 0)&&(X_4 == 0)) {return 'D';} // S16 is pressed
return 'n';
}
char keypad_scanner_num(void)
{
PORTB = 0X0E; // PORTBbits -> 0000001, D0 is high
if ((Y_1 == 0)&&(X_1 == 0)) {return '1';} // S1 is pressed
else if ((Y_2 == 0)&&(X_1 == 0)) {return '2';} // S2 is pressed
else if ((Y_3 == 0)&&(X_1 == 0)) {return '3';} // S3 is pressed
else if ((Y_4 == 0)&&(X_1 == 0)) {return 'n';} // S4 is pressed
PORTB = 0X0D; // PORTBbits -> 00000010, D1 is high
if ((Y_1 == 0)&&(X_2 == 0)) {return '4';} // S5 is pressed
else if ((Y_2 == 0)&&(X_2 == 0)) {return '5';} // S6 is pressed
else if ((Y_3 == 0)&&(X_2 == 0)) {return '6';} // S7 is pressed
else if ((Y_4 == 0)&&(X_2 == 0)) {return 'n';} // S8 is pressed
PORTB = 0X0B; // PORTBbits -> 00000100, D2 is high
if ((Y_1 == 0)&&(X_3 == 0)) {return '7';} // S9 is pressed
else if ((Y_2 == 0)&&(X_3 == 0)) {return '8';} // S10 is pressed
else if ((Y_3 == 0)&&(X_3 == 0)) {return '9';} // S11 is pressed
else if ((Y_4 == 0)&&(X_3 == 0)) {return 'n';} // S12 is pressed
PORTB = 0X07; // PORTBbits -> 00001000, D3 is high
if ((Y_1 == 0)&&(X_4 == 0)) {return 'n';} // S13 is pressed
else if ((Y_2 == 0)&&(X_4 == 0)) {return '0';} // S14 is pressed
else if ((Y_3 == 0)&&(X_4 == 0)) {return '#';} // S15 is pressed
else if ((Y_4 == 0)&&(X_4 == 0)) {return 'n';} // S16 is pressed
return 'n';
}
// Read pressed key value from keypad and return its value
char switch_press_scan(void) // Get key from user
{
char key = 'n'; // Assume no key pressed
while(key=='n') // Wait until a key is pressed
key = keypad_scanner(); // Scan the keys again and again
return key; //when key pressed then return its value
}
char switch_press_scan_num(void) // Get key from user
{
char key = 'n'; // Assume no key pressed
while(key=='n') // Wait until a key is pressed
key = keypad_scanner_num(); // Scan the keys again and again
return key; //when key pressed then return its value
}
/**************** end matrix keypad functions ******************/
/********************* GSM UART Functions **********************/
void InitGSM();
void sms(unsigned char *msg);
void tx(char a);
unsigned char rx(void);
void tx_str(const char *s);
char phoneno[10];
void InitGSM(){
TRISCbits.TRISC6 = 0; //Set RC6 (TX) as output
TRISCbits.TRISC7 = 1; //Set RC7 (RX) as input
TXSTA = 0b00100100; //Refer one note
RCSTA = 0b10010000; //Refer one note
SPBRG = 10;
TXIF = RCIF = 0; //Disable interrupt(For reseting purpose)
}
void sms(unsigned char *msg){
tx_str("AT\r\n");
__delay_ms(500);
tx_str("AT+CMGF=1\r\n"); //Enter GSM SMS TEXT MODE
__delay_ms(500);
tx_str("AT+CMGS="); //SEND SMS to Phone number
tx('"');
for(int l = 0; l <= 9; l++){
tx(phoneno[l]); //Enter phone number with size 10 array
}
tx('"');
tx_str("\r\n"); //After enter will be entering SMS
__delay_ms(1000);
while(*msg)
tx(*msg++); //Send SMS
tx(0x1A); //CTRL Z ASCII code to send
__delay_ms(500);
}
void tx(char a) //Function to send one byte of data to UART
{
while(TXIF==0) continue; //Hold the program until TX buffer is free to transmit
TXREG=a;
}
unsigned char rx(void){
while(PIR1bits.RCIF == 0); //Hold the program until RX buffer is free to receive
return RCREG;
}
void tx_str(const char *s) //Function to convert string to byte
{
while(*s)
tx(*s++);
}
/******************* end GSM UART Functions ********************/
void main(void)
{
int a,b;
a = b = 0;
static char PHnum[11];
char Key = 'n';
LCD_Begin();
InitKeypad();
LCD_Cmd(LCD_CLEAR);
LCD_Print("Enter Phone No.");
while(a<10){
Key = switch_press_scan_num();
if ((Key == '#')&&(t>0))
{
PHnum[a-1]=" ";
LCD_Goto(1, 2);
LCD_Printr(PHnum);
__delay_ms(200);
t--;
}
else if (Key != '#')
{
LCD_Goto(1, 2);
PHnum[a] = Key;
LCD_Printr(PHnum);
__delay_ms(200);
a++;
}
};
while (b<10){
phoneno[b]=PHnum[b];
b++;
}
sms("Hello World");
}