Sunday 7 June 2015

7 Segment display Interfacing with 8051

7 Segment Display Interfacing with 8051


A seven-segment display (SSD), or seven-segment indicator, is a form of electronic display device for displaying decimal numerals that is an alternative to the more complex dot matrix displays.
Seven-segment displays are widely used in digital clocks, electronic meters, basic calculators, and other electronic devices that display numerical information.

The seven elements of the display can be lit in different combinations to represent the arabic numerals. Often the seven segments are arranged in an oblique (slanted) arrangement, which aids readability. In most applications, the seven segments are of nearly uniform shape and size (usually elongated hexagons, though trapezoids and rectangles can also be used), though in the case of adding machines, the vertical segments are longer and more oddly shaped at the ends in an effort to further enhance readability.

The numerals 6, 7 and 9 may be represented by two or more different glyphs on seven-segment displays.



The seven segments are arranged as a rectangle of two vertical segments on each side with one horizontal segment on the top, middle, and bottom. Additionally, the seventh segment bisects the rectangle horizontally. There are also fourteen-segment displays and sixteen-segment displays (for full alphanumerics) , however, these have mostly been replaced by dot matrix displays.

The segments of a 7-segment display are referred to by the letters A to G, where the optional DP decimal point (an "eighth segment") is used for the display of non-integer numbers.



Program to interface single seven segment

CODE

#include<reg51.h>
int main()

{
unsigned int a[10]={0X3F,0X06,0X5B,0X4F,0X66,0X6D,0X7D,0X07,0X7F,0X6F},i,j;  // Hex values corresponding to digits 0 to 9

while(1)
{
for(i=0;i<=10;i++)
{
P2=a[i];
for(j=0;j<33000;j++);    // Time delay
}
}
}

PROTEUS DIAGRAM





For more information contact me at chandershh@gmail.com.

Sunday 24 May 2015

Pulse Width Modulation

Pulse-width modulation (PIC16F877A)

In this tutorial we are using PIC 16F877A for demonstrating PWM generation using CCP module. PIC 16F877A contains two CCP modules.





Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off.

In creating pulse with various widths for PWM two factors are important.

1.    Period  of the pulse.
2.    Its Duty Cycle.

DC = Duty Cycle is pulse that stays high relative to entire period.
It is stated in the form of percentages(%).

For example = Pulse with a 4ms,2ms period that stays HIGH for 1ms has DC of 25%.
                       
                     (1ms/4ms=25%)
                     (1ms/2ms=50%)….







PERIOD OF PWM

Frequency  of PWM is a fraction of the Fosc, the crystal frequency.

PIC16F877A microcontroller is used here  PWM control.

Use PR2 register to set the PWM period .

Tpwm=[(PR2) +1]4xNxTosc.
Tosc=1/Fosc,the crystal frequency.
Tpwm=Desired PWM period.
N=Prescaler.


SETUP FOR PWM OPERATION

The following steps should be taken when configuring
the CCP module for PWM operation:

1. Set the PWM period by writing to the PR2 register.
2. Set the PWM duty cycle by writing to the
CCPR1L register and CCP1CON<5:4> bits.
3. Make the CCP1 pin an output by clearing the
TRISC<2> bit.
4. Set the TMR2 prescale value and enable Timer2
by writing to T2CON.
5. Configure the CCP1 module for PWM operation.


 Note 2 : All PWM modules in a PIC Microcontroller uses Timer 2 for its operation, so you cannot set different frequencies for different PWM modules.
In Pulse Width Modulation mode, the CCPx pin produces up to a 10-bit resolution PWM output. The PWM duty cycle is specified by writing to the CCPR1L register and to the CCP1CON<5:4> bits. Up to 10-bit resolution is available. The CCPR1L contains the eight MSbs and the CCP1CON<5:4> contains the two LSbs. This 10-bit value is represented by CCPR1L:CCP1CON<5:4>.


CODE
#include<pic.h>
#include<htc.h>
#define _PIC16F877A_H _CONFIG(WDTE_OFF & FOSC_HS & PWRTE_OFF & LVP_OFF & BOREN_OFF);

void main()
{
 PR2=0xFF;                            // setting minmum frequency i.e 255.
CCPR1L=80;                         // 8 upper bit resolution or PWM duty cycle
TRISC2=0;                             //setting CCP1 as output
CCP1CON=0x3C;                 //2 lower bit resolution
T2CON=0x01;                       // setting prescaler.
TMR2ON=1;                         //timer2 on
{
while(!TMR2IF) ;                  //checking timer2 flag overflow 
        TMR2IF=0;                   //reset the timer 2 flag to 0
    }
}

NOTE: By changing the the value of CCPR1L you can change the PWM duty cycle.

PROTEUS DIAGRAM


 WAVEFORM


 For more information contact me at chandershh@gmail.com.








Sunday 17 May 2015

Blinking LED using PIC Microcontroller with Hi-Tech C

Blinking LED using PIC Microcontroller with Hi-Tech C


