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 CCS Technical Support

Controlling LCD flicker

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



Joined: 17 Oct 2005
Posts: 116

View user's profile Send private message

Controlling LCD flicker
PostPosted: Thu Oct 20, 2005 4:56 pm     Reply with quote

I've been playing around with a program that I cobbled together from various sources on the internet. It reads a digital rangefinder and outputs the value to an LCD.

The problem is that the LCD image flickers quite badly. Two changes I've made, based on what I've read in the archives, have had some effect.

1. Introducing a 1/2 second delay in the data refresh rate helped but still left a little flicker. I would prefer not to have any delay at all.

2. Moving the LCD_INIT () statement out with the WHILE(1) statement didn't help and it actually removed my first line on the LCD.

Here is a sample of the code; the relevent portion is in the bottom 20 lines.
Code:

#include <16F628.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#include <LCD_4LINE.c> //this file set up to use port B

#define GP2D02_VIN PIN_B3
#define GP2D02_VOUT PIN_A2

set_tris_a(0xff);
set_tris_b(0x00);
int reading;
int get_ir_reading();


int get_ir_reading()
{
   int counter=9;
   int reading=0;
   output_low(GP2D02_VIN);         // start cycle with Vin low
   delay_ms(1);                       // give the sensor a little time before we bug it
   while (!input(GP2D02_VOUT)); //wait for Vout to go high

   do {
      delay_cycles(4);    // minimum wait is 2us, max is 170us, 4 worked
      output_low(GP2D02_VIN);  // tell the sensor that we want a bit
      delay_cycles(2);         // might be as low as 1 (maybe), 2 worked

      reading=reading<<1;      // left shift the reading value
      reading=reading|(input(GP2D02_VOUT)); // put the newest reading into the
                                            // LSB of reading
      output_high(GP2D02_VIN); // we read bit, and get ready for the next one
      counter--;
      } while (counter>0);


      printf(lcd_putc,"\f IR Measurement");
      printf(lcd_putc,"\n================");
      lcd_gotoxy(1,3);
      printf(lcd_putc,"Value =%u",reading);
      lcd_gotoxy(1,4);
      printf(lcd_putc,"Distance =");
      delay_ms(500);
      return(reading);

}

void main()
{
while(true)
{
lcd_init();
get_ir_reading();
}
}


Any ideas on how I might be able to further reduce the flicker?

Thanks....Harry
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Oct 20, 2005 5:13 pm     Reply with quote

The reason it's flickering is because you're continuously clearing the
screen. You've got "\f" in there:
Quote:
printf(lcd_putc,"\f IR Measurement");


You shouldn't have to continuously re-init the LCD. If you have
to do that, then something is wrong.

Also your code as posted won't compile. You have a couple
floating set_tris statements.
Harry Mueller



Joined: 17 Oct 2005
Posts: 116

View user's profile Send private message

PostPosted: Thu Oct 20, 2005 7:23 pm     Reply with quote

Thanks, that was the problem. I was also able to make the changes suggested in the archives. A couple of things:

1. It does compile. I don't know what floating set_tris statements are.

2. When the variable "Reading" goes under a hundred, the value displayed on the LCD goes to three digits. For example 99 becomes around 999, etc. I assume it has something to do with the bit shifting done withing the get_IR_reading function.

Here is the new code:
Code:

#include <16F628.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#include <LCD_4LINE.c> //this file set up to use port B

#define GP2D02_VIN PIN_B3
#define GP2D02_VOUT PIN_A2

set_tris_a(0xff);
set_tris_b(0x00);
int reading;
int get_ir_reading();


int get_ir_reading()
{
   int counter=9;
   int reading=0;
   output_low(GP2D02_VIN);         // start cycle with Vin low
   delay_ms(1);                       // give the sensor a little time before we bug it
   while (!input(GP2D02_VOUT)); //wait for Vout to go high

   do {
      delay_cycles(4);    // minimum wait is 2us, max is 170us, 4 worked
      output_low(GP2D02_VIN);  // tell the sensor that we want a bit
      delay_cycles(2);         // might be as low as 1 (maybe), 2 worked

      reading=reading<<1;      // left shift the reading value
      reading=reading|(input(GP2D02_VOUT)); // put the newest reading into the
                                            // LSB of reading
      output_high(GP2D02_VIN); // we read bit, and get ready for the next one
      counter--;
      } while (counter>0);

      lcd_gotoxy(1,1);
      printf(lcd_putc," IR Measurement");
      lcd_gotoxy(1,2);
      printf(lcd_putc,"================");
      lcd_gotoxy(1,3);
      printf(lcd_putc,"Value =%u",reading);
      lcd_gotoxy(1,4);
      printf(lcd_putc,"Distance =");

      return(reading);

}

