Thursday 1 November 2012

WEEK 14

To Preparation and Present about the project


The presentation will start on 8/11/12 (Thursday) week 14 and all FYP Degree sem 2 will present their project. Each student will be examined by two checks,.  My Assessors are also many suggestions for my project and insyakallah I will propose my ideas on Assessors to my supervisor Mr. Ahmad Bin Che Suhairi Rizuan.

WEEK 13

Poster


WEEK 11 & WEEK 12

Circuit Problem 

problematic circuit, the output is not available and I will find the problem and troubleshooting be done......

WEEK 8, WEEK 9 & WEEK 10


programming project

Source Code
//define flag and pin used
#define success               flag.F0
#define fail                  flag.F1
#define timeout_success       flag.F4
#define sec_flag              timer_flag.F0
#define min_flag              timer_flag.F1
#define buzzer                PORTE.F0
#define sw1                   PORTA.F0
#define sw2                   PORTA.F1
#define sw3                   PORTA.F2
#define sw4                   PORTA.F3
#define door_flag             flag.F5
#define engine_flag           flag.F6
#define door_relay            PORTD.F0
#define engine_relay          PORTD.F1
#define led_ok                PORTD.F2
#define led_fail              PORTD.F
//**************************************
//define variables
char j;
static char flag,timer_flag;
char msec,seconds, minutes, hours;
char temp[13];
char suprema[13];
unsigned long id;
char txt[12];
char kp; //keypad store
char timer1;
int timer2;
char state;
int loop;
int id_display;
char x;
char finger_state;
char mode;
//**********************************************************************

// declare function prototype
void check_free_scan(void);
void delay1s(void);void delay100us(void);void delay10ms(void);void delay500ms(void);
delay100ms(void);
void counter1(void);
void counter2(void);
//********************************************************
//declare suprema protocol
void on_freescan(void);
void off_freescan(void);
void cancel_suprema(void);
//*********************************************************
void write_suprema(char,char,char,char,char,char,char,char,char,char,char,char);
void rx_suprema();
void enrol_user_addnew(void);
void delete_user(void);
void delete_all_user(void);
void beep(void);
void beep_long(void);
void display_id(void);
void display_finger(void);
void check_mode(void);
//*************************************************************************

//intterupt for fingerprint sensor
//read fingerprint data
//if data = 61 = success
//else fail

