-->

PIC microcontroller tutorial

By
PIC Microcontroller Tutorial

Every Microcontroller (also MCU) consists of several major units:
  • Input / Output Ports
  • Control Pins: reset, power, clock
  • Processor (CPU)
  • Memory (RAM, ROM, EEPROM)
  • Serial and parallel ports
  • Timers
  • Analog-to-digital (A/D) and digital-to-analog (D/A) converters
Microcontrollers PICmicro MCU from Microchip Company divided into 4 large families. Each family has a variety of components that provide built-in special features:
1. The first family, PIC10 (10FXXX) - is called Low End.
The PIC10FXXX devices from Microchip Technology are low-cost, high-performance, 8-bit, fully static, Flash-based CMOS microcontrollers. They employ a RISC architecture with only 33 single-word/ single-cycle instructions. The 12-bit wide instructions are highly symmetrical. The easy-to-use and easy to remember instruction set reduces development time significantly. The PIC10FXXX devices contain an 8-bit ALU and working register.

2. The second family, PIC12 (PIC12FXXX)– is called Mid-Range.
The PIC12FXXX most popular among these starter their way in this field. Mid-Range devices feature 14-bit program word architecture and are available in 8 to 64-pin packages that offer an operating voltage range of 1.8-5.5V, small package footprints, interrupt handling, an 8-level hardware stack, multiple A/D channels and EEPROM data memory. Mid-range devices offer a wide range of package options and a wide range of peripheral integration. These devices feature various serial analog and digital peripherals, such as: SPI, I2C™, USART, LCD and A/D converters.
3. The third family is PIC16(16FXXX).
With six variants ranging from 3.5K-14 Kbytes of Flash memory, up to 256 bytes of RAM and a mix of peripherals including EUSART, CCP and onboard analog comparators. These devices are well suited for designers with applications that need more code space or I/O than 14-pin variants supply, and are looking to increase system performance and code efficiency by employing hardware motor control and communications capability.
4. The fourth family is PIC 17/18(18FXXX).
The PIC18 family utilizes a 16-bit program word architecture and incorporates an advanced RISC architecture with 32 level-deep stack, 8x8 hardware multiplier, and multiple internal and external interrupts. With the highest performance in Microchip’s 8-bit portfolio, the PIC18 family provides up to 16 MIPS and linear memory. PIC18 is the most popular architecture for new 8-bit designs where customers want to program in C language

Here we are trying t the thord family exceptionally PIC16F877a

The PIC microcontroller PIC16f877a is one of the most renowned microcontrollers in the industry. This controller is very convenient to use, the coding or programming of this controller is also easier. One of the main advantages is that it can be write-erase as many times as possible because it use FLASH memory technology. It has a total number of 40 pins and there are 33 pins for input and output.

An EEPROM is also featured in it which makes it possible to store some of the information permanently like transmitter codes and receiver frequencies and some other related data. The cost of this controller is low and its handling is also easy. Its flexible and can be used in areas where microcontrollers have never been used before as in coprocessor applications and timer functions etc.


PIN CONFIGURATION AND DESCRIPTION OF PIC16F877A

As it has been mentioned before, there are 40 pins of this microcontroller IC. It consists of two 8 bit and one 16 bit timer. Capture and compare modules, serial ports, parallel ports and five input/output ports are also present in it.


