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.

pls help me pls help me in simple calculator with[keybad&lcd] in mikro c

Status
Not open for further replies.

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
hi friends,i need some assistant to catch the error in my
i have caculator using keybad+lcd in mikro c with pic16f452
/∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
in the project a 4 x 4 keypad is connected to PORTB of a PIC18F452
microcontroller. Also an LCD is connected to PORTC. The project is a
simple calculator which can perform integer arithmetic.
The keys are organized as follows:
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
The keys are labeled as follows:
1 2 3 4
5 6 7 8
9 0 Enter
+ − *
HTML:
#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);
}
}
this is the Img of errors
 

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
 

Thank you for your reply……………
i tried your idea but there is no rresult yet there are new errors appear '' Not enouh parmeter"
 

I have seen an example here MikroC "Hello World!" LCD example | PIC Microcontroller Note
so the Lcd_Init() is called with an address of a port as you did before.

You get an error LCD_CLEAR is not defined, something must be missing.

You should read the help files for usage of lcd or open an example or read the library source to see how to use the init.

Alex
 

Thank you Alex for your assistance , and i want to explain the program task
The program consists of a function called getkeypad, which reads the pressed keys, and
the main program. Variable MyKey stores the key value (0 to 15) pressed, variables Op1
and Op2 store respectively the first and second numbers entered by the user. All these
variables are cleared to zero at the beginning of the program. A while loop is then
formed to read the first number and store in variable Op1. This loop exits when the user
presses the ENTER key. Similarly, the second number is read from the keyboard in a
second while loop. Then the operation to be performed is read and stored in variable
MyKey, and a switch statement is used to perform the required operation and store the
result in variable Calc. The result is converted into a string array using function
LongToStr,The program displays the result on the LCD, waits for five seconds, and then clears the screen and is
ready for the next calculation. This process is repeated forever.
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;
}
the DPL of this part as following:
Function getkeypad:
START
IF a key is pressed
Get the key code (0 to 15)
Return the key code
ENDIF
END

the 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 != ' ') // If a blank
{
lcd[j]=op;
j++;
}
}
Lcd_Out_Cp(lcd); // Display result
Delay_ms(5000); // Wait 5 seconds
Lcd_Cmd(LCD_CLEAR);
}
}

the PDL:
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
Thanks.

Best regards,

alaalwi11
 

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
 

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

Hi ,

That's the right syntax for mikroc pro compiler, so change the pins for portc accordingly ...
or get the old mikroc compiler v.8.2 ... that could work !
 
Last edited:

Hi friend,

Sorry for late reply
and i tried the program in the new version mikroc PRO for pic but it seem there is a problem in the working
i hope some one share me to solve it
Thanks.
code
#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);
}
}



Best regards,

alaalwi11
 

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

You are right that no parameters are expected and the port is to be defined by #define.
However, older versions of mikroC required that you indicate which port is to be used for LCD, like
Code:
LCD_Init(&PORTC);

Tahmid.
 

hello friend, i exchanged pic16f877 by the pic18f452 and i developed the program it executed well but the circuit did not work
i hope anybode give me some help
#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);
}
}



Best regards,

alaalwi11
 
  • Like
Reactions: inad

    inad

    Points: 2
    Helpful Answer Positive Rating
hi anyone here good evening,
i need some assistant,
i want know if there are no any output in LCD, what's common problem probability is?




Best regards,

alaalwi11
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top