stip
Newbie level 5

Hi everybody!
I am using PIC18F2550 Mikroc as compiler. My project has a matricial keypad (https://www.arduiner.com/en/tastier...switch-keypad-keyboard-for-arduinoavrpic.html) to get a five digits number that will be a reference to a motor's speed in RPM. At the same time every digit is displayed on an LCD. I want to be able to use one of the key to return( when key '*' is pressed) or advance(when key '#' is pressed) for any digit and replace it in case the user want it. I am trying to use the code below. I have made a function called "print" to print each character. My idea to replace any character was to decrement or increment an integer called "k" so I could know the position of the cursor on the LCD. Then I just printed the new character in the "k" position, but it is not working properly. I can get any digit and print it, but when I press the key "#" or '*' it is not working. Could someone help me with any tips to do it?
Thanks in advance!
I am using PIC18F2550 Mikroc as compiler. My project has a matricial keypad (https://www.arduiner.com/en/tastier...switch-keypad-keyboard-for-arduinoavrpic.html) to get a five digits number that will be a reference to a motor's speed in RPM. At the same time every digit is displayed on an LCD. I want to be able to use one of the key to return( when key '*' is pressed) or advance(when key '#' is pressed) for any digit and replace it in case the user want it. I am trying to use the code below. I have made a function called "print" to print each character. My idea to replace any character was to decrement or increment an integer called "k" so I could know the position of the cursor on the LCD. Then I just printed the new character in the "k" position, but it is not working properly. I can get any digit and print it, but when I press the key "#" or '*' it is not working. Could someone help me with any tips to do it?
Thanks in advance!
Code:
char keypadPort at PORTB;
sbit LCD_RS at RC6_bit;
sbit LCD_EN at RA0_bit;
sbit LCD_D4 at RC0_bit;
sbit LCD_D5 at RC1_bit;
sbit LCD_D6 at RC2_bit;
sbit LCD_D7 at RC7_bit;
sbit LCD_RS_Direction at TRISC6_bit;
sbit LCD_EN_Direction at TRISA0_bit;
sbit LCD_D4_Direction at TRISC0_bit;
sbit LCD_D5_Direction at TRISC1_bit;
sbit LCD_D6_Direction at TRISC2_bit;
sbit LCD_D7_Direction at TRISC7_bit;
int i, k=0;
char string[5];
void print(){
Lcd_Chr(2, k+1 , string[k]);
}
char menu()
{
Lcd_Init(); // Initialize the LCD
Lcd_Cmd(_LCD_CLEAR);// Clear the LCD
Lcd_Out(1, 1, "RPM"); // Write on LCD
Lcd_Cmd(_LCD_SECOND_ROW);//Move the cursor to the second row
Lcd_Cmd(_LCD_BLINK_CURSOR_ON);// Blink the cursor
while(k<5)
{
while (string[k] == 0) // wait for any key to be pressed
{
string[k] = Keypad_Key_Click();
}
switch(string[k]){
case 1:
string[k]= '1';
print();
k++;
break;
case 2:
string[k]='2';
print();
k++;
break;
case 3:
string[k]='3';
print();
k++;
break;
case 5:
string[k]='4';
print();
k++;
break;
case 6:
string[k]= '5';
print();
k++;
break;
case 7:
string[k]= '6';
print();
k++;
break;
case 9:
string[k]= '7';
print();
k++;
break;
case 10:
string[k]='8';
print();
k++;
break;
case 11:
string[k]='9';
print();
k++;
break;
case 13: // key '*' was pressed
for(i=0;i<1;i++){
Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT);// return the cursor
string[k]=0;
}
break;
case 14:
string[k]='0';
print();
k++;
break;
case 15: //key '#' was pressed
for(i=0;i<1;i++){
Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);//Advance the cursor
string[k]=0;
}
break;
}
}
}
void main(){
ADCON0 = 0; // ADCON0 and ADCON1 registers to enable digital I/O in PORTA (where is an LCD pin)
ADCON1 = 0XFF;
CMCON |= 7;// disable comparators
Keypad_Init();//initialize keypad
menu();//calls the menu function
}