Wednesday, April 29, 2015

MikroC Tutorial - LCD Display [01]


In this lesson you can learn, how to use LCD Display with PIC micro controller. This tutorial created using MikroC Pro v5.6 compiler and Proteus 7.1 simulation software. If you haven't them, you can download from here (software page).

You can solve below problems after complete this lesson, 

  1. How configer lcd display using mikroC ?
  2. How write texts on lcd display ?
  3. How move texts on lcd display ?

Simple Circuit Diagram



Parts list, If you try this on real world,

  • 16F628A PIC
  • 16x2 Lcd Display
  • 10K resistor
  • 10K variable resistor (add this to lcd [VEE] pin for adjust brightness of lcd)


MikroC C code


/*
'*******************************************************************************
'  Lesson 03:
'          LCD Display 01
'  Written by:
'          Hasitha Suneth, 2014.
'  Description:
'          In this lesson, we are going to learn, how to use LCD display with
'          PIC Micro controller.
'  Test configuration:
'    MCU:                        PIC16F628A
'    SW:                           MikroC PRO for PIC 2012 (version v5.6.1)
'  Configuration Word
'    Oscillator:                     4MHz INTOSC Oscillator
'    Watchdog Timer:             OFF
'    Power up Timer:             Disabled
'    Master Clear Enable:       Enabled
'    Browun Out Detect:         Enabled
'    Low Voltage Program:     Disabled
'    Data EE Read Protect:     Disabled
'    Code Protect:                 OFF
'*******************************************************************************
*/
// You can use any Ports or pins for connect LCD.
// LCD module connections
sbit LCD_RS at RA0_bit;                     //PortA.B0 as RS
sbit LCD_EN at RA1_bit;                     //PortA.B1 as EN
sbit LCD_D4 at RB0_bit;                     //PortB.B0 as D4
sbit LCD_D5 at RB1_bit;                     //PortB.B1 as D5
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISA0_bit;        //PortA.B0 Direction
sbit LCD_EN_Direction at TRISA1_bit;        //PortA.B1 Direction
sbit LCD_D4_Direction at TRISB0_bit;        //PortB.B0 Direction
sbit LCD_D5_Direction at TRISB1_bit;        //PortB.B1 Direction
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

// Define Messages

 char text1[] = "Hello World 01";
 char text2[] = "hasisuneth.blogspot.com";
 char text3[] = "THE END";
 int i;
  
 void Move_Delay()                 // Function used for text moving
 {  Delay_ms(100); }               // You can change the moving speed here

void main()

 {
  Lcd_Init();                                             // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);                      // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);           // Cursor off
  do 
    {
          // *** Method 01 ***  //             // Lcd_Out(Row, Column, Text)
    Lcd_Out(1,2,text1);                         // Write message1 in 1st row
    Delay_ms(1000);                            // 1 sec delay
          // *** Method 02 ***  //           // Lcd_Out(Row, Column, "Massage write")
    Lcd_Out(2,5,"METHOD 02");          // Write your message in 2nd row
    Delay_ms(1000);                           // 1 sec delay
    
    Lcd_Cmd(_LCD_CLEAR);                        // Clear display
    Lcd_Cmd(_LCD_CURSOR_OFF);            // Cursor off
    Delay_ms(500);                                       // 0.5 sec delay
          // *** Method 03 ***  //                    //This method can not change culomn
    Lcd_Cmd(_LCD_SECOND_ROW);         // Move cursor to the 2nd row
    Lcd_Out_Cp(" METHOD 03 (row)");      // Write "text" at current cursor position:
    Delay_ms(1000);
          // *** Moving Text *** //
    Lcd_Cmd(_LCD_CLEAR);
    Lcd_Out(1,16,text2);
    Delay_ms(100);
                                                                         // Coding of Moving texts //
    for(i=0; i<38; i++)                                        // Move text to the left 38 times
     {
        Lcd_Cmd(_LCD_SHIFT_LEFT);
        Move_Delay();                                          // Goto void ( Move_Delay )
     }
    for(i=0; i<39; i++)                                        // Move text to the right 39 times
     {
        Lcd_Cmd(_LCD_SHIFT_RIGHT);
        Move_Delay();
     }
    Delay_ms(800);
    Lcd_Cmd(_LCD_CLEAR);
    Lcd_Out(1,5,text3);
    Delay_ms(2000);
    }
  while(1);
  }



