Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Help ! my project is working on simulation BUT NOT in real life !!!!!

Status
Not open for further replies.

sakerooo

New Member
Hi , I'm doing a ' digital lock ' project ...
i'm using mikro C for the coding ...
I finished the coding and used the simulator and it's working fine ...

but when i connected the REAL parts which are the PIC16F877A and keypad and LCD
they didn't work , even though they are working on the simulation !
i don't know what i am doing wrong :(

could you please help me ..
This is the code for the program

I Used MikroC 8.2

Code:
                              // global variables declaration

char i = 0 , m = 0 , error = 0 , error2 = 0 ,  j=1 , k=0;
const int number[]={'y','y',3,2,1,'x',6,5,4,'x',9,8,7,'x','x',0 ,'y'} ;
char temp[4] ;
char temp2[4] ;
unsigned cnt ;
   // interrupt subroutine

void interrupt()
{
     cnt++ ;
     INTCON = 0x20 ;
}

                                // subroutines


              /*
              *   SET NEW PASSWORD
              *
             */


void set_new_pass()
{
     S1:
     lcd_cmd(lcd_clear) ;
     lcd_out(1,1,"ENTER NEW PASS:") ;
     lcd_cmd(lcd_second_row) ;
     i = 0 ;
     get_new_pass :
     m = 0 ; error = 0 ;
     m = keypad_released() ;
     while(m==0 || m==14) goto get_new_pass ;
     if(m==16)                  // if CANCEL button is pressed
     {

            if(i!=0)
            {
                    while(portc.f2==0) {}
                    lcd_out(2,i," ") ;
                    lcd_cmd(lcd_move_cursor_left);
                    i-- ;
                    goto get_new_pass ;
            }
           else goto end_sub ;
     }
     lcd_chr_cp('*') ;
     temp[i] = number[m] ;
     i++ ;
   if(i==4) goto confirm_new_pass ;
     goto get_new_pass ;
     

lcd_cmd(lcd_clear) ;


              /*
              *   CONFIRM NEW PASSWORD
              *
             */

confirm_new_pass:
     
     
     
     while(keypad_released() !=14 && portc.f2==1) {}
     if(portc.f2==0) goto get_new_pass ;

start2:

     lcd_cmd(lcd_clear) ;
     lcd_out(1,1,"CONFIRM NEW PASS") ;
     lcd_cmd(lcd_second_row) ;
     i=0;
     ok:
     m = 0 ; error = 0 ;
     m = keypad_released() ;
     while(m==0 || m==14) goto ok ;

     edit2 :
     if(m==16)
     {
            if(i!=0)
            {
                  while(portc.f2==0) {}
                  if(eeprom_read(i-1)!=temp2[i-1] && error>0 ) error-- ;
                  lcd_out(2,i," ") ;
                  lcd_cmd(lcd_move_cursor_left);
                  i-- ;
                  goto ok ;
            }
            else goto end_sub ;
     }
     lcd_chr_cp('*') ;
     temp2[i] = number[m] ;
     if(temp[i]!=temp2[i]) error++ ;
     i++ ;
     if(i==4)
     {
                     wait4 :
                     m = 0 ;
                     m = keypad_released() ;
                     if(m!=16 && m!=14)goto wait4 ;
                     if(m==16) goto edit2 ;
                     goto compare2 ;
     }
     goto ok ;

     compare2:
     if(error!=0)
     {
                     lcd_cmd(lcd_clear) ;
                     lcd_out(1,4,"INVALID!!") ;
                     while(keypad_released()==0) {}
                     goto S1 ;
     }

      else

       for(i=0;i<6;i++)
     {
                     eeprom_write(i,temp[i]) ; // copy whatever inside temp[i] to i
                     delay_ms(20) ;
     }


    end_sub :
    lcd_cmd(lcd_clear) ;


    

}

              /*
              *   CHECK OLD PASSWORD
              *
              */

