CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Increment count on LCD display?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
hemnath



Joined: 03 Oct 2012
Posts: 241
Location: chennai

View user's profile Send private message

Increment count on LCD display?
PostPosted: Mon Nov 05, 2012 10:28 pm     Reply with quote

I want to increment a count and display in lcd, when pushbutton is pressed. Button connected to PIN C0. It compiles successfully. In the display it shows count, but when i press push button it shows different characters at location 0x86 in the display. Please help...
Code:
#include "18F2520.h"
#fuses  INTRC_IO
#use delay(clock=4000000)     // internal clock

#define RS PIN_A2   
#define EN PIN_A1

void lcd_init();
void lcd_cmd(unsigned char);
void lcd_data(unsigned char);
unsigned char data[] = "count";
unsigned int i = 0;
unsigned int counter = 0;
int16 temp;

void main()
{
lcd_init();
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256|RTCC_8_BIT);

while(1)
  {
   while(data[i]!='\0')
     {
      lcd_data(data[i]);
      i++;
     }
   
   lcd_cmd(0x86);
   if(input(PIN_C0)==0)
     {
      delay_ms(200);
      temp=counter++;
      lcd_data(temp);
     }
  }
}

void lcd_init()
{
lcd_cmd(0x30);      // Configure the LCD in 8-bit mode, 1 line and 5x7 font
lcd_cmd(0x0C);      // Display On and Cursor Off
lcd_cmd(0x01);      // Clear display screen
lcd_cmd(0x06);      // Increment cursor
lcd_cmd(0x80);      // Set cursor position to 1st line, 1st column
}

void lcd_cmd(unsigned char c)
{
output_b(c);
output_low(RS);
output_high(EN);
delay_ms(15);
output_low(EN);
}

void lcd_data(unsigned char z)
{
output_b(z);
output_high(RS);
output_high(EN);
delay_ms(15);
output_low(EN);
}
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Nov 06, 2012 2:36 am     Reply with quote

Please explain what you are expecting to see on the display with what you actually get.

Maybe you're confusing ASCII char with numeric value.

Mike
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Nov 06, 2012 3:35 am     Reply with quote

As Mike already suggested, the value you are sending to the display is a binary value. What you see on the LCD is the corresponding ASCII value and this won't make sense to us humans.

You need to convert the binary data to readable text first, for example using the function itoa() or sprintf("%d").

CCS has implemented a very useful extension to the printf function where you can redirect the output to another function that will display the data. This output function should have a single character as parameter, just like your lcd_data function so that makes it very easy.
Replace
Code:
lcd_data(temp);
by:
Code:
printf(lcd_data, "%d", temp);


Note that you can make your program a lot simpler when you add your text 'count' to the above line, but I leave that as an exercise to you.
Kumara



Joined: 16 Oct 2013
Posts: 7
Location: Sri Lanka

View user's profile Send private message

PostPosted: Tue Oct 22, 2013 4:05 am     Reply with quote

Mike Walne wrote:
Please explain what you are expecting to see on the display with what you actually get.

Maybe you're confusing ASCII char with numeric value.

Mike



Hi, Please refer following code for incensement counter with debouncing solution.

Kumara

Code:

#include <16F877A.h>              // Pic Microcontroller (MCU) Select
#DEVICE ADC=10                    // 8 Bit analog resolution
#fuses NOWDT,HS, NOPUT, NOPROTECT // Fuses
#fuses NOBROWNOUT, NOLVP          // Fuses
#use delay(clock=20000000)        // Crystal Oscillator Frequency ( 20 MHZ)

#include <lcd.c>                  // Driver for LCD Module



   void main()                    // Main block
   {


   INT16 PRE_COUNT=0;               // Declare variable)
   INT16 SET_COUNT;                 // Declare variable
   INT1 SW1;                      // Declare variable
   BOOLEAN ISPRESSED=FALSE;       // Boolean logic=0;



   setup_adc_ports(AN0);
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);


   lcd_init();
   
   while (TRUE) {                // Endless loop

   SW1=INPUT(PIN_C0);            // Positive inputs

         IF(PRE_COUNT==0)
         {
         lcd_putc("\f");         // LCD clear
         lcd_gotoxy(1,1);        // Select Raw 1 & start digit 1 position
         printf(lcd_putc," PRE_COUNT:%04LU ",PRE_COUNT);

         lcd_gotoxy(1,2);        // Select Raw 2 & start digit 1 position
         printf(lcd_putc," SET_COUNT:%04LU ",SET_COUNT);
         DELAY_MS(200);
         }

         ////////////////////////////////////////////////////////

         SET_ADC_CHANNEL(0);     // INPUT AN0
         DELAY_US(100);
         SET_COUNT=READ_ADC();   // SET VALUE= RA1 ( 10K V/R )
         SET_COUNT=(SET_COUNT);  // FOR 4 DIGITS UP TO 1023
         ////////////////////////////////////////////////////////

         IF (SW1 && !ISPRESSED)  // This line must be here
         {

         DELAY_MS(10);           // This line must be here

         SW1=INPUT(PIN_A1);      // This line must be here
         IF(SW1)                 // This line must be here
         {PRE_COUNT++;           // This line must be here

         ISPRESSED=TRUE;         // This line must be here

         lcd_putc("\f");         // LCD clear

         lcd_gotoxy(1,1);        // Select Raw 1 & start digit 1 position
         printf(lcd_putc," PRE_COUNT:%04LU ",PRE_COUNT);

         lcd_gotoxy(1,2);        // Select Raw 2 & start digit 1 position
         printf(lcd_putc," SET_COUNT:%04LU ",SET_COUNT);
         DELAY_MS(100);


         IF(PRE_COUNT>=SET_COUNT) // This line must be here
            {
            OUTPUT_HIGH(PIN_B0);

            }
         ELSE
            {
            OUTPUT_LOW(PIN_B0);

            }
       }
   }

         IF(!SW1)                 // This line must be here
         {
         ISPRESSED=FALSE;         // This line must be here

         }
     }
 }

Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group