alaalwi11
Member level 1
- Joined
- Nov 30, 2010
- Messages
- 32
- Helped
- 1
- Reputation
- 2
- Reaction score
- 1
- Trophy points
- 1,288
- Location
- Al khurtum-Sudan
- Activity points
- 1,626
#define MASK 0xF0
#define Enter 11
#define Plus 12
#define Minus 13
#define Multiply 14
#define Divide 15
//
// This function gets a key from the keypad
//
unsigned char getkeypad()
{
unsigned char i, Key = 0;
PORTB = 0x01; // Start with column 1
while((PORTB & MASK) == 0) // While no key pressed
{
PORTB = (PORTB << 1); // next column
Key++; // column number
if(Key == 4)
{
PORTB = 0x01; // Back to column 1
Key = 0;
}
}
Delay_Ms(20); // Switch debounce
for(i = 0x10; i !=0; i <<=1) // Find the key pressed
{
if((PORTB & i) != 0)break;
Key = Key + 4;
}
PORTB=0x0F;
while((PORTB & MASK) != 0); // Wait until key released
Delay_Ms(20); // Switch debounce
return (Key); // Return key number
}
//
// Start of MAIN program
//
void main()
{
unsigned char MyKey, i,j,lcd[5],op[12];
unsigned long Calc, Op1, Op2;
TRISC = 0; // PORTC are outputs (LCD)
TRISB = 0xF0; // RB4-RB7 are inputs
//
// Configure LCD
//
Lcd_Init(&PORTC); // LCD is connected to PORTC
Lcd_Cmd(LCD_CLEAR);
Lcd_Out(1,1,"CALCULATOR"); // Display CALCULATOR
Delay_ms(2000); // Wait 2 seconds
Lcd_Cmd(LCD_CLEAR); // Clear display
//
// Program loop
//
for(;;) // Endless loop
{
MyKey = 0;
Op1 = 0;
Op2 = 0;
Lcd_Out(1,1,"No1: "); // Display No1:
while(1) // Get first no
{
MyKey = getkeypad();
if(MyKey == Enter)break; // If ENTER pressed
MyKey++;
if(MyKey == 10)MyKey = 0; // If 0 key pressed
Lcd_Chr_Cp(MyKey + '0');
Op1 = 10*Op1 + MyKey; // First number in Op1
}
Lcd_Out(2,1,"No2: "); // Display No2:
while(1) // Get second no
{
MyKey = getkeypad();
if(MyKey == Enter)break; // If ENTER pressed
MyKey++;
if(MyKey == 10)MyKey = 0; // If 0 key pressed
Lcd_Chr_Cp(MyKey + '0');
Op2 = 10*Op2 + MyKey; // Second number in Op2
}
Lcd_Cmd(LCD_CLEAR); // Clear LCD
Lcd_Out(1,1,"Op: "); // Display Op:
MyKey = getkeypad(); // Get operation
Lcd_Cmd(LCD_CLEAR);
Lcd_Out(1,1,"Res="); // Display Res=
switch(MyKey) // Perform the operation
{
case Plus:
Calc = Op1 + Op2; // If ADD
break;
case Minus:
Calc = Op1 - Op2; // If Subtract
break;
case Multiply:
Calc = Op1 * Op2; // If Multiply
break;
case Divide:
Calc = Op1 / Op2; // If Divide
break;
}
LongToStr(Calc, op); // Convert to string in op
//
// Remove leading blanks
//
j=0;
for(i=0;i<=11;i++)
{
if(op[i] != ' ') // If a blank
{
lcd[j]=op[i];
j++;
}
}
Lcd_Out_Cp(lcd); // Display result
Delay_ms(5000); // Wait 5 seconds
Lcd_Cmd(LCD_CLEAR);
}
}
the DPL of this part as following:unsigned char getkeypad()
{
unsigned char i, Key = 0;
PORTB = 0x01; // Start with column 1
while((PORTB & MASK) == 0) // While no key pressed
{
PORTB = (PORTB << 1); // next column
Key++; // column number
if(Key == 4)
{
PORTB = 0x01; // Back to column 1
Key = 0;
}
}
Delay_Ms(20); // Switch debounce
for(i = 0x10; i !=0; i <<=1) // Find the key pressed
{
if((PORTB & i) != 0)break;
Key = Key + 4;
}
Function getkeypad:
START
IF a key is pressed
Get the key code (0 to 15)
Return the key code
ENDIF
END
void main()
{
unsigned char MyKey, i,j,lcd[5],op[12];
unsigned long Calc, Op1, Op2;
TRISC = 0; // PORTC are outputs (LCD)
TRISB = 0xF0; // RB4-RB7 are inputs
//
// Configure LCD
//
Lcd_Init(&PORTC); // LCD is connected to PORTC
Lcd_Cmd(LCD_CLEAR);
Lcd_Out(1,1,"CALCULATOR"); // Display CALCULATOR
Delay_ms(2000); // Wait 2 seconds
Lcd_Cmd(LCD_CLEAR); // Clear display
//
// Program loop
//
for(;// Endless loop
{
MyKey = 0;
Op1 = 0;
Op2 = 0;
Lcd_Out(1,1,"No1: "); // Display No1:
while(1) // Get first no
{
MyKey = getkeypad();
if(MyKey == Enter)break; // If ENTER pressed
MyKey++;
if(MyKey == 10)MyKey = 0; // If 0 key pressed
Lcd_Chr_Cp(MyKey + '0');
Op1 = 10*Op1 + MyKey; // First number in Op1
}
Lcd_Out(2,1,"No2: "); // Display No2:
while(1) // Get second no
{
MyKey = getkeypad();
if(MyKey == Enter)break; // If ENTER pressed
MyKey++;
if(MyKey == 10)MyKey = 0; // If 0 key pressed
Lcd_Chr_Cp(MyKey + '0');
Op2 = 10*Op2 + MyKey; // Second number in Op2
}
Lcd_Cmd(LCD_CLEAR); // Clear LCD
Lcd_Out(1,1,"Op: "); // Display Op:
MyKey = getkeypad(); // Get operation
Lcd_Cmd(LCD_CLEAR);
Lcd_Out(1,1,"Res="); // Display Res=
switch(MyKey) // Perform the operation
{
case Plus:
Calc = Op1 + Op2; // If ADD
break;
case Minus:
Calc = Op1 - Op2; // If Subtract
break;
case Multiply:
Calc = Op1 * Op2; // If Multiply
break;
case Divide:
Calc = Op1 / Op2; // If Divide
break;
}
LongToStr(Calc, op); // Convert to string in op
//
// Remove leading blanks
//
j=0;
for(i=0;i<=11;i++)
{
if(op != ' ') // If a blank
{
lcd[j]=op;
j++;
}
}
Lcd_Out_Cp(lcd); // Display result
Delay_ms(5000); // Wait 5 seconds
Lcd_Cmd(LCD_CLEAR);
}
}
Thanks.Main program:
START
Configure LCD
Wait 2 seconds
DO FOREVER
Display No1:
Read first number
Display No2:
Read second number
Display Op:
Read operation
Perform operation
Display result
Wait 5 seconds
ENDDO
END
Yes but the main problem at this point is that there is an error with the LCD, and i can't see in there is any initialization before the main().
for example i have seen a microC code using this before main, i don't know if you should use it or not.
// 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;
// End LCD module connections
Can you open another example project to see how is the lcd initialized and if you need to define any pins?
And try to show a simple message in the lcd in an empty project, when you are able to do this then add the rest of your code.
Alex
#define Enter 11
#define Plus 12
#define Minus 13
#define Multiply 14
#define Divide 15
char txt[6];
char keypadPort at PORTD;
// 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;
// End LCD module connections
void initMain(){ //main initialization of SFR registers
INTCON = 0x00; //turn off interrupts
ADRESH = 0x00;
ADRESL = 0x00;
ADCON1 = 0x06; //all inputs are digital
ADCON0 = 0x00;
TRISD = 0xFF; //PORTD is input
TRISB = 0x00;
PORTB = 0x00;
PORTD = 0x00;
}
void main() {
unsigned char MyKey, i,j,lcd[5],op[12];
unsigned long Calc, Op1, Op2;
initMain(); //Main initialization
Keypad_Init(); //Initialize Keyboard
Lcd_Init(); //Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
for(;{ //infinite loop
MyKey = 0;
Op1 = 0;
Op2 = 0;
Lcd_Out(1,1,"No1: "); // Display No1:
while(1) // Get first no
{
MyKey = Keypad_Key_Click();
if(MyKey == Enter)break; // If ENTER pressed
MyKey++;
if(MyKey == 10)MyKey = 0; // If 0 key pressed
Lcd_Chr_Cp(MyKey + '0');
Op1 = 10*Op1 + MyKey; // First number in Op1
}
Lcd_Out(2,1,"No2: "); // Display No2:
while(1) // Get second no
{
MyKey = Keypad_Key_Click();
if(MyKey == Enter)break; // If ENTER pressed
MyKey++;
if(MyKey == 10)MyKey = 0; // If 0 key pressed
Lcd_Chr_Cp(MyKey + '0');
Op2 = 10*Op2 + MyKey; // Second number in Op2
}
Lcd_Cmd(_LCD_CLEAR); // Clear LCD
Lcd_Out(1,1,"Op: "); // Display Op:
MyKey = Keypad_Key_Click(); // Get operation
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Res="); // Display Res=
switch(MyKey) // Perform the operation
{
case Plus:
Calc = Op1 + Op2; // If ADD
break;
case Minus:
Calc = Op1 - Op2; // If Subtract
break;
case Multiply:
Calc = Op1 * Op2; // If Multiply
break;
case Divide:
Calc = Op1 / Op2; // If Divide
break;
}
LongToStr(Calc, op); // Convert to string in op
// Remove leading blanks
j=0;
for(i=0;i<=11;i++)
{
if(op != ' ') // If a blank
{
lcd[j]=op;
j++;
}
}
Lcd_Out_Cp(lcd); // Display result
Delay_ms(5000); // Wait 5 seconds
Lcd_Cmd(_LCD_CLEAR);
}
}
I haven't used microC but usually the LCDinit does't take parameters, the port is probably set in the header file with a #define.
The compiler tell you too many actual parameters, try Lcd_Init().
Even if it was expecting a parameter i don't think it would be an address (&PORTC).
Alex
LCD_Init(&PORTC);
#define MASK 0xF0
#define Enter 11
#define Plus 12
#define Minus 13
#define Multiply 14
#define Divide 15
// Keypad module connections
char keypadPort at PORTB;
// End Keypad module connections
// LCD module connections
sbit LCD_RS at RC4_bit;
sbit LCD_EN at RC5_bit;
sbit LCD_D4 at RC0_bit;
sbit LCD_D5 at RC1_bit;
sbit LCD_D6 at RC2_bit;
sbit LCD_D7 at RC3_bit;
sbit LCD_RS_Direction at TRISC4_bit;
sbit LCD_EN_Direction at TRISC5_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 TRISC3_bit;
// End LCD module connections
// This function gets a key from the keypad
//
unsigned char getkeypad()
{
unsigned char i, Key = 0;
PORTB = 0x01; // Start with column 1
while((PORTB & MASK) == 0) // While no key pressed
{
PORTB = (PORTB << 1); // next column
Key++; // column number
if(Key == 4)
{
PORTB = 0x01; // Back to column 1
Key = 0;
}
}
Delay_Ms(20); // Switch debounce
for(i = 0x10; i !=0; i <<=1) // Find the key pressed
{
if((PORTB & i) != 0)break;
Key = Key + 4;
}
PORTB=0x0F;
while((PORTB & MASK) != 0); // Wait until key released
Delay_Ms(20); // Switch debounce
return (Key); // Return key number
}
void main()
{
unsigned char MyKey, i,j,lcd[5],op[12];
unsigned long Calc, Op1, Op2;
TRISC = 0; // PORTC are outputs (LCD)
TRISB = 0xF0;
Keypad_Init(); // Initialize Keypad
// Configure AN pins as digital I/O
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"CALCULATOR"); // Display CALCULATOR
Delay_ms(2000); // Wait 2 seconds
Lcd_Cmd(_LCD_CLEAR); // Clear display
// Program loop
//
for(;// Endless loop
{
MyKey = 0;
Op1 = 0;
Op2 = 0;
Lcd_Out(1,1,"No1: "); // Display No1:
while(1) // Get first no
{ MyKey = getkeypad();
if(MyKey == Enter)break; // If ENTER pressed
MyKey++;
if(MyKey == 10)MyKey = 0; // If 0 key pressed
Lcd_Chr_Cp(MyKey + '0');
Op1 = 10*Op1 + MyKey; // First number in Op1
}
Lcd_Out(2,1,"No2: "); // Display No2:
while(1) // Get second no
{
MyKey = getkeypad();
if(MyKey == Enter)break; // If ENTER pressed
MyKey++;
if(MyKey == 10)MyKey = 0; // If 0 key pressed
Lcd_Chr_Cp(MyKey + '0');
Op2 = 10*Op2 + MyKey; // Second number in Op2
}
Lcd_Cmd(_LCD_CLEAR); // Clear LCD
Lcd_Out(1,1,"Op: "); // Display Op:
MyKey = getkeypad(); // Get operation
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Res="); // Display Res=
switch(MyKey) // Perform the operation
{
case Plus:
Calc = Op1 + Op2; // If ADD
break;
case Minus:
Calc = Op1 - Op2; // If Subtract
break;
case Multiply:
Calc = Op1 * Op2; // If Multiply
break;
case Divide:
Calc = Op1 / Op2; // If Divide
break;
}
LongToStr(Calc, op); // Convert to string in op
//
// Remove leading blanks
//
j=0;
for(i=0;i<=11;i++)
{
if(op != ' ') // If a blank
{ lcd[j]=op;
j++;
}
}
Lcd_Out_Cp(lcd); // Display result
Delay_ms(5000); // Wait 5 seconds
Lcd_Cmd(_LCD_CLEAR);
}
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?