CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

non-matrix keypad problem
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
danyboy666



Joined: 01 Mar 2011
Posts: 30

View user's profile Send private message

non-matrix keypad problem
PostPosted: Tue Mar 15, 2011 12:49 pm     Reply with quote

Hello, first of all I'm a student just starting to learn coding with C.

I have a problem, I manage to combine to input ports into one as so to make a 16 Bits input.
Code:
#define InputAB (((input_a() & 0b00001111) * 0x100) + input_b())

I need to read the input state of that custom port and store this value into a variable.

I want to make a program which will activate a buzzer if a button is pressed. The keypad is a non-matrix keypad with 12 common.

Right now I have this...
Code:

void main()
{

    do{

        if (InputAB<0xFFF)
        {   
           
            output_HIGH(buzzer);
            output_HIGH(ledR);
        }
        else
        {
            output_LOW(buzzer);
            output_LOW(ledR);
        }

    } while(true);
}

The code works but now I need to program a pin code to activate a door lock. I just don't know how I could do this, the projects I was working on before this one were fairly simple but this one is a bit more complex because I need to read a 16 Bits value and store this into a variable.

If someone could lend me a hand it would be greatly appreciated.
danyboy666



Joined: 01 Mar 2011
Posts: 30

View user's profile Send private message

PostPosted: Tue Mar 15, 2011 3:58 pm     Reply with quote

ok up to now i have this....

Code:
#define InputAB (((input_a() & 0b00001111) * 0x100) + input_b())

#define kp_0 0b111111111110
#define kp_1 0b111111111101
#define kp_2 0b111111111011
#define kp_3 0b111111110111
#define kp_4 0b111111101111
#define kp_5 0b111111011111
#define kp_6 0b111110111111
#define kp_7 0b111101111111
#define kp_8 0b111011111111
#define kp_9 0b110111111111
#define kp_ast 0b101111111111
#define kp_car 0b011111111111
#define ledV PIN_C0
#define ledR PIN_C1
#define buzzer PIN_C2

void sub();
void sub2();
int passwordValid;
int8 i,j,k;
int16 inputRead;

#define PASSWORD_LENGTH 4

int16 password[PASSWORD_LENGTH] = {kp_1,kp_2,kp_3,kp_4}, userInput[PASSWORD_LENGTH] = {0,0,0,0};
int8 inputCount=0;

void main()
{
   DO
   {
      IF (InputAB < 0xFFF)
      {
         inputRead = InputAB;
         sub () ;
         output_HIGH (buzzer) ;
         output_HIGH (ledR) ;
      }

      ELSE
      {
         output_LOW (buzzer) ;
         output_LOW (ledR) ;
      }
   } WHILE (true) ;
}

void sub()
{
   IF(inputCount < PASSWORD_LENGTH)
   {
      // User hasn't typed 4 numbers yet, add them
      userInput[inputCount] = inputRead;
      inputCount++;
   }

   ELSE
   {
      passwordValid = true;
      FOR (k = 0; k < PASSWORD_LENGTH; k++)
      {
         IF (userInput[k] != password[k])
         {
            passwordValid = false;
         }

         // Empty userInput so we can have another try ! Initialize at zero.
         userInput[k] = 0;
      }

      inputCount = 0; // Don't forget to get this to 0 too
      IF (passwordValid)
         sub2 () ;

   }
}

void sub2()
{
   FOR (i = 0; i <= 3; i++)
   {
      output_HIGH (buzzer) ;
      output_LOW (ledR) ;
      delay_ms (100) ;
      output_LOW (buzzer) ;
      output_HIGH (ledR) ;
      delay_ms (50) ;
   };

   output_LOW (ledR) ;
   output_HIGH (ledV) ;
   delay_ms (10000) ;
   
   FOR (j = 0; j <= 1; j++)
   {
      output_HIGH (buzzer) ;
      output_LOW (ledV) ;
      delay_ms (100) ;
      output_LOW (buzzer) ;
      output_HIGH (ledV) ;
      delay_ms (50) ;
   };

   output_LOW (ledV) ;
   output_HIGH (ledR) ;
}


seems that the variable "inputRead" is to short tough....
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Mar 16, 2011 2:37 am     Reply with quote

Look sharp! What happens, if a button is pressed. Input count will immediately cycle through 0 to 3, without taking care for individual key presses. Apparently something is missing...

Some aspects yet ignored by your code:
- debounce
- detection of a new key press

