Pic16f877a Practical Programs

Embed Size (px)

Citation preview

PIC16F877A PRACTICAL PROGRAMS

1. PARALLEL PORT INTERFACES

1.1. A. BLINKING LED

Objective:

The main objective of this program is to interface the LEDs with the PIC16F877A And to verify the output.

Coding:

#include //controller specification#use delay (clock=11059200) // oscillator selection

#byte trisd=0x88 //RAM(SFR address)#byte portd=0x08 //RAM address

Void main (){ Trisd=0x00; //portd in output mode Portd=0x00; // initializing portd

While (1) { Portd=0xff;//all LEDs are on delay_ms (1000); //1 sec delay Portd=0x00; //all LEDs are off delay_ms(1000); //(or) portd=~portd; delay_ms(1000); }}

1.1.b. RING COUNTER Objective : The main objective of this program is to interface the LEDs with the PIC 16f877a and to verify the ring counter output.

Coding:#include #use delay(clock=11059200)#byte portd=0x08#byte trisd=0x88int i=1; //declare and initialise the value Void main () {Portd=0x00;Trisd=0x00;While (1) { Portd=i; delay_ms(1000);//or i=i*2; if (i>=128) { portd=i; delay_ms(1000); i=1; //reload the value } }}

1.1.c. BINARY COUNTER Objective : The main objective of this program is to interface the LEDs with the PIC 16f877a and to verify the binary counter output.Coding:#include #use delay(clock=11059200)#byte portd=0x08#byte trisd=0x88#byte portc=0x07#byte trisc=0x87

#bit relay=0x07.1 //one bit (pin) declaration #zero_ram //all variables are clearedint i;void main(){portd=0x00;trisd=0x00;trisc=0x00;while(1) { for(i=0;i=43) //period for one sec delay { data=0;count++; } } void main(){trisd=0x00;portd=0x00;trisc=0x00;portc=0x00;trisa=0x00;porta=0x00;

option_reg=0x07; //declaring timer and prescalerintcon=0xa0; //declaring the interrupttmr0=0x00; //declaring the timer

while(1){portd = seg_data [count%10];seg1=1;delay_ms(1);seg1=0;portd = seg_data [count/10];seg2=1;delay_ms(1);seg2=0;

}}

1.2.2. COUNTER USING TIMER_1 Objective: The main objective of this program is to count the external pulse using timer1 in PIC 16f877a.

Coding:#include #use delay(clock=11059200)#fuses nowdt,hs,nobrownout

#byte trisa=0x85#byte porta=0x05#byte trisc=0x87#byte portc=0x07#byte trisd=0x88#byte portd=0x08

#byte t1con=0x10#byte tmr1l=0x0e#byte tmr1h=0x0f

#bit seg1=porta.1#bit seg2=porta.2#bit seg3=porta.3#bit seg4=porta.4

int16 Val=0;char seg_data[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67};#zero_ramvoid refreshes(int16);

void main(){

trisa=0x00;trisc=0x01;trisd=0x00;porta=0x00;portc=0x00;portd=0x00;

t1con=0x03; // declaring timer1 as counter tmr1l=0x00; //timer1 low registor initial valuetmr1h=0x00; //timer1 high registor initial value

while(1){Val=make16(tmr1h,tmr1l);refresh(Val);}}

void refreshes(int16 n){int x[4],i;for(i=0;i=59) { min++;sec=0; relay=~relay; } if(min>=59) { min=0;hr++; } }}

void main(){trisd=0x00;portd=0x00;trisc=0x00;portc=0x00;trisa=0x00;porta=0x00;

intcon=0xc0;t1con=0x7f; // declaring timer and prescaler,postscalertmr2=0x00; //timer2 intial value pie1=0x02; //timer2 interrupt enable

while(1){portd=seg_data [sec%10];seg1=1;delay_ms(1);seg1=0;portd=seg_data [sec/10];seg2=1;delay_ms(1);seg2=0;portd=seg_data [min%10]+0x80;seg3=1;delay_ms(1);seg3=0;portd=seg_data [min/10];seg4=1;delay_ms(1);seg4=0;}}

1.3 CAPTURE COMPARE AND PLUSE WIDTH MODULATION 1.3.1. CAPTURE Objective: The main objective of this in PIC 16f877a.Coding:#include #use delay(clock=11059200)#fuses hs,nowdt

#byte trisa=0x85#byte porta=0x05#byte trisc=0x87#byte portc=0x07#byte trisd=0x88#byte portd=0x08

#bit seg1=porta.1#bit seg2=porta.2#bit seg3=porta.3#bit seg4=porta.4

#byte ccpr1l=0x15#byte ccpr1h=0x16#byte ccp1con=0x17

#byte intcon=0x0b#byte t1con=0x10#byte tmr1l=0x0e#byte tmr1h=0x0f#byte pie1=0x8c#byte pir1=0x0c

#bit relay=0x07.1#bit ccpp=0x0c.2

void refresh(unsigned int16 n);

