• 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.

MikroC help

Status
Not open for further replies.

etekamba

New Member
Hi guys,
I'm kinda stuck with this code, it compiles without errors but I can't debugg it to know why it is not executing the part where it accepts passwords during simulation on proteus.
What's it supposed to do? Turn off LEDs on port A when passcode is inputed. I've attached the proteus simulation project for your info. Thanks in advance.

Code:
unsigned short kp, pwd[4], l01, l02, l03, l04, l05, l06, pchk;
int i;

char keypadPort at PORTD;

sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;

void enter(){
do {
  i = 0;
  kp = 0;
  do
   kp = Keypad_Key_Click();
  while (!kp);
  switch (kp) {
   case 1: kp = 49; break;
   case 1: kp = 49; break;
   case 1: kp = 49; break;
   case 1: kp = 49; break;
   case 1: kp = 49; break;
   case 1: kp = 49; break;
   case 1: kp = 49; break;
   case 1: kp = 49; break;
   case 1: kp = 49; break;
   case 1: kp = 49; break;
   case 1: kp = 49; break;
   case 1: kp = 49; break;
   }
  pwd[i] = kp;
  i++;
} while(1);
}

void main(){
Keypad_Init();
ADCON1  = 7;
CMCON = 7;

TRISA = 0x00;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,"LINEMAN");
Lcd_Out(2,1,"PROTECTION");
Delay_ms(100);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Initializing...");
Delay_ms(500);

pchk = EEPROM_Read(0x00);
Delay_ms(100);
 
if(pchk == 0x00){
  EEPROM_Write(0x01,0x01);
  EEPROM_Write(0x02,0x01);
  EEPROM_Write(0x03,0x01);
  EEPROM_Write(0x04,0x02);
  EEPROM_Write(0x05,0x02);
  EEPROM_Write(0x06,0x02);
  Delay_ms(500);
  }
  
l01 = EEPROM_Read(0x01);
l02 = EEPROM_Read(0x02);
l03 = EEPROM_Read(0x03);
l04 = EEPROM_Read(0x04);
l05 = EEPROM_Read(0x05);
l06 = EEPROM_Read(0x06);
Delay_ms(500);

Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"LINE 1: ON");
PORTA.B0 = 1;
PORTA.B2 = 1;
PORTA.B3 = 0;
Lcd_Out(2,1,"LINE 2: ON");
PORTA.B1 = 1;
PORTA.B4 = 1;
PORTA.B5 = 0;

enter();
enter();
enter();
enter();
if(pwd[0]==42 && pwd[1]==42 && pwd[2]==49 && pwd[3]==35){
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Out(1,1,"Enter Old L1 Pwd");
  enter();
  enter();
  enter();
  enter();
  if(pwd[0]==l01 && pwd[1]==l02 && pwd[2]==l03 && pwd[3]==35){
   Lcd_Cmd(_LCD_CLEAR);
   Lcd_Out(1,1,"Enter New L1 Pwd");
   enter();
   enter();
   enter();
   enter();
   Delay_ms(500);
   EEPROM_Write(0x01,pwd[0]);
   EEPROM_Write(0x02,pwd[1]);
   EEPROM_Write(0x03,pwd[2]);
   EEPROM_Write(0x00,0x01);
   Delay_ms(500);
   }
  }

if(pwd[0]==42 && pwd[1]==42 && pwd[2]==50 && pwd[3]==35){
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Out(1,1,"Enter Old L2 Pwd");
  enter();
  enter();
  enter();
  enter();
  if(pwd[0]==l04 && pwd[1]==l05 && pwd[2]==l06 && pwd[3]==35){
   Lcd_Cmd(_LCD_CLEAR);
   Lcd_Out(1,1,"Enter New L2 Pwd");
   enter();
   enter();
   enter();
   enter();
   Delay_ms(500);
   EEPROM_Write(0x04,pwd[0]);
   EEPROM_Write(0x05,pwd[1]);
   EEPROM_Write(0x06,pwd[2]);
   EEPROM_Write(0x00,0x01);
   Delay_ms(500);
   }
  }