void check_old_pass()
{

     start2:
     m = 0 ; i = 0 ; error = 0 ;
     lcd_cmd(lcd_clear) ;
     lcd_out(1,1,"ENTER OLD PASS:") ;
     lcd_cmd(lcd_second_row) ;
     while(keypad_released()==16) {}
     wait2 :
     m = keypad_released() ;
     while(m==0 || m==14) goto wait2 ;

     edit2 :
     if(m==16)
     {
            if(i!=0)
            {
                  while(portc.f2==0) {}
                  if(eeprom_read(i-1)!=temp2[i-1] && error>0 ) error-- ;
                  lcd_out(2,i," ") ;
                  lcd_cmd(lcd_move_cursor_left);
                  i-- ;
                  goto wait2 ;
            }
            else goto end_sub ;
     }
     lcd_chr_cp('*') ;
     temp2[i] = number[m] ;
     if(eeprom_read(i)!=temp2[i]) error++ ;
     i++ ;
     if(i==4)
     {
                     wait4 :
                     m = 0 ;
                     m = keypad_released() ;
                     if(m!=16 && m!=14)goto wait4 ;
                     if(m==16) goto edit2 ;
                     goto compare2 ;
     }
     goto wait2 ;
     compare2:
     if(error!=0)
     {
                     lcd_cmd(lcd_clear) ;
                     lcd_out(1,4,"INVALID!!") ;
                     while(keypad_released()==0) {}
                     goto start2 ;
     }

      else set_new_pass() ;
      end_sub:
      lcd_cmd(lcd_clear) ;
}




                           // main program

void main()
{


     trisb = 0 ; portb = 0 ;
     trisd = 0 ; portd = 0 ;
     trisc.f1 = 0 ; portc = 0 ;
     trisc.f2 =1 ;

     OPTION_REG = 0x84 ;       // Assign prescaler to TMR0
     INTCON = 0xA0 ;           // Enable TMRO interrupt


                      // lcd initialization
     lcd_init(&portb) ;
     lcd_cmd(lcd_cursor_off) ;

                        // keypad initialization
     keypad_init(&portd) ;


     while(1)
     {
             start :

            //checking_new_pass();
           for (j;j>0;j--)
            {
             if(eeprom_read(0) != 0xff)
             set_new_pass();
             }
             if(eeprom_read(0) != 0xff) goto get_current_pass ;
             get_current_pass :

             i = 0 ;
             error = 0 ;
             lcd_cmd(lcd_clear) ;
             lcd_out(1,4,"ENTER PASS:") ;

             lcd_cmd(lcd_second_row) ;


             wait :

             m = keypad_read() ;
             while(m==0) goto wait ;

             cancel_or_set :
             if(m==16)
             {
                    cnt = 0 ;
                    tmr0 = 0 ;
                    while(keypad_read() == 16)
                    {
                          if( cnt >= 300 )
                          {
                              check_old_pass() ;
                              goto get_current_pass ;
                          }
                    }
                          if(i!=0)
                          {
                               while(keypad_released()==16) {}
                               if(eeprom_read(i-1)!=temp2[i-1] && error>0 ) error-- ;
                               lcd_out(2,i," ") ;
                               lcd_cmd(lcd_move_cursor_left);
                               i-- ;
                               goto wait ;
                          }
             }


             m = 0 ;
             m = keypad_released() ;
             while(m==0) goto wait ;
             if(m==16) goto cancel_or_set ;

             lcd_chr_cp('*') ;
             temp2[i] = number[m] ;

             if(eeprom_read(i)!=temp2[i]) error++ ;
             i++ ;
             if(i==4)
             {
                             wait4 :
                             m = 0 ;
                             m = keypad_released() ;
                             while(m!=14 && m!=16 && portc.f2==1) goto wait4 ;
                             if(m == 16) goto cancel_or_set  ;
                             if(portc.f2==0) goto wait ;
                             goto compare ;
             }
             goto wait ;

             compare :
             for(i=0;i<4;i++)
             {
                             if(temp2[i]!=eeprom_read(i)) error++ ;
             }

             if(error!=0)
             {
                             lcd_cmd(lcd_clear) ;
                             lcd_out(1,3,"INVALID PASS") ;
                             error = 0 ;
                             while(keypad_released()==0) {}
                             goto get_current_pass ;
             }

             else
             {
                             lcd_cmd(lcd_clear) ;
                             lcd_out(1,7,"OPEN") ;
                             portc.f1 = 1 ; delay_ms(5000) ;
                             portc.f1 = 0 ;

            }
}
}


