Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

increment/decrement numbers on an lcd

Status
Not open for further replies.

dawson

New Member
Hi,
I have been try the experiments from rajbex and am currently trying to implement his latest experiment into my project.

I want to set a number in the eeprom and can do this thanks to rajbex. however if I want to change that number I am having difficulties.

I have the following code:

Code:
/*
  Project Name: Read/Write Internal EEPROM
  * Copyright:
     (c) Rajendra Bhatt, 2009.
 * Description:
     This code is an example of accessing internal EEPROM.

 * Test configuration:
     MCU:             PIC16F628
     Oscillator:      IntRC I/O
*/

// LCD connections definitions
sbit LCD_RS at RA0_bit;
sbit LCD_EN at RA1_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 TRISA0_bit;
sbit LCD_EN_Direction at TRISA1_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 connections definitions
// Define Messages
char message1[] = "1.READ";
char message2[] = "2.INC";
char message3[] = "3.DEC";
char message4[] = "Set Temp:";

char digits[] = "0";
unsigned short  i, NUM ;
unsigned int ADD = 0x00, temp; // Start EEPROM Location


void main() {
CMCON  |= 7;      // Disable Comparators
TRISB = 0x0F;
PORTB = 0x00;
Lcd_Init();
start:
Lcd_Cmd(_LCD_CLEAR);               // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
Lcd_Out(1,1,message1);             // Write message1 in 1st row
Lcd_Out(1,8,message2);
Lcd_Out(2,1,message3);
do {
// Read Operation
if (Button(&PORTA, 2, 1, 0)) {  // Detect logical one to zero
      Delay_ms(300);
      Lcd_Cmd(_LCD_CLEAR);
      Lcd_Out(1,1,message4);
      NUM = EEPROM_Read(0x000);
      digits[0] = NUM+48;
      Lcd_Out(2,1,digits);
      
      delay_ms(1000);
      goto start;

     }
 //increment
 if (Button(&PORTA, 3, 1, 0)) {  // Detect logical one to zero
      NUM = EEPROM_Read(0x000)+1;
      EEPROM_Write(0x000,NUM);

      Lcd_Cmd(_LCD_CLEAR);
      Lcd_Out(1,1,message4);
      digits[0] = NUM+48;
      Lcd_Out(2,1,digits);
      
      delay_ms(1000);
      goto start;
  }
//decrement
   if (Button(&PORTA, 4, 1, 0)) {  // Detect logical one to zero
      NUM = EEPROM_Read(0x000)-1;
      EEPROM_Write(0x000,NUM);

      Lcd_Cmd(_LCD_CLEAR);
      Lcd_Out(1,1,message4);
      digits[0] = NUM+48;
      Lcd_Out(2,1,digits);
      
      delay_ms(1000);
      goto start;
  }
 } while(1);
}

The above code does work and when pressing the corresponding button the number is incresed or decreased, however I can only display numbners 0 - 9. I am led to believe that the next number 10 is being stored but I need to seperate it and display 1+48 and 0+48. How can I do this? Also I would like to be able to store and display negative numbers as I need a scale of -55 to +125

Thanks
 

dawson

New Member
thanks for the advice I now have it working for +numbers but still having trouble with negative numbers. Also it displays as 001,010,100 etc how can I make it just display 1,10,100:

here is the code i have now:

Code:
/*
  Project Name: Read/Write Internal EEPROM
  * Copyright:
     (c) Rajendra Bhatt, 2009.
 * Description:
     This code is an example of accessing internal EEPROM.

 * Test configuration:
     MCU:             PIC16F628
     Oscillator:      IntRC I/O
*/

// LCD connections definitions
sbit LCD_RS at RA0_bit;
sbit LCD_EN at RA1_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 TRISA0_bit;
sbit LCD_EN_Direction at TRISA1_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 connections definitions
// Define Messages
char message1[] = "1.READ";
char message2[] = "2.INC";
char message3[] = "3.DEC";
char message4[] = "Set Temp:";

char digits[] = "000";
unsigned short  i, NUM ;

void lcd_convert() {
     // check if temperature is negative
     if (NUM & 0x8000) {
        digits[0] = '-';
        // Negative temp values are stored in 2's complement form
        NUM = ~NUM + 1;
     }

     if (NUM/100)  {
        // 48 is the decimal character code value for displaying 0 on LCD
        digits[0] = NUM/100  + 48;
     }
     else   {
         digits[0] = '0';
         digits[1] = (NUM/10)%10 + 48;             // Extract tens digit
         digits[2] =  NUM%10     + 48;             // Extract ones digit
     }
}
void main() {
     CMCON  |= 7;      // Disable Comparators
     TRISB = 0x0F;
     PORTB = 0x00;
     Lcd_Init();
     start:
     Lcd_Cmd(_LCD_CLEAR);               // Clear display
     Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
     Lcd_Out(1,1,message1);             // Write message1 in 1st row
     Lcd_Out(1,8,message2);
     Lcd_Out(2,1,message3);
     do {
        // Read Operation
        if (Button(&PORTA, 2, 1, 0)) {  // Detect logical one to zero
           Delay_ms(300);
           NUM = EEPROM_Read(0x000);

           lcd_convert();

           Lcd_Cmd(_LCD_CLEAR);
           Lcd_Out(1,1,message4);
           Lcd_Out(2,1,digits);

           delay_ms(1000);
           goto start;
         }
         //increment
         if (Button(&PORTA, 3, 1, 0)) {  // Detect logical one to zero
            NUM = EEPROM_Read(0x000)+1;
            EEPROM_Write(0x000,NUM);

            lcd_convert();
            
            Lcd_Cmd(_LCD_CLEAR);
            Lcd_Out(1,1,message4);
            Lcd_Out(2,1,digits);

            delay_ms(1000);
            goto start;
          }
          //decrement
          if (Button(&PORTA, 4, 1, 0)) {  // Detect logical one to zero
             NUM = EEPROM_Read(0x000)-1;
             EEPROM_Write(0x000,NUM);

             lcd_convert();
             
             Lcd_Cmd(_LCD_CLEAR);
             Lcd_Out(1,1,message4);
             Lcd_Out(2,1,digits);

             delay_ms(1000);
             goto start;
           }
        } while(1);
}
 

rajbex

Member
Try this.
1. Change unsigned short NUM to signed short NUM.
2. change the following code
else {
digits[0] = '0';
digits[1] = (NUM/10)%10 + 48; // Extract tens digit
digits[2] = NUM%10 + 48; // Extract ones digit
}

TO

else {
digits[1] = (NUM/10)%10 + 48; // Extract tens digit
digits[2] = NUM%10 + 48; // Extract ones digit
}

You will still see 0s but you should be able to display negative numbers. Give it a try.

- Raj
Experiments with PIC16F628A
 

dawson

New Member
Yeah that and another minor adjustment have solved the negative numbers issue thanks.

Any ideas on how to remove the extra 0's?
 

dawson

New Member
I have been exploring and reading up on c programming, but have only been learning a few days and am struggling. I will continue to work on it and read the tutorials you suggested in an earlier post but so far I have not come across anything that will help me here.
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top