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

Bargraph on LCD & Pic16f877a

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



Joined: 17 Oct 2005
Posts: 68
Location: Brisbane

View user's profile Send private message

Bargraph on LCD & Pic16f877a
PostPosted: Mon Feb 20, 2006 12:20 am     Reply with quote

The code on bargraph in the code library section work fine but it leaves vertical bars on the first & last last ends of the pixel.
Code:
#include <16F877.h>
#device adc=10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#include <LCD.c>
#include <LCD_bargraph.c>

/*  bargraph_test.c  /***LCD_bargraph.c************************************************************
Purpose:  An enhancement to the standard capabilities of LCD.c that enables
          one to pass a value to be bargraphed on a LCD.
Prerequisites:  Must also include LCD.c in main program or add this code
                to your own copy of LCD.c
//****************************************************************************
* void init_user_chars
* Purpose:   Set pointer in LCD memory to CGRAM and load first 8 bytes with
*         user defined characters. This function must be called once before
*         a bargraph is implemented. At exit it resets pointer to line 1,
*/
void init_user_chars(void) {
   // User defined characters are 5 x 7 but remember the cursor on 8
   int8 i;  //counting variable
 // ** setup an array of special characters to form the bars **** 
   // char 0x00 left vertical line of bar
    const char user_char[24] = {0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00,
   // char 0x01 two units of bar
                          0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x00,
   // char 0x02 three units of bar (full bar)
                          0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x00};

   lcd_send_byte(0,64); // Sets ram address to beginning of CGRAM area

   for (i=0; i<24;i++)  // fill in CGRAM with the data from array above.
      lcd_send_byte(1,user_char[i]);

   lcd_gotoxy(1,1); // Resets address to top line / position 1
}

/******************************************************************************
*     void bargraph(int8 percent)
*  Purpose:  This function generates the bar graph indicator.
*  Written for a 2 X 16 Intelligent LCD Display with HD44780 type controller.
*  It expects a 0 to 48 value input to generate 0 to full width bar.
*  B_Wdth could be modified for larger LCD's.
*/
void bargraph(BYTE percent) {
//' ** Declare Variables **
   int16 Bar_Val;        // Value to be graphed.
   int Bars;          // Number of full ||| bars to draw.
   int Balance;      //   Balance left after all |||s are drawn.
   int i;           // counting variable
//' ** Declare Constants **
   int const B_Wdth = 16;             // Max width in characters of bar.
   int const Maxbar = B_Wdth * 3;   //   Max bar counts.
// meat of code **
   Bars = percent/3;        // how many full bars to print?
   Balance = percent%3;    // are partial bars required?
   lcd_gotoxy(1,2);       // go to beginning of line 2
  for (i=0; i<Bars;i++)
   lcd_send_byte(1,0x02);  // print the bars
  switch (Balance) {
      case 0: break;
      case 1: lcd_send_byte(1, 0x00);
              break;
      case 2: lcd_send_byte(1, 0x01);
              break; }
}
/*  bargraph_test.c  *******************************************************
Authors:  John Hansknecht and Daniel Charles
Purpose:  This program demonstrates the use of the LCD_bargraph.c in a real
          application.  A PIC16F877 is set up to write to a 2X16 LCD and read
          an incoming voltage on an ADC input pin.  The ADC value is written
          on the top line and the bottom line displays it in bargraph mode.
*/         

long value,Temp;
int A;
void main() {
   setup_adc_ports(AN0);
   setup_adc(DIV_32);
   lcd_init();
   delay_ms(2000);
   init_user_chars();    // loads special characters into LCD CGRAM for bargraph
   set_adc_channel(0);
   delay_us(20);

   while(1)
   {
      value=read_adc(); //read the ADC
      delay_ms(20);     //pause so we don't blur the LCD display   
     Temp= (value-559L)/2;  //Calculate the temperature in degrees
      A=Temp;         //calculate the full scale for our bar
      lcd_putc('\f');   //clear the LCD
      printf(lcd_putc, "value = %lu \n",Temp); //Print top line with ADC value
      bargraph(A);      //print line 2 with the bargraph output.
   }
}


For examples if temp=25oC,then 8 full bars are printed plus the remaining 1 bar.If the temperature increases the numbers printed increases. But when temperature is dropping, two vertical bars areleft at the start and end of the of a pixel i.e.
Code:
.             .
.             .
.             .
.             .
instead of a blank bar?
Anyone have any idea? coz you won't want have a bargraph that leaves some bars when values are dropping.it suppose to clear them when there are no values.
Thanks
jecottrell



Joined: 16 Jan 2005
Posts: 559
Location: Tucson, AZ

View user's profile Send private message

PostPosted: Mon Feb 20, 2006 8:18 am     Reply with quote

Kel,

I'm not sure if your problem is with "remnants" left over as the value decreases or if it is a problem displaying the bar? I remember when I modified the code for a battery indicator I believe there were some missing characters. Try this:

Code:

/*******************************************************************************
* void generatebar(int8 percent)
* Purpose:  This function generates the text line for the bar graph indicator.
            This will generate a 16 character bar graph.
*/

void generate_bar(int16 percent) {

   int16 solid;
   int16 last;
   int8  i = 0;

   solid = (percent * 16) / 100;                  // number of solid characters to display
   last =  ((percent * 16) - (solid * 100)) / 10; // work out the last character
   last /= 2;

   if (solid) {

      for (i = 0; i < solid; i++)

        bar[i] = 0x05;
   }

   if (last) {
      last--;
      bar[i] = last;        // select last character
      i++;
   }

   bar[i] = 0x00;           // null terminator

}



/*****************************************************************************
* init_user_chars
* Purpose:   Set pointer in LCD memory to CGRAM and load first 8 bytes with
*         user defined characters. Reset pointer to line 1, position 1
*/
void init_bar_graph_chars(void) {
   // User defined characters 5 x 7 but remember the cursor on 8
   int8 i;

    const char user_char[49] = {

    // char 0x00 low battery indicator
    0x0E, 0x1B, 0x11, 0x11, 0x11, 0x11, 0x11, 0x1F,

    // char 0x01 20% solid vertical for bar graph
    0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,

    // char 0x02 40% solid vertical for bar graph
    0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,

    // char 0x03 60% solid vertical for bar graph
    0x00, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C,

    // char 0x04 80% solid vertical for bar graph
    0x00, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E,

    // char 0x05 solid block for bar graph
    0x00, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F,

    };

   lcd_send_byte(0,64); // Sets ram address

   for (i = 0; i < 49; i++)
      lcd_send_byte(1,user_char[i]);

   lcd_gotoxy(1,1); // Resets address to top line / position 1
} // init_user_chars
kel



Joined: 17 Oct 2005
Posts: 68
Location: Brisbane

View user's profile Send private message

16f877a bargraph problem
PostPosted: Mon Feb 20, 2006 9:29 pm     Reply with quote

Indeed the problem is with "remnants" left over as the values decreases.
By the way did you define
Code:
bar[]
as static global?
tomal



Joined: 20 Nov 2011
Posts: 9

View user's profile Send private message

PostPosted: Thu Jul 25, 2013 11:33 am     Reply with quote

I'm suffering from the same "remnants" (left over as the values decreases) issues for the bargraph.
Anybody have a workaround?

thanks
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