PIN 1: MCLR
The first pin is the master clear pin of this IC. It resets the microcontroller and is active low, meaning that it should constantly be given a voltage of 5V and if 0 V are given then the controller is reset. Resetting the controller will bring it back to the first line of the program that has been burned into the IC.
A push button and a resistor is connected to the pin. The pin is already being supplied by constant 5V. When we want to reset the IC we just have to push the button which will bring the MCLR pin to 0 potential thereby resetting the controller.
PIN 2: RA0/AN0
PORTA consists of 6 pins, from pin 2 to pin 7, all of these are bidirectional input/output pins. Pin 2 is the first pin of this port. This pin can also be used as an analog pin AN0. It is built in analog to digital converter.
PIN 3: RA1/AN1
This can be the analog input 1.
PIN 4: RA2/AN2/Vref-
It can also act as the analog input2. Or negative analog reference voltage can be given to it.
PIN 5: RA3/AN3/Vref+
It can act as the analog input 3. Or can act as the analog positive reference voltage.
PIN 6: RA0/T0CKI
To timer0 this pin can act as the clock input pin, the type of output is open drain.
PIN 7: RA5/SS/AN4
This can be the analog input 4. There is synchronous serial port in the controller also and this pin can be used as the slave select for that port.
PIN 8: RE0/RD/AN5
PORTE starts from pin 8 to pin 10 and this is also a bidirectional input output port. It can be the analog input 5 or for parallel slave port it can act as a ‘read control’ pin which will be active low.
PIN 9: RE1/WR/AN6
It can be the analog input 6. And for the parallel slave port it can act as the ‘write control’ which will be active low.
PIN 10: RE2/CS/A7
It can be the analog input 7, or for the parallel slave port it can act as the ‘control select’ which will also be active low just like read and write control pins.
PIN 11 and 32: VDD
These two pins are the positive supply for the input/output and logic pins. Both of them should be connected to 5V.
PIN 12 and 31: VSS
These pins are the ground reference for input/output and logic pins. They should be connected to 0 potential.
PIN 13: OSC1/CLKIN
This is the oscillator input or the external clock input pin.
PIN 14: OSC2/CLKOUT
This is the oscillator output pin. A crystal resonator is connected between pin 13 and 14 to provide external clock to the microcontroller. ¼ of the frequency of OSC1 is outputted by OSC2 in case of RC mode. This indicates the instruction cycle rate.
PIN 15: RC0/T1OCO/T1CKI
PORTC consists of 8 pins. It is also a bidirectional input output port. Of them, pin 15 is the first. It can be the clock input of timer 1 or the oscillator output of timer 2.
PIN 16: RC1/T1OSI/CCP2
It can be the oscillator input of timer 1 or the capture 2 input/compare 2 output/ PWM 2 output.
PIN 17: RC2/CCP1
It can be the capture 1 input/ compare 1 output/ PWM 1 output.
PIN 18: RC3/SCK/SCL
It can be the output for SPI or I2C modes and can be the input/output for synchronous serial clock.
PIN 23: RC4/SDI/SDA
It can be the SPI data in pin. Or in I2C mode it can be data input/output pin.
PIN 24: RC5/SDO
It can be the data out of SPI in the SPI mode.
PIN 25: RC6/TX/CK
It can be the synchronous clock or USART Asynchronous transmit pin.
PIN 26: RC7/RX/DT
It can be the synchronous data pin or the USART receive pin.
PIN 19,20,21,22,27,28,29,30:
All of these pins belong to PORTD which is again a bidirectional input and output port. When the microprocessor bus is to be interfaced, it can act as the parallel slave port.
PIN 33-40: PORT B
All these pins belong to PORTB. Out of which RB0 can be used as the external interrupt pin and RB6 and RB7 can be used as in-circuit debugger pins.

HOW TO PROGRAM THE INPUT AND OUTPUT PORTS OF PIC16F877A


As we have studied 5 input and output ports namely PORTA, PORTB, PORTC, PORTD and PORTE which can be digital as well as analog. We will configure them according to our requirements. But in case of analog mode, the pins or the ports can only act as inputs. There is a built in A to D converter which is used in such cases. Multiplexer circuits are also used.

But in digital mode, there is no restriction. We can configure the ports as output or as input. This is done through programming. For PIC the preferable compiler is mikro C pro which can be downloaded from their website.

There is a register named as ‘TRIS’ which controls the direction of ports. For different ports there are different registers such as TRISA, TRISB etc.

  • If we set a bit of the TRIS register to 0, the corresponding port bit will act as the digital output.
  • If we set a bit of the TRIS register to 1, the corresponding port bit will act as the digital input.

