Up / Down Counter on LCD

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
babulp
Posts: 16
Joined: 16 Jan 2022 01:48

Up / Down Counter on LCD

#1 Post by babulp » 27 Feb 2022 15:12

Hi All,
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);

  }
 }
}

davegsm82
Posts: 156
Joined: 29 Mar 2011 20:35

Re: Up / Down Counter on LCD

#2 Post by davegsm82 » 18 Mar 2022 12:55

Sorry you haven't had an earlier reply on this one, it's a fairly simple problem.

When you send your count number to the display, if it's 3 digits, lets say '123' then that's all that's sent. Next time, if you send only 1 or 2 digits, then the digits already on the display won't be overwritten. So lets say your count suddenly jumped from 123 to 0, your display will show 023.

Ideally you should clear the spaces where your digits reside before sending the new value, so if you will only ever have 3 digits, i.e. 999 max, then print 3x spaces to the LCD first, so send " " then send your numerical value.

Hope this helps!

babulp
Posts: 16
Joined: 16 Jan 2022 01:48

Re: Up / Down Counter on LCD

#3 Post by babulp » 18 Mar 2022 18:37

Thank you very much davegsm82, it worked. earlier i tried LCD clear and it was flashing.

but this worked thank you once again.

Post Reply

Return to “mikroC PRO for PIC General”