Although not absolutely necessary, key decoding seems reasonable.
danyboy666



Joined: 01 Mar 2011
Posts: 30

View user's profile Send private message

PostPosted: Wed Mar 16, 2011 9:06 pm     Reply with quote

Well I thought this line
Code:
inputRead = InputAB;

would return the proper value of the pin ....

I've done some test, when I change this
Code:
#define PASSWORD_LENGTH 4

int16 password[PASSWORD_LENGTH] = {kp_1,kp_2,kp_3,kp_4}, userInput[PASSWORD_LENGTH] = {0,0,0,0};

for this
Code:

#define PASSWORD_LENGTH 1

int16 password[PASSWORD_LENGTH] = {kp_1}, userInput[PASSWORD_LENGTH] = {0};

Its read the pin alright and runs my sub function properly but that's all, the minute I change the array for 2 or more the code is not working anymore...
danyboy666



Joined: 01 Mar 2011
Posts: 30

View user's profile Send private message

PostPosted: Fri Mar 18, 2011 4:42 pm     Reply with quote

nobody can lend a hand?
danyboy666



Joined: 01 Mar 2011
Posts: 30

View user's profile Send private message

PostPosted: Tue Mar 29, 2011 2:30 pm     Reply with quote

i've come up with this...

Code:
//Nom donné au .hex lors de la compilation
#EXPORT (FILE=Amelie_Dany_non-matrix_keypad_code.hex)

#define inputAB (((input_a() & 0b00001111) * 0x100) + input_b())

#define kp_0 0b0000111111111110
#define kp_1 0b0000111111111101
#define kp_2 0b0000111111111011
#define kp_3 0b0000111111110111
#define kp_4 0b0000111111101111
#define kp_5 0b0000111111011111
#define kp_6 0b0000111110111111
#define kp_7 0b0000111101111111
#define kp_8 0b0000111011111111
#define kp_9 0b0000110111111111

#define kp_ast 0b0000101111111111
#define kp_car 0b0000011111111111

#define ledV PIN_C0
#define ledR PIN_C1

#define buzzer PIN_C2

#define code_admin 9876

//Variables globales.
int8 i,input=0,strobe,try=0;
int16 inputRead=0,code_validation=0;
int16 user_password=4321,temp=0,val1=0,val2=0,val3=0,val4=0,temp_password=0;

//Addition des valeurs décalés et mettre en constante le code d'entré.
#define code_entre ( (val4 * 1000) + (val3 * 100) + (val2 * 10) + (val1 * 1) )

//Déclarations des sous-fonctions.
void unlock();
void kp_read();
void alarm();