if(pwd[0]==l01 && pwd[1]==l02 && pwd[2]==l03 && pwd[3]==35){
  Lcd_Out(1,1,"LINE 1: OFF");
  PORTA.B0 = 0;
  PORTA.B2 = 0;
  PORTA.B3 = 1;
  }

if(pwd[0]==l04 && pwd[1]==l05 && pwd[2]==l06 && pwd[3]==35){
  Lcd_Out(1,1,"LINE 2: OFF");
  PORTA.B1 = 0;
  PORTA.B4 = 0;
  PORTA.B5 = 1;
  }

while(1);
}
 

Attachments

etekamba

New Member
OMG! I was so stupid. I copied the first switch statement and pasted it so i can edit it later to other cases instead of typing them one-by-one. Alas, i forgot to edit them. But i've done that now and its still the same. Still doesn't process past the "LINE 1: ON" and "LINE 2: ON" screen.
This is the new code:
Code:
unsigned short kp, pwd[4], l01, l02, l03, l04, l05, l06, pchk;
int i;

char keypadPort at PORTD;

sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;

void enter(){
do {
  i = 0;
  kp = 0;
  do
   kp = Keypad_Key_Click();
  while (!kp);
  switch (kp) {
   case 1: kp = 49; break;
   case 2: kp = 50; break;
   case 3: kp = 51; break;
   case 4: kp = 52; break;
   case 5: kp = 53; break;
   case 6: kp = 54; break;
   case 7: kp = 55; break;
   case 8: kp = 56; break;
   case 9: kp = 57; break;
   case 10: kp = 42; break;
   case 11: kp = 48; break;
   case 12: kp = 35; break;
   }
  pwd[i] = kp;
  i++;
} while(1);
}

void main(){
Keypad_Init();
ADCON1  = 7;
CMCON = 7;

TRISA = 0x00;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,"LINEMAN");
Lcd_Out(2,1,"PROTECTION");
Delay_ms(100);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Initializing...");
Delay_ms(500);

pchk = EEPROM_Read(0x00);
Delay_ms(100);
 
if(pchk == 0x00){
  EEPROM_Write(0x01,0x01);
  EEPROM_Write(0x02,0x01);
  EEPROM_Write(0x03,0x01);
  EEPROM_Write(0x04,0x02);
  EEPROM_Write(0x05,0x02);
  EEPROM_Write(0x06,0x02);
  Delay_ms(500);
  }
  
l01 = EEPROM_Read(0x01);
l02 = EEPROM_Read(0x02);
l03 = EEPROM_Read(0x03);
l04 = EEPROM_Read(0x04);
l05 = EEPROM_Read(0x05);
l06 = EEPROM_Read(0x06);
Delay_ms(500);

Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"LINE 1: ON");
PORTA.B0 = 1;
PORTA.B2 = 1;
PORTA.B3 = 0;
Lcd_Out(2,1,"LINE 2: ON");
PORTA.B1 = 1;
PORTA.B4 = 1;
PORTA.B5 = 0;

enter();
enter();
enter();
enter();
if(pwd[0]==42 && pwd[1]==42 && pwd[2]==49 && pwd[3]==35){
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Out(1,1,"Enter Old L1 Pwd");
  enter();
  enter();
  enter();
  enter();
  if(pwd[0]==l01 && pwd[1]==l02 && pwd[2]==l03 && pwd[3]==35){
   Lcd_Cmd(_LCD_CLEAR);
   Lcd_Out(1,1,"Enter New L1 Pwd");
   enter();
   enter();
   enter();
   enter();
   Delay_ms(500);
   EEPROM_Write(0x01,pwd[0]);
   EEPROM_Write(0x02,pwd[1]);
   EEPROM_Write(0x03,pwd[2]);
   EEPROM_Write(0x00,0x01);
   Delay_ms(500);
   }
  }

if(pwd[0]==42 && pwd[1]==42 && pwd[2]==50 && pwd[3]==35){
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Out(1,1,"Enter Old L2 Pwd");
  enter();
  enter();
  enter();
  enter();
  if(pwd[0]==l04 && pwd[1]==l05 && pwd[2]==l06 && pwd[3]==35){
   Lcd_Cmd(_LCD_CLEAR);
   Lcd_Out(1,1,"Enter New L2 Pwd");
   enter();
   enter();
   enter();
   enter();
   Delay_ms(500);
   EEPROM_Write(0x04,pwd[0]);
   EEPROM_Write(0x05,pwd[1]);
   EEPROM_Write(0x06,pwd[2]);
   EEPROM_Write(0x00,0x01);
   Delay_ms(500);
   }
  }

