Sie sind auf Seite 1von 39

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 <16f877a.h> #use delay (clock=11059200) #byte trisd=0x88 #byte portd=0x08 Void main () { Trisd=0x00; Portd=0x00; While (1) { Portd=0xff; delay_ms (1000); Portd=0x00; delay_ms(1000); } } //controller specification // oscillator selection //RAM(SFR address) //RAM address

//portd in output mode // initializing portd

//all LEDs are on //1 sec delay //all LEDs are off //(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 <16f877a.h> #use delay(clock=11059200) #byte portd=0x08 #byte trisd=0x88 int i=1; 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; } } }

//declare and initialise the value

//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 <16f877a.h> #use delay(clock=11059200) #byte portd=0x08 #byte trisd=0x88 #byte portc=0x07 #byte trisc=0x87 #bit relay=0x07.1 #zero_ram int i; void main() { portd=0x00; trisd=0x00; trisc=0x00; while(1) { for(i=0;i<0xff;i++) { portd=i; relay=~relay; delay_ms(500); } } } //one bit (pin) declaration //all variables are cleared

//toggle the relay

1.1.d. KEY DEBOUNCE Objective : The main objective of this program is to interface the KEYs with the PIC 16f877a and to verify output. Coding: #include <16f877a.h> #use delay(clock=11059200) #byte portb=0x06 #byte trisb=0x86 #byte portc=0x07 #byte trisc=0x87 #byte portd=0x08 #byte trisd=0x88 #bit key=portb.1 #bit relay=portc.1 #byte option=0x81 #zero_ram int i=0,flag=0; void main() { portb=0x00; trisb=0x02; portc=0x00; trisc=0x00; portd=0x00; trisd=0x00; option=0x00; while(1) { while(!key) { flag=1; delay_ms(10); if(key==1 && flag==1) { flag=0;i++; relay=~relay;} portd=i; }

//port.1 is set to input mode

} }

1.1.e. SEVEN-SEGMENT DISPLAY Objective : The main objective of this program is to interface the 7 SEGMENT with the PIC 16f877a and to verify output. Coding: #include<16F877a.h> #use delay(clock=11059200) #byte trisa=0x85 #byte trisd=0x88 #byte porta=0x05 #byte portd=0x08 #bit seg1=porta.1 #bit seg2=porta.2 #bit seg3=porta.3 #bit seg4=porta.4 #byte option_reg=0x81 #byte intcon=0x0b void main() { trisa=0x00; trisd=0x00; option_reg=0x00; porta=0x00; portd=0x00; intcon=0x00; while(1) { portd=0x76;seg4=1;delay_ms(1);seg4=0; portd=0x79;seg3=1;delay_ms(1);seg3=0; portd=0x38;seg2=1;delay_ms(1);seg2=0; portd=0x3f;seg1=1;delay_ms(1);seg1=0; } }

//display //display //display //display

H E L 0

1.1.e. SEVEN-SEGMENT DISPLAY WITH KEYPAD Objective : The main objective of this program is to interface the 7 SEGMENT and KEYs with the PIC 16f877a and to verify output. Coding: #include <16F877A.h> #use delay(clock=11059200) #byte trisd=0x88 #byte portd=0x08 #byte trisb=0x86 #byte portb=0x06 #byte trisa=0x85 #byte porta=0x05 #bit seg1=porta.1 #bit seg2=porta.2 #bit seg3=porta.3 #bit seg4=porta.4 #bit key1=portb.1 #bit key2=portb.2 #bit key3=portb.3 #bit key4=portb.4 #byte option=0x81

void main() { trisd=0x00; portd=0x00; trisb=0x1e; portb=0x00; trisa=0x00; porta=0x00; option=0x00; while(1) { while(!key1) {

//portd is set as output mode //port.1 to port.4 is set as input mode

//porta is set as output mode

portd=0x76; seg4=1; delay_ms(500); seg4=0; } while(!key2) { portd=0x79; seg3=1; delay_ms(500); seg3=0; } while(!key3) { portd=0x38; seg2=1; delay_ms(500); seg2=0; } while(!key4) { portd=0x38; seg1=1; delay_ms(500); seg1=0; } } }

1.1.f. LCD DISPLAY Objective : The main objective of this program is to interface the LCD with the PIC 16f877a and to verify output. Coding: #include<16f877a.h> #use delay(clock=11059200) #fuses hs,nowdt #byte portd=0x08 #byte trisd=0x88 #byte porte=0x09 #byte trise=0x89 #bit reg=0x09.0 #bit rw=0x09.1 #bit en=0x09.2 //register selection //read and write //enable

void command(unsigned char com) { portd=com; reg=0; rw=0; en=1; delay_ms(1); en=0; } void data(unsigned char da) { portd=da; reg=1; rw=0; en=1; delay_ms(1); en=0; }

