Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Code Lock Project - CCS C Compiler

Status
Not open for further replies.

AD76XYZ

Junior Member level 1
Joined
Jul 4, 2012
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,408
Hi,

I am newbie just completed the embedded course in which I learned 8051 and PIC. And for coding C language and learned Keil and CCS C.
My English is very average and hence please don't get irritate with my English. Some times I am not able to express what is in my mind.

Now I am doing project on code lock. Following is the details.

PIC16F886
4X3 Matrix Keypad
Three LEDs to show status like OPEN, CLOSED and LOCKED etc.
Buzzer
Solenoid


In this project I am not using LCD module. Very simple project. My hardware is ready on breadboard and now I want to just code in C for the same.
I will do coding in part, after successfully completing one part will move to another.

I am using PIC16F886 and will code in CCS C compiler. I am using CCS C Version 4.105. For testing purpose I have edited sample file and it ran successfully. And for sake of testing I used LCD 16X2 otherwise I am not using LCD in project.

I used driver for LCD and matrix keypad which come with CCS C. This LCD16X2.C which I used in code is nothing but LCD.C driver which comes with CCS C, I rename it to LCD16X2.C for sake of understanding for me. And KBD4X3.C driver I got from CCS C forum.

Following is the program which ran successfully.

Code:
#include <main.h>
#include <lcd16x2.c>
#include <KBD4X3.c>

#use delay(clock=4000000)

#define  OPEN_LED   RC3
#define  CLOSE_LED RC4
#define  LOCK_LED   RC5
#define  BUZZER      RC6
#define  SOLENOID   RC7





unsigned char pw_digit[4]={1,2,3,4}; // Fixed password
char digit[4]={0,0,0,0}; //Password entered by keypad will save in this array





void main()
{

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_2);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_ccp1(CCP_OFF);
   setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
   setup_oscillator(OSC_4MHZ);



lcd_init();
kbd_init();

lcd_putc("\fTest1\n");
lcd_putc("Test2");
delay_ms(2000);

lcd_putc("\fTest3\n");
lcd_putc("Test4");
delay_ms(2000);

lcd_putc("\fEnter Password..\n");

while(1)
   {
   
   
   k=kbd_getc();
      if(k!=0)
        if(k=='*')
          lcd_putc('\f');
        else
          lcd_putc(k);
    }


The above code ran and shows on LCD what ever I press on keypad. Now second part. I want to store what ever coming from keypad to digit[] array so I will compare it with fixed password array. For this I I coded on function and tested but not working.

This function.
Code:
void keyarray()
{
  char k=0;
  for(i=0;i<4;i++)
      {
      k=kbd_getc();
         if(k!=0)
            {
            digit[i]=k;
            }
      }
}

Please tell me where the mistake.

Another thing I do not understand is if(k!=0) in first program. Why is this and what is the function of this.
 

I assume that kbd_getc() returns 0 when nothing is pressed so if(k!=0) is there to ensure that the following code is only executed when something is pressed.

Your keyarray function uses a loop that immediately executes four loops and only stores a numbers if (k!=0) but, it doesn't wait for the keys to be pressed.
you have to use a condition that only proceeds in the next loop if something is pressed.
 
WOW! I got very first reply and also that is from SUPER MODERATOR. Now am sure I will get proper help.
Thanks alexan_e for reply and giving me information.

Can you give some code that how to implement in keyarray(). How to test any keypress and if keypress then go to ahead and also how to test for four times, if four numbers enter then it should quite.
 

Found this in Driver File of CCS PIC-C Compiler

Code:
 kbd_init()   Must be called before any other function.           ////
////                                                                   ////
////  c = kbd_getc(c)  Will return a key value if pressed or /0 if not ////
////                   This function should be called frequently so as ////
////                   not to miss a key press.
 
You can use several methods, one way is

Code:
void keyarray()
{
    char k=0;
	unsigned char i;

    for(i=0; i<4; i++)
    {
        do {
            k=kbd_getc();
        }
        while(k==0);

        digit[i]=k;
    }
}
 

Sir thank you very much the function is very much working fine. Thank you very much again.
 

Alexan sir, the function you suggested is working fine.
In next stage I want to implement number of wrong password entry and time out. When three time wrong password enter happen then it goes in locked state and require master password. And during password entry a some second of time is set, if user delayed in entering password it automatically goes to normal state in some second say 10 second.

For time out thing, I thought about using timer, so it will count accordingly.

