??? 09/10/10 00:38 Modified: 09/10/10 01:06 Read: times |
#178503 - what about this timer program Responding to: ???'s previous message |
is this any better
#include<reg51.h> //keypad values #define key_0 '0' #define key_1 '1' #define key_2 '2' #define key_3 '3' #define key_4 '4' #define key_5 '5' #define key_6 '6' #define key_7 '7' #define key_8 '8' #define key_9 '9' #define key_enter 'A' #define key_esc 'B' #define key_C 'C' #define key_D 'D' #define key_E 'E' #define key_F 'F' #define special_q '?' //flag_g and flag_c states #define enter_val 0xaa #define mismatch_val 0xDD #define error_val 0xff // delay timer 1 ms #define Th 0xfc #define Tl 0x65 // delay timer 50 ms #define Th50 0xb4 #define Tl50 0x00 #define Tm 0x01 //timer 0 mode 1 sfr lcd = 0x90; //0x90 is port 1 address sfr hex = 0x80; //0x80 is port 0 address sbit RS = P2^0; sbit RW = P2^1; sbit EN = P2^2; sbit door_state = P2^3; //let state of 1 mean door is closed and state of 0 mean door is open sbit motor1=P2^6; sbit motor2=P2^7; sbit alarm_1=P2^4; void timer(); void delay_ms(unsigned char); void lcd_init(); void lcd_line(bit); void lcd_com(unsigned char); void lcd_disp(unsigned char); void lcd_disp1(unsigned char dat[],bit delay1); unsigned char keypad(); unsigned char invalid(unsigned char,unsigned char); unsigned char compare(unsigned char one[], unsigned char two[], bit); void change_key(); void close(); void open(); void alarm(); unsigned char present_key[]= "12345600" ,wrong_count=0,right_count=0; code unsigned char zero2nine[]= "0 to 9 only",wrong[]= "wrong key..", lock_error[]= "Lock error"; code unsigned char key_state[]= {0xEE , 0xED , 0xEB , 0xE7, 0xDE , 0xDD , 0xDB , 0xD7, 0xBE , 0xBD , 0xBB , 0xB7, 0x7E , 0x7D , 0x7B , 0x77 }; code unsigned char digit[]= { key_0 , key_4 , key_8 , key_C , key_1 , key_5 , key_9 , key_D , key_2 , key_6 ,key_enter,key_E , key_3 , key_7 , key_esc , key_F }; // main program starts here void main() { unsigned char i,j,temp,password[8]=0,flag_g=0; code unsigned char right[]= "Right Key",p_wd[]= "Key :"; code unsigned char lcd_open[]= "Open...", lcd_close[]= "Close..."; code unsigned char lcd_door[]= "A-> Open/Close"; code unsigned char lcd_change[]= "B-> Change Key", lcd_exit[]= "E-> Exit"; while(1) { lcd_init(); lcd_disp1(p_wd,0); lcd_line(1); //This is where password is taken as input for(i=0; i<8; i++) { password[i]=keypad(); // reading from keypad flag_g=invalid(password[i],i); if(flag_g==enter_val || flag_g==error_val || flag_g==mismatch_val) { break; } lcd_disp( special_q ); // 0x3f is hex value of ascii character "?" (question mark) } if(flag_g==error_val || flag_g==mismatch_val) { flag_g=0; continue; } flag_g=0; right_count=0; //comparing is done here flag_g = compare(password,present_key,1); if(flag_g==error_val) { flag_g=0; continue; } if(right_count==7) { wrong_count=0; lcd_disp1(right,1); } // The menu is shown here j=0; while(j<=100) { j++; for(i=0; i<1; i++) { lcd_disp1(lcd_door,0); temp=keypad(); if(temp != error_val) break; lcd_disp1(lcd_change,0); temp=keypad(); if(temp != error_val) break; lcd_disp1(lcd_exit,0); temp=keypad(); if(temp != error_val) break; } if(temp==key_C) { if(door_state==1) { lcd_disp1(lcd_open,0); open(); } else { lcd_disp1(lcd_close,0); close(); } } else if(temp==key_D) { change_key(); } else if(temp==key_esc) { temp=0; //flag_g=error_val; break; } } } } /*void delay_ms(unsigned char u) { unsigned char i; unsigned int j; for(i=0; i<u; i++) { for(j=0; j<1275; j++); } }*/ void timer_50() { TMOD = Tm; TH0 = Th50; TL0 = Tl50; TR0 = 1; } void timer() { TMOD = Tm; TH0 = Th; TL0 = Tl; TR0 = 1; } /* Purpose: This function is used to create delay between operations with a minimum of 1 ms and maximum of about 256 ms of delay Input: input is 8 bit i.e. unsigned char. The input specifies the amount of delay called in ms */ void delay_ms(unsigned char u) { unsigned char i; //if crystal frequency is 11.0592 MHz the delay dute to timer is about 1 ms for(i=0; i<u; i++) { timer(); while(!TF0); TF0 = TR0 =0; } } /* Purpose: This function is used to pass on commands to the LCD display connected to port P1. Input: input is 8 bit i.e. unsigned char. The input specifieds command to be given to the LCD. */ void lcd_com(unsigned char x) { RS=0; //command mode RW=0; EN=1; lcd=x; delay_ms(5); EN=0; delay_ms(5); } /* Purpose: This function initialises the LCD screen */ void lcd_init() { lcd_com(0x0E); lcd_com(0x38); lcd_com(0x06); } /* Purpose: This function is used to change/define the line which is to be used in the LCD. Input: input is 1 bit. The input of '0' makes the cursor to go to the line 0 and input of '1' makes the cursor to go to line 1. */ void lcd_line(bit line) { if(line==0) lcd_com(0x80); //line one else lcd_com(0xC0); //return home } /* Purpose: This function is used to pass on data to the LCD display connected to port P1. Input: Input is 8 bit i.e. unsigned char. The input specifieds data to be given to the LCD. */ void lcd_disp(unsigned char x) { RS=1; RW=0; EN=1; lcd=x; delay_ms(5); EN=0; delay_ms(5); //lcd_com(0x06); } void lcd_disp1(unsigned char dat[], bit a) { unsigned char i; lcd_com(0x01); lcd_line(0); for(i=0; i <sizeof(dat) ; i++) { if(i==8) { lcd_line(1); } lcd_disp(dat[i]); } // Four second delay is to be expected if(a==1) { delay_ms(255); delay_ms(255); delay_ms(255); delay_ms(255); delay_ms(255); delay_ms(255); delay_ms(255); delay_ms(255); delay_ms(255); delay_ms(255); delay_ms(255); delay_ms(255); delay_ms(255); delay_ms(255); delay_ms(255); delay_ms(255); delay_ms(255); delay_ms(255); } else { delay_ms(255); delay_ms(255); } } /* Purpose: This function is used to get data from the keypad connected to port P0. Return: Return is 8 bit i.e. unsigned char. The Return is the value of the button pressed in the keypad. */ unsigned char keypad() { unsigned char i,temp=0,temp1=0,temp2=0xff,wait=0;//,wait1=0; bit flag_x=0; while(1) { //write program to prevent indefinite waiting here //wait++; timer_50(); hex = 0x0f; //delay_ms(10); for(i=0; i<4; i++) { if(i==0) hex = temp1 = 0xEF; else if(i==1) hex = temp1 = 0xDF; else if(i==2) hex = temp1 = 0xBF; else if(i==3) hex = temp1 = 0x7F; temp = hex; temp &= 0x0f; temp1&= 0xf0; if(temp != 0x0F) { delay_ms(250); temp=hex; temp&=0x0F; if(temp!=0x0F) { temp = temp | temp1; flag_x=0x01; break; } } } for(i=0; i<16; i++) { if(key_state[i]==temp) { temp2=digit[i]; break; } } if(flag_x==0x01) break; if(TF0) { wait++; if(wait==255) return error_val; TF0=TR0=0; } } return temp2; } /* Purpose: This function should be used to close the lock of the door */ void close() { unsigned char a=0; if(door_state==0) { motor1=0; motor2=1; while(a<=200) { a++; if(door_state==1) { return; } delay_ms(5); } lcd_com(0x01); lcd_line(0); lcd_disp1(lock_error,1); return; } } /* Purpose: This function should be used to open the lock of the door */ void open() { unsigned char a=0; if(door_state==1) { motor1=0; motor2=1; while(a<=200) { a++; if(door_state==0) { return; } delay_ms(5); } lcd_com(0x01); lcd_line(0); lcd_disp1(lock_error,1); return; } } /* Purpose: This function checks for illegal buttons pressed and gives appropriate return Input: (x,i) : x is the input from the keypad and i is the index of the key presed Return: (flag) :return value of 0xaa is given if enter is pressed(enter_val) 0xDD is returned if there is mismatch (mismatch_val) and 0xff is given if escape is pressed or due to error (error_val) */ unsigned char invalid(unsigned char x,unsigned char i) { unsigned char flag=0xff,temp; while(i==7) { temp=keypad(); if(temp==key_enter) { flag=enter_val; break; } else if(temp == key_esc || temp == error_val) { flag=error_val; break; } } if(x==key_enter) { flag=enter_val; } /* Do not accept the values of 10,11,12,13. Password must contain only 0 to 9 only */ else if(x==key_C || x==key_D || x==key_E || x==key_F) { flag=mismatch_val; lcd_disp1(zero2nine,1); } else if(x==key_esc || x==error_val) { flag=error_val; } return flag; } /* Purpose: this is used to compare the 2 variables and give corresponfing output */ unsigned char compare(unsigned char one[],unsigned char two[], bit a) { unsigned char flag_c,i; code unsigned char mismatch[]= {"Key mismatch"},try_again[]= {"Try again"};; flag_c=0x00; right_count=0; for(i=0; i<8; i++) { if(one==two) { right_count++; } else { wrong_count++; right_count=0; flag_c=error_val; break; } } if(flag_c==error_val) { if(a==1) { lcd_disp1(wrong,1); if(wrong_count>=5) { alarm(); return error_val; } } else if(a==0) { lcd_disp1(mismatch,0); delay_ms(200); lcd_disp1(try_again,0); return error_val; } } return flag_c; } /* Purpose: 1. This function is used to get data from the keypad connected to port P0 twice. 2. Then compare them and then change the password. */ void change_key() { unsigned char i,flag_g,temp, password[8]=0, pass_confirm[8]=0;// j, count; code unsigned char new_key[]= {"New key:"},confirm[]= {"Confirm:"}; code unsigned char key_changed[]= {"Key Changed..."}; while(1) { lcd_disp1(new_key,0); lcd_line(1); temp=0; /* take new password */ for(i=0; i<8; i++) { // count++; password[i]=keypad(); flag_g=invalid(password[i],i); if(flag_g==enter_val || flag_g==mismatch_val) { break; } else if(flag_g==error_val) { return; } lcd_disp( special_q ); // 0x3f is hex value of ascii character "?" } if(flag_g==mismatch_val) { flag_g=0; continue; } flag_g=0; //confirmation is done here lcd_disp1(confirm,0); lcd_line(1); /* take new password */ for(i=0; i<8; i++) { pass_confirm[i]=keypad(); flag_g=invalid(pass_confirm[i],i); if(flag_g==enter_val || flag_g==mismatch_val) { break; } else if(flag_g==error_val) { return; } // count++; lcd_disp( special_q ); // 0x3f is hex value of ascii character "?" } if(flag_g==mismatch_val) { flag_g=0; continue; } flag_g=0; flag_g=compare(password,pass_confirm,0); if(flag_g==error_val) { flag_g=0; continue; } flag_g=0; if(right_count==7) { for(i=0; i<8; i++) { present_key[i] = password[i]; } lcd_disp1(key_changed,1); right_count=0; } } return; } /* Purpose:This function calls alarm and switches off the LCD display until a 5 digit master password is pressed */ void alarm() { code unsigned char crack[]= "0F3C7"; unsigned char temp[5],a,b,count; /* Program to switch off lcd display here */ lcd_com(0x08); alarm_1=1; while(1) { for (a=0; a<5; a++) { temp[4]=keypad(); } count=0; for (a=0 ; a<5 ; a++) { if(temp[a] != crack[a]) { alarm_1=1; for (b=0; b<5; b++) { temp[a]=0; } break; } else if(temp[a]==crack[a]) { count++; } } if(count!=4) { continue; } else { delay_ms(200); alarm_1=0; return; } } } |
Topic | Author | Date |
Please assess the c code written by me | 01/01/70 00:00 | |
Lots of details to improve on | 01/01/70 00:00 | |
REALLY confusing | 01/01/70 00:00 | |
timer delay review | 01/01/70 00:00 | |
what about this timer program | 01/01/70 00:00 | |
Still a lot of work to do | 01/01/70 00:00 | |
I have implemented many of the suggestions | 01/01/70 00:00 | |
Still not thinking about your symbol names | 01/01/70 00:00 | |
variable name dilema | 01/01/70 00:00 |