if(pwd[0]==l01 && pwd[1]==l02 && pwd[2]==l03 && pwd[3]==35){
  Lcd_Out(1,1,"LINE 1: OFF");
  PORTA.B0 = 0;
  PORTA.B2 = 0;
  PORTA.B3 = 1;
  }

if(pwd[0]==l04 && pwd[1]==l05 && pwd[2]==l06 && pwd[3]==35){
  Lcd_Out(1,1,"LINE 2: OFF");
  PORTA.B1 = 0;
  PORTA.B4 = 0;
  PORTA.B5 = 1;
  }

while(1);
}
 

etekamba

New Member
Removing it from the routine and setting it just before enter() is called should fix that, or no?
New code:
Code:
unsigned short kp, pwd[4], l01, l02, l03, l04, l05, l06, pchk;
int i;

char keypadPort at PORTD;

sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;

void enter(){
do {

  kp = 0;
  do
   kp = Keypad_Key_Click();
  while (!kp);
  switch (kp) {
   case 1: kp = 49; break;
   case 2: kp = 50; break;
   case 3: kp = 51; break;
   case 4: kp = 52; break;
   case 5: kp = 53; break;
   case 6: kp = 54; break;
   case 7: kp = 55; break;
   case 8: kp = 56; break;
   case 9: kp = 57; break;
   case 10: kp = 42; break;
   case 11: kp = 48; break;
   case 12: kp = 35; break;
   }
  pwd[i] = kp;
  i++;
} while(1);
}

void main(){
Keypad_Init();
ADCON1  = 7;
CMCON = 7;

TRISA = 0x00;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,"LINEMAN");
Lcd_Out(2,1,"PROTECTION");
Delay_ms(100);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Initializing...");
Delay_ms(500);

pchk = EEPROM_Read(0x00);
Delay_ms(100);
 
if(pchk == 0x00){
  EEPROM_Write(0x01,0x01);
  EEPROM_Write(0x02,0x01);
  EEPROM_Write(0x03,0x01);
  EEPROM_Write(0x04,0x02);
  EEPROM_Write(0x05,0x02);
  EEPROM_Write(0x06,0x02);
  Delay_ms(500);
  }
  
l01 = EEPROM_Read(0x01);
l02 = EEPROM_Read(0x02);
l03 = EEPROM_Read(0x03);
l04 = EEPROM_Read(0x04);
l05 = EEPROM_Read(0x05);
l06 = EEPROM_Read(0x06);
Delay_ms(500);

Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"LINE 1: ON");
PORTA.B0 = 1;
PORTA.B2 = 1;
PORTA.B3 = 0;
Lcd_Out(2,1,"LINE 2: ON");
PORTA.B1 = 1;
PORTA.B4 = 1;
PORTA.B5 = 0;

i=0;
enter();
enter();
enter();
enter();
if(pwd[0]==42 && pwd[1]==42 && pwd[2]==49 && pwd[3]==35){
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Out(1,1,"Enter Old L1 Pwd");
  i=0;
  enter();
  enter();
  enter();
  enter();
  if(pwd[0]==l01 && pwd[1]==l02 && pwd[2]==l03 && pwd[3]==35){
   Lcd_Cmd(_LCD_CLEAR);
   Lcd_Out(1,1,"Enter New L1 Pwd");
   i=0;
   enter();
   enter();
   enter();
   enter();
   Delay_ms(500);
   EEPROM_Write(0x01,pwd[0]);
   EEPROM_Write(0x02,pwd[1]);
   EEPROM_Write(0x03,pwd[2]);
   EEPROM_Write(0x00,0x01);
   Delay_ms(500);
   }
  }