//Boucle principale.
void main()
{



   
   DO
   {
      //Si une touche est appuyer.
      IF (inputAB < 0xFFF)
      {
         
         input++;
         try = input / 4;
         inputRead = inputAB; //Mettre en variable la touche entrée.
         kp_read (); //Appel de la fonction lecture de clavier.
         
         
         
         output_HIGH (buzzer); //Activer le buzzeur.
         //Boucle avec indicateur lumineux de touches appuyées.
         FOR (strobe = 0; strobe <= 1; strobe++)
         {
           
            output_HIGH (ledR);
            delay_ms (50);
            output_LOW (ledR);
            delay_ms (50);
         }

         
         IF (try == 5)
         {
           
           
            alarm ();
           
            input = 0;
           
            temp_password = user_password;
            user_password = code_admin;
         }

      }

      ELSE
      {
         output_LOW (buzzer); //Sinon buzzeur est éteint.
      }

      //Comparer la valeur essaie avec le mot de passe.
     
      IF (code_validation == user_password && input > 3)
      {
         
         
         unlock (); //Appel de la fonction débarrer.
         //Remettre les variables de lectures à 0.
         val4 = 0;
         val3 = 0;
         val2 = 0;
         val1 = 0;
         input = 0;
         try--;
         
         //Remettre la variable essaie à 0.
         code_validation = 0;
         
         IF (user_password == code_admin) user_password = temp_password;
      }

     
      IF (code_validation == code_admin && input > 3)
      {
         
         unlock (); //Appel de la fonction débarrer.
         //Remettre les variables de lectures à 0.
         val4 = 0;
         val3 = 0;
         val2 = 0;
         val1 = 0;
         input = 0;
         try--;
         
         //Remettre la variable essaie à 0.
         code_validation = 0;
         
         
         IF (user_password == code_admin) user_password = temp_password;
      }

   }

   
   //Fin de la boucle infinie.
   WHILE (true);
}

   //Fonction débarre.
   VOID unlock ()
   {
     
      //Boucle indication sonore.
      FOR (i = 0; i < 4; i++)
      {
         delay_ms (50);
         output_HIGH (buzzer);
         output_LOW (ledR);
         delay_ms (100);
         output_LOW (buzzer);
         output_HIGH (ledR);
      }

      output_LOW (ledR);
      FOR (i = 0; i < 10; i++)
      {
         
         output_LOW (ledV);
         delay_ms (500);
         
         output_HIGH (ledV);
         delay_ms (500);
      }

     
      FOR (i = 0; i < 2; i++)
      {
         delay_ms (50);
         output_HIGH (buzzer);
         output_LOW (ledV);
         delay_ms (100);
         output_LOW (buzzer);
         output_HIGH (ledV);
      }

      output_LOW (ledV);
   }

   //Fonction lecture de clavier.
   VOID kp_read ()
   {
      IF (inputRead == kp_0) temp = 0;
      IF (inputRead == kp_1) temp = 1;
      IF (inputRead == kp_2) temp = 2;
      IF (inputRead == kp_3) temp = 3;
      IF (inputRead == kp_4) temp = 4;
      IF (inputRead == kp_5) temp = 5;
      IF (inputRead == kp_6) temp = 6;
      IF (inputRead == kp_7) temp = 7;
      IF (inputRead == kp_8) temp = 8;
      IF (inputRead == kp_9) temp = 9;

      //Remettre la variable lecture à 0.
      inputRead = 0;

      //Décalage de la variable temporaire 4 fois.
      val4 = val3;
      val3 = val2;
      val2 = val1;
      val1 = temp;
     
      //Mettre en variable le code entré.
      code_validation = code_entre;
     
   }

   VOID alarm  ()
   {
     
      output_HIGH (buzzer);
      FOR (i = 0; i < 10; i++)
      {
         
         output_LOW (ledR);
         delay_ms (500);
         output_HIGH (ledR);
         delay_ms (500);
      }

      output_LOW (buzzer);
   }


problem now is when i want to start my password with a zero, it won't get reconized as an input .... let say i want 0312 , the code will reconize 321 instead of 0321, i've made a modification so the verification will only take the last four inputs on the keypad but still it's not ok .. obviously i still have some things to work out here
danyboy666



Joined: 01 Mar 2011
Posts: 30

View user's profile Send private message

PostPosted: Tue Mar 29, 2011 3:14 pm     Reply with quote

Another thing, when I include a "0" in the password it will be recognized, as well as if my password is all zeros, "0000", some help would be greatly appreciated. Thanks
pebbert9



Joined: 31 Dec 2010
Posts: 39

View user's profile Send private message

PostPosted: Tue Mar 29, 2011 9:28 pm     Reply with quote

Instead of storing your key presses as numbers 0-9, why don't you store them as characters '0' to '9'. Your password would then be a string, "0123" and the zeroes would still be recognized.
danyboy666



Joined: 01 Mar 2011
Posts: 30

View user's profile Send private message

PostPosted: Sat Apr 30, 2011 2:04 pm     Reply with quote

Ok now I got this pretty much done, I've still got a problem though.
If anyone can help me here it would be appreciated...

I have a problem with my function Gestion_Code();....
Code:

//Incure la bibliothèque du microcontrolleur
#include <18F4550.h>

//Fuses
#fuses HSPLL,NOWDT,NOPROTECT,NODEBUG,NOBROWNOUT,USBDIV,PLL5,CPUDIV1,VREGEN,PUT,NOMCLR,NOLVP
#use delay(clock=48000000)

//Réserver les 800 premières CASE mémoire pour le bootloader.
#build(reset=0x800, interrupt=0x808)
#org 0, 0x7ff { }
 

//Broches utilisés pour l'écran LCD
#define LCD_TYPE 2 
#define LCD_ENABLE_PIN  PIN_E2
#define LCD_RW_PIN      PIN_E1
#define LCD_RS_PIN      PIN_E0
#define LCD_DATA4       PIN_D4
#define LCD_DATA5       PIN_D5
#define LCD_DATA6       PIN_D6
#define LCD_DATA7       PIN_D7