void main()
{
lcd_init();
while(true)
{
get_ir_reading();
}
}


Thanks....Harry
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Oct 20, 2005 7:53 pm     Reply with quote

The two lines in bold are lines of code, they're not function prototypes.
They call CCS functions. They are not inside main() or any other
function. They cause the following errors when I try to compile the
program:

*** Error 28 Line 10(12,16): Expecting an identifier
*** Error 43 Line 10(17,18): Expecting a declaration
*** Error 28 Line 11(12,16): Expecting an identifier
*** Error 43 Line 11(17,18): Expecting a declaration

Quote:
#include <16F628.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#include <LCD_4LINE.c> //this file set up to use port B

#define GP2D02_VIN PIN_B3
#define GP2D02_VOUT PIN_A2


set_tris_a(0xff);
set_tris_b(0x00);

int reading;
int get_ir_reading();
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Thu Oct 20, 2005 8:47 pm     Reply with quote

Quote:
When the variable "Reading" goes under a hundred, the value displayed on the LCD goes to three digits.


You aren't clearing the rest of the line so stale data is on the display.
Harry Mueller



Joined: 17 Oct 2005
Posts: 116

View user's profile Send private message

PostPosted: Thu Oct 20, 2005 9:17 pm     Reply with quote

[quote="Mark"]
Quote:

You aren't clearing the rest of the line so stale data is on the display.


Ahhhh....thanks.How can I fix that? If I use a /f then the display flickers.
Harry Mueller



Joined: 17 Oct 2005
Posts: 116

View user's profile Send private message

PostPosted: Thu Oct 20, 2005 9:20 pm     Reply with quote

PCM programmer wrote:
They cause the following errors when I try to compile the
program:

*** Error 28 Line 10(12,16): Expecting an identifier
*** Error 43 Line 10(17,18): Expecting a declaration
*** Error 28 Line 11(12,16): Expecting an identifier
*** Error 43 Line 11(17,18): Expecting a declaration


Does that mean that my compiler is not working properly and I should report this to CCS?

Harry
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

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

PostPosted: Thu Oct 20, 2005 9:39 pm     Reply with quote

[quote="Harry Mueller"]
Mark wrote:
Quote:

You aren't clearing the rest of the line so stale data is on the display.


Ahhhh....thanks.How can I fix that? If I use a /f then the display flickers.


One way to to fix the field to a specific length
Code:
      lcd_gotoxy(1,3);
      printf(lcd_putc,"Value =%4u",reading);


Another way is to pad spaces at end
Code:
      lcd_gotoxy(1,3);
      printf(lcd_putc,"Value =%u   ",reading);


Another way is to clear the field first
Code:
      lcd_gotoxy(8,3);
      printf(lcd_putc,"      ");
      lcd_gotoxy(1,3);
      printf(lcd_putc,"Value =%u",reading);

_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!


Last edited by asmallri on Thu Oct 20, 2005 10:50 pm; edited 1 time in total
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

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

PostPosted: Thu Oct 20, 2005 9:40 pm     Reply with quote

Harry Mueller wrote:
PCM programmer wrote:
They cause the following errors when I try to compile the
program:

*** Error 28 Line 10(12,16): Expecting an identifier
*** Error 43 Line 10(17,18): Expecting a declaration
*** Error 28 Line 11(12,16): Expecting an identifier
*** Error 43 Line 11(17,18): Expecting a declaration


Does that mean that my compiler is not working properly and I should report this to CCS?

Harry


If you can really compile the code you posted without errors then it is a bug.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Oct 20, 2005 10:10 pm     Reply with quote

You're not using the latest version of the compiler, so reporting a
bug on an older version won't do much good. Also, you're fairly
new. I would hold off on reporting bugs. Rather, look for other causes.
Harry Mueller



Joined: 17 Oct 2005
Posts: 116

View user's profile Send private message

PostPosted: Fri Oct 21, 2005 8:44 am     Reply with quote

PCM programmer wrote:
You're not using the latest version of the compiler, so reporting a
bug on an older version won't do much good. Also, you're fairly
new. I would hold off on reporting bugs. Rather, look for other causes.

I'm going to pm you on this later.

Asmallri: Thanks a bunch, those suggestion were all better than the solution I came up with.

Cheers....Harry
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