char dat[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

int16 capt;int i,x[4];

#zero_ram

#int_ccp1ccp1_isr(){}

void main(){

trisd=0x00;portd=0x00;trisc=0x04; //for capturing the signal initialize as inputportc=0x00;trisa=0x00;porta=0x00;

ccp1con=0x07; //declaring as a captureccpr1l=0x00; //loading compare value in low regccpr1h=0x00; //loading compare value in high reg

intcon=0xc0; //declaring interrupt value t1con=0x31;tmr1l=0x00;tmr1h=0x00;pie1=0x04; //interrupt declaration for the ccp

while(1){

capt=make16(ccpr1h,ccpr1l);ret(capt);

}}

void refresh(unsigned int16 n){for(i=0;i4)&0x0f;

hrs=read_ds1307(0xd0,0xd1,0x02);hrs_lsb=hrs&0x0f;hrs_msb=(hrs>>4)&0x0f;

day=read_ds1307(0xd0,0xd1,0x03);day_lsb=day&0x0f;day_msb=(day>>4)&0x0f;

date=read_ds1307(0xd0,0xd1,0x04);date_lsb=date&0x0f;date_msb=(date>>4)&0x0f;

month=read_ds1307(0xd0,0xd1,0x05);month_lsb=month&0x0f;month_msb=(month>>4)&0x0f;

year=read_ds1307(0xd0,0xd1,0x06);year_lsb=year&0x0f;year_msb=(year>>4)&0x0f;

}}

void refresh()

{command(0x80);data("Time:");

command(0x85);data(hrs_msb|0x30);data(hrs_lsb|0x30);

data(":");data(min_msb|0x30);data(min_lsb|0x30);

data(":");data(sec_msb|0x30);data(sec_lsb|0x30);

if(hrs_msb>=1 && hrs_lsb>=2){command(0x8e);data("pm");}else{command(0x8e);data("am");}

command(0xc0);

if(day_msb==0){if(day_lsb==1) {data("sun");} elseif(day_lsb==2) {data("mon");} elseif(day_lsb==3) {data("tue");} elseif(day_lsb==4) {data("wen");} elseif(day_lsb==5) {data("thu");} elseif(day_lsb==6) {data("fri");} elseif(day_lsb==7) {data("sat");}

}

command(0xc5);data(date_msb|0x30);data(date_lsb|0x30);data(".");

if(month_msb==0){if(month_lsb==1) {data("jan");}if(month_lsb==2) {data("feb");}if(month_lsb==3) {data("mar");}if(month_lsb==4) {data("apr");}if(month_lsb==5) {data("may");}if(month_lsb==6) {data("jun");}if(month_lsb==7) {data("jul");}if(month_lsb==8) {data("aug");}if(month_lsb==9) {data("sep");}}

else if(month_msb==1){if(month_lsb==0) {data("oct");}if(month_lsb==1) {data("nov");}if(month_lsb==2) {data("dec");}}data(".");data(year_msb|0x30);data(year_lsb|0x30);

}void write_ds1307(unsigned int device_write_word,unsigned int write_add,unsigned int write_data){i2c_start();i2c_write(device_write_word);i2c_write(write_add);i2c_write(write_data);i2c_stop();}unsigned int8 read_ds1307(unsigned int device_write_word,unsigned int device_read_word,unsigned int read_add){int data;i2c_start();i2c_write(device_write_word);i2c_write(read_add);i2c_start();i2c_write(device_read_word);data=i2c_read(0);i2c_stop();return data;}

void rtc_init(){write_ds1307(0xd0,0x00,0x58);delay_ms(100);write_ds1307(0xd0,0x01,0x10);delay_ms(100);write_ds1307(0xd0,0x02,0x06);delay_ms(100);write_ds1307(0xd0,0x03,0x05);delay_ms(100);write_ds1307(0xd0,0x04,0x26);delay_ms(100);write_ds1307(0xd0,0x05,0x08);delay_ms(100);write_ds1307(0xd0,0x06,0x10);delay_ms(100);}

1.7.2.AT24C16(EEPROM) INTERFACE USING I2C Objective: The main objective of this is to write the data in the external eeprom and also read the data written and verify the outputs.Coding:#include#device *=16#use delay(clock=11059200)

#byte trisa=0x85#byte trisb=0x86#byte trisd=0x88

#byte porta=0x05#byte portb=0x06#byte portd=0x08#byte option_reg=0x81

#bit key1=0x06.2

#bit sel1=0x05.1#bit sel2=0x05.2#bit sel3=0x05.3#bit sel4=0x05.4

#define EEPROM_SDA PIN_C4#define EEPROM_SCL PIN_C3

#use i2c(master, sda=EEPROM_SDA, scl=EEPROM_SCL)

int8 i,j,m,n=0,var,k[95];const int8 seg[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

void write_ext_EEPROM(unsigned int device_write_word,unsigned int write_add,unsigned int write_data) { i2c_start(); i2c_write(device_write_word); i2c_write(write_add); i2c_write(write_data); i2c_stop(); }

unsigned int8 read_ext_EEPROM(unsigned int device_write_word,unsigned int device_read_word,unsigned int read_add) { int data; i2c_start(); i2c_write(device_write_word); i2c_write(read_add); i2c_start(); i2c_write(device_read_word); data=i2c_read(0); i2c_stop(); return data; }

#zero_ramvoid main(){ trisa= 0X00; trisb= 0X1e; trisd= 0X00; porta = 0X00; portb = 0X00; portd = 0X00; option_reg=0x00; for(j=0;j