Sunday, 19 July 2015

Lesson 6 LCD Scrolling Display With PIC 16F877A (MikroC)

In the previous tutorial, we talked about a 2x16 LCD, the number of pins it has and their various functions. We also discussed on its different connection mode. Do well to go through the previous tutorial (Lesson 5 MikroC) to get acquainted with it.
Here we are going to create a Scrolling Message on an LCD, which is very easy to achieve given the rich set of Library functions in MikroC. Lets get started with the program.

MikroC Code:

/**********************************
Project Name :
   Scrolling display with LCD
Author : Promise
Description :
   This project demonstrate how to Make scrolling Message on an LCD
Test configuration:
   MCU:  PIC 16F877A
   Oscillator: XT 8.0000 MHz, 8.0000 MHz Crystal
***********************************/

// Lcd pinout settings
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

// Pin direction
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections

char txt[] = "WELCOME TO OUR..." ;
char txt1[] = "BLOG!!!" ;
char txt2[] = "www.mechatronicsandyou.blogspot.com" ;


void main() {

 Lcd_Init();                        // Initialize LCD

  Lcd_Cmd(_LCD_CLEAR);          // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);     // Cursor off

Lcd_Out(1,1,txt);              // output display
Lcd_Out(2,6,txt1);
delay_ms(3000);

Lcd_Cmd(_LCD_CLEAR);          // Cursor off

Lcd_Out(1,1,txt2);            // output display
   delay_ms(500);
 
while(1){                     //endless loop
 Lcd_Cmd(_LCD_SHIFT_LEFT);    // shift display to left
 delay_ms(100);
   }
 }


Proteus Schematic:

   
Scrolling display with LCD in 4 bits mode

Lesson 5 LCD Interfacing With PIC 16F877A (MikroC)

LCD Interfacing With PIC 16F877A (MikroC)

Liquid Crystal Display(LCD) is used for displaying messages. In this tutorial we are using 2x16  LCD. It has 2 lines and 40 characters per line. Only the first 16 characters of each line are visible. The LCD  has 14 pins plus 2 additional pins for its backlight.

 
2x16 LCD


  • Vss is the ground pin (0V)
  • Vdd is the power supply (5v)
  • Vee is LCD contrast (0-5V)
  • RS is Register Select
  • R/W is Read/Write pin (allows data to be written to or read from an LCD)
  • E is Enable pin (enables/disable access to LCD)
  • D0 - D7 are data lines 

LCD CONNECTION

It can be connected in two ways :
  • 8 bit Mode
  • 4 bit Mode