I used this technique to compare the password if there any better way please suggest.
Code:
if(digit[0]==pw_digit[0] && digit[1]==pw_digit[1] && digit[2]==pw_digit[2] && digit[3]==pw_digit[3]
 

Alexan sir, the function you suggested is working fine.
In next stage I want to implement number of wrong password entry and time out. When three time wrong password enter happen then it goes in locked state and require master password. And during password entry a some second of time is set, if user delayed in entering password it automatically goes to normal state in some second say 10 second.

For time out thing, I thought about using timer, so it will count accordingly.

I used this technique to compare the password if there any better way please suggest.
Code:
if(digit[0]==pw_digit[0] && digit[1]==pw_digit[1] && digit[2]==pw_digit[2] && digit[3]==pw_digit[3]

you can use....
string.h header file and uses its function for string comparision

https://www.tutorialspoint.com/ansi_c/c_strcmp.htm
 

I used this technique to compare the password if there any better way please suggest.
Code:
if(digit[0]==pw_digit[0] && digit[1]==pw_digit[1] && digit[2]==pw_digit[2] && digit[3]==pw_digit[3]

For me that way if fine ant it will use very low resources.
You can add the string library and use the included functions but I don't think there is an advantage with such a simple comparison.

You can take a look at the suggested solutions https://www.edaboard.com/threads/255791/
 

alexan sir thanks for giving me link for how to shorten the if loop. And as you said it uses less resources. So I will use that one only but for get familiar with another method I will experiment with string function also as said by arunsharma0731.
 

Till date I have implemented the things. Reading keypad, storing into array and then comparing the two array. Now I want to store this array data, that is password in EEPROM of PIC16F886, since this controller has 256 byte of EEPROM built in. I read CCS C help files and other code. I have some questions, please suggets.

What is the use of #ROM?
I did not understand this, please give some example.

To write into internal EEPROM.
write_eeprom(address, value)

To read from internal EEPROM.
read_eeprom(address)

After reading keypad I am getting ASCII value of keys, say after pressing 1 I will get ASCII value of 1. So how I will write to eeprom.
Like this.
write_eeprom(0, digit), will use loop. Any change is required in format of number to store in eeprom.
Like wise reading from eeprom.

Another thing, when eeprom writing is going on we have to wait in code, right? We have to wait to finish writing of eeprom, so how to implement this. Can please any body give code or example.

- - - Updated - - -
 

I want to compare two array for password.

There are two array in code, one contain password and other one will be compared with first one.

digit[] and pwdigit[]

I come to know three methode of comparing password.

(1)
Using if, (if digit[0] == pwdigit[0] && digit[1] == pwdigit[1])

(2)
Using loop.
Code:
for(i=0;i<4;i++)
               {
                  if(digit[i] != pwdigit[i])
                     {
                        lcd_putc("\fWrong Password");
                     }
                     else
                     {
                        lcd_putc("\fRight Password");
                     }
               }

(3)
Using strcmp() function of C.


I want to know which one is better for my application. I am doing project on code lock for safe.
Am using.
PIC16F886
4X3 matrix keypad.
Leds
Solenoid
 
Last edited by a moderator:

Code:
for(i=0;i<4;i++)
               {
                  if(digit[i] != pwdigit[i])
                     {
                        lcd_putc("\fWrong Password");
                     }
                     else
                     {
                        lcd_putc("\fRight Password");
                     }
               }
[\code][/QUOTE]

\code is wrong so as you see the code tags don't work, you have to use /code.
If you use the codetag icon you can avoid such mistakes
 
alexan sir, you showed me the way to use code tag but you havent said any thing about the thing I asked.
 

All three ways give correct result , I don't know how we should decide which one is best.
The fists and second way are similar while the third one required an additional library, , you can use any of them it is a matter of taste and resources ( strcmp() ).

I'm not sure if you are doing what you intend with the loop, the given code will compare all characters and will output a message for each one of them if it is wrong or right.
You should only give one message at the end so that the user doesn't know which character was wrong
 

Thanks alexan sir for reply. Then I will do IF type or strcmp().
Another problem I am facing of EEPROM writing reading of PIC16F886. I read help file as well as saw sample code but still not able to figure out that how to write my digit[] array into EEPROM. Please help me in this. Dont give me direct code but atleast hints or dummie code so I will learn and understand.
 

I can't help you with the eeprom writing process in PIC, I have no idea of the instructions involved.

There should be a function that writes bytes in eeprom so use it to write digit[0], digit[1], digit[2]...
 

Thanks alexan sir. I will read EEPROM section fully and also experiment with code.

arunsharma, the link you provided is very nice and informative. Is this your site. Where this lab is located?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top