pic 16f877 ans lcd 16*2

Thread Starter

zapollon

Joined Jun 1, 2014
3
hello ,
I am new to this forum and I do not speak much anglai, here is my setup is a pic 16f877 connected a variable resistor 2 and 2 and a bargraph LCD 16 * 2 to display the liquid level,

and the code :
Rich (BB code):
unsigned int niv_bac1;
unsigned int niv_bac2;
unsigned char i,j;
unsigned int  niveaux[8] = {127, 254, 382, 510, 638, 776, 894, 1022};
 
unsigned char VnDuNiveau(unsigned int val) {
   unsigned char i = 0;
   while (i < sizeof(niveaux)) {
      if (val < niveaux)
         return i;
      else i++;
   }
   }
 
   void main() {
   // Configuration des pins 2 et 3 en analogique
 
  TRISA  = 0xFF;  // programmtion du PORTA en entrée
  TRISB  = 0;  // programmtion du PORTB en sortie
  TRISC  = 0;  // programmtion du PORTC en sortie
  TRISD  = 0;  // programmtion du PORTD en sortie
  OPTION_REG.NOT_RBPU = 0;
 
  do {
  adcon0=0x80;
  niv_bac1= Adc_Read(0); // lecture du niveau de fluide du bac1
  //portB=niv_bac1>>2;  //Affichage en binaire du niveau de fluide
  j= VnDuNiveau(niv_bac1);
 
   switch ( j ) {
   case 0:
        // Code
        portB = 0x01;
     break;
 
   case 1:
        // Code
        portB = 0x03;
        break;
   case 2:
        // Code
        portB = 0x07;
   break;
 
   case 3:
        // Code
        portB = 0x0F;
   break;
   case 4:
    // Code
    portB = 0x1F;
   break;
   case 5:
    // Code
    portB = 0x3F;
   break;
   case 6:
    // Code
    portB = 0x7F;
   break;
   case 7:
    // Code
    portB = 0xFF;
   break;
 
        }
 
 
 
   adcon0=0x88;                    //du bac1 sur le portB
   niv_bac2= Adc_Read(1);   // lecture du niveau de fluide du bac2
 
 
 
   //PORTC = niv_bac2>>2;  //Affichage en binaire du niveau de fluide
                           //du bac2 sur le portC
    j= VnDuNiveau(niv_bac2);
 
   switch ( j ) {
   case 0:
        // Code
        portC = 0x01;
     break;
 
   case 1:
        // Code
        portC = 0x03;
        break;
   case 2:
        // Code
        portC = 0x07;
   break;
 
   case 3:
        // Code
        portC = 0x0F;
   break;
   case 4:
    // Code
    portC = 0x1F;
   break;
   case 5:
    // Code
    portC = 0x3F;
   break;
   case 6:
    // Code
    portC = 0x7F;
   break;
   case 7:
    // Code
    portC = 0xFF;
   break;
 
        }
               } while(1);
 }

i need the program code for lcd 16*2 to show the result please help me
 

MrChips

Joined Oct 2, 2009
30,802
Before tackling your request, there are ways to eliminate switch/case statements.

If you have eight possible values of j from 0 to 7, you can create an array of eight results, for example:

Rich (BB code):
unsigned char bar[8] = { 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF };

// then all you need to do is

PORTC = bar[j];
 

MrChips

Joined Oct 2, 2009
30,802
Controlling a standard 16 x 2 character LCD is a very common task.
There are library functions that will do this. Or you can create your own code or you can copy code off the internet.
 
Top