For example to set the whole portb to output we can write the program statement as:

TRISB=0;

Now the port will act as the output port and we can send any value on the output such as

PORTB=0XFF;

FF represents all 1’s in binary i.e. FF=11111111, now all the pins of port b are high. If we connect LEDs at all the pins then they will all start glowing in this condition.

If we want to negate the values of the port b we can use the statement:

PORTB=~PORTB;

Now all the pins of the port b will be low.

CODE TO LIGHT UP A SINGLE LED/ FLASHING LED

void main()
 {
      TRISB.F0 = 0  // the direction of RB0 is set as output
                 //or TRISB = 0xFE (0xFE = 11111110)
      do // setting the infinite loop
      {
        PORTB.F0 = 1; // setting the RB0 pin to high
        Delay_ms(500); // delay of 500 milli seconds
        PORTB.F0 = 0; // setting the RB0 pin to low
        Delay_ms(500); // again a delay of 500 milli seconds
     }while(1);
 }

LCD 4-bit Mode Interfacing

By
In  LCD 4-bit Mode Interfacing  we need to connect only 4+3 wire to LCD from Microcontroler . So we save 4 pins of micro controller.But we need to send 8 bit data or command to lcd by using   4 wire How ?
 By spiting the data or command  into two parts (4 bit nibble) ie ,lower 4 bits and higher 4 bits (the technique is called masking ). 
We need first initialize the LCD  as 4 bit according to the data sheet .
that is as follows
Powering LCD .Wait for abour 20mS
Send the first init value (0x30) 
Wait for about 10mS
Send second init value (0x30)  
Wait for about 1mS
Send third init value (0x30)
Wait for 1mS
Select bus width  (0x20) so we give 0x20 and selected as 4 bit mode
Wait for 1mS

Then, we send higher4bit  and then lower 4bit . This means in both command and data sending function we need to separate the higher 4-bits and lower 4-bits.




Mask lower 4-bits
Send to the LCD port (that to 4 pins of the LCD )
Send enable signal  
Mask higher 4-bits
Send to LCD port
Send enable signal

 Programming section as follows 

I am using the controller 8051  with the compailer Keil  and with C language .

The port 2 of the 8051  is used . the circuit as follows




                         LCD.h header File


 #include"delay.h"
#define LCDPORT P2 // named the Port2 as LCDPORT
sbit RS=LCDPORT^2; // P2.2 named RS
sbit E=LCDPORT^3; // P2.3 named as E
#define LINE2 lcd_cmd(0xc0); // used to display the second line oxc0 is


void latch(void) // used to a high to low pulse the pin E
{
E = 1;
delay(1);
E = 0;
}

void lcd_cmd(unsigned char c) // used to send the command / Instruction to the lcd port
{
RS = 0; // send a '0' value to select to send command
delay(1);
LCDPORT = c & 0xf0; // send the command c only 4 bit by masking the lower bit
latch();
delay(1);
LCDPORT = (c << 4); // giving the lowerbit by shifting the 4 bit to left
latch();
}

void lcd_data(unsigned char c)
{
RS =1; // send 1 to send data
delay(1);
LCDPORT = c & 0xf0 | 0x4; //send the data only 4 bit by masking the lower bit and also making the RS pin high by giving 0x04 .
delay(1);
latch();

LCDPORT = (c << 4)| 0x4; ; // giving the lower bit by shifting the 4 bit to left
latch();
}

void lcd_init()
{
delay(20);
lcd_cmd(0x30); //as per data sheet
delay(20);
lcd_cmd(0x30); //as per data sheet
delay(4);
lcd_cmd(0x32); //as per data sheet
delay(4);
lcd_cmd(0x28); // Function set (4-bit interface, 2 lines, 5*7Pixels)
lcd_cmd(0x28); // Function set (4-bit interface, 2 lines, 5*7Pixels)
lcd_cmd(0x0c); // Make cursorinvisible
lcd_cmd(0x6); // Set entry Mode(auto increment of cursor)
}

