

program electronique_pic_tuto_base_eeprom_001a;
var
iValue: byte;
// initialisation générale
procedure Init;
begin
TRISA := %00000000; // port A configuré en sortie
TRISB := %11111111; // port B configuré en entrée
CMCON := $07; // désactivation comparateurs
OPTION_REG.NOT_RBPU := 0; // mise en service pullup
iValue := 0;
end;
// clignotement led
procedure Led_Blink(iCount: byte);
var
i: byte;
begin
for i := 1 to iCount do
begin
PORTA.0 := 1;
Delay_ms(200);
PORTA.0 := 0;
Delay_ms(200);
end;
end;
// sauvegarde dans EEProm
procedure Value_Save(iVal: byte);
begin
iValue := iVal;
EEProm_Write(0, iValue);
Led_Blink(iValue);
end;
// lecture dans EEProm
procedure Value_Load;
begin
iValue := EEProm_Read(0);
if iValue > 4 then
iValue := 0;
Led_Blink(iValue);
end;
// programme principal
begin
Init;
Value_Load;
while true do
begin
if Button(PORTB, 0, 50, 0) then Value_Save(1);
if Button(PORTB, 1, 50, 0) then Value_Save(2);
if Button(PORTB, 2, 50, 0) then Value_Save(3);
if Button(PORTB, 3, 50, 0) then Value_Save(4);
Delay_ms(100);
end;
end.