A lot of you may heard about microcontrollers and its applications. Well it is a bit difficult to start learning microcontrollers. And the guides and tutorials also do not start from zero level which makes learning far more difficult than anticipated. I have tried to start from zero level in here also. All you need is the simplest knowledge of electronics or digital circuits. But you surely should have a decent knowledge of C language.
The software for programming Microchip microcontrollers comes by the name MPLAB IDE. MPLAB is the name of the software and IDE stands for Integrated Development Environment. IDE means that the software itself has all or most of the features that is needed. MPLAB IDE can perform the following operations :
  • Source code editing
  • Project management
  • Machine code generation from Assembly
  • Device simulation
  • Device emulation
  • Device programming
Hence it is called an Integrated Development Environment.
Some things should be clear before starting the tutorial. We can write programs for the PIC microcontrollers in MPLAB but the syntax and code depends upon the library, which may be different from family to family and even a lot of libraries are available for the same family of microcontrollers too.
We are discussing about the Hi-Tech C Compiler library (HTC) for MPLAB in this tutorial. It is used for programming the microcontrollers of series 16F which will be enough for the beginner users. Now if you want to have very high performance applications through microcontrollers then you need to go to higher and more powerful family of microcontrollers like 18F, 24F series and for them the programming library is different. But as the 16F series microcontrollers are 8 bit and support clock rates up to 20 MHz, they are useful in most of our non-commercial applications.
So let’s cut directly to the chase. The software MPLAB IDE comes with integrated HTC compiler library from the version 8.56 on wards.
Let’s start our first project, Blinking LED using PIC 16F877A. 16F877A is a very commonly used PIC microcontroller.
  • Download and Install MPLAB IDE with Hi-Tech C compiler
  • Open MPLAB IDE
  • Click on Project >> Project Wizard
Creating New Project - MPLAB
Creating New Project – MPLAB
  • Click on Next
  • MPLAB - Selecting Microcontroller
    MPLAB – Selecting Microcontroller
    • Select PIC 16F877A and click Next
    MPLAB - Selecting Language Toolsuite
    MPLAB – Selecting Language Toolsuite
    • Select Hi-Tech C compiler as show above and click Next
    MPLAB - Giving Project path and File name
    MPLAB – Giving Project path and File name
    • Select Project path, give a file name and click Next
    MPLAB - Adding files to Project
    MPLAB – Adding files to Project
    • Add required files to Project (Not required here) and click Next
    MPLAB - Completing the Project Creation
    MPLAB – Completing the Project Creation
    • Click Finish to complete project creation
    MPLAB - After Creating Project
    MPLAB – After Creating Project
    Next step is writing the program. All the information about writing programs which include its commands, operations and the microcontroller registers are available on the datasheet and Hi-Tech Toolsuite guide. But I will also try to make you understand the first steps so that the datasheet and guide may prove useful. You can start making your own programs at the end of this tutorial.
    Now let us view a program for 16F877A where 8 LED are connected at the PORT B (8 pins of port B, from pin no 33-40). This programs blinks the LEDs which means the LEDs remain on for a second and off for another second.

    Hi-Tech C Code

    #include <htc.h>
    #define _XTAL_FREQ 8000000
    void main()
    {
      TRISB=0X00;
      PORTB=0X00;
      while(1)
      { 
        PORTB=0XFF;
        __delay_ms(1000);
        PORTB=0X00;
        __delay_ms(1000);
      }
    }
    This is the core style of writing the microcontroller program. As you can see, the style is completely similar to that of the normal C programs. Just some additional keywords here. Remember that all the syntax and mathematical and logical operations supported by stdio and conio libraries are accepted here in htc library with some additional ones also.
    1. In the first line, we start the program by including the library. This step is nothing new.
    2.  Now we need to define the frequency of oscillator used in the system. This is the style of defining the oscillator frequency. The value 8000000 means its frequency is 8MHz.
    3. Start of the main program code
    4. TRIS is the command used for initializing the ports of the microcontroller. TRISB is an 8 bit register, in fact every word written in capital represents a register inside the microcontroller and you can get all the information about the register in the datasheet. Using this line will start the use of PORT B and if the value is ‘1’ in the respective register bit, that pin will be made input and if the value is ‘0’, the pin will be made output. One more thing, the use of ‘0x’ in that line represents that we are giving the data in hexadecimal number system. You can use ‘0b’ if you wish to give the data in binary and use nothing if you use decimal number system. So this line makes each 8 lines of port B output. Other way to give the same command is: TRISB=0; (decimal) and TRISB=0b00000000; (binary).
    5. PORTB is also a register like TRISB. This register passes the value out of port B. This means PORTB=0x00 will give low output from each eight line of port B. This is equivalent to clearing of port B at the start from any stray values. This line is not needed because there are no stray values at the start.
    6. Start of while loop. We write the entire program inside the while loop. After all initializations have been done, the main code is written here. This is done because this segment of program has to repeat over and over but the initializations do not need to be repeated. That is why the main program is written inside the while loop.
    7. PORTB is also a register like TRISB. This register passes the value out of port B. This means PORTB=0xff will give high output from each eight line of port B.
    8.  This line is written to have a delay of 1000ms. So high logic will pass from port B pins for 500ms.
    9. This line will make the output from port B get down to 0V, ie logic ‘0’. It is to be considered that until the value in PORTB or any other port register is changed, the output from the corresponding port will also not change. So the output will drop from 5V (logic 1) to 0V (logic 0) only when this line is executed.
    10. Again a delay of 1000ms.
    11. End
    So you can now understand why the LEDs connected to the port B will blink. For half a second, 5V or logic 1 is coming from port B and for the other half 0V or logic 0 is coming. See how easy it is to do anything using a microcontroller!
    • Go to File-> New, write the program and save as a C file (*.c).
    MPLAB - After Writing and Saving the Program
    MPLAB – After Writing and Saving the Program
    • Add this C file to Source group, Right Click on Source File >> Add Files
    Adding Program C file to Source Group
    Adding Program C file to Source Group
    • Set the Configuration Bits using the Configuration bits tool. Click on Configure >> Configuration Bits
    Configuration Bits - MPLAB
    Configuration Bits – MPLAB

    • Build the project to generate hex file. Click on Project >> Build
    MPLAB - After Building the Program
    MPLAB – After Building the Program
    This was just a basic demonstration. In the same manner, there are registers for numerous operations in the microcontroller. You have to set the register and provide some command and it will be carried out. PIC 16F877A also has general purpose resisters and RAM so you can even use a lot of variables and constants. It supports floating point arithmetic and ASCII values also. Programming in Hi-Tech C is just like programming in any other C compiler with some additional commands.

    Circuit Diagram


  • Blinking LED using PIC Microcontroller – Circuit Diagram
    8MHz crystal is used to provide the required clock for the PIC 16F877A microcontroller. 22pF capacitors are used to stabilize the oscillation of the crystal. The first pin of the microcontroller (MCLR) is the Reset pin (stands for Memory Clear) which is tied to Vdd since it is an active low input. LEDs are connected to PORTB via 470Ω resistors to limit current through them.
    You can simulate the working using Proteus.
  • The above generated hex file doesn’t contain Configuration Bits, we should use the Export option in MPLAB to export the hex file with configuration bits.
    • Click on File >> Export
    Exporting Hex File - MPLAB
    Exporting Hex File – MPLAB
    • Click OK
    Exporting Hex File - MPLAB
    Exporting Hex File – MPLAB
    • Give the file name and Click Save.
    Exporting Hex File - MPLAB
    Exporting Hex File – MPLAB
    • Now burn this exported hex file to the microcontroller
    You can download Hi-Tech C files and Proteus files here…
    Blinking LED PIC Hi-Tech C
