MicroChip- PIC16F877
Introduction:-
This document contains device specific information. Additional information may be found in the PICmicro Mid-Range Reference Manual (DS33023), which may be obtained from your local Microchip Sales Representative or downloaded from the Microchip website. The Reference Manual should be considered a complementary document to this data sheet, and is highly recommended reading for a better understanding of the device architecture and operation of the peripheral modules.
Features of PIC16F877:-
• High performance RISC CPU
• Only 35 single word instructions to learn
• All single cycle instructions except for program branches which are two cycle
DC - 200 ns instruction cycle
• Up to 8K x 14 words of FLASH Program Memory, Up to 368 x 8 bytes of Data Memory
Up to 256 x 8 bytes of EEPROM Data Memory
• Pinout compatible to the PIC16C73B/74B/76/77
• Interrupt capability (up to 14 sources)
• Eight level deep hardware stack
• Direct, indirect and relative addressing modes
• Power-on Reset (POR)
• Power-up Timer (PWRT) and Oscillator Start-up Timer (OST)
• Watchdog Timer (WDT) with its own on-chip RC oscillator for reliable operation
• Programmable code protection
• Power saving SLEEP mode
• Selectable oscillator options
• Low power, high speed CMOS FLASH/EEPROM technology
• Fully static design
• In-Circuit Serial Programming (ICSP) via two pins
• Single 5V In-Circuit Serial Programming capability
• In-Circuit Debugging via two pins
• Processor read/write access to program memory
• Wide operating voltage range: 2.0V to 5.5V
• High Sink/Source Current: 25 mA
• Commercial, Industrial and Extended temperature ranges
• Low-power consumption:
- < 0.6 mA typical @ 3V, 4 MHz
- 20 μA typical @ 3V, 32 kHz
- < 1 μA typical standby current
Peripheral Features:
• Timer0: 8-bit timer/counter with 8-bit prescaler
• Timer1: 16-bit timer/counter with prescaler, can be incremented during SLEEP via external
crystal/clock
• Timer2: 8-bit timer/counter with 8-bit period register, prescaler and postscaler
• Two Capture, Compare, PWM modules
- Capture is 16-bit, max. resolution is 12.5 ns
- Compare is 16-bit, max. resolution is 200 ns
- PWM max. resolution is 10-bit
• 10-bit multi-channel Analog-to-Digital converter
• Synchronous Serial Port (SSP) with SPI (Master mode) and I2C (Master/Slave)
• Universal Synchronous Asynchronous Receiver Transmitter (USART/SCI) with 9-bit address
Detection
Analog to Digital Converter
// Analog to Digital converter
#include<htc.h>
unsigned int data1,data2;
void main(void)
{
PORTC=0x00;
TRISC=0x00;
PORTB=0x00;
TRISC=0x00;
ADIE=1; //enable AD conversion interrupt
while(1)
{
ADIF=0; //make AD interrupt flag 0 initially
ADCON0=0x00; //channel-0 fosc/2
ADCON1=0b10001111;//----select analog AN0 input
ADON=1; //---start AD
GODONE=1; //--start conversion
while(GODONE); //--stay until conversion completed
if(ADIF==1) //--AD overflow
{
data1=ADRESH; //storing higher byte in data1
data2=ADRESL; //storing lower byte in data2
}
PORTB=data1; //writing data1 onto port-b
PORTC=data2; //writing data2 onto port-c
}
}
Timer Programming
//1sec delay using timer-0 and blinking leds on port-b
#include<htc.h>
#define LEDS PORTB
void delay_us(void);
unsigned int i,j;
void main(void)
{
// LEDS=0x00;
TRISB=0x00;
while(1)
{
LEDS=0xFF; //write 1111_1111 on portB
delay_us(); //---- calling delay
LEDS=0x7E; //----- 0111_1110
delay_us();
LEDS=0x3C; //----- 0011_1100
delay_us();
LEDS=0x18; //----- 0001_1000
delay_us();
LEDS=0x3C; //----- 0011_1100
delay_us();
LEDS=0x7E; //----- 0111_1110delay_us();
}
}
void delay_us()
{
GIE=1; //global interrupt enable
OPTION_REG=0b00011000; //0,1,2 bits 1:2 prescalar ; 4 bit PSA prescalar
Timer=0;5 bit T0sE rising edge clk ;6 bi
//T0cs enables internal clk source
T0IE=1; //enables timer-0 interrupt
for(j=0;j<1000;j++)
for(i=0;i<200;i++)
{
TMR0=0x05; //loading starting value of timer0
while(T0IF==0); //wait until timer-0 overflow occur
T0IF=0; //clear after overflow
}
}
USART_Rxn
// USART data receiving mode
#include<htc.h>
unsigned int data;
void main(void){
SPBRG=0b00011111; //31 setting baud rate 9600 for 20MHz
TXSTA=0b00000100; //setting highspeed baudrate
PORTC=0x00;
TRISC=0x00; //set PORTB as output port
RCIF=0; //initially clear receive flag
SPEN=1; //serial port enable
RCIE=1; //receive interrupt enable
PEIE=1; //peripheral interrupt enable
while(1)
{
CREN=1; //start to receive
while(RCIF==0); //stay until the receive buffer is full
data=RCREG;
PORTC=RCREG; //write the portC value as received value
CREN=0; //clear cren
RCIF=0; //clear rcif
}
}
USART_Txn
//USART trasmit mode
#include<htc.h>
unsigned char data[10]="EMBEDDED";
unsigned int i;
void main(void)
{
GIE=1; //enable global internal
PEIE=1; //enable peripheral interrupts
SPBRG=0b00011111; //31'D select baud rate 9600 for 20MHz
BRGH=1; //selecting high baudrate
SYNC=0; //Asynchronus transmission
TXIE=1; //Transmit interrupt enable
TXIF=0; //initially reset
while(1)
{
for(i=0;i<10;i++)
{
TXEN=1; //Transmission enable
TXREG=data[i]; //sending each char
while(TXIF==1); //wait until the bit is transmitted or untill the buffer clear
TXEN=0; //disable transmission
TXIF=0; //clear transmission flag
}
}
}
EEPROM_READ_WRITE
//EEPROM read/write operations
#include<htc.h>
void delay_1s(void);
unsigned int i,j;
#define LEDS PORTD
void main(void)
{
LEDS=0x00;
TRISD=0x00;
EEIE=1;
RP1=0; //bank-0
RP0=0;
for(i=0;i<20;i++)
{
EEADR=i; //load i as address
WREN=1; //write enable
EECON2=i+10; //write data onto the eecon2 reg
EEDATA=EECON2; //write data onto the EEPROM DATA reg
WR=1; //start writing
while(EEIF==0); //stay until writing completed
EEIF=0;
}
for(i=0;i<20;i++)
{
EEADR=i; //load i as address
RD=1; //start reading
LEDS=EEDATA; //place read data onto the leds
delay_1s();
RD=0;
}
}
//-----providing 1sec delay
void delay_1s()
{ GIE=1;
OPTION_REG=0B00011000;
T0IE=1;
for(i=0;i<1000;i++)
{
for(j=0;j<20;j++)
{ TMR0=0x05;
while(T0IF==0);
T0IF=0;
}
}
}