Skip to main content

PIC16F887 877 programming in C Tutorial 5 LCD Interfacing


LCD Interfacing:
In this tutorial i will show you how to interface 16x2 LCD with micro-controller. 16x2 means there are two rows and each row contain maximum 16 characters. 
For more detail refer to the LCD datasheet, which you are using.
Basic Connection:
Applies 5v to pin 2 and gnd to pins 1 & 5. Use variable resistor at pin 3 to set contrast. Pins 7 to 14 are the data pins,, used to send/rec data. Pin 6 is of enable; every time when you write to lcd you should have to give high to low, to this pin. pin 4 is register select pin use to give commands like clear, home etc.  

In this tutorial i will interface lcd in 4-bit instead of 8-bit, so we only required four data pins.

Code:
Lets write a code that will display the motor status and its direction; it will be fun!!!!

Requirements:
Design a motor controller circuit using l298 and display its status on lcd. LCD is connected to portb and motor controller circuit is at portd. Required two switches to change motor direction; if both are open/close the motor should remain off. Also the switches are connected to portd.

// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;

// End LCD module connections
char txt3[] = "Motor Direction";
char txt2[] = "Clock Wise ";
char txt1[] = "Counter C.W";
char txt[]  = "Motor Off  ";

void main() {
/////// comment for 877//////////////////
     ansel=0;
     anselh=0;
    
     c1on_bit=0;
     c2on_bit=0;
 ///////////////////////////////////////
     trisd0_bit=0;
     trisd1_bit=0;
     trisd2_bit=0;
    
     trisd3_bit=1;                 // CW dir
     trisd4_bit=1;                  //CCW dir

     Lcd_Init();                    // lcd ini
     Lcd_Cmd(_LCD_CLEAR);           //lcd clear
     Lcd_Cmd(_LCD_CURSOR_OFF);
     while(1){

      Lcd_Out(1,1,txt3);             //1row 1col
      delay_ms(500);
      if(rd3_bit){
        Lcd_Out(2,1,txt2);
        rd1_bit=0;
        rd0_bit=1;
      }
       
      if(rd4_bit){
        Lcd_Out(2,1,txt1);
        rd0_bit=0;
        rd1_bit=1;
      }
       
      if(rd3_bit & rd4_bit){             //invalid selction
        lcd_out(2,1,txt);
        rd0_bit=0;                       //to stop motor
        rd1_bit=0;
      }
       
      if(!rd3_bit & !rd4_bit){           //inv sec
        lcd_out(2,1,txt);
        rd0_bit=0;
        rd1_bit=0;
      }
      }

}


This is the simple code according to out requirement. To interface the LCD you must have to specify the port (port i/o), as well as the sfr of that port,  you are using 'sbit LCD_RS at RB4_bit' and 'sbit LCD_RS_Direction at TRISB4_bit' doing this job. As we are using LCD in four bit so, just specify the four data pins (pins 11 to 14 of LCD), E and RS pins.

txt1 to txt3 are the character array containing the message you want to display. Pins 0, 1, and 2 of portD is configure as o/p whereas pins 3, and 4 as i/p. L298 is connected to the pins0-2 and switches are at pins 3 and 4.

After setting the pins for LCD we used Lcd_Init(); a mikroc lcd library function, to initialize the LCD. Lcd_Cmd(char) is used to send commands to LCD like clear screen, curs0r off etc.
Lcd_Out(char row, char column, char *text); Prints text on Lcd starting from specified position; 1, 1 means first row and first column. Then a simple logic off switches and their functions.

L298 Connections:
Important thing is the connection of l298, as we are using out1 & out2 so we have to use in1 & in2, also have to enable ena pin for these selections. Use a 10 ohm resistor to connect between sensa pin and ground. Apply 9/12v to VCC, pin 9, depending on your motor voltage and 5v at pin 4, VC. To change to direction just change the bit value of in1 and in2; or in other words swap these values. If the both have same value, 0 or 1, motor will not rotate; as potential is same, current can't flow!!.

Schematic:
motor controller

Comments

Popular posts from this blog

Power Amplifier with voltage regulator 4 × 50 Watt TDA8588

Power Amplifier with voltage regulator 4 × 50 Watt TDA8588    The TDA8588 is a multiple voltage regulator combined with four independent audio power amplifiers configured in bridge tied load with diagnostic capability. The output voltages of all regulators except regulators 2 and 3 can be controlled via the I2C-bus. However, regulator 3 can be set to 0 V via the I2C-bus. The output voltage of regulator 2 (microcontroller supply) and the maximum output voltage of regulator 3 (mechanical digital and microcontroller supplies) can both be either 5 V or 3.3 V depending on the type number. The maximum output voltages of both regulators are fixed to avoid any risk of damaging the microcontroller that may occur during a disturbance of the I 2C-bus. The amplifier diagnostic functions give information about output offset, load, or short-circuit. Diagnostic functions are controlled via the I2C-bus. The TDA8588 is protected against short-circuit, over-temperature, open ground and open VP connectio

Digital Voltmeter Circuit with ICL7107

Description. The circuit given here is of a very useful and accurate digital voltmeter with LED display using the ICL7107 from Intersil. The ICL7107 is a high performance, low power, 3.5 digit analog to digital converter. The IC includes internal circuitry for seven segment decoders, display drivers, reference voltage source and a clock. The power dissipation is less than 10mW and the display stability is very high. The working of this electronic circuit is very simple. The voltage to be measured is converted into a digital equivalent by the ADC inside the IC and then this digital equivalent is decoded to the seven segment format and then displayed. The ADC used in ICL7107 is dual slope type ADC. The process taking place inside our ADC can be stated as follows. For a fixed period of time the voltage to be measured is integrated to obtain a ramp at the output of the integrator. Then a known reference voltage of opposite polarity is applied to the input of the integrator and allowed to r

Akira HTS 38DVD HTS – Circuit Diagram Home Theater system

Home theater system - AkiraHTS38DVD   Used ICs: KA5H0165RN (SMPS control) – LC6883807 – BU1923F – BD4740G – STA505 – PS9702B – WW8721 – LA1844M Exploded Circuit Diagram Click on the schematics to magnify google.com/+GopakumarGopalan