//Bibliothèques utilisés
#include <lcd.c>
#include <string.h>
#include <lcd_db666.h>
#define inputAB (((input_A()&0b00001111)*0x100)+input_B())
#define kp_0 0b0000111111111110
#define kp_1 0b0000111111111101
#define kp_2 0b0000111111111011
#define kp_3 0b0000111111110111
#define kp_4 0b0000111111101111
#define kp_5 0b0000111111011111
#define kp_6 0b0000111110111111
#define kp_7 0b0000111101111111
#define kp_8 0b0000111011111111
#define kp_9 0b0000110111111111
#define kp_S 0b0000101111111111
#define kp_P 0b0000011111111111
#define buzzer PIN_D0
#define ledR PIN_D1
#define ledV PIN_D2
#define ledY PIN_D3
#define star_key 10
#define pound_key 11

//Mots de passe.
LONG code_admin=9876;
LONG user_password=4321;

//Variables de lectures.
LONG code_validation=0,inputRead=0,temp_pass;
CHAR msg_essai=5;

//Addition des valeurs décalés et mettre en constante le code d'entré.
#define code_entre ((val4*1000)+(val3*100)+(val2*10)+(val1*1))

//Déclarations des sous-fonctions.
VOID unlock();
LONG kp_read();
VOID alarm();
VOID Wrong_Code();
LONG LireCombine();
VOID Gestion_Code();

