Está en la página 1de 15

// lcd module connections sbit LCD_RS at RB2_bit; sbit LCD_EN at RB3_bit; sbit LCD_D4 at RB4_bit; sbit LCD_D5 at RB5_bit;

sbit LCD_D6 at RB6_bit; sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB2_bit; sbit LCD_EN_Direction at TRISB3_bit; sbit LCD_D4_Direction at TRISB4_bit; sbit LCD_D5_Direction at TRISB5_bit; sbit LCD_D6_Direction at TRISB6_bit; sbit LCD_D7_Direction at TRISB7_bit;

unsigned char Convert(unsigned char); void disp(void); void msgsend(void); unsigned long Vin, mV,Vdec,Vfrac; unsigned char op[12]; unsigned char i,j,lcd[5],ch1,ch2,temp;

unsigned char ch; unsigned int adc_rd; char *text; long tlong; // end lcd module connections

// set of AT commands const char atc1[] = "ATE0"; // disable command echo

//const char atc10[] = "AT+CMGD=1"; const char atc10[] = "AT+CMGF=1"; const char atc12[] = "AT+CMGS=";

// Delete 1st msg // msg //semding msg

const char atc14[] = "High Temperature is read from Patient of 333"; const char atc13[] = "+919848160532" ; const char atc15[] = "Doctor Attended The Patient of 333"; const char atc16[] = "Sensor 3 reading is out of range";

// lcd interface messages const LCD_MESSAGE_LENGTH = 16; const char lcd1[] = "Initializing...."; const char lcd2[] = "Power-Up GSM!!!"; const char lcd3[] = "RD3-Continue"; const char lcd4[] = "System Ready! ";

const char lcd10[] = "SENDING MSG";

const char lcd11[] = "RECV MSG";

// responses to parse const GSM_OK = 0; const GSM_RING = 1;

char gsm_state = 0; char response_rcvd = 0; short responseID = -1, response = -1; char msg[LCD_MESSAGE_LENGTH+1];

// copy const to ram string char * CopyConst2Ram(char * dest, const char * src){ char * d ; d = dest; for(;*dest++ = *src++;) ; return d; }

// uart rx interrupt handler void interrupt(){

char tmp; if (PIR1.RCIF == 1) { tmp = UART1_Read(); // do we have uart rx interrupt request? // get received byte

// process reception through state machine // we are parsing only "OK" and "RING" responses switch (gsm_state) { case 0: { response = -1; if (tmp == 'O') gsm_state = 1; break; } case 1: { if (tmp == 'K') { response = GSM_OK; gsm_state = 50; } else gsm_state = 0; break; } // reset state machine // we have 'K' -> // we have "OK" response // expecting CR+LF // clear response // we have 'O', it could be "OK" // expecting 'K'

case 50: { if (tmp == 13) // we have 13, it could be CR+LF

gsm_state = 51; else gsm_state = 0; break; } case 51: { if (tmp == 10) { response_rcvd = 1; responseID = response; } gsm_state = 0; break; }

// expecting LF

// reset state machine

// we have LF, response is complete // set reception flag // set response ID

// reset state machine

default: { gsm_state = 0; break; } } } }

// unwanted character // reset state machine

// send ATC command void send_atc(const char *s)