void string(const char *q) // used to send single charcter to display the lcd
{
while (*q) {
lcd_data(*q++);
}
}



Finaly The code is lcd.c

#include <REGX51.H>   
#include "lcd.h"


void main(){
lcd_init();
string("Lcd Testing ");
LINE2
string("its displaying ");
while(1);
}

Download the full project including Proteus File Clik here

LCD 4-bit Mode Interfacing

By
In  LCD 4-bit Mode Interfacing  we need to connect only 4+3 wire to LCD from Microcontroler . So we save 4 pins of micro controller.But we need to send 8 bit data or command to lcd by using   4 wire How ?
 By spiting the data or command  into two parts (4 bit nibble) ie ,lower 4 bits and higher 4 bits (the technique is called masking ). 
We need first initialize the LCD  as 4 bit according to the data sheet .
that is as follows
Powering LCD . Wait for abour 20mS
Send the first init value (0x30) 
Wait for about 10mS
Send second init value (0x30)  
Wait for about 1mS
Send third init value (0x30)
Wait for 1mS
Select bus width  (0x20) so we give 0x20 and selected as 4 bit mode
Wait for 1mS

Then, we send higher4bit  and then lower 4bit . This means in both command and data sending function we need to separate the higher 4-bits and lower 4-bits.




Mask lower 4-bits
Send to the LCD port (that to 4 pins of the LCD )
Send enable signal  
Mask higher 4-bits
Send to LCD port
Send enable signal

 Programming section as follows 

I am using the controller 8051  with the compailer Keil  and with C language .

The port 2 of the 8051  is used . the circuit as follows




                         LCD.h header File


 #include"delay.h"
 #define LCDPORT P2  // named the Port2 as LCDPORT
 sbit RS=LCDPORT^2;  // P2.2 named RS
 sbit E=LCDPORT^3;  // P2.3 named as E
 #define LINE2 lcd_cmd(0xc0);   // used to display the second line oxc0 is 
 
 
 void latch(void)  // used to a high to low pulse the pin E 
{
    E = 1;
    delay(1);
    E = 0;
}

void lcd_cmd(unsigned char c)   // used to send the command / Instruction to the lcd port 
{          
RS = 0;    // send a '0' value to select to send command
delay(1);     
LCDPORT = c & 0xf0;  //  send  the command c only 4 bit by masking the lower bit 
latch();
delay(1); 
LCDPORT =  (c << 4); // giving the lowerbit  by shifting the 4 bit to left 
latch(); 
}

 void lcd_data(unsigned char c)
 {
 RS =1;    // send 1 to send data 
 delay(1);
LCDPORT = c & 0xf0 | 0x4; //send  the data  only  4 bit by masking the lower bit and also making the RS pin high by giving 0x04 .
delay(1);
latch();

LCDPORT = (c << 4)| 0x4; ; // giving the lower bit  by shifting the 4 bit to left 
latch(); 
}
 
void lcd_init()
{
  delay(20);
    lcd_cmd(0x30);  //as per data sheet
  delay(20);
    lcd_cmd(0x30);    //as per data sheet
  delay(4);
    lcd_cmd(0x32);    //as per data sheet
 delay(4);
    lcd_cmd(0x28);            // Function set (4-bit interface, 2 lines, 5*7Pixels)
    lcd_cmd(0x28);            // Function set (4-bit interface, 2 lines, 5*7Pixels)
    lcd_cmd(0x0c);            // Make cursorinvisible
    lcd_cmd(0x6);            // Set entry Mode(auto increment of cursor)
}

 void string(const char *q)    // used to send single charcter to display the lcd 
{
    while (*q) {
        lcd_data(*q++);
    }
}



Finaly The code is lcd.c

#include <REGX51.H>   
#include "lcd.h"


void main(){
lcd_init();
string("Lcd Testing ");
LINE2
string("its displaying ");
while(1);
}

Download the full project including Proteus File Clik here
By