//Boucle principale.
VOID main()
{
   lcd_init();
   
   DO
   {
      clr_display();
     
      code_validation=LireCombine();//Mettre la valeur retourné dans une variable
      //Si code validation est égale au mot de passe utilisateur ou administrateur
      IF(code_validation==code_admin)
      {
         Gestion_Code();
      }

     
      ELSE
      {
         
         
         
         IF((code_validation==user_password)/*||(code_validation==code_admin)*/)
         {
            unlock();//Appel de la fonction débarrer.
            msg_essai=5;
         }

         ELSE
         {
           
            Wrong_Code();
           
            IF(msg_essai==0)
            {
               alarm();//Appel de la fonction alarme.
               //Le code utilisateur est maintenant égale au code administrateur.
               temp_pass=user_password;
               user_password=code_admin;
               msg_essai=255;
            }
         }
      }
   }

   
   WHILE(true);//Fin de la boucle infinie.
}

   //Fonction débarre.
   VOID unlock()
   {
      CHAR buzz_led_loop,door_opening,door_closing,Door_MSG[21];//Variables locales.
     
     
      clr_display();
      Cursor_OFF();
      lcd_gotoxy(5,1);
      strcpy(Door_MSG,"Door Unlocked");
      printf(lcd_putc,"%s",Door_MSG);
      user_password=temp_pass;
      code_validation=user_password;//Code validation est égale au code utilisateur.
      //Boucle indication sonore pour indiquer que la porte est ouverte.
      FOR(buzz_led_loop=0;buzz_led_loop<4;buzz_led_loop++)
      {
         delay_ms(50);
         output_HIGH(buzzer);
         output_LOW(ledR);
         delay_ms(100);
         output_LOW(buzzer);
         output_HIGH(ledR);
      }

     
      output_LOW(ledR);

      //Boucle indicateur lumineux pour indiquer que la porte est ouverte.
      FOR(door_opening=0;door_opening<10;door_opening++)
      {
         output_LOW(ledV);
         delay_ms(500);
         output_HIGH(ledV);
         delay_ms(500);
      }

      //Indicateur sonore pour indiquer que la porte est barrée.
      FOR(door_closing=0;door_closing<2;door_closing++)
      {
         delay_ms(50);
         output_HIGH(buzzer);
         output_LOW(ledV);
         delay_ms(100);
         output_LOW(buzzer);
         output_HIGH(ledV);
      }

     
      output_LOW(ledV);//Remettre l'indicateur lumineux à off.
   }

   
   //Fonction lecture de clavier.
   LONG kp_read()
   {
     
      LONG temp=15;//Variable de lecture de touche temporaire.
      //Si une des touches est appuyée on la met en banque.
      IF(inputRead==kp_0)temp=0;
      IF(inputRead==kp_1)temp=1;
      IF(inputRead==kp_2)temp=2;
      IF(inputRead==kp_3)temp=3;
      IF(inputRead==kp_4)temp=4;
      IF(inputRead==kp_5)temp=5;
      IF(inputRead==kp_6)temp=6;
      IF(inputRead==kp_7)temp=7;
      IF(inputRead==kp_8)temp=8;
      IF(inputRead==kp_9)temp=9;
     
      IF(inputRead==kp_S)temp=star_key;
      IF(inputRead==kp_P)temp=pound_key;
     
      inputRead=0;//Remettre la variable lecture à 0
     
      RETURN temp;//Retour de la touche vers le programme appelant.
   }

   //Fonction de l'alarme.
   VOID alarm()
   {
      CHAR Pulsar1,Alrm_MSG[21],Admn_MSG[21];//Variable pour le compteur.
     
     
      clr_display();
      lcd_gotoxy(8,1);
      strcpy(Alrm_MSG,"Alarm!");
      printf(lcd_putc,"%s",Alrm_MSG);
      delay_ms(500);
      lcd_gotoxy(4,2);
      strcpy(Admn_MSG,"Admin required");
      printf(lcd_putc,"%s",Admn_MSG);
     
      output_HIGH(buzzer);//Niveau haut au buzzeur
      FOR(Pulsar1=1;Pulsar1<=10;Pulsar1++)//Compteur de zéro à dix.
      {
         output_HIGH(ledR);//LED Rouge allumée.
         delay_ms(500);//Pause de une demi seconde.
         output_LOW(ledR);//LED Rouge éteinte.
         delay_ms(500);//Pause de une demi seconde.
      }

     
     
      output_LOW(buzzer);//Niveau bas au buzzeur
   }

   //Fonction lire combine qui retourne une variable.
   LONG LireCombine()
   {
     
      CHAR Code_enter_MSG[21],Count_input;
     
      LONG val1=15,val2=15,val3=15,val4=15,touche;//Variables de lecture.
      //Boucle à compteur permettant d'accumuler 4 touches entrées.
      lcd_gotoxy(2,1);
      strcpy(Code_enter_MSG,"Please Enter Code");
      printf(lcd_putc,"%s",Code_enter_MSG);
      lcd_gotoxy(9,2);
      Cursor_Blink_ON();
      FOR(Count_input=1;Count_input<=4;Count_input++)
      {
         DO
         {
           
            inputRead=inputAB;//Mettre en variable la touche entrée.
         }

         WHILE(inputRead>=0xFFF);//Tant que que lecture clavier est plus grand que 4095.
         
         
         touche=kp_read();//Appel de la fonction lecture de clavier.
         
         
         //Décalage de la variable temporaire 4 fois.
         val4=val3;
         val3=val2;
         val2=val1;
         val1=touche;
         
         lcd_putc("*");
         output_HIGH(buzzer);//Activer le buzzeur.
         delay_ms(250);//Pause de 250 ms.
         output_LOW(buzzer);//Sinon buzzeur est éteint.
      }

     
      RETURN code_entre;//Retour du code entre vers le programme appelant.
   }

   VOID Wrong_Code()
   {
      CHAR Pulsar2,Code_wMSG[21];
     
      FOR(Pulsar2=0;Pulsar2<3;Pulsar2++)
      {
         delay_ms(50);
         output_HIGH(buzzer);
         output_LOW(ledR);
         delay_ms(100);
         output_LOW(buzzer);
         output_HIGH(ledR);
      }

     
     
      msg_essai--;
      clr_display();
      Cursor_OFF();
      lcd_gotoxy(6,1);
      strcpy(Code_wMSG,"Wrong Code");
      printf(lcd_putc,"%s",Code_wMSG);
      delay_ms(1000);
     
      clr_display();
     
      lcd_gotoxy(6,2);
     
     
      printf(lcd_putc,"Try Left%u",msg_essai);
     
      delay_ms(5000);
      output_LOW(ledR);
   }

   VOID Gestion_Code()
   {
      CHAR Gestion_Code_MSG[21],Pass_Mod_Choice[21],Respond_MSG[21],Response,i;
      LONG temp_code;
     
      clr_display();
      Cursor_OFF();
      lcd_gotoxy(5,1);
      strcpy(Gestion_Code_MSG,"Code Gestion");
      printf(lcd_putc,"%s",Gestion_Code_MSG);
     
     
      lcd_gotoxy(2,2);
      strcpy(Pass_Mod_Choice,"*=Admin #=Public");
      printf(lcd_putc,"%s",Pass_Mod_Choice);
     
     
     
     
 
     
      delay_ms(3000);
     
          clr_display();
     
      Response=kp_read();
     
     
     
     
     
      IF(Response==star_key)
      {
         FOR(i=0;i<=Response;i++)
         {
            output_HIGH(ledR);
            delay_ms(100);
            output_LOW(ledR);
            delay_ms(100);
         }

         lcd_gotoxy(2,1);
         strcpy(Respond_MSG,"Code Superviseur");
         printf(lcd_putc,"%s",Respond_MSG);
         delay_ms(3000);
         
         temp_code=LireCombine();
         
     
      }
     
     
     
     
   
     
      IF(Response==pound_key)
      {
         
         lcd_gotoxy(5,1);
         strcpy(Respond_MSG,"Code Public");
         printf(lcd_putc,"%s",Respond_MSG);
         delay_ms(3000);
         
         temp_code=LireCombine();
      }
     
}