void main() { trisd=0x00; trise=0x00; command(0x38); command(0x06); command(0x0c); command(0x01); while(1) { command(0x80); data("GRT"); delay_ms(1000); command(0x01); delay_ms(50); command(0xc0); data("EMBEDDED"); delay_ms(1000); command(0x01); delay_ms(50); } } //to display in second line centre location //to display in first line first location //set function //entry mode set //display on //clear display

1.2.TIMERS 1.2.1. DELAY USING TIMER_0 Objective: The main objective of this program is to generate one second delay using timer0 in PIC 16f877a. Coding: #include <16F877A.h> #use delay(clock=11059200) #fuses hs,nowdt #byte trisd=0x88 #byte portd=0x08 #byte trisc=0x87 #byte portc=0x07 #byte porta=0x05 #byte trisa=0x85 #bit seg1=porta.3 #bit seg2=porta.4 #byte option_reg=0x81 #byte intcon=0x0b #byte tmr0=0x01 #bit relay=portc.1 int data=0,count=0x01; char seg_data[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; #zero_ram #int_timer0 timer0_isr() { data++; if(data>=43) { data=0;count++; } } void main() { trisd=0x00; portd=0x00; trisc=0x00; portc=0x00; trisa=0x00; //interrupt routine

//period for one sec delay

porta=0x00; option_reg=0x07; intcon=0xa0; tmr0=0x00; //declaring timer and prescaler //declaring the interrupt //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 <16F877A.h> #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_ram void refreshes(int16); void main() { trisa=0x00; trisc=0x01; trisd=0x00; porta=0x00; portc=0x00; portd=0x00; t1con=0x03; tmr1l=0x00; tmr1h=0x00; while(1) // declaring timer1 as counter //timer1 low registor initial value //timer1 high registor initial value

{ Val=make16(tmr1h,tmr1l); refresh(Val); } } void refreshes(int16 n) { int x[4],i; for(i=0;i<4;i++) { x[i]=n%10; n=n/10; } portd=seg_data[x[0]];seg1=1;delay_ms(1);seg1=0; portd=seg_data[x[1]];seg2=1;delay_ms(1);seg2=0; portd=seg_data[x[2]];seg3=1;delay_ms(1);seg3=0; portd=seg_data[x[3]];seg4=1;delay_ms(1);seg4=0; }

1.2.3. DIGITAL CLOCK USING TIMER_2 Objective: The main objective of this program is to design digital clock using timer2 in PIC 16f877a. Coding: #include <16F877A.h> #use delay(clock=11059200) #fuses hs,nowdt #byte trisa=0x85 #byte porta=0x05 #byte trisb=0x86 #byte portb=0x06 #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 intcon=0x0b #byte t1con=0x12 #byte tmr2=0x11 #byte pie1=0x8c #bit relay=0x07.1 #zero_ram char seg_data[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; int data,sec,min,hr; #int_timer2 timer2_isr() { data++; if(data==43) { sec++;data=0; if(sec>=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; tmr2=0x00; pie1=0x02;

// declaring timer and prescaler,postscaler //timer2 intial value //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 <16f877a.h> #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_ccp1 ccp1_isr() {

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

//for capturing the signal initialize as input

ccp1con=0x07; ccpr1l=0x00; ccpr1h=0x00; intcon=0xc0; t1con=0x31; tmr1l=0x00; tmr1h=0x00; pie1=0x04;

//declaring as a capture //loading compare value in low reg //loading compare value in high reg //declaring interrupt value

//interrupt declaration for the ccp

while(1) { capt=make16(ccpr1h,ccpr1l); ret(capt); } } void refresh(unsigned int16 n) { for(i=0;i<4;i++) { x[i]=n%10; n=n/10; }

portd = dat[x[0]];seg1=1;delay_ms(1);seg1=0; portd = dat[x[1]];seg2=1;delay_ms(1);seg2=0; portd = dat[x[2]];seg3=1;delay_ms(1);seg3=0; portd = dat[x[3]];seg4=1;delay_ms(1);seg4=0; }

1.3.2. COMPARE Objective: The main objective of this in PIC 16f877a. Coding: #include<16f877a.h> #use delay(clock=11059200) #fuses hs,nowdt #byte trisc=0x87 #byte portc=0x07 #byte portd=0x08 #byte trisd=0x88

#byte intcon=0x0b #byte tmr1l=0x0e #byte tmr1h=0x0f #byte t1con=0x10 #byte ccpr1l=0x15 #byte ccpr1h=0x16 #byte ccp1con=0x17 #byte pir1=0x0c #byte pie1=0x8c #bit relay=portc.1

#zero_ram #int_ccp1 //interrupt rountine ccpr1() { relay=~relay; }

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