Array Variable (declared as Global) and changing the value inside a function -C programming 


#include<stdio.h>

int a[3] ={10,15,20}; // declaring array variable as global
int test_function(); // a function to change the array varriable value in side the function
int main()
{

int i,j;

for(i=0;i<3; i++)
printf("Inside the main function i = %d\n",a[i]);

test_function();
printf("\n\n");
for(i=0;i<3; i++)
printf("Inside the test function i = %d\n",a[i]);
return 0;
}

int test_function()
{
a[0] = 18;
a[1] = 100;
a[2] = 250;

return 0;
}

Output and program image 

By

Array Variable (declared as Global) and changing the value inside a function -C programming 


#include<stdio.h>
 
 int a[3] ={10,15,20};  // declaring array variable as global 
int test_function();    // a function to change the array varriable value in side the function 
int main()
{
 
 int i,j;
 
 for(i=0;i<3; i++)
   printf("Inside the main function i = %d\n",a[i]);
 
 test_function();
 printf("\n\n");
 for(i=0;i<3; i++)
   printf("Inside the test  function i = %d\n",a[i]);
 return 0;
}

int test_function()
{
a[0] = 18;
a[1] = 100;
a[2] = 250;
 
return 0; 
}

Output and program image 

LED Blinking program by using Mikro C -

By
LED Blinking program  by using Mikro  (PIC 16F877)

void main() {
TRISB = 0; //Makes PORTB0 or RB0 Output Pin

while(1) //Infinite Loop
{
PORTB = 0x55; //LED ON
Delay_ms(1000); //1 Second Delay
PORTB = 0xAA; //LED OFF
Delay_ms(1000); //1 Second Delay
}
}

LED Blinking program by using Mikro C -

By
LED Blinking program  by using Mikro  (PIC 16F877)

void main() {
 TRISB = 0; //Makes PORTB0 or RB0 Output Pin

  while(1) //Infinite Loop
  {
    PORTB = 0x55; //LED ON
    Delay_ms(1000); //1 Second Delay
    PORTB = 0xAA; //LED OFF
    Delay_ms(1000); //1 Second Delay
  }
}

LED Blinking program by Using Atmega32 - Atmel Studio 6

By
LED Blinking program by Using Atmega32 - Atmel Studio 6 

#ifndef F_CPU                    // defining clock speed of the processor
#define F_CPU 16000000UL // 16 MHz clock speed
#endif

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
DDRC = 0xFF; //Nakes PORTC as Output
while(1) //infinite loop
{
PORTC = 0xFF; //Turns ON All LEDs
_delay_ms(30); //1 second delay
PORTC= 0x00; //Turns OFF All LEDs
_delay_ms(30); //1 second delay
}
}


LED Blinking By using 8051 in Keil IDE
LED Blinking using PIC 16f877a

LED Blinking program by Using Atmega32 - Atmel Studio 6

By
LED Blinking program by Using Atmega32 - Atmel Studio 6 

#ifndef F_CPU                    // defining clock speed of the processor
#define F_CPU 16000000UL         // 16 MHz clock speed
#endif

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
 DDRC = 0xFF;                //Nakes PORTC as Output
 while(1) //infinite loop
 {
  PORTC = 0xFF;      //Turns ON All LEDs
  _delay_ms(30);    //1 second delay
  PORTC= 0x00;      //Turns OFF All LEDs
  _delay_ms(30);   //1 second delay
 }
}


LED Blinking By using 8051 in Keil IDE
LED Blinking using PIC 16f877a

LED Blinking Program by using PIC 16f877a MPLAB X .

By
 compiler - sdcc ,XC8.
 


#define _XTAL_FREQ 8000000 // setting the crystal frequency 

#include <xc.h> // Header file inclusion ,to add function like _delay_ms()

#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config CP = OFF // FLASH Program Memory Code Protection bits (Code protection off)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = ON // Low Voltage In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF // Data EE Memory Code Protection (Code Protection off)
#pragma config WRT = ON // FLASH Program Memory Write Enable (Unprotected program memory may be written to by EECON control)



