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

Help with LCD counter code

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



Joined: 12 Dec 2014
Posts: 77

View user's profile Send private message

Help with LCD counter code
PostPosted: Fri Dec 12, 2014 3:27 pm     Reply with quote

Hello folks. I am a newbie to CCS and also newbie to code programming. I am presently a beginner in C language. However I am striving to learn all I can.
I am presently trying to program a LCD with a pushbutton in that whenever I push the button the text on the LCD screen changes accordingly and when it reaches the last text, if the pushbutton is pressed again, it will return to the first text again. So this is basically a loop counter and I am sure ridiculously easy for you guys here. In fact I have accomplished this programming as I show in the code below.

Problem is that I am employing delays to keep the text still until the next change. I am using 300ms delays but the texts appear to blink once or twice and sometimes the command will not work in the first press or takes too long to change the screen. Also if I keep pressing the button, the texts keep changing. What I need is that the texts do not keep rolling if I keep pressing the button and only change when the button is released (output_low(ipunt_x) maybe?

So please I would like to know what instruction I should include in place of the delays to accomplish what I have just described.
Thanks a lot in advance for any help.
Here is the code:
Code:

WHILE(true)
   {
 
   if(INPUT(PIN_B4)==0)
   {
   cont++;
   delay_ms(300);
   }
   if(cont==1)
   {
 
   output_low(pin_b4);
   delay_ms(200);
   lcd_putc("\f<<<<<<TEXT1>>>>>>");
 
 
   }
   if(cont==2)
   {
 
   output_low(pin_b4);
   delay_ms(200);
   lcd_putc("\f<<<<<TEXT2>>>>>");
 
 
   }
 
   if(cont==3)
   {
 
   output_low(pin_b4);
   delay_ms(200);
   lcd_putc("\f <<<<TEXT3>>>>");
 
 
   }
   if(cont==4)
   {
   output_low(pin_b4);
   delay_ms(200);
   printf(lcd_putc,"\f");
   lcd_gotoxy(3,1);
   lcd_putc("BATTERY FULL");
 
   {
   if(cont==4)// repeats the selection group after last entry
   for(cont=5;cont>=5;cont++)
   {}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Dec 12, 2014 4:11 pm     Reply with quote

First, your hardware. Does your push-button circuit look like this ?
Hopefully it does.
Code:

          +5v
           |
           < 4.7K
           > resistor       
           <          ___ Switch 
To         |         _|_|_
PIC -----------------o   o------ 
pin                            |             
B4                            --- Ground
                               -   
 
picman62



Joined: 12 Dec 2014
Posts: 77

View user's profile Send private message

PostPosted: Fri Dec 12, 2014 7:25 pm     Reply with quote

Thanks for your reply PCM programmer.
Yes, it's the same configuration, except the resistor is 10K.
What do you say?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Dec 12, 2014 8:20 pm     Reply with quote

I would do something like this. Use the button() function from this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=23837

If I get a button press, then I execute a switch-case statement. The
switch-case will display a message on the LCD, dependent upon the
value of your 'cont' variable. After displaying the message, 'cont'
is incremented, so that when the next button press occurs, the next
message will be displayed. When the last message is displayed,
'cont' is reset back to 0.

There is no delay_ms() statement after the message is displayed,
because the button() function only returns TRUE if a button was pressed
once, or if it was held down and then it will return TRUE at the repeat rate.
Code:

#include <16F887.h>
#fuses INTRC_IO, NOWDT, BROWNOUT
#use delay(clock=4M)

#include "flex_lcd.c"

int8 B4_var;
#include "button.c"
//======================
void main()     
{
int8 cont;

cont = 0;

lcd_init();

while(TRUE)
  {
   if(button(PIN_B4, 0, 50, 10, B4_var, 1))   
     {
      switch(cont)
        {
         case 0:
            lcd_putc("\f 1st line");
            break;
         case 1:
            lcd_putc("\f 2nd line");
            break;
         // etc.
        }
      cont++;
      if(cont > 4)
         cont = 0;
     }

   delay_ms(10);  // Required for button() function
  }
           
}
picman62



Joined: 12 Dec 2014
Posts: 77

View user's profile Send private message

PostPosted: Sat Dec 13, 2014 3:58 am     Reply with quote

Thank you so much PCM prgrammer. It worked.

The text in the screen does not give sporadic blinks anymore.
I only had to modify the delay time to 50ms so that when I press the button, the LCD screen does not roll the texts. With delay time of 10ms, this was still happening.

Also I noticed that the command takes effect at the moment I press the button and not when I release it, but I can perfectly live with that.
Thanks a lot for being helpful.
I would like to ask you what good and practical source you recommend to learn C language with focus in CCS compiler.

This is how the whole code now looks like:

Code:


#include <button.c>
#include <LCD.C>
#define LCD_ENABLE_PIN PIN_D0
#define LCD_RS_PIN PIN_D1
#define LCD_RW_PIN PIN_D2
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
#define read_bit_var(x)  bit_test(*(int8 *)(x >> 3), x & 7)
#include <lcd.c>
#int_rb
int Cont;
int8 B4_var;



void main()
{
   port_B_pullups(0xFF);
   setup_adc_ports(AN0);
   lcd_init();

   lcd_init();

   while(TRUE){
   
   int Cont;
   char load[16]; //atividade de barras
   {
   
   lcd_gotoxy(3,1);
   lcd_putc("text general");// text of display
   }


   while(TRUE){
   Cont=0;
   
   while (Cont<16){
   load [Cont]=255;
   
   Printf(lcd_putc,"\n%s",load,);
   Cont++;
 
   delay_ms(250); //bargraph
   }
   Cont=0;
   lcd_putc("\f");
   lcd_gotoxy(4,1);
   lcd_putc("*gen text2*");
   delay_ms(1500);
   
   lcd_gotoxy(3,1);
   lcd_putc("gern text3");
   lcd_gotoxy(4,2);
   lcd_putc("gen text4");
   int8 cont;
   cont = 0;
   WHILE(true)
   {
   if(button(PIN_B4, 0, 50, 10, B4_var, 1))   
     {
   switch(cont)
        {
   case 0:
   lcd_putc("\f TEXT 1");
   break;
   
   case 1:
   lcd_putc("\f TEXT 2");
   break;
         
   case 2:
   lcd_putc("\f TEXT 3");
   break;
         
   case 3:
   printf(lcd_putc,"\f");
   lcd_gotoxy(3,1);
   lcd_putc("BATTERY ");
   break;
 
 }
   cont++;
   if(cont >= 4)
   cont = 0;
     }

   delay_ms(50);  // Required for button() function
  }
           
}
   }
 
}


Last edited by picman62 on Sun Dec 14, 2014 4:38 am; edited 1 time in total
jgschmidt



Joined: 03 Dec 2008
Posts: 184
Location: Gresham, OR USA

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Sat Dec 13, 2014 7:18 pm     Reply with quote

To learn C you can always google "C tutorial". I googled "ccs pic c tutorial" and got several good hits. You can also google "pic tutorial" to learn more about the PIC hardware.

I've been going through Mark Siegesmund's book "Embedded C Programming" which focuses on the CCS dialect - it's OK. I think it moves too fast for a total beginner and the typos in some of the example code don't help. Mark is the author of the CCS compiler so I'm just using the book as a refresher for my next project.
_________________
Jürgen
www.jgscraft.com
picman62



Joined: 12 Dec 2014
Posts: 77

View user's profile Send private message

PostPosted: Sun Dec 14, 2014 4:40 am     Reply with quote

Thanks for your suggestions Jurgen. Appreciated.
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