# PIC18F4550, button and counting code [on hold]

I am using PIC18F4550. I have some problem about the counting. How should i write the code when i press the button the counting were add 1?

this is my code

#include <P18F4550.h>
#include<xlcd.h>                //built-in LCD library
#include<delays.h>
#define BB PORTBbits.RB0
#pragma config FOSC = XT_XT, PWRT = ON, MCLRE = ON
#pragma config BOR = ON, WDT = OFF, PBADEN = OFF, LVP = OFF
void DelayPORXLCD(void) //15mS delay
{
Delay1KTCYx(15);
}
void DelayFor18TCY(void) //18 cycle delay
{
Nop(); Nop(); Nop(); Nop();
Nop(); Nop(); Nop(); Nop();
Nop(); Nop(); Nop(); Nop();
Nop(); Nop(); Nop(); Nop();
Nop(); Nop();
}
void DelayXLCD(void) //5mS delay
{
Delay1KTCYx(5);
}
void main(void)
{
unsigned char minute [] = "00";
unsigned int i = 0;
TRISD=0;                //outputs
TRISB=0;
TRISBbits.TRISB0 =1;

OpenXLCD(EIGHT_BIT & LINES_5X7); //LCD set-up

while(BusyXLCD( ));
WriteCmdXLCD(0x0C); //on display, off cursor

while(BusyXLCD( ));

while(BusyXLCD( ));
WriteCmdXLCD(SHIFT_DISP_LEFT); //cursor moves right
//for each character

putrsXLCD("Adjust minute");     //string placed in ROM

putsXLCD(minute);

for(;;)
{
if(BB == 0)
{
minute++;
}
}

}


## put on hold as too broad by brhans, laptop2d, Dmitry Grigoryev, JRE, Rev1.02 days ago

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• somebody can help me? i am trying many day but still cannot work as well – Yze Lim Aug 10 at 9:24
• Please format your code properly. Use the {} button and make sure that your indents are correct. Check the preview before you submit your question. You should remove any code that is not related to your problem. – Transistor Aug 10 at 9:26
• my program is correct ,at the button that side i can't to make it – Yze Lim Aug 10 at 9:30
• i have seen arduino program but i have a lot not understand about the program , because i am using microcontroller. – Yze Lim Aug 10 at 9:43
• Sorry. I got mixed up which controller you are using. I will delete the Arduino comment. You should be able to find button examples for PIC. – Transistor Aug 10 at 9:44

You're not displaying the number on the LCD during updates. This could be one of several problems. But you can try:

for(;;)   // And empty <for> will loop forever, just like while(True)
{
if(BB == 0)
{
minute++;         // BUG. This variable is a string. See updated answer
putsXLCD(minute); // Write the updated number to the LCD
}
}


With this you will still have bouncing problems. The minutes will increase several times with a single button press.

Update

The minute variable should be an integer. You can't do math on strings, only on numbers. So do the math on a number, and convert it to a string. Try:

unsigned int minute = 0;
char minute_str [5] = "0";

for(;;)   // And empty <for> will loop forever, just like while(True)
{
if(BB == 0)
{
minute++;
sprintf (minute_str , "%d", minute);
putsXLCD(minute_str); // Write the updated number to the LCD
}
}


Seems like you still need to understand a few programming basics. I would try to start with something much simpler than a microcontroller.

• i try it cannot make the program its show warning (suspicious pointer conversion,scalar type expected for increment operator) what is this mean? – Yze Lim Aug 10 at 10:04
• I updated my question with the second answer. – Hein Wessels Aug 10 at 10:13
• also cant , but it can build success , on the LCD the number 0 why its show a symbol not 0? – Yze Lim Aug 10 at 10:18
• i am a newbie on microcontroller i can do it show the character on lcd ,its just can't to do a counting when button is push – Yze Lim Aug 10 at 10:19
• You also need to add #include <stdio.h> to your includes, otherwise sprintf() will not work. Also: show us a schematic of how you connected the switch. – MartinF Aug 10 at 10:46