Download MikroC Code and Proteus simulation file.
password : hasisuneth.blogspot.com

Monday, April 27, 2015

MikroC Tutorial - Digital I/O [Input][Switches]


In this lesson you can learn, how to use PIC micro controller's digital I/O pins as input pins. This tutorial created using MikroC Pro v5.6 compiler and Proteus 7.1 simulation software. If you haven't them, you can download from here (software page).

You can learn, how to use singal switch, dual switch and push switch with PIC micro controller in this tutorial.

Simple Circuit Diagram 



MikroC C code

/*
'*******************************************************************************
'  Lesson 02:
'          Digital I/O (Switch) [02]
'  Written by:
'          Hasitha Suneth, 2014.
'  Description:
'          In this lesson, we are going to learn, how to use PIC Digital Input
'          pins properly for switches.
'        * Singal Switch
'        * Dual Switch
'        * Push Switch (on/off)
'  Test configuration:
'    MCU:                        PIC16F628A
'    SW:                         MikroC PRO for PIC 2012 (version v5.6.1)
'  Configuration Word
'    Oscillator:                       4MHz INTOSC Oscillator
'    Watchdog Timer:              OFF
'    Power up Timer:               Disabled
'    Master Clear Enable:         Enabled
'    Browun Out Detect:           Enabled
'    Low Voltage Program:       Disabled
'    Data EE Read Protect:       Disabled
'    Code Protect:                  OFF
'*******************************************************************************
*/
int i=0;                     //unsigned short
void main() 
{
 TRISA = 0xFF;          // Port A as Input
 TRISB = 0x00;          // Port B as Output
 PORTB = 0x00;
  do
  {
  //              Dual Switch              //
  //       ***  Method 01  ***        //
 if (Button(&PORTA, 0, 1, 1))         // if (Button(&PORT(input port),(bit number),(time),(active_state))
    { 
    PORTB.B0 = 1;                       //PortB bit0 On
    PORTB.B1 = 0;                       //PortB bit1 Off
    }
    else 
    {
    PORTB.B0 = 0;                       //PortB bit0 Off
    PORTB.B1 = 1;                       //PortB bit1 On
    }

  //       ***  Method 02  ***      //
    if(PORTA.B1==1)                     //If PortA bit 1 is high, Go in to
     {
     PORTB.B2 = 1;
     PORTB.B3 = 0;
     }
    else                                     //If PortA bit 1 is not high, Go in to
    {
     PORTB.B2 = 0;
     PORTB.B3 = 1;
    }
    
  //             Singal Switch            //
  //       ***  Method 01  ***       //

   if (Button(&PORTA, 2, 1, 1))         //If PortA bit 2 is high, Go in to
    { PORTB.B4 = 1; }
   else                                         //If PortA bit 2 is not high, Go in to
    { PORTB.B4 = 0; }

  //       ***  Method 02  ***        //
   if(PORTA.B3==1)
    { PORTB.B5 = 1; }
   else
    { PORTB.B5 = 0; }
    
  //             Push Switch               //
  //       ***  Method 01  ***        //
   if (Button(&PORTA, 4, 1, 1))
   if(i==1)                                           //If 'i' is high, Go in to
    {PORTB.B6 = 0; i=0; Delay_ms(500);} //PortB Bit 6 = low / i= low / Delay 0.5 seconds for press time out
   else
    {PORTB.B6 = 1; i=1; Delay_ms(500);}
  }
 while(1);                              //Endless loop
}


Download MikroC Code and Proteus simulation file.
password : hasisuneth.blogspot.com