It won't do the IF verification. I've put a For loop for debugging. If I write the For loop right after Response=kp_read(); it will read the keypad ok but in the If it won't read it at all...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Apr 30, 2011 4:29 pm     Reply with quote

Add an rs232 interface to your board. (Max232 chip and a DB-9 connector).

Then just before you are going to test a value with an if() statement,
display the variables (and constants) in Hex format, with a printf statement.

Then, at least you will know what values are being tested. If they are
incorrect, then you can look the code before that line, and find out why
they are incorrect. Put in another printf statement at an earlier point.

Some people would use a debugger with breakpoints and watch window
for this. I prefer printfs. It's just quicker for me. Use whatever method
you prefer.

----------

One of the reasons you don't get very many replies is because you are
posting too much code. Cut it down to only 1/4 the size, or less. Cut out
all sections of code that are not needed to show the problem.

Another reason is because you are not using C style conventions.
For example, these are all constants, and you have them all in lower case.
But the convention is to put constants in All Caps:
Code:

#define kp_0 0b0000111111111110
#define kp_1 0b0000111111111101
#define kp_2 0b0000111111111011
#define kp_3 0b0000111111110111
#define kp_4 0b0000111111101111
#define kp_5 0b0000111111011111
#define kp_6 0b0000111110111111
#define kp_7 0b0000111101111111
#define kp_8 0b0000111011111111
#define kp_9 0b0000110111111111
#define kp_S 0b0000101111111111
#define kp_P 0b0000011111111111
#define buzzer PIN_D0
#define ledR PIN_D1
#define ledV PIN_D2
#define ledY PIN_D3
#define star_key 10
#define pound_key 11


Example:
Code:

#define KP_0 0b0000111111111110
#define KP_1 0b0000111111111101
#define KP_2 0b0000111111111011
.
.
.
#define BUZZER PIN_D0
#define LEDR PIN_D1
#define LEDV PIN_D2
.
.
.

Also change every place in your code where they are used, so they
are in All Caps there.

3rd, you are putting C keywords in All Caps, such as 'IF', and the
convention is all lower case.

When you change the well-known C style conventions, you make it
much harder to read for a normal programmer. We like to skim the
code with our eyes very quickly. We can do this most easily if the
style conventions are followed.

There's more. In some places in your program, you are making things
more complicated than is necessary. For example, here you copy text
to a ram buffer before printing it. You do this in several places:
Code:

strcpy(Respond_MSG,"Code Superviseur");
printf(lcd_putc,"%s",Respond_MSG);


But those two lines can be replaced by this:
Code:
lcd_putc("Code Superviseur");

When we scan the program with our eyes, we see this stuff and we
wonder "Why is he doing this ?". It slows down the detection of problems.

I'm not trying to beat up on you. But I'm trying to explain how to post
code so you will get more replies. Probably the most important thing
is to keep it short.
danyboy666



Joined: 01 Mar 2011
Posts: 30

View user's profile Send private message

PostPosted: Sat Apr 30, 2011 9:24 pm     Reply with quote

ok i'll take note of your recommendations and make the appropriate changes , thank you for the help.

db666


ok i've made the changes so the code will be easier to read from now on...

here's the part i still have trouble with ...

