LCD and MikroC

Discussion in 'The Projects Forum' started by PlayaSlaya, Apr 19, 2010.

  1. PlayaSlaya

    Thread Starter Member

    Jun 22, 2009
    16
    0
    I'm interested in using an LCD display in conjunction with a PIC16 micro for a university project. I haven't used such devices before and hence have extremely little experience with both the PIC and LCD.

    I came across the following code example. It was programmed using MikroC and a PIC16F887. I am however only interested in the LCD part, and not the A/D part.

    Code ( (Unknown Language)):
    1. /*Header******************************************************/
    2.  
    3. // LCD module connections
    4. sbit LCD_RS at RB4_bit;
    5. sbit LCD_EN at RB5_bit;
    6. sbit LCD_D4 at RB0_bit;
    7. sbit LCD_D5 at RB1_bit;
    8. sbit LCD_D6 at RB2_bit;
    9. sbit LCD_D7 at RB3_bit;
    10. sbit LCD_RS_Direction at TRISB4_bit;
    11. sbit LCD_EN_Direction at TRISB5_bit;
    12. sbit LCD_D4_Direction at TRISB0_bit;
    13. sbit LCD_D5_Direction at TRISB1_bit;
    14. sbit LCD_D6_Direction at TRISB2_bit;
    15. sbit LCD_D7_Direction at TRISB3_bit;
    16. // End LCD module connections
    17.  
    18. unsigned char ch;                    //
    19. unsigned int adc_rd;                 // Declare variables
    20. char *text;                          //
    21. long tlong;                          //
    22.  
    23. void main() {
    24.     INTCON = 0;                      // All interrupts disabled
    25.     ANSEL = 0x04;                    // Pin RA2 is configured as an analog input
    26.     TRISA = 0x04;
    27.     ANSELH = 0;                      // Rest of pins are configured as digital
    28.  
    29.     Lcd_Init();                      // LCD display initialization
    30.     Lcd_Cmd(_LCD_CURSOR_OFF);        // LCD command (cursor off)
    31.     Lcd_Cmd(_LCD_CLEAR);             // LCD command (clear LCD)
    32.  
    33.     text = "mikroElektronika";       // Define the first message
    34.     Lcd_Out(1,1,text);               // Write the first message in the first line
    35.     text = "LCD example";            // Define the second message
    36.     Lcd_Out(2,1,text);               // Define the first message
    37.  
    38.     ADCON1 = 0x82;                   // A/D voltage reference is VCC
    39.     TRISA = 0xFF;                    // All port A pins are configured as inputs
    40.     Delay_ms(2000);
    41.  
    42.     text = "voltage:";               // Define the third message
    43.  
    44.     while (1) {
    45.         adc_rd = ADC_Read(2);        // A/D conversion. Pin RA2 is an input.
    46.         Lcd_Out(2,1,text);           // Write result in the second line
    47.         tlong = (long)adc_rd * 5000; // Convert the result in millivolts
    48.         tlong = tlong / 1023;        // 0..1023 -> 0-5000mV
    49.         ch = tlong / 1000;           // Extract volts (thousands of millivolts)
    50.                                      // from result
    51.         Lcd_Chr(2,9,48+ch);          // Write result in ASCII format
    52.         Lcd_Chr_CP('.');
    53.         ch = (tlong / 100) % 10;     // Extract hundreds of millivolts
    54.         Lcd_Chr_CP(48+ch);           // Write result in ASCII format
    55.         ch = (tlong / 10) % 10;      // Extract tens of millivolts
    56.         Lcd_Chr_CP(48+ch);           // Write result in ASCII format
    57.         ch = tlong % 10;             // Extract digits for millivolts
    58.         Lcd_Chr_CP(48+ch);           // Write result in ASCII format
    59.         Lcd_Chr_CP('V');
    60.         Delay_ms(1);
    61.     }
    62. }
    I tried the same code in MikroC on a PIC16F690, except changed the PORTB configuration to PORTC, both in software and hardware. In the code i changed RB to RC and TRISB to TRISC.

    As i understand the code should still work, or am i missing something. Are there any modifications i need to make to the code or to the hardware?
     
  2. PlayaSlaya

    Thread Starter Member

    Jun 22, 2009
    16
    0
    Heres the schematic that was included with the code:
    On my circuit i changed the connections from RB0-RB5 to RC0-RC5.

    Also as far as i know MikroC sets up all the necessary configurations for the PIC using the wizard. The clock was set to 8 MHZ, if that applies at all.
     
Loading...