// Lcd pinout settings
sbit LCD_RS at RB5_bit;
sbit LCD_EN at RB4_bit;
sbit LCD_D7 at RB0_bit;
sbit LCD_D6 at RB1_bit;
sbit LCD_D5 at RB2_bit;
sbit LCD_D4 at RB3_bit;
// Pin direction
sbit LCD_RS_Direction at TRISB5_bit;
sbit LCD_EN_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB0_bit;
sbit LCD_D6_Direction at TRISB1_bit;
sbit LCD_D5_Direction at TRISB2_bit;
sbit LCD_D4_Direction at TRISB3_bit;
//control pin configuration
sbit Bed1 at RD2_bit;
sbit Bed2 at RD3_bit;
sbit Bok at RD4_bit;
sbit sounder at RD0_bit;
sbit Bed1_dir at TRISD2_bit;
sbit Bed2_dir at TRISD3_bit;
sbit Bok_dir at TRISD4_bit;
sbit sounder_dir at TRISD0_bit;
char NEWLINE[] = {13,10,0};
void initialize(){
ADCON1 = 0x0F;
INTCON = 0xC0;
T0CON = 0xC7;
RCIE_bit = 0;
TMR0IE_bit = 0;
Uart1_init(9600);
soft_uart_init(&PORTC, 0, 1, 9600, 0);
Bed1_dir = 1;
Bed2_dir = 1;
Bok_dir = 1;
sounder_dir = 0;
sounder = 0;
lcd_init();
lcd_cmd(_lcd_clear);
lcd_cmd(_lcd_cursor_off);
delay_ms(100);
}
void Display_Home(){
lcd_cmd(_lcd_clear);
lcd_out(1,2,"GSM HOSPITAL");
lcd_out(2,4,"PROJECT");
}
unsigned short isButtonPressed(){
if(Bed1){
return 1;
}
else if(Bed2){
return 1;
}
else return 0;
}
unsigned short WhichButton(){
if(Bed1){
delay_ms(300);
return 1;
}
else if(Bed2){
delay_ms(300);
return 2;
}
}
void setAlarm(unsigned short Bed_num){
sounder = 1;
if(Bed_num == 1){
lcd_cmd(_lcd_clear);
lcd_out(2,1,"Ward one, Bed 1");
}
else if(Bed_num == 2){
lcd_cmd(_lcd_clear);
lcd_out(2,1,"Wart one, Bed 2");
}
}
void soft_uart_write_text(char * text){
int i;
for(i=0;i<strlen(text); i++){
soft_uart_write(text[i]);
}
}
void SendMessageToH(){
soft_uart_write_text("SENDING MESSAGE TO HOSPITAL NUMBER");
}
unsigned short time_delay = 200;
unsigned short send_flag = 0;
unsigned short uart_flag = 0;
int index = 0;
unsigned short buf_flag = 1;
char uart_buffer[256];
int cnt = 0;
void interrupt(){
if(RCIF_bit)
{
TMR0IE_bit = 1;
uart_buffer[index] = uart1_read();
index++;
RCIF_bit = 0;
}
if(TMR0IF_bit){
TMR0IF_bit = 0;
cnt++;
if(cnt == 23){
cnt = 0;
buf_flag = 0;
uart_buffer[index] = 0;
index = 0;
TMR0IE_bit = 0;
}
}
}
void main() {
initialize();
RCIE_bit = 1;
Display_home();
delay_ms(1000);
do{
if(isbuttonPressed()){
setAlarm(whichButton());
while(Bok == 0){
time_delay--;
delay_ms(20);
if(time_delay == 1){
send_flag = 1;
break;
}
}
if(send_flag){
send_flag = 0;
sendMessageToH();
soft_uart_write_text(NEWLINE);
}
sounder = 0;
delay_ms(1000);
}
if(buf_flag == 0){
lcd_cmd(_lcd_clear);
lcd_out(1,1,uart_buffer);
sounder = 1;
buf_flag = 1;
}
if(Bok){
sounder = 0;
}
} while(1);
}