Вы находитесь на странице: 1из 6

// Column connections

sbit Col1 at RB4_bit;


sbit Col2 at RB5_bit;
sbit Col3 at RB6_bit;
sbit Col4 at RB7_bit;
// Define LCD connections
sbit LCD_RS at RE2_bit;
sbit LCD_RW at RE1_bit;
sbit LCD_EN at RE0_bit;
sbit LCD_D0 at RD0_bit;
sbit LCD_D1 at RD1_bit;
sbit LCD_D2 at RD2_bit;
sbit LCD_D3 at RD3_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_RS_Direction at TRISE2_bit;
sbit LCD_RW_Direction at TRISE1_bit;
sbit LCD_EN_Direction at TRISE0_bit;
sbit LCD_D0_Direction at TRISD0_bit;
sbit LCD_D1_Direction at TRISD1_bit;
sbit LCD_D2_Direction at TRISD2_bit;
sbit LCD_D3_Direction at TRISD3_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
// End LCD module connections
sbit sel_0 at RC0_bit;
sbit sel_1 at RC1_bit;
sbit Sel_2 at RC2_bit;
sbit relay at RC4_bit;
// Define Messages
unsigned char rcv_count=0;
unsigned char receive;
unsigned char gsm_flag =0;
unsigned char owner_flag=0,employee_flag=0;
unsigned char yes_flag=0,no_flag=0,sms_read_flag=0;
unsigned count =0;
unsigned char km[6];
unsigned char last=25;
unsigned int adcvalue,value;
unsigned char car,x,y;
unsigned char first_warning=0,final_warning=0;
void interrupt() // uart receive command from PC(MATL
AB)
{
if (PIR1.RCIF) // checking receiver interrupt flag
{
if (UART1_Data_Ready() == 1) // wait for receive the character
{
receive=UART1_Read();
if(receive=='#' && rcv_count==0&& sel_0==0) // wait for Tur
n Left command
{
rcv_count=1;
}
else if(receive=='S' && rcv_count==1 && sel_0==0) // wait
for Turn Left command
{
yes_flag=1;
}
else if(receive=='N' && rcv_count==1 && sel_0==0) // wait
for Turn Left command
{
no_flag=1;
}
if(receive=='C' && sel_0==1 ) // wait for Turn Left comm
and
{
owner_flag=1;
}
else if(receive=='D' && sel_0==1) // wait for Turn Left c
ommand
{
employee_flag=1 ;
}
}
}
}
void lcd_puts(char row, char column, const char *s)
{
char ii;
switch(row){
case 1: Lcd_Cmd(_LCD_FIRST_ROW);
break;
case 2: Lcd_Cmd(_LCD_SECOND_ROW);
break;
}
for(ii=1;ii<column;ii++)Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
while(*s)Lcd_Chr_Cp(*s++);
}
void ShowADC(int x, int y, unsigned int adcvalue) // Routine to show the va
lue of the ADC_read
{
car = adcvalue / 1000;
LCD_Chr(x,y,48+car);
Delay_ms(3);
adcvalue = adcvalue-1000*car;
car = (adcvalue / 100);
LCD_Chr_CP(48+car);
Delay_ms(3);
adcvalue = adcvalue-100*car;
car = (adcvalue / 10);
LCD_Chr_CP(48+car);
Delay_ms(3);
adcvalue = adcvalue-10*car;
car = adcvalue;
LCD_Chr_CP(48+car);
delay_ms(100);
}
void Debounce(){
Delay_ms(250);
}
char Get_Key(){
// Scan row 1
PORTB = 0b00001110;
if (!Col1){
Debounce();
return '1';
}
if (!Col2){
Debounce();
return '2';
}
if (!Col3){
Debounce();
return '3';
}
if (!Col4){
Debounce();
return 'A';
}
// Scan row 2
PORTB = 0b00001101;
if (!Col1){
Debounce();
return '4';
}
if (!Col2){
Debounce();
return '5';
}
if (!Col3){
Debounce();
return '6';
}
if (!Col4){
Debounce();
return 'B';
}
// Scan row 3
PORTB = 0b00001011;
if (!Col1){
Debounce();
return '7';
}
if (!Col2){
Debounce();
return '8';
}
if (!Col3){
Debounce();
return '9';
}
if (!Col4){
Debounce();
return 'C';
}
// Scan row 4
PORTB = 0b00000111;
if (!Col1){
Debounce();
return '*';
}
if (!Col2){
Debounce();
return '0';
}
if (!Col3){
Debounce();
return '#';
}
if (!Col4){
Debounce();
return 'D';
}
return ' ';
}
void main() {
char key[] = " ";
ADCON1 = 0x07;
CMCON = 0x07;
TRISE = 0x00;
TRISD = 0x00;
TRISA = 0b00100000; // PORTA output except RA5
TRISB = 0b11110000; // R1-R4 output, C1-C4 input
OPTION_REG = 0x00;
PORTB = 0b00001111; // All rows equal to 1
TRISC.RC0= 0;
TRISC.RC1= 0;
TRISC.RC2= 0;
TRISC.RC4= 0;
relay = 0;
sel_0 = 1;
sel_1 = 0;
sel_2 = 0;
UART1_init(9600);
Lcd_Init();
uart1_write_text("AT+CMGF=1\r");
delay_ms(1000);
lcd_puts(1,1,"VEHICLE MONITOR");
delay_ms(500);
lcd_cmd(0x01);
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
INTCON.GIE = 1;
INTCON.PEIE = 1;
PIE1.RCIE=1;
while(1){
if(owner_flag==1)
{
lcd_cmd(0x01);
lcd_puts(1,1,"OWNER ACCESS");
delay_ms(1000);
relay = 1;
owner_flag=0;
lcd_cmd(0x01);
}
while(employee_flag==1)
{
lcd_puts(1,1,"DRIVER ACESS");
sel_0 = 0;
sel_1 = 0;
sel_2 = 0;
key[0] = Get_Key();
if (key[0] != ' ')
{
km[count++]=key[0];
lcd_out(2,count,key);
delay_ms(200);
}

if( key[0] == '#')
{
gsm_flag = 1;
km[count]='\0';
employee_flag=0;
}
}

if(gsm_flag==1)
{
lcd_cmd(0x01);
lcd_puts(1,1,"TOTAL KM");
lcd_out(2,1,km);
uart1_write_text("AT+CMGS=");
delay_ms(500);
uart1_write('"');
uart1_write_text("9944120946");
uart1_write('"');
delay_ms(500);
uart1_write('\r');
delay_ms(500);
uart1_write_text("DRIVE:");
delay_ms(1000);
uart1_write_text(km);
delay_ms(1000);
uart1_write(0x1A);
delay_ms(500);
lcd_cmd(0x01);
gsm_flag=0;
sms_read_flag=1;
}
if(sms_read_flag==1)
{
uart1_write_text("AT+CMGR=1");
delay_ms(1000);
uart1_write(0x0D);
delay_ms(1000);
}
if( no_flag==1)
{
lcd_puts(1,1,"OWNER DENIED");
while(1);
}
while(yes_flag==1)
{
relay = 1;
sms_read_flag = 0;
last--;
ShowADC(2,1,last);
delay_ms(2000);
if(last==0 && first_warning==0)
{
lcd_puts(1,1,"FIRST_warning");
delay_ms(2000);
first_warning=1;
last=5;
}
if(last==0 && first_warning==1)
{
lcd_puts(1,1,"Final_warning");
delay_ms(2000);
first_warning=0;
relay = 0;
last=25;
yes_flag =0;
}
}

}
}

Вам также может понравиться