11. PRÁTICA 11: USO DE MEMÓRIA EEPROM COM BOTÃO E CONTROLE DO LED
Referencial Teórico: Operadores de atribuição; Funções; Memórias do Microcontrolador.
Objetivo: Controlar o estado de funcionamento de um led presente na placa, por meio da gravação e leitura de dados em memória não volátil.
Materiais: Utilizar led e botão presentes na placa e memória EEPROM interna.
11.1 Arquivo Principal
#include "iniciog.h"
#include "pratica.h"
void main(){
configura_portas();
while(1){
if (botao == 1){
escrever_memoria(0x0A,'H');
}
if (botao == 0){
escrever_memoria(0x0A,'L');
}
ler_memoria(0x0A);
if (dado == 'H') led = 1;
if (dado == 'L') led = 0;
}
}
11.2 Arquivo Secundário
// acrescentar ao iniciog.h ou arquivo pratica.h
unsigned char escrever_memoria(unsigned char endereco, unsigned char dado){
EEADR = endereco;
EEDATA = dado;
EECON1bits.EEPGD = 0;
EECON1bits.CFGS = 0;
EECON1bits.WREN = 1;
INTCONbits.GIE = 0;
EECON2 = 0x55;
EECON2 = 0xAA;
EECON1bits.WR = 1;
while(EECON1bits.WR);
INTCONbits.GIE = 1;
EECON1bits.WREN = 0;
}
unsigned char dado;
unsigned char ler_memoria(unsigned char endereco){
EEADR = endereco;
EECON1bits.EEPGD = 0;
EECON1bits.CFGS = 0;
EECON1bits.RD = 1;
dado = EEDATA;
return dado;
}