


program electronique_pic_tuto_base_can_001a;
var
wIn: word; // variable contenant la valeur numérisée de la tension analogique
procedure Init;
begin
OPTION_REG := %10000000; // pullup désactivé (bit 7 à 1)
TRISIO.0 := 1; // GP0 configuré en entrée (analogique ou numérique)
TRISIO.1 := 0; // GP1 configuré en sortie, commande led D4
TRISIO.2 := 0; // GP2 configuré en sortie, commande led D3
TRISIO.3 := 1; // GP3 non utilisé, configuré en entrée
TRISIO.4 := 0; // GP4 configuré en sortie, commande led D2
TRISIO.5 := 0; // GP5 configuré en sortie, commande led D1
CMCON := %00000111; // désactivation comparateurs, on peut aussi écrire CMCON := 7;
ANSEL.ANS0 := 1; // utilisation de l'entrée GPIO.0 comme entrée analogique (AN0)
ANSEL.ANS1 := 0; // utilisation de l'entrée GPIO.1 comme sortie numérique (GP1)
ANSEL.ANS2 := 0; // utilisation de l'entrée GPIO.2 comme sortie numérique (GP2)
ANSEL.ANS3 := 0; // utilisation de l'entrée GPIO.4 comme sortie numérique (GP4)
ADCON0.ADFM := 1; // 0 = left justified, 1 = right justified
ADCON0.VCFG := 0; // tension de référence = VDD
ADCON0.ADON := 1; // activation du module convertisseur A/D
ADCON0.ADCS0 := 1; // |
ADCON0.ADCS1 := 0; // | ADCS = 100 = Fosc/4
ADCON0.ADCS2 := 0; // |
GPIO.1 := 0;
GPIO.2 := 0;
GPIO.4 := 0;
GPIO.5 := 0;
end;
// main program
begin
Init;
while true do
begin
wIn := ADC_Read(0); // lecture entrée analogique AN0
GPIO.1 := ((wIn >= 767) and (wIn < 1024)); // led D4 : +3.75V < Vin < +5V
GPIO.2 := ((wIn >= 512) and (wIn < 767)); // led D2 : +1.25V < Vin < +2.5V
GPIO.4 := ((wIn >= 255) and (wIn < 512)); // led D3 : +2.5V < Vin < +3.75V
GPIO.5 := ((wIn >= 0) and (wIn < 255)); // led D1 : 0V < Vin < +1.25V
delay_ms(300);
end;
end.
// main program
begin
Init;
while true do
begin
wIn := ADC_Read(0); // lecture entrée analogique AN0
// led D1 : 0V < Vin < +1.25V
if ((wIn >= 0) and (wIn < 255)) then
begin
GPIO.1 := 0;
GPIO.2 := 0;
GPIO.4 := 0;
GPIO.5 := 1;
end
// led D2 : +1.25V < Vin < +2.5V
else if ((wIn >= 255) and (wIn < 512)) then
begin
GPIO.1 := 0;
GPIO.2 := 0;
GPIO.4 := 1;
GPIO.5 := 0;
end
// led D3 : +2.5V < Vin < +3.75V
else if ((wIn >= 512) and (wIn < 767)) then
begin
GPIO.1 := 0;
GPIO.2 := 1;
GPIO.4 := 0;
GPIO.5 := 0;
end
// led D4 : +3.75V < Vin < +5V
else if ((wIn >= 767) and (wIn < 1024)) then
begin
GPIO.1 := 1;
GPIO.2 := 0;
GPIO.4 := 0;
GPIO.5 := 0;
end;
delay_ms(300);
end;
end.