intcon =0xc0; pie1 =0x04; t1con = 0x31; tmr1l = 0x00; tmr1h = 0x00; ccp1con =0x08; ccpr1l =0xff; ccpr1h =0xff; while(1); }

//interrupt declaration for the ccp

//declaring as a capture

1.3.3. PWM Objective: The main objective of this in PIC 16f877a Coding: #include<16f877a.h> #use delay(clock=11059200) #byte trisb=0x86 #byte portb=0x06 #byte trisc=0x87 #byte portc=0x07 #byte option=0x81 #byte pr2=0x92 #byte ccpr1h=0x16 #byte ccpr1l=0x15 #byte ccp1con=0x17 #byte t2con=0x12

#bit k1=0x06.3 #bit k2=0x06.4 int8 duty; void main() { trisb=0x1e; trisc=0x00; portb=0x00; portc=0x00;

option=0x00; ccp1con=0x0d; t2con=0x7d; pr2=0xff; duty=0x00;

//declaring as pwm //max periodic value //initilizing the duty cycle

while(1) { if(k1==0) { delay_ms(200); if(duty<5) { duty=0; } else { duty=duty-5; } } if(k2==0) { delay_ms(200); if(duty>(0xff-5)) { duty=0xff; } else { duty=duty+5; } } ccpr1l=duty; delay_ms(20); } }

1.3.4. CAPTURE MODE USING LCD: Objective: The main objective of this in PIC 16f877a. Coding: #include <16f877a.h> #use delay(clock=11059200) #fuses hs,nowdt

#byte trisc=0x87 #byte portc=0x07 #byte trisd=0x88 #byte portd=0x08 #byte trise=0x89 #byte porte=0x09 #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 #bit reg=0x09.0 #bit rw=0x09.1 #bit en=0x09.2 void ret(unsigned int16 n); int16 capt; int i,x[5]; #zero_ram #int_ccp1 ccp1_isr() { relay=~relay; delay_ms(1000); relay=~relay; }

void main() { trisd=0x00; portd=0x00; trisc=0x04; portc=0x00; trise=0x00; porte=0x00;

ccp1con=0x07; ccpr1l=0x00; ccpr1h=0x00; intcon=0xc0; t1con=0x31; tmr1l=0x00; tmr1h=0x00; pie1=0x04;

while(1) { capt=make16(ccpr1h,ccpr1l); ret(capt); } }

void command(unsigned char com) { portd=com; reg=0; rw=0; en=1; delay_ms(1); en=0; } void data(unsigned char da) { portd=da; reg=1; rw=0; en=1; delay_ms(1); en=0;

} void ret(unsigned int16 n) { for(i=0;i<5;i++) { x[i]=n%10; n=n/10; } command(0x8c); data(x[4]|0x30); data(x[3]|0x30); data(x[2]|0x30); data(x[1]|0x30); data(x[0]|0x30); }

1.4.1. USART INTERFERACE :transmission Objective: The main objective of this in PIC 16f877a. Coding: #include <16F877A.h> #use delay (clock=11059200) #byte portb=0x06 #byte trisb=0x86 #byte trisc=0x87 #byte portc=0x07 #byte intcon=0xc0 #byte txreg=0x19 #byte rcreg=0x1a #byte spbrg=0x99 #byte txsta=0x98 #byte rcsta=0x18 #byte pie1=0x8c #bit key1=0x06.1 char a[13]={"GRT EMBEDDED"}; int i=0; void main() { trisb=0xff; trisc=0x80; portb=0x00; portc=0x00; txsta=0x20; porta=0x00; spbrg=0x11; rcsta=0x80; txreg=0x00; while(1) { if(key1==0) { for(i=0;i<13;i++) { txreg=a[i]; delay_ms(100); } }

} 1.4.2. USART INTERFERACE :reception Objective: The main objective of this in PIC 16f877a.

Coding: #include <16F877A.h> #use delay (clock=11059200) #byte trisd=0x88 #byte portd=0x08 #byte trisc=0x87 #byte portc=0x07

#byte intcon=0x0b #byte pir1=0x0c #byte rcsta=0x18 #byte rcreg=0x1a #byte txreg=0x19 #byte pie1=0x8c #byte txsta=0x98 #byte spbrg=0x99 #bit rxt=0x0c.5 int a; void main() { portd=0x00; trisd=0x00; trisc=0x80; portc=0x00; rcsta=0x98; rcreg=0x00; txreg=0x00; txsta=0x20; spbrg=0x11; while(1)

{ if(rxt==1) { rxt=0; a=rcreg; } portd=a; delay_ms(200); } }

1.5. ADC Objective: The main objective of this in PIC 16f877a. Coding: #include <16F877A.h> #device adc=10 #FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT #use delay(clock=11059200)