if(pwd[0]==42 && pwd[1]==42 && pwd[2]==50 && pwd[3]==35){
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Out(1,1,"Enter Old L2 Pwd");
  i=0;
  enter();
  enter();
  enter();
  enter();
  if(pwd[0]==l04 && pwd[1]==l05 && pwd[2]==l06 && pwd[3]==35){
   Lcd_Cmd(_LCD_CLEAR);
   Lcd_Out(1,1,"Enter New L2 Pwd");
   i=0;
   enter();
   enter();
   enter();
   enter();
   Delay_ms(500);
   EEPROM_Write(0x04,pwd[0]);
   EEPROM_Write(0x05,pwd[1]);
   EEPROM_Write(0x06,pwd[2]);
   EEPROM_Write(0x00,0x01);
   Delay_ms(500);
   }
  }

if(pwd[0]==l01 && pwd[1]==l02 && pwd[2]==l03 && pwd[3]==35){
  Lcd_Out(1,1,"LINE 1: OFF");
  PORTA.B0 = 0;
  PORTA.B2 = 0;
  PORTA.B3 = 1;
  }

if(pwd[0]==l04 && pwd[1]==l05 && pwd[2]==l06 && pwd[3]==35){
  Lcd_Out(1,1,"LINE 2: OFF");
  PORTA.B1 = 0;
  PORTA.B4 = 0;
  PORTA.B5 = 1;
  }

while(1);
}
 

jayanthd

New Member
A similar project which I had done a few years ago. Here is the code.

Code:
unsigned short kp;
char uart_rd;

char code1[15] ,user1[4];
int i = 0, j, cnt = 0;

int w1;

//keypad module connections
char keypadPort at PORTD;
//end keypad module connections

//lcd module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
char m;
char result=0;
char tens=0,ones=0;

void code_enter(){
    kp = 0;       //Reset key code variable
    //wait for key to be pressed and released
    do
     //kp = Keypad_Key_Press();     // store key code in kp variable
    kp = Keypad_Key_Click();       // store key code in kp variable
    while (!kp);
    //prepare value for output, transform key to it's ASCII value
    switch (kp){
            case  1: kp = '1'; break; // 1
                        case  2: kp = '2'; break; // 2
                        case  3: kp = '3'; break; // 3
                        case  5: kp = '4'; break; // 4
                        case  6: kp = '5'; break; // 5
                        case  7: kp = '6'; break; // 6
                        case  9: kp = '7'; break; // 7
                        case 10: kp = '8'; break; // 8
                        case 11: kp = '9'; break; // 9
                        case 13: kp = 42; break; // *
                        case 14: kp = 48; break; // 0
                        case 15: kp = '#'; break; // #
    }

    code1[i] = kp;
    i++;
    Lcd_Chr(2, i, code1[i-1]);// Replace key ASCII value by "*" on Lcd


}

//20ms delay function no need of it

void code_read(){      //read data from eeprom
     Delay_ms(20);
     user1[0] = EEPROM_Read(0x00);            // Read data from address 0
     Delay_ms(20);
     user1[1] = EEPROM_Read(0x01);            // Read data from address 1
     Delay_ms(20);
     user1[2] = EEPROM_Read(0x02);            // Read data from address 2
     Delay_ms(20);
     user1[3] = EEPROM_Read(0x03);            // Read data from address 3
     Delay_ms(20);
}

void code_write(){       //write data to eeprom
     Delay_ms(20);
     EEPROM_Write(0x00,code1[0]);           // Write data to address 0
     Delay_ms(20);
     EEPROM_Write(0x01,code1[1]);           // Write data to address 1
     Delay_ms(20);
     EEPROM_Write(0x02,code1[2]);           // Write data to address 2
     Delay_ms(20);
     EEPROM_Write(0x03,code1[3]);           // Write data to address 3
}

void change_code(){
     Lcd_Cmd(_LCD_CLEAR);           // clear display
     Lcd_Out(1,1, "Enter New Code:");

     i = 0;

        while(i != 3) {
                code_enter();
        }

    code_write();
    delay_ms(20);
    code_read();
    delay_ms(20);

    Lcd_Cmd(_LCD_CLEAR);           // clear display
    Lcd_Out(1,1, "New Code Set");
    Delay_ms(5000);
}


