aplikasi input dan eeprom internal pada pic 16f877


kita akan membuat aplikasi count up dan count down. untuk inputnya berupa push button terhubung ke RB0 dan ke RB1.

Setiap RB1 di tekan nilai counter akan naik kemudian disimpan ke eeprom internal. dan setiap RB1 di tekan nilai counter akan turun kemudian disimpan keeeprom internal. hasil counter akan ditampilkan di 7 segment 3 digit.

untuk setting port sebagai input yaitu dengan memberi nilai 1. berikut contoh SYNTAX untuk RB0 dan RB1 di setting sebagai input

 

TRISB0=1;

TRISB1=1;

 

untuk membaca port RB0 dan RB1 syntaxnya seperti ini

if(RB0==0) //jika RB0 di tekan maka
{

}

//=======================================================================

pada xc8 untuk akses eepeom internal sudah terdapat librarynya.  syntax untuk menulis dan membaca eeprom internal.

temp1=eeprom_read(0); // untuk baca alamat ke 0

eeprom_write(0,temp1); // untuk tulis alamat ke 0 dengan data pada temp1

//==========================================================================

berikut contoh program keseluruhan yang akan kita buat

#include <xc.h>

// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
#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 = OFF // Low Voltage In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#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)
#define _XTAL_FREQ 16000000
#define data_hi PORTD|=(1<<0)
#define data_lo PORTD&=~(1<<0)
#define strobe_hi PORTD|=(1<<2)
#define strobe_lo PORTD&=~(1<<2)
#define clock_hi PORTD|=(1<<1)
#define clock_lo PORTD&=~(1<<1)
unsigned char temp,temp1;
unsigned char i;
unsigned char buff[10];
const unsigned char textdata[] ={
//dot g f e d c b a
0b11000000,
0b11111001,
0b10100100,
0b10110000,
0b10011001,
0b10010010,
0b10000010,
0b11111000,
0b10000000,
0b10010000,
};
//===================================================================
//74595
//=====================================================================
void clock_74595()
{
clock_hi;
__delay_us(1);
clock_lo;
}

void strobe_74595()
{
strobe_hi;
__delay_us(10);
strobe_lo;

}
void kirimbyte74595(unsigned char antrian)
{
unsigned char dataled;
dataled=antrian;
if(dataled&(1<<7))data_hi;else data_lo;
clock_74595();
if(dataled&(1<<6))data_hi;else data_lo;
clock_74595();
if(dataled&(1<<5))data_hi;else data_lo;
clock_74595();
if(dataled&(1<<4))data_hi;else data_lo;
clock_74595();
if(dataled&(1<<3))data_hi;else data_lo;
clock_74595();
if(dataled&(1<<2))data_hi;else data_lo;
clock_74595();
if(dataled&(1<<1))data_hi;else data_lo;
clock_74595();
if(dataled&(1<<0))data_hi;else data_lo;
clock_74595();
}
void tampil()
{
temp=temp1;
buff[2]=(temp/100);
buff[1]=((temp%=100)/10);
buff[0]=(temp%10);
kirimbyte74595(textdata[buff[0]]);
kirimbyte74595(textdata[buff[1]]);
kirimbyte74595(textdata[buff[2]]);
strobe_74595();

}
//=================================================================
int main()
{
__delay_ms(100);
TRISB0=1;
TRISB1=1;

TRISD=0;
PORTD=0;

data_lo;
strobe_lo;
clock_lo;

temp1=eeprom_read(0);
tampil();
__delay_ms(1000);

while(1)
{
if(RB0==0)
{
temp1++;
temp=temp1;
eeprom_write(0,temp1);
tampil();
__delay_ms(500);
}
else if(RB1==0)
{
temp1–;
temp=temp1;
eeprom_write(0,temp1);
tampil();
__delay_ms(500);
}
}
}

 

berikut hasil simulasi proteusnya

 

input

hasil penulisan eeprom di alamat 0

eeprom

 

 

 

 

 

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s