In 8 bit mode all the data lines D0 - D7 transfer data (it doesn't save MCU I/O pin).
4 bit mode uses only upper 4 bits (D4 - D7) and the lower data lines are left unconnected. Thus saving MCU I/O pins.
Vee is normally connected to a potentiometer to vary LCD contrast.
R/W is connected to the ground since data is rarely read from the LCD.

MikroC has LCD library functions which makes writing the code very easy. You can check out the library functions and how to use them from MikroC help files. Search for LCD library

**   Ensure to check LCD in the library manager to avoid errors when compiling.
*** For the LCD backlight pins, a limiting resistor should be connected to one of the pins during operation.

MikroC Code;

/**********************************
Project Name :
   LCD Display
Author : Promise
Description :
   This project displays text on the LCD
Test configuration:
   MCU:  PIC 16F877A
   Oscillator: XT 8.0000 MHz, 8.0000 MHz Crystal
***********************************/

// Lcd pinout settings
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D4 at RB4_bit;

// Pin direction
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D7_Direction at TRISB7_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB4_bit;
// End LCD module connections


char txt[]= "WELCOME TO OUR " ;
char txt1[]="BLOG!!!";
void main() {
   Lcd_Init();                        // Initialize LCD

  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  
while(1){                            //endless loop
Lcd_Out(1,1,txt);                   // Output txt
Lcd_Out(2,6,txt1);                  // outut txt1
}

}
Proteus Schematic:

LCD in 4 bits mode




Friday, 17 July 2015

Lesson 4 PWM to Control Brightness of an LED (MikroC)

In Lesson 3, we made mention of how PWM can be used to control LED brightness and motor speed control. In this tutorial we'll see how we can use it to Control the brightness of an LED. We advice that you check out Lesson 3(MikroC) before going through this tutorial in order to have an understanding of PWM. So let's get over to the program.

MikroC Code:
 
     /**********************************
Project Name :
   Using PWM to control LED brightness
Author : Promise
Description :
   This project demonstrate how to use PWM to control the brightness of an LED
Test configuration:
   MCU:  PIC 16F877A
   Oscillator: XT 8.0000 MHz, 8.0000 MHz Crystal
***********************************/


int i;
unsigned short duty_cycle;

void main() {

TRISC = 0x00;                       // PORTC set as output
PORTC.F2 = 0;                      // Set PORTC to Zero(low)
 PWM1_Init(5000);                  // PWM1 Initalized to 5KHz
PWM1_Start();                      // Start PWM1

while(1)  {                       // endless Loop

for(i=0;i<255;i++){               // loop to increase LED brightness

duty_cycle = i;                   // Assign Counter(i) to the duty_cycle
PWM1_Set_Duty(duty_cycle);        // Set duty Cycle
delay_ms(5);
}                                 // end of for-loop
delay_ms(5);                      // delay

for(;i>0;i--){                    // loop to decrease LED brightness

duty_cycle = i;
PWM1_Set_Duty(duty_cycle);
delay_ms(5);
}                                 // end of for-loop
delay_ms(10);
}
}



Proteus Schematic:

     
When Duty Cycle is Almost 0%

When Duty Cycle is Almost 100%

Thursday, 16 July 2015

Lesson3 Push Button & PWM With PIC 16F877A (MikroC)

Pulse Width Modulation (PWM) is a technique where digital input is used to control the amount of power transferred to a load. This can be used to control motor speed, LED brightness etc.
                                                                                                                                                          


  
Modulating the duty cycle varies average DC value of the signal can be varied. The energy delivered depends on the ratio of ON and OFF time as well as the frequency of the pulses.

Pulse Width: refers to the amount of time the signal is high.

Duty Cycle: refers to the ratio of ON time to the total time period. It is expressed in Percent, 0% means signal is OFF, 50% means equal ON and OFF time, 100% means signal is high. Other values can be calculated by:
(Percent / 100) * 255 (for 8 bit Resolution)
for e.g to have a 75% duty cycle, (75/100) *255 = 192

PWM is done with the CCP Module in PIC Microcontrollers. CCP stands for Compare Capture Pulse Width Modulation.
MikroC has a rich set of Library Functions to perform this task easily.

MikroC PWM Functions:
  • PWMx_Init
  • PWMx_Start
  • PWMx_Set_Duty
  • PWMx_Stop
Some MCUs have multiple modules (CCPx) where x stands for the module number. In this case (CCP1,CCP2 etc) and also (PWM1_Init, PWM2_Init, etc) depending on the module you are using.
You can read more about this library functions from MikroC help file.

** Before Using this functions make sure that PWM is checked in the Library manager.
*** Remember to check your datasheet to know the port pin associated with each pwm module

MikroC Code:

/********************************** Project Name : Push Button & PWM with 16F877A Author : Promise Description : This project demonstrate using of push button to vary the duty cycle of PWM on CCP1 (PORTC Bit2) Test configuration: MCU: PIC 16F877A Oscillator: XT 8.0000 MHz, 8.0000 MHz Crystal ***********************************/ unsigned short current_duty; void Init() { CMCON = 0x07; // turn off Comparator ADCON1 = 0x06; // digital input PORTA = 0; TRISA = 255; // configure PORTA pins as input PORTC = 0; // set PORTC to 0 TRISC = 0; // PORTC pins as output PWM1_Init(5000); // Initialize PWM1 module at 5KHz } void main() { Init(); current_duty = 200; // current_duty initial value PWM1_Start(); // start PWM1 PWM1_Set_Duty(current_duty); // Set current_duty for PWM1 do { // endless loop if (PORTA.B0==1) { // button on RA0 pressed Delay_ms(20); // Switch debounce  if (PORTA.B0==1){  
current_duty++; // increment current_duty PWM1_Set_Duty(current_duty); // Set duty to Current Duty delay_ms(10); }
} if (PORTA.B1==1) { // button on RA1 is pressed Delay_ms(20);  if (PORTA.B1==1) {  
current_duty--; // decrement current_duty PWM1_Set_Duty(current_duty); delay_ms(10); } } if(current_duty==255){ current_duty = 200; PWM1_Set_Duty(current_duty); Delay_ms(5); // slow down } } } while (1); }


Proteus Schematic :


Saturday, 4 July 2015

Flow code lesson 1 LED interfacing with PIC 16f877A



In this example we develop a system that accepts a LED to blink and run continuously.
NOTE : The flowcode version used here is (version 5)
  • ·         Run the flowcode application.
  • ·         Click on the OK button when the reminder screen appears.
  • ·         Next you have the option of creating a new flowcode flowchart, or opening an existing one. Select the option of creating a new one.
  • ·         You now have to choose a target PIC chip. The 44-pin Demo Board is fitted with a PIC 16f877a, so choose from that list.
  • ·         A new workspace is created called ‘Main’ opens.  Click and drag icons from the icons Toolbar running down the left hand, edge of the screen to make the flowcode chart shown below. For the moment don’t worry about configuring them – we will do that next.
  • ·         Now add the hardware. Click on the LED icon in the components Toolbar, running just inside the icon Toolbar. You now have the led component in the workspace.
  • ·         Select the LED component in the workspace, click on the drop down menu button and select the component connection option. In the’ connect to port’ menu, the port A is already selected. So therefore the single LED is now connected on the port A bit 0 of the PIC. It should look like the following.
  • ·         Then select the calculation icon and create a new variable constant and name it whatever name you desire ‘led_limt’. Also you can select the variable type you want, byte is the appropriate variable type e to use in this scenario. It must look like the one shown below.


·         Then next you then go to your calculations workspace and equate your variable name to zero. Reason because your led is starting from an OFF position which is zero. Your program must look like the one below.

·         After that has been done you then move to your delay icon add type in the amount of time you want your program to delay depending on your choice, but make it a reasonable delay so the LED blinking can be visible.
·         You then call your component macro so as to tell the LED what to do. You’ll then select ‘LEDON’ just as shown below.
·         You’ll then put a delay icon again so as to give our program the effect of the blinking.
·         Then you call the component macro icon again and set it this time around to ‘LEDOFF’
·         Then you again add your delay icon and put in your desired delay seconds
NOTE: your delay time must be the same throughout for uniformity sake.
·         Now you can simulate your program

You can watch the full video here

Lesson2 Switch Controlled LED (MikroC)

Switch Controlled LED

In this tutorial we're going to show how a push button switch is used to turn ON and OFF an LED. The target MCU is PIC 16F877A. We'll introduce 2 registers here together with the I/O registers discussed in Lesson 1.

  •  It is ADCON1 register and CMCON register.
  •  In PIC 16F877A, PORTA is a 6 bit wide bi-directional port. Apart from RA4 (PORTA BIT 4), Other PORTA pins are multiplexed with analog inputs and the analog VREF input for both the A/D converters and the comparators.

Switches accepts digital inputs, so we'll set the bits of ADCON1 to digital input by writing 0x06 (6 in decimal) to the register

**ADCON1 - Analogue/Digital Control Register 1 ; used to make a PORT Pin with A/D module either digital or analogue
**CMCON - Comparator Control Register ; controls the comparator input and output multiplexers

*** For ADC operation we don't need CMCON Register so we are turning it OFF by writing 0x07 (7        in decimal) to the register.

You can read more about the ADCON1 and CMCON register from the device data sheet, www.microchip.com

CIRCUIT DIAGRAM

The switch can be connected in 2 ways :

  • In Active High Mode - In this mode , the PORT pin is High (Logic 1) when the button is pressed. Here the button is connected between the supply voltage and the PORT pin, a pull down resistor is connected between the PORT pin and ground.                                                         
    PUSH BUTTON ON PORTA BIT 1 CONNECTED IN ACTIVE HIGH MODE


  • In Active Low Mode - When the button is pressed the PORT pin is low (Logic 0). Here the PORT pin is connected through a pull up resistor to the supply voltage, and the button is connected between the PORT pin and ground .
    PUSH BUTTON ON PORTA BIT 1 CONNECTED IN ACTIVE LOW MODE
    **The button connected to the Master Clear Pin (MCLR) is the RESET BUTTON. It is used to RESET the MCU.                                                                                                                             
**Switch Debounce : when a switch closes, the switch makes several contacts quickly(open and closes several times). This contacts can be read by the MCU, thereby causing the wrong switch state to be read. One way of eliminating switch bounce is to delay reading the input after the switch changes its state, say 10ms.
Here is the MikroC code:

/**********************************
Project Name :
   SWITCH CONTROLLED LED
Description :
   This project demonstrate how to controll an LED using a push button switch,the LED is connected to PORTB BIT ZERO
Test configuration:
   MCU:  PIC 16F877A
   Oscillator: XT 8.0000 MHz, 8.0000 MHz Crystal
***********************************/

void main() {
TRISB.B0 = 0;     // PORTB Bit Zero as OUTPUT
TRISA = 0xFF;   // PORTA set as input.FF is hexadecimal equivalent of 255(decimal), 11111111(binary)
CMCON = 0x07;    // Turn OFF Comparator
ADCON1 = 0x06;  // configure  PORTA as digital input
PORTB.B0 = 0;
while(1){
if(PORTA.B1){
delay_ms(10);   // Switch debounce
if(PORTA.B1){
PORTB.B0 = 1;   //turn ON PORTB.B0
}
}
else  {
PORTB.B0 = 0; }   //turn OFF PORTB.B0
}
}

Wednesday, 1 July 2015

Lesson 1 Led Interfacing with PIC 16F877A (MikroC)

Lesson 1
Led Interfacing with PIC 16F877A
Please check the datasheet of the target microcontroller (MCU) to know the Registers associated with it. The data sheet can be downloaded  from www.microchip.com


We will use the Input/Output (I/O) registers very often. Two registers are associated with the I/O port:

  • The Data register - The data register is named after the port it controls. For PIC 16F MCU the data register is called PORT register e.g PORTA,PORTB etc. The data register sets the Port to high (Logic 1) and Low (Logic 0). For example PORTA = 255 sets all the bit of PORTA to high(Logic 1).  PORTA=0 sets all the bit of PORTA to Low(Logic 0). 
  • The Direction Register - The direction register is known as Tri-State Register as the name suggests, it is used to set the direction of the Port. It is written as TRIS together with the Port letter. E.g TRISA,TRISB,TRISC etc. TRISA=255 sets all the bit of PORTA to input. TRISA=0, sets all the bits of PORTA to output.


An LED can be connected in 2 ways to a MCU.
1. In current Sourcing mode - when it is connected in this mode , the LED will be ON when output PORT is at Logic 1.


2. In current sinking mode - when connected in this mode, the LED will be ON when output PORT is at Logic 0.


A MCU can source and  sink 25mA of current and the output voltage of a MCU is 5V.  LED requires about 2V, so it should be connected to a resistor. The value of the resistor can be calculated thus depending on the current you want through the LED. Most LEDs require 10mA of current.


  Resistor Value = ( Output Voltage of MCU (5v) - LED Voltage (2v)) / (LED current)

This is the C source code for LED Interfacing with 16F877A:

/**********************************
Project Name :
   LED BLINKING
Description :
   This project demonstrate blinking of an LED connected to PORTB BIT ZERO and an LED light chaser program
MCU:  PIC 16F877A
Oscillator: XT 8.0000 MHz, 8.0000 MHz Crystal
***********************************/


void led_blink(){     // function that executes LED blinking
 TRISB.B0 = 0;    // sets PORTB Bit Zero to OUTPUT
 while(1)
 {
  PORTB.B0 = 1;   // Turns ON PORTB Bit Zero
  delay_ms(200);
  PORTB.B0 = 0; // turns OFF PORTB Bit zero
  delay_ms(200);
 } // end of while loop
}

void led_light_chaser(){    // function that executes LED light chaser
  int i;
  TRISB = 0;
while(1){
  PORTB = 1;
  delay_ms(100);

for(i=0;i<=7;i++){
  PORTB = PORTB<<1;
  delay_ms(100);
  } // end of for loop
}  // end of while loop
}
void main() {
led_blink();     //LED Blink function call