this is the simulation file
 

Attachments

  • PROJECT.zip
    21.9 KB · Views: 188
Last edited:
hi,
Which simulator and what language is your program written in.???
and in what way does it not work.?

BTW: Please use normal text fonts and formatting.:)
 
i wrote it using MikroC 8.2 and I used Proteus 7.6 simulator..
and it doesn't work at all .. i mean the LCD is blank and i don't know if even the keypad is working :(
what should i do ?
 
i wrote it using MikroC 8.2 and I used Proteus 7.6 simulator..
and it doesn't work at all .. i mean the LCD is blank and i don't know if even the keypad is working :(
what should i do ?

hi,
I do not write in 'C', other members do, so I would suggest you post the 'C' program without all those other simulation files..
 
hi,
I do not write in 'C', other members do, so I would suggest you post the 'C' program without all those other simulation files..

I posted the simulation file to make sure that my connections are correct ..
because the weird thing is the project working just fine on the simulation .
So i'm guessing that the problem isn't from the coding , right ?
 
Last edited:
I posted the simulation file to make sure that my connections are correct ..
because the weird thing is the project working just fine on the simulation .
So i'm guessing that the problem isn't from the coding , right ?

hi,
I find that any delays in the simulation may not be suitable for real hardware, especially the LCD.
They work fine in simulation but not in an actual PIC.
 
This is the code for the program

Code:
                              // global variables declaration

char i = 0 , m = 0 , error = 0 , error2 = 0 ,  j=1 , k=0;
const int number[]={'y','y',3,2,1,'x',6,5,4,'x',9,8,7,'x','x',0 ,'y'} ;
char temp[4] ;
char temp2[4] ;
unsigned cnt ;
   // interrupt subroutine

void interrupt()
{
     cnt++ ;
     INTCON = 0x20 ;
}

                                // subroutines


              /*
              *   SET NEW PASSWORD
              *
             */


void set_new_pass()
{
     S1:
     lcd_cmd(lcd_clear) ;
     lcd_out(1,1,"ENTER NEW PASS:") ;
     lcd_cmd(lcd_second_row) ;
     i = 0 ;
     get_new_pass :
     m = 0 ; error = 0 ;
     m = keypad_released() ;
     while(m==0 || m==14) goto get_new_pass ;
     if(m==16)                  // if CANCEL button is pressed
     {

            if(i!=0)
            {
                    while(portc.f2==0) {}
                    lcd_out(2,i," ") ;
                    lcd_cmd(lcd_move_cursor_left);
                    i-- ;
                    goto get_new_pass ;
            }
           else goto end_sub ;
     }
     lcd_chr_cp('*') ;
     temp[i] = number[m] ;
     i++ ;
   if(i==4) goto confirm_new_pass ;
     goto get_new_pass ;
     

lcd_cmd(lcd_clear) ;


              /*
              *   CONFIRM NEW PASSWORD
              *
             */

confirm_new_pass:
     
     
     
     while(keypad_released() !=14 && portc.f2==1) {}
     if(portc.f2==0) goto get_new_pass ;

start2:

     lcd_cmd(lcd_clear) ;
     lcd_out(1,1,"CONFIRM NEW PASS") ;
     lcd_cmd(lcd_second_row) ;
     i=0;
     ok:
     m = 0 ; error = 0 ;
     m = keypad_released() ;
     while(m==0 || m==14) goto ok ;

     edit2 :
     if(m==16)
     {
            if(i!=0)
            {
                  while(portc.f2==0) {}
                  if(eeprom_read(i-1)!=temp2[i-1] && error>0 ) error-- ;
                  lcd_out(2,i," ") ;
                  lcd_cmd(lcd_move_cursor_left);
                  i-- ;
                  goto ok ;
            }
            else goto end_sub ;
     }
     lcd_chr_cp('*') ;
     temp2[i] = number[m] ;
     if(temp[i]!=temp2[i]) error++ ;
     i++ ;
     if(i==4)
     {
                     wait4 :
                     m = 0 ;
                     m = keypad_released() ;
                     if(m!=16 && m!=14)goto wait4 ;
                     if(m==16) goto edit2 ;
                     goto compare2 ;
     }
     goto ok ;

     compare2:
     if(error!=0)
     {
                     lcd_cmd(lcd_clear) ;
                     lcd_out(1,4,"INVALID!!") ;
                     while(keypad_released()==0) {}
                     goto S1 ;
     }

      else

       for(i=0;i<6;i++)
     {
                     eeprom_write(i,temp[i]) ; // copy whatever inside temp[i] to i
                     delay_ms(20) ;
     }


    end_sub :
    lcd_cmd(lcd_clear) ;


    

}

              /*
              *   CHECK OLD PASSWORD
              *
              */

void check_old_pass()
{

     start2:
     m = 0 ; i = 0 ; error = 0 ;
     lcd_cmd(lcd_clear) ;
     lcd_out(1,1,"ENTER OLD PASS:") ;
     lcd_cmd(lcd_second_row) ;
     while(keypad_released()==16) {}
     wait2 :
     m = keypad_released() ;
     while(m==0 || m==14) goto wait2 ;

     edit2 :
     if(m==16)
     {
            if(i!=0)
            {
                  while(portc.f2==0) {}
                  if(eeprom_read(i-1)!=temp2[i-1] && error>0 ) error-- ;
                  lcd_out(2,i," ") ;
                  lcd_cmd(lcd_move_cursor_left);
                  i-- ;
                  goto wait2 ;
            }
            else goto end_sub ;
     }
     lcd_chr_cp('*') ;
     temp2[i] = number[m] ;
     if(eeprom_read(i)!=temp2[i]) error++ ;
     i++ ;
     if(i==4)
     {
                     wait4 :
                     m = 0 ;
                     m = keypad_released() ;
                     if(m!=16 && m!=14)goto wait4 ;
                     if(m==16) goto edit2 ;
                     goto compare2 ;
     }
     goto wait2 ;
     compare2:
     if(error!=0)
     {
                     lcd_cmd(lcd_clear) ;
                     lcd_out(1,4,"INVALID!!") ;
                     while(keypad_released()==0) {}
                     goto start2 ;
     }

      else set_new_pass() ;
      end_sub:
      lcd_cmd(lcd_clear) ;
}




                           // main program

void main()
{


     trisb = 0 ; portb = 0 ;
     trisd = 0 ; portd = 0 ;
     trisc.f1 = 0 ; portc = 0 ;
     trisc.f2 =1 ;

     OPTION_REG = 0x84 ;       // Assign prescaler to TMR0
     INTCON = 0xA0 ;           // Enable TMRO interrupt


                      // lcd initialization
     lcd_init(&portb) ;
     lcd_cmd(lcd_cursor_off) ;

                        // keypad initialization
     keypad_init(&portd) ;


     while(1)
     {
             start :

            //checking_new_pass();
           for (j;j>0;j--)
            {
             if(eeprom_read(0) != 0xff)
             set_new_pass();
             }
             if(eeprom_read(0) != 0xff) goto get_current_pass ;
             get_current_pass :

             i = 0 ;
             error = 0 ;
             lcd_cmd(lcd_clear) ;
             lcd_out(1,4,"ENTER PASS:") ;

             lcd_cmd(lcd_second_row) ;


             wait :

             m = keypad_read() ;
             while(m==0) goto wait ;

             cancel_or_set :
             if(m==16)
             {
                    cnt = 0 ;
                    tmr0 = 0 ;
                    while(keypad_read() == 16)
                    {
                          if( cnt >= 300 )
                          {
                              check_old_pass() ;
                              goto get_current_pass ;
                          }
                    }
                          if(i!=0)
                          {
                               while(keypad_released()==16) {}
                               if(eeprom_read(i-1)!=temp2[i-1] && error>0 ) error-- ;
                               lcd_out(2,i," ") ;
                               lcd_cmd(lcd_move_cursor_left);
                               i-- ;
                               goto wait ;
                          }
             }


             m = 0 ;
             m = keypad_released() ;
             while(m==0) goto wait ;
             if(m==16) goto cancel_or_set ;

             lcd_chr_cp('*') ;
             temp2[i] = number[m] ;

             if(eeprom_read(i)!=temp2[i]) error++ ;
             i++ ;
             if(i==4)
             {
                             wait4 :
                             m = 0 ;
                             m = keypad_released() ;
                             while(m!=14 && m!=16 && portc.f2==1) goto wait4 ;
                             if(m == 16) goto cancel_or_set  ;
                             if(portc.f2==0) goto wait ;
                             goto compare ;
             }
             goto wait ;

             compare :
             for(i=0;i<4;i++)
             {
                             if(temp2[i]!=eeprom_read(i)) error++ ;
             }

             if(error!=0)
             {
                             lcd_cmd(lcd_clear) ;
                             lcd_out(1,3,"INVALID PASS") ;
                             error = 0 ;
                             while(keypad_released()==0) {}
                             goto get_current_pass ;
             }

             else
             {
                             lcd_cmd(lcd_clear) ;
                             lcd_out(1,7,"OPEN") ;
                             portc.f1 = 1 ; delay_ms(5000) ;
                             portc.f1 = 0 ;

            }
}
}
 
hi,
I ran your hex file in Oshonsoft, it runs OK, but I would say the LCD delays are too short for the real world. See my sim image.
 

Attachments

  • 000esp03.gif
    000esp03.gif
    5.8 KB · Views: 271
hi,
I ran your hex file in Oshonsoft, it runs OK, but I would say the LCD delays are too short for the real world. See my sim image.

okay , then i should increase the delays for the LCD..
but are the rest of connections correct ?
and thank you
 
ummm okay i have a question .
Well, i didn't actually use delays with the LCD ..
because the LCD shows for example " enter pass " and it waits until the user enter the password ..
so why should i use delay ?
 
Because a pic can do over a 1000000 instructions a second do you think you could see that happen thats at 4 mhz
20 mhz it's 4000000 instructions Where fast but not that fast
 
Have you proven the hardware? I.E. the PIC connections and the oscillator for example?

Write a simple program that toggles a pin high to low and program the PIC with that and see if the actual hardware is working.

You can connect either a multimeter or a led to the pin to get feedback on if it is switching or not, I would add a 1 second delay between toggling the pin to get a good reading on the meter or visual on the LED.

Wilksey
 
Simulators tend to miss certain issues....just today I had a really nice bit of sim. tested asm code machine gun my actual device.

I was multiplexing a pin to both transmit a 1Khz one wire simplex data stream and sample a 0-5VDC signal.
Well the sim was happy but the adc on the 16F got quite confused with stray voltages left over by the data stream. Seems the PCB capacitances etc caused some of the datastream voltage to alter the ADC sampling. Lots of jitter resulted. Possibly some ringing on the pcb as well as the LED display of the ADC was doing a superfast oscillation to become a blur.

Killing the datastream multiplex solved the prob.
 
Wilksey makes a good point. I have dealt with many "fried" PICs before. Best way to tell is to check pins by switching high and low with delays in between.
Also, I'm not very familiar with that C compiler, I deal mostly with assembly, but... correct me if I am wrong, I don't see where you either disable the watch dog timer (WDT) or clear the WDT. This timer counts up, and once it reaches a certain value, it'll reset the PIC. Thus, causing problems. I would disable it in your config bits just to be sure.
 
Have you proven the hardware? I.E. the PIC connections and the oscillator for example?

Write a simple program that toggles a pin high to low and program the PIC with that and see if the actual hardware is working.

You can connect either a multimeter or a led to the pin to get feedback on if it is switching or not, I would add a 1 second delay between toggling the pin to get a good reading on the meter or visual on the LED.

Wilksey

you make a good point , i'll test it now
thanks
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top