pic 16f877 ans lcd 16*2

Discussion in 'Embedded Systems and Microcontrollers' started by zapollon, Jun 1, 2014.

  1. zapollon

    Thread Starter New Member

    Jun 1, 2014
    3
    0
    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,
    [​IMG]
    and the code :
    Code ( (Unknown Language)):
    1. unsigned int niv_bac1;
    2. unsigned int niv_bac2;
    3. unsigned char i,j;
    4. unsigned int  niveaux[8] = {127, 254, 382, 510, 638, 776, 894, 1022};
    5.  
    6. unsigned char VnDuNiveau(unsigned int val) {
    7.    unsigned char i = 0;
    8.    while (i < sizeof(niveaux)) {
    9.       if (val < niveaux[i])
    10.          return i;
    11.       else i++;
    12.    }
    13.    }
    14.  
    15.    void main() {
    16.    // Configuration des pins 2 et 3 en analogique
    17.  
    18.   TRISA  = 0xFF;  // programmtion du PORTA en entrée
    19.   TRISB  = 0;  // programmtion du PORTB en sortie
    20.   TRISC  = 0;  // programmtion du PORTC en sortie
    21.   TRISD  = 0;  // programmtion du PORTD en sortie
    22.   OPTION_REG.NOT_RBPU = 0;
    23.  
    24.   do {
    25.   adcon0=0x80;
    26.   niv_bac1= Adc_Read(0); // lecture du niveau de fluide du bac1
    27.   //portB=niv_bac1>>2;  //Affichage en binaire du niveau de fluide
    28.   j= VnDuNiveau(niv_bac1);
    29.  
    30.    switch ( j ) {
    31.    case 0:
    32.         // Code
    33.         portB = 0x01;
    34.      break;
    35.  
    36.    case 1:
    37.         // Code
    38.         portB = 0x03;
    39.         break;
    40.    case 2:
    41.         // Code
    42.         portB = 0x07;
    43.    break;
    44.  
    45.    case 3:
    46.         // Code
    47.         portB = 0x0F;
    48.    break;
    49.    case 4:
    50.     // Code
    51.     portB = 0x1F;
    52.    break;
    53.    case 5:
    54.     // Code
    55.     portB = 0x3F;
    56.    break;
    57.    case 6:
    58.     // Code
    59.     portB = 0x7F;
    60.    break;
    61.    case 7:
    62.     // Code
    63.     portB = 0xFF;
    64.    break;
    65.  
    66.         }
    67.  
    68.  
    69.  
    70.    adcon0=0x88;                    //du bac1 sur le portB
    71.    niv_bac2= Adc_Read(1);   // lecture du niveau de fluide du bac2
    72.  
    73.  
    74.  
    75.    //PORTC = niv_bac2>>2;  //Affichage en binaire du niveau de fluide
    76.                            //du bac2 sur le portC
    77.     j= VnDuNiveau(niv_bac2);
    78.  
    79.    switch ( j ) {
    80.    case 0:
    81.         // Code
    82.         portC = 0x01;
    83.      break;
    84.  
    85.    case 1:
    86.         // Code
    87.         portC = 0x03;
    88.         break;
    89.    case 2:
    90.         // Code
    91.         portC = 0x07;
    92.    break;
    93.  
    94.    case 3:
    95.         // Code
    96.         portC = 0x0F;
    97.    break;
    98.    case 4:
    99.     // Code
    100.     portC = 0x1F;
    101.    break;
    102.    case 5:
    103.     // Code
    104.     portC = 0x3F;
    105.    break;
    106.    case 6:
    107.     // Code
    108.     portC = 0x7F;
    109.    break;
    110.    case 7:
    111.     // Code
    112.     portC = 0xFF;
    113.    break;
    114.  
    115.         }
    116.                } while(1);
    117.  }[/i]

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

    Moderator

    Oct 2, 2009
    12,449
    3,364
    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:

    Code ( (Unknown Language)):
    1.  
    2. unsigned char bar[8] = { 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF };
    3.  
    4. // then all you need to do is
    5.  
    6. PORTC = bar[j];
    7.  
    8.  
     
  3. zapollon

    Thread Starter New Member

    Jun 1, 2014
    3
    0
    and how to display on lcd?
    thanks
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,364
    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.
     
  5. zapollon

    Thread Starter New Member

    Jun 1, 2014
    3
    0
    i dont found the code in the internet , you can give me the code for my lcd ?
    thanks
     
  6. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,364
Loading...