Recently at my work I started a project for conveyor system where it has to feed the product up to the quantity set, which can be set. I am using PIC16F1459 with external 20 Mhz crystal and MCU clock at 32 Mhz and 16 x 2 LCD 4 bit connection. the problem I am facing is setting the batch quantity where I am using two push buttons, one for increasing and other for decreasing, when I increase the count above 9 i.e. 10 – 11 – 12 up to 100 it works perfect but, when I decrease the count for 10 or 100, the right most zero stays ON, so when I decrease from 100 to 99, it shows 990 same when I decrease from 10 to 9, it shows 90. If I do not use Ltrim() then it works perfect but it has empty space in front.
Is there any way to solve this problem?
Thanking everyone.
Code: Select all
/*
* Project Name : Up Down Counter with Push Button
* Description :
- Making a counter work with Push Buttons
- Push Button 1 will increase the Counter and Push Button 2 decrease
the counter
- Counter value will be displayed on 16 x 2 LCD
* Configuration :
MCU : PIC16F1459
Dev. Board : EasyPIC V7
Oscillator : HS-PLL, 20 MHz Crystal, 32 MHz MCU clock
SW : mikroC Pro for PIC V 7.6.0
*/
// LCD module connections on PORT C
sbit LCD_RS at LATC2_bit;
sbit LCD_EN at LATC3_bit;
sbit LCD_D4 at LATC4_bit;
sbit LCD_D5 at LATC5_bit;
sbit LCD_D6 at LATC6_bit;
sbit LCD_D7 at LATC7_bit;
sbit LCD_RS_Direction at TRISC2_bit;
sbit LCD_EN_Direction at TRISC3_bit;
sbit LCD_D4_Direction at TRISC4_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D6_Direction at TRISC6_bit;
sbit LCD_D7_Direction at TRISC7_bit;
// End LCD module connections
#define CountUp LATA.F0 // Push Button at RA0 for Counting UP
#define CountDn LATA.F1 // Push Button at RA1 for Counting Down
#define CountSet LATA.F3 // Push Button at RA3 for Count Seting
#define LED1 LATB.F4 // LED at B4 for Counting UP
#define LED2 LATB.F5 // LED at B5 for Counting Down
#define ON 1
#define OFF 0
char txt1[] = "Count=";
char Batch[7];
signed int i = 0;
void main() {
ANSELC =0;
TRISA = 1;
TRISB.B4 = 0;
TRISB.B5 = 0;
LCD_Init();
LCD_Cmd(_LCD_CLEAR);
LCD_CMD(_LCD_CURSOR_OFF);
LCD_Out(1,1, txt1);
while(1) {
if (PORTA.F3 == 1) {
LCD_out(1,1, "Count=");
if (PORTA.F0 == 1) {
delay_ms(30); // Debounce and reduce the speed if button kept pressed
LED1 = ON; // Just for Indication
if (i < 100) { i++; // If increase button pressed count up max to 100
}
}
else {
LED1 = OFF;
}
if (PORTA.F1 == 1) {
delay_ms(30); // Debounce and reduce the speed if button kept pressed
LED2 = ON; // Just for Indication
if (i > 0) { i--; // If decrease button pressed count down minimum to 0
}
}
else {
LED2 = OFF;
}
IntToStr(i, Batch);
Ltrim(Batch); // to reduce the length of displyed numbers.
LCD_Out(1,8, Batch);
}
}
}