void interrupt(){
if (PIR1.RCIF){
     PIE1.RCIE = 0;
temp[0] = RCREG;
     INTCON.GIE = 0;
loop = 0;
if(temp[0] == 0x40){
while(loop<12){
while(!Usart_Data_Ready()) ;
//                while(!PIR1.RCIF);
temp[1+loop] = RCREG;
loop++;}
if( temp[10] == 0x61){
success = 1;
fail = 0;}
else{
success = 1;
fail = 1;}

        }
     PIE1.RCIE = 1;
     }
//**********************************************
//main routine
//
void main() {

  ADCON1 = 0x06;                                     //1 = input,  0 = output
TRISA  = 0b00001111;                               // RA0 i RA1 are a inputs
TRISB  = 0x00;                                     //setup port direction for A,B<C,D,E
  TRISE = 0b00000110;
  TRISC = 0b10000000;
  TRISD = 0b00000000;
  PORTC = 0x00;
  PORTD = 0x00;
  TRISB = 0b11110000;
  OPTION_REG = 0b00000101;                          //timer ratio 1:64
  PORTB = 0x00;
  Usart_Init(9600);                                 //baud rate to communicate with sensor = 9600
  INTCON = 0xA0;                                   // Enable TMRO interrupt
  INTCON.GIE = 1;
  INTCON.PEIE = 1;
  PIE1.RCIE = 1;
  PIE1.RCIE = 0;
  LCD_Config(&PORTB,6,5,4,3,2,1,0);
  Soft_Uart_Init(PORTC, 5, 4, 1200, 0);
  Lcd_Init(&PORTB);                        // Lcd_Init_EP4, see Autocomplete
  LCD_Cmd(LCD_CURSOR_OFF);                 // send command to LCD (cursor off)
  LCD_Cmd(LCD_CLEAR);                      // send command  to LCD (clear LCD)
  cancel_suprema();                        //if sensor detect
  on_freescan();
  Lcd_Out(1,1, "Finger Print");            //then fingerprint system
  Lcd_Out(2,4, "System");
  Delay_ms(2000);
  LCD_Cmd(LCD_CLEAR);
  engine_flag=0;                          //off all flag and pins
  door_flag = 0;
  engine_relay=0;
  door_relay=0;
  display_finger();
mode=2;                                //start with m2

if(PORTA.F0)                            //during startup when sw1 press??
      {
        LCD_Cmd(LCD_CLEAR);                  //then delete all users
        Lcd_Out(1,1, "Delete All User:");
        PIE1.RCIE = 0;
        off_freescan();
        delay_ms(500);
state = 1;
        delete_all_user();
        delay_ms(500);
        on_freescan();
        PIE1.RCIE = 1;
beep();
        beep_long();
        LCD_Cmd(LCD_CLEAR);
        display_finger();
      }
lMainLoop:                            //main loop

if (RCSTA.FERR){
kp = RCREG;}


state ='C';
if(mode==1 && sw4)             //check if mode=1 and sw4 press??
      {
success=1;                    //then success
       check_free_scan();            //activate door
      }
if(mode==2)                    //when mode = 2
      check_free_scan();             //check if finger place only??

if(mode==3 && sw4)             //when mode==3 and sw4 press??
      {
       check_free_scan();            //wait for success and wait finger
      }
if (PORTA.F0)                       //when sw1 on??
      {
        finger_state = 'E';               //enrol user at module
        LCD_Cmd(LCD_CLEAR);               //when finger place, will save in module
        Lcd_Out(1,1, "Enrol User:");
        PIE1.RCIE = 0;
        off_freescan();
        delay_ms(500);
state = 3;
        enrol_user_addnew();
        display_id();
        delay_ms(500);
        on_freescan();
        PIE1.RCIE = 1;
beep();
        beep_long();
        LCD_Cmd(LCD_CLEAR);
        display_finger();
      }

if (PORTA.F1)                     //when sw2 press??
      {
        finger_state = 'D';             //delete user and remove finger form module
        LCD_Cmd(LCD_CLEAR);
        Lcd_Out(1,1, "Delete User:");
        PIE1.RCIE = 0;
        off_freescan();
        delay_ms(500);
state = 1;
        delete_user();
        display_id();
        delay_ms(500);
        on_freescan();
        PIE1.RCIE = 1;
beep();
        beep_long();
        LCD_Cmd(LCD_CLEAR);
        display_finger();
      }

if(PORTA.F2)                   //when sw3 press??
      {                              //change mode from m1,m2,m3
mode++;
if(mode>=4)
mode=1;
       check_mode();
success=0;
while(PORTA.F2);
      }



goto lMainLoop;                  //loop main loop

}//~!


//******************************************************************************
void check_free_scan(void){        //when finger detect
if (success){
    LCD_Cmd(LCD_CLEAR);
    min_flag = 1;
if(fail==0){                  //when in success mode
success = 0;
        RCSTA.CREN = 1;
        timeout_success = 1;
        display_id();

         finger_state = 'C';
if(!door_flag)            //when off mode, then toggle to turn on
         {
          lcd_chr(1,1,'T');
          lcd_chr_cp('u');
          lcd_chr_cp('r');
          lcd_chr_cp('n');
          lcd_chr_cp(' ');
          lcd_chr_cp('O');
          lcd_chr_cp('n');
          lcd_chr_cp(' ');
//          Lcd_Out(1,1, "Turn On   ");
          door_relay=1;          //on relay
          door_flag=1;
          led_ok=1;
          led_fail=0;
         }
else                   //when in on mode, then toggle to turn off
         {
          lcd_chr(1,1,'T');
          lcd_chr_cp('u');
          lcd_chr_cp('r');
          lcd_chr_cp('n');
          lcd_chr_cp(' ');
          lcd_chr_cp('O');
          lcd_chr_cp('f');
          lcd_chr_cp('f');
//          Lcd_Out(1,1, "Turn Off   ");
          door_relay=0;      //off relay
          door_flag=0;
         }
seconds=0;
beep();
         Delay_ms(3000);
         display_finger();
         led_ok=1;
         led_fail=0;
        }
else{
         Lcd_Out(1,1, "Fail:    ");     //when in fail mode
         RCSTA.CREN = 1;                //then display fail and beep buzzer
success = 0;
fail = 0;
beep();
beep();
beep();
         engine_flag=0;
         door_flag = 0;
         engine_relay=0;
         door_relay=0;
         timeout_success = 1;
         LCD_Cmd(LCD_CLEAR);
         display_finger();
         led_ok=0;
         led_fail=1;
         }

  }
}

//******************************************************************************

void delay1s(void){            //delay timer routine
  Delay_ms(1000);}

void delay100us(void){
  Delay_us(100);}

void delay10ms(void){
  Delay_ms(10);}

void delay100ms(void){
  Delay_ms(100);}

void delay500ms(void){
  Delay_ms(500);}
//******************************************************************************
//function protocol send to sensor to enrol user
void enrol_user_addnew(void){
if (state==3)
  write_suprema(0x40,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x79,0x0A);
  rx_suprema();
}
//******************************************************************
//send protocol to delete user
void delete_user(void){
if (state==1)
  write_suprema(0x40,0x1E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0A);
  rx_suprema();
if (temp[10] == 0x61){
delay1s();
   }
else{
delay1s();
  }
}

//send protocol to delete all user
void delete_all_user(void){
if (state==1)
  write_suprema(0x40,0x17,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0A);
  rx_suprema();
if (temp[10] == 0x61){
delay1s();
   }
else{
delay1s();
  }
}

//*************************************************************************
//suprema protocol
// protocol activate scan
void on_freescan(){
do{
  write_suprema(0x40,0x01,0x00,0x00,0x00,0x00,0x31,0x00,0x00,0x00,0x84,0x0A);
  rx_suprema();
delay100us();
}while(temp[10] != 0x61);
}

//protocol of disable scan
void off_freescan(){
do{
  write_suprema(0x40,0x01,0x00,0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x84,0x0A);
  rx_suprema();
delay100us();
}while(temp[10] != 0x61);
}

void cancel_suprema(){
  write_suprema(0x40,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0A);
  rx_suprema();
}


//write data to sensor
//send 13byte
//send address,parameter,flag,checksum
void write_suprema(char add,char cmd,char param1,char param2,char param3,
char param4,char size1,char size2,char size3,char size4,char flag,char end){
char checksum;

checksum = add+cmd+param1+param2+param3+param4+size1+size2+size3+size4+flag;
Usart_write(add);
Usart_write(cmd);
Usart_write(param1);
Usart_write(param2);
Usart_write(param3);
Usart_write(param4);
Usart_write(size1);
Usart_write(size2);
Usart_write(size3);
Usart_write(size4);
Usart_write(flag);
Usart_write(end);
}

//wait for reply
//data save in temp
void rx_suprema(){
char count;
count = 0;
temp[10] = 0;
while(count<=12){
while(!PIR1.RCIF);
temp[count]=RCREG;
count++;
  }
}


//************************************************************************
//normal beep
void beep(void){
buzzer=1;
Delay_ms(100);
buzzer=0;
Delay_ms(100);
}

//long beep
void beep_long(void){
buzzer=1;
Delay_ms(500);
buzzer=0;
}
//*************************************************************************
//display id at lcd
//read from sensor
void display_id()
{
 id_display=0;
 id_display = temp[3];
 id_display  = id_display <<8 | temp[2] ;
inttostr(id_display,txt);
 lcd_out(2,1,"ID:");
 soft_uart_write(finger_state);
for(x=2;x<=5;x++)
 {
if(txt[x]==' ')
  {
txt[x]='0';
   soft_uart_write('0');
  }
else
  {
   lcd_chr_cp(txt[x]);
   soft_uart_write(txt[x]);
  }

 }

}

//display place finger
void display_finger(void)
{
 lcd_out(1,1,"    Place Finger");
 check_mode();
}

//display mode on lcd
void check_mode(void)
{
if(mode==1)
       {
        lcd_chr(1,1,'M');
        lcd_chr_cp('1');
       }
else if (mode==2)
       {
        lcd_chr(1,1,'M');
        lcd_chr_cp('2');
       }
else if(mode==3)
       {
        lcd_chr(1,1,'M');
        lcd_chr_cp('3');
       }
}



Circuit Design