For more information contact me at chandershh@gmail.com.

Thursday 5 March 2015

Making a Simple PIC Programmer.


In this tutorial we will make a simple serial port based programmer for PIC microcontrollers. I have tried several easy to make programmers and software, and here I a presenting the programmer that worked the best. The design is based on JDM. The software we will use is PICPgm by Christian Stadler. I liked the performance of the software, its fast and easy to use. The programmer will make uses the COM port of Computer for communication.



Components required.

S.No
Item
Value/Part No
Qty
01
Transistor
BC337-40 or BC337-25
2
02
Capacitor Electrolytic
100uF 16VDC
2
03
Zener Diode
5.1v 0.5Watt
1
04
Zener Diode
6.2v 0.5Watt
1
05
Diode
1N4148
4
06
Resistor
1.5K
1
07
Resistor
10K
1
08
LED
RED Colour
1
09
DB9 Female Connector with Cover
-
1
10
6 PIN Female Connector with Wire
-
1
11
Veroboard, Wires etc
-
-

Circuit Diagram

Now assemble the circuit as shown below in a piece of veroboard.

Fig. : Simple Serial Port Based PIC Programmer.

So our PIC programmer will have two interface
  1. A Serial Interface for Connecting it with PC
  2. A 6 PIN ICSP Connecter – This will be connected to our PIC which will sit in our project (say in a Breadboard).

The technique of programming we are using is ICSP i.e. In circuit Serial Programming. In this method our PIC will stay in the end application board while programming. There will be a simple 6 PIN header in our end application. We have to connect this programmer to the end application using this connecter. Now we can connect the programmer with PC and upload the HEX file to our PIC micro. The figure below illustrate the process.

Fig. : Using A ISCP Programmer.

Now our PIC Programmer is complete. In next tutorial I will show you how to write a "Hello World" application for PIC MCUs. After that you will have all the tools and basic skills to work with PIC related projects. Then we will move on an learn the PIC MCU step by step.

Important Note

This programmer works with a physical serial port only! It does NOT work with USB to Serial Converters.

BY CHANDERKETU