Code:
void code_gestion()
   {
      char Response,i;
      long Temp_Code;
     
     
      clr_display();
      Cursor_OFF();
      lcd_gotoxy(5,1);
      lcd_putc("Code Gestion");
      lcd_gotoxy(2,2);
      lcd_putc("*=Admin #=Public");
      delay_ms(3000);
      clr_display();
      Response=kp_read();
     
      if(Response==STAR_KEY)
      {
         
      for(i=0;i<=Response;i++)
         {
            output_high(LEDR);
            delay_ms(100);
            output_low(LEDR);
            delay_ms(100);
         }
      lcd_gotoxy(2,1);
         
         lcd_putc("Code Superviseur");
         delay_ms(3000);
         
         Temp_Code=read_combination();   
      }

       
      if(Response==POUND_KEY)
      {
         
         lcd_gotoxy(5,1);
         
         lcd_putc("Code Public");
         delay_ms(3000);
         
         Temp_Code=read_combination();
      }
   }


i managed to found out the trouble


Last edited by danyboy666 on Fri May 06, 2011 10:44 am; edited 2 times in total
danyboy666



Joined: 01 Mar 2011
Posts: 30

View user's profile Send private message

PostPosted: Fri May 06, 2011 10:43 am     Reply with quote

Again I need your help...

I'm trying to store/read my passwords and be able to change them in my function gestion_code();

I initialise the default passwords as so...
Code:
#rom 0x2100={4,3,2,1}
#rom 0x2120={9,8,7,6}


I have 2 function here ...
Code:
long read_from_EEPROM(int adr)
{
   int8 i;
   int8 val1,val2,val3,val4,val_temp,val_code;
   for(i=0;i<=3;i++)
   {
     
      val_temp=read_EEPROM(adr+i);
      val4=val3;
      val3=val2;
      val2=val1;
      val1=val_temp;
   }
   
   return CALCUL_VAL;
}

void write_to_EEPROM(int8 adr,int16 code)
{
   int8 j,divider;
   divider=1000;
   int16 tableau[4]={0,0,0,0};
   for(j=0;j<=3;j++)
   {
      tableau[j]=code/divider;
      code=code%10;
      divider=divider/10;
   }
   for(j=0;j<=3;j++)
   {
      write_EEPROM((adr+j),(tableau[j]));
   }
}


and I call the read_to_EEPROM(); functions in the beginning of the main(),
for some reasons now the program is not recognizing the passwords...

oh and here is the macro i use ...
Code:
#define CALCUL_VAL ((val4*1000)+(val3*100)+(val2*10)+(val1*1))
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri May 06, 2011 11:17 am     Reply with quote

Quote:
#include <18F4550.h>

#rom 0x2100={4,3,2,1}
#rom 0x2120={9,8,7,6}

0x2100 is not the eeprom address for this PIC.

See this thread. It shows how to use the getenv() method to automatically
get the correct eeprom address:
http://www.ccsinfo.com/forum/viewtopic.php?t=43607

Also, you should have debugged this problem by deciding to verify if
the eeprom was really set to your desired values. You could have put
some read_eeprom() statements at the start of your program, and used
printf to see the values in the eeprom. This would have shown you
where the problem was.
danyboy666



Joined: 01 Mar 2011
Posts: 30

View user's profile Send private message

PostPosted: Fri May 06, 2011 11:38 am     Reply with quote

ohh ok, it's my mistake here, at school I'm working on a 16F877 pic but at home i have my breadboard interfaced with a 18F4550 ... I'll correct the mistake and see if it's working, thanks.
danyboy666



Joined: 01 Mar 2011
Posts: 30

View user's profile Send private message

PostPosted: Sat May 07, 2011 7:44 pm     Reply with quote

so i fix the EEPROM address issue, now i still have a problem ...

Code:
float read_from_EEPROM(long int adr)
{
   int i;
   float val1=0,val2=0,val3=0,val4=0,val_code=0;
   
   for(i=0;i<4;i++)
   {
   
   
    *((int8*)&val_temp + i) = read_eeprom(i + adr);

      *((int8*)&val4 + i)=*((int8*)&val3 + i);
      *((int8*)&val3 + i)=*((int8*)&val2 + i);
      *((int8*)&val2 + i)=*((int8*)&val1 + i);
      *((int8*)&val1 + i)=*((int8*)&val_temp + i);
 
   }
     
   return CALCUL_VAL;
}


this is the only way i found to be able so get the function to read my password but i can't store the value of read_eeprom(i + adr) into a variable...

it's supposed to look like this
Code:
for(i=0;i<4;i++)
   {
     
      val_temp=read_EEPROM(adr+i);
      val4=val3;
      val3=val2;
      val2=val1;
      val1=val_temp;
   }
   
   return CALCUL_VAL;


edit: i managed to find the trouble it's ok now.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group