void main() {

        ADCON1 != 0x07;
    ANSEL  = 0;                              // Configure AN pins as digital I/O
    ANSELH = 0;
    c1on_bit=0;                           // disable comparator
    c2on_bit=0;
    TRISC = 0x00;
    PORTC = 0x00;
    TRISD = 0x00;
    PORTD = 0x00;
    Keypad_Init();             // Initialize Keypad
    Lcd_Init();                              // Initialize LCD
    Lcd_Cmd(_LCD_CLEAR);                     // Clear display
    Lcd_Cmd(_LCD_CURSOR_OFF);                // Cursor off

    UART1_Init(9600);               // Initialize UART module at 9600 bps
    Delay_ms(100);

    code_read();

    Lcd_Cmd(_LCD_CLEAR);        //clear display
    Lcd_Cmd(_LCD_CURSOR_OFF);     // cursor off
    //Lcd_Out(1, 1, msg1);
    //Delay_ms(500);
    Lcd_Cmd(_LCD_CLEAR);
    delay_ms(2000);

    cnt = 0;

        lcd_cmd(_LCD_CURSOR_OFF);

    if(user1[0] == 0xFF && user1[1] == 0xFF && user1[2] == 0xFF && user1[3] == 0xFF )
        {
                EEPROM_Write(0x00,'2');               // Write some data at address 00
                Delay_ms(20);
                EEPROM_Write(0x01,'3');               // Write some data at address 02
                Delay_ms(20);
                EEPROM_Write(0x02,'4');               // Write some data at address 04
                Delay_ms(20);
                EEPROM_Write(0x03,'5');               // Write some data at address 08
        }

        code_read();
        Lcd_Cmd(_LCD_CLEAR);                      // Clear display
        Lcd_Cmd(_LCD_CURSOR_OFF);                 // Cursor off
        Lcd_Out(1, 1,  "WELCOME");
        delay_ms(2000);
        //Lcd_Cmd(_LCD_CLEAR);                      // Clear display


L1:  do{
        Lcd_Cmd(_LCD_CLEAR);
        Lcd_Out(2, 3, "Press *");
        i = 0;
        code_enter();
        if(code1[0] == 42){      // *
                Lcd_Cmd(_LCD_CLEAR);       // clear display
                Lcd_Out(1, 1, "Enter Code");
                //delay_ms(500);
                i = 0;

                code_enter();
                code_enter();
                code_enter();
                code_enter();
                code_enter();

                if(code1[0] == '1' && code1[1] == '2'  && code1[2] == '3' && code1[3] == '4' && code1[4] == '5' ){      // master code
                        code_enter();
                        if(code1[5] == '#'){      // #
                                change_code();
                        }
                }
                //else if((cnt<3) && (code1[0] == '2') && (code1[1] == '3') && (code1[2] == '4') && (code1[3] == '5') && (code1[4] == 35)){     // save password verification
                else if((cnt<3) && (code1[0] == user1[0]) && (code1[1] == user1[1]) && (code1[2] == user1[2]) && (code1[3] == user1[3]) && (code1[4] == '#')){     // save password verification           Lcd_Cmd(_LCD_CLEAR);                      // Clear display
                           Lcd_Cmd(_LCD_CURSOR_OFF);
                           lcd_out(1,1,"Enter the number");
                           lcd_out(2,1," of relay");
                           i = 0;
                           code_enter();
                           if(code1[0]=='1' || code1[0]=='2' || code1[0]=='3' || code1[0]=='4' || code1[0]=='5' || code1[0]=='6' ){
                                      Lcd_Cmd(_Lcd_Clear);
                                      Lcd_Chr(2,1,code1[0]);
                                      UART1_Write(code1[0]);
                                      delay_ms(500);
                                      i = 0;
                           }
                           else{
                                     Lcd_out_cp("error");
                                     delay_ms(1000);
                           }
                }
                else{
                    Lcd_Cmd(_LCD_CLEAR);     //clear display
                    Lcd_Out(1, 1, "Wrong Password");
                    Delay_ms(2000);
                }
                }
        } while(1);
 

jayanthd

New Member
Thanks jayanthd. Why did you have to use the uart module? Were you using an external eeprom?
I don't remember why I used UART but you can discard UART related code. No, I used internal eeprom. The code is mikroC PRO PIC code. I have used its libraries.
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top