#byte trisa=0x85 #byte porta=0x05 #byte trisd=0x88 #byte portd=0x08 #byte trise=0x89 #byte porte=0x09 #byte adcon0=0x1f #byte adcon1=0x9f #byte adresh=0x1e #byte adresl=0x9e #byte pir1=0x0c #byte intcon=0x0b #byte cmcon=0x9c #bit GO=0x1f.2

#bit reg=0x09.0 #bit rw=0x09.1 #bit en=0x09.2 int16 value,value1; void function(int16); void command(unsigned char com); void data(unsigned int da); #zero_ram void main() { trisd=0x00; trisa=0x3f; trise=0x00; porte=0x00; portd=0x00; porta=0x00; adresh=0x00;

adresl=0x00; adcon0=0x81; adcon1=0x0e; cmcon = 0x07 ; command(0x38); //set fn command(0x06); //entery mode command(0x0c); //display on command(0x01); //clear display while(1) { delay_ms(1); GO=1; while(GO==1); value=make16(adresh,adresl); command(0x80); data("adc value"); function(value); } }

void command(unsigned char com) { portd=com; reg=0; rw=0; en=1; delay_ms(1); en=0; } void data(unsigned int da) { portd=da; reg=1; rw=0; en=1; delay_ms(1); en=0; } void function(int16 value) { int8 a[4],i; for(i=0;i<4;i++) { a[i]=value%10; value=value/10;

} command(0x8c); data(a[3]|0x30); data(a[2]|0x30); data(a[1]|0x30); data(a[0]|0x30); delay_ms(1000); }

1.6. INTERNAL EEPROM: Objective: The main objective of this program is to write the value in internal eeprom and then read The same thing and display it in led which are inbuild in PIC and verify the outputs. Coding:

1.7.1. I2C Objective: The main objective of this is to receive the real time clock value through i2c communication and display calendar,day,time in PIC 16f877a. Coding: #include <16F877A.h> #use delay(clock=11059200) #byte trisa=0x85 #byte porta=0x05 #byte trisc=0x87 #byte portc=0x07 #byte trisd=0x88 #byte portd=0x08 #byte trise=0x89 #byte porte=0x09 #define rtc_SCL PIN_C3 #define rtc_SDA PIN_C4 #use i2c(master,sda=rtc_SDA,scl=rtc_SCL) #bit reg=0x09.0 #bit rw=0x09.1 #bit en=0x09.2 int8 count=0,sec,sec_lsb,sec_msb; int8 min,min_lsb,min_msb; int8 hrs,hrs_lsb,hrs_msb; int8 day,day_lsb,day_msb; int8 date,date_lsb,date_msb; int8 month,month_lsb,month_msb; int8 year,year_lsb,year_msb;

void write_i2c(unsigned int,unsigned int,unsigned int); unsigned int read_i2c(unsigned int,unsigned int,unsigned int); void rtc_init(); void write_ds1307(unsigned int,unsigned int,unsigned int); unsigned int8 read_ds1307(unsigned int,unsigned int,unsigned int); void refresh();

#zero_ram void command(unsigned char com) { portd=com; reg=0; rw=0; en=1; delay_ms(1); en=0; } void data(unsigned char da) { portd=da; reg=1; rw=0; en=1; delay_ms(1); en=0; } void main() { trisa=0x00; porta=0x00; trisc=0x00; portc=0x00; trisd=0x00; portd=0x00; trise=0x00; porte=0x00; command(0x38); command(0x06); command(0x0c); command(0x01); rtc_init();

while(1) { refresh(); sec=read_ds1307(0xd0,0xd1,0x00); sec_lsb=sec&0x0f; sec_msb=(sec>>4)&0x0f; min=read_ds1307(0xd0,0xd1,0x01); min_lsb=min&0x0f; min_msb=(min>>4)&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");} else if(day_lsb==2) {data("mon");} else if(day_lsb==3) {data("tue");} else if(day_lsb==4) {data("wen");} else if(day_lsb==5) {data("thu");} else if(day_lsb==6) {data("fri");} else if(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<16f877a.h> #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_ram void main() { trisa= 0X00; trisb= 0X1e; trisd= 0X00; porta = 0X00; portb = 0X00; portd = 0X00; option_reg=0x00; for(j=0;j<15;j++) { write_ext_EEPROM(0xa0,j,j*10); // WRITING VALUES ON MEMORY ADDRESS FROM -00H delay_ms(10); } for(i=0;i<15;i++) { k[m] = read_ext_EEPROM(0xa0,0xa1,i); delay_ms(10); m++; } while(1) { if(key1==0) { delay_ms(300); n++; } portd=seg[k[n]/100];sel3=1;delay_ms(1);sel3=0;var=k[n]%100; portd=seg[var/10];sel2=1;delay_ms(1);sel2=0; portd=seg[k[n]%10];sel1=1;delay_ms(1);sel1=0; }

Das könnte Ihnen auch gefallen