int main()
{
TRISB0 = 0; //RB0 as Output PIN
while(1) // endless loop
{
RB0 = 1; // LED ON
__delay_ms(1000); // 1 Second Delay
RB0 = 0; // LED OFF
__delay_ms(1000); // 1 Second Delay
}
return 0;
}




LED Blinking By using 8051 in Keil IDE
/led-blinking-program-by-using-atmega32

LED Blinking Program by using PIC 16f877a MPLAB X .

By
 compiler - sdcc ,XC8.
 


#define _XTAL_FREQ 8000000 // setting the crystal frequency 

#include <xc.h>           // Header file inclusion ,to add function like _delay_ms() 

#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config CP = OFF         // FLASH Program Memory Code Protection bits (Code protection off)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = ON         // Low Voltage In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF        // Data EE Memory Code Protection (Code Protection off)
#pragma config WRT = ON         // FLASH Program Memory Write Enable (Unprotected program memory may be written to by EECON control)



int main()
{
  TRISB0 = 0; //RB0 as Output PIN
  while(1)    // endless loop
  {
    RB0 = 1;  // LED ON
    __delay_ms(1000); // 1 Second Delay
    RB0 = 0;  // LED OFF
    __delay_ms(1000); // 1 Second Delay
  }
  return 0;
}




LED Blinking By using 8051 in Keil IDE
/led-blinking-program-by-using-atmega32

LED Blinking By using 8051 in Keil IDE

By
sbit LED1 = P2^0;  // P2.0 of the pin name cahnged to LED1
void delay(); // a delayis used to give some delay in between turn ON and OFF

void main()
{
while(1)
{

LED1 = 0;

delay();

LED1 = 1 ;
delay();

}
}

void delay()
{
int j;

for(j=0; j<500; j++)
;
}

LED Blinking By using 8051 in Keil IDE

By
sbit LED1 = P2^0;  // P2.0 of the pin name cahnged to LED1
void delay();    // a delayis used  to give some delay in between turn ON  and OFF  

void main()
{
  while(1)
  {
  
 LED1 = 0;
 
 delay();

 LED1 = 1 ;
 delay();
 
   }
}

 void delay()
 {
   int j;
     
    for(j=0; j<500; j++)
    ;
 }

Transport Layer

By
Next Page
  TCP/IP layers 


  • recall the 5-layer model above
  • the network interface layer is often called the link layer
  • we use the generic term packet for each block of data transmitted
  • recall that each layer adds its own header, so nature of "packet" varies
  • so in fact the following terms are usually used for "packets" at each layer
    • frames at the link layer
    • datagrams at the internet layer
    • segments at the transport layer
  • we focus on the transport layer in this section

Transport Layer

By
Next Page
  TCP/IP layers 


  • recall the 5-layer model above
  • the network interface layer is often called the link layer
  • we use the generic term packet for each block of data transmitted
  • recall that each layer adds its own header, so nature of "packet" varies
  • so in fact the following terms are usually used for "packets" at each layer
    • frames at the link layer
    • datagrams at the internet layer
    • segments at the transport layer
  • we focus on the transport layer in this section

More Posts

gElectron. Powered by Blogger.

Contributors

16x2 LCD Interfacing with STM32,STM32F103C6

 16x2 LCD  Interfacing with STM32,STM32F103C6 lcd_init(); LCD_LINE1; lcd_String(" GeElectron"); LCD_LINE2; lc...

Contact Form

Name

Email *

Message *

Contact us

Name

Email *

Message *

Follow Us

https://www.facebook.com/gElectron-393939667321867/ FBbox/https://www.facebook.com/IVYthemes

Comments

[blogger]

MKRdezign

Test

Latest

[recent][newsticker]

Technology

Top Ads

RECENT COMMENTS

Subscribe Via Email

Subscribe to our newsletter to get the latest updates to your inbox. ;-)


Your email address is safe with us!

RECENT COMMENTS