{ // send command string while(*s) { UART1_Write(*s++); } // terminate command with CR UART1_Write(0x0D); }

void send_atc1(const char *st) { // send command string while(*st) { UART1_Write(*st++); } } // get GSM response, if there is any short get_response() { if (response_rcvd) { response_rcvd = 0; return responseID; } else return -1;

// wait for GSM response void wait_response(char rspns) { while (get_response() != rspns) ; }

// pause void wait() { Delay_ms(1000); }

void main() { unsigned char t; unsigned int m=0;

// all pins as digital I/Os //ANSEL = 0; //ANSELH = 0;

// set PORTD inputs

TRISB=0x00; TRISC=0x00; TRISD0_bit = 1; TRISD1_bit = 1; TRISD2_bit = 1; TRISD3_bit = 1; // place a call input // hang up input // answer a call input // continue with PIC program

// set RTS pin to zero, we will use only RX i TX TRISC.B3 = 0; PORTC.B3 = 0;

// enable uart rx interrupt PIE1.RCIE = 1; INTCON.PEIE = 1; INTCON.GIE = 1;

//unsigned char temp = ; // setup lcd module /* Lcd_Init(); Lcd_Cmd(_LCD_CURSOR_OFF); */

disp(); // LCD_Out: power-up gsm LCD_Out(1,1,CopyConst2Ram(msg,lcd2));

LCD_Out(2,1,CopyConst2Ram(msg,lcd3));

UART1_init(9600);

// initialize USART module

// wait for start // while(!PORTD.B3) //;

Lcd_Cmd(_LCD_CLEAR); // LCD_Out: Initializing... LCD_Out(1,1,CopyConst2Ram(msg,lcd1));

Delay_ms(5000);

// wait for the GSM module to initialize it self

LCD_Out(1,1,"--- Sensor --- "); text = "VALUE: 0.00"; LCD_Out(2,1,text); // assign text to string

while(1) { LCD_Out(1,12,"1"); Vin = ADC_read(1); // get ADC value from 2nd channel

; t= Convert(adc_rd);

//comparision

if(t>0x28) {

msgsend()

; // Mesg to be sent

send_atc1(atc14); UART1_Write(0x1A); m=1; }

/*

Delay_ms(4000);

//comparision

LCD_Out(1,12,"2"); Vin = ADC_read(2); t = Convert(adc_rd); // get ADC value from 2nd channel

if(t<0x09) { if(m==1)

{ msgsend() ; // Mesg to be sent

send_atc1(atc15); UART1_Write(0x1A); m=0; } }

Delay_ms(4000);

LCD_Out(1,12,"3");

Vin = ADC_read(3);

// get ADC value from 2nd channel

t = Convert(adc_rd);*/

/*

//comparision

if(t>0x28) { msgsend() ; // Mesg to be sent

send_atc1(atc16); UART1_Write(0x1A);

*/

//Delay_ms(2000);

// while (1)

// Lcd_Cmd(LCD_CLEAR);

unsigned char Convert(unsigned char ) {

Vin = 488*Vin; // Scale up the result Vin = Vin /10; // Convert to temperature in C Vdec = Vin / 100; // Decimal part Vfrac = Vin % 100; // Fractional part LongToStr(Vdec,op); // Convert Vdec to string in "op" temp = Vdec;

j=0; for(i=0;i<=11;i++) { if(op[i] != ' ') // If a blank { lcd[j]=op[i]; j++; } }

Lcd_Out(2,7,lcd); // Output to LCD Lcd_Chr_Cp('.'); // Display "." ch1 = Vfrac / 10; // Calculate fractional part ch2 = Vfrac % 10; // Calculate fractional part Lcd_Chr_Cp(48+ch1); // Display fractional part Lcd_Chr_Cp(48+ch2); // Display fractional part

return temp;

void disp(void) {

Lcd_Init();

// Lcd_Init_EP4, see Autocomplete // send command to LCD (cursor off) // send command to LCD (clear LCD)

LCD_Cmd(_LCD_CURSOR_OFF); LCD_Cmd(_LCD_CLEAR);

text = "WELCOME TO YPRCET "; LCD_Out(1,1,text);

// assign text to string

// print string a on LCD, 1st row, 1st column

text = "Patient M. Sys "; LCD_Out(2,1,text);

// assign text to string // print string a on LCD, 2nd row, 1st column

Delay_ms(4000); }

void msgsend(void) {

send_atc1(atc12); UART1_Write(0x22);

//sending mesg

send_atc1(atc13); UART1_Write(0x22); UART1_Write(0x0D) ;

//Phone number

// wait_response(GSM_OK);

Delay_ms(500); }

También podría gustarte