Bonjour à tous (et toutes ?)
Voici une fonction de lecture d'un port numérique "ch", avec ch compris entre 0 et 11. Tous les ports sont pris en compte, même 8-11, contrairement à certains exemples, qui se plantent quand ch > 7
unsigned int ad_in(unsigned char ch)
{
unsigned int i;
i=0;
if (ch>11) ch=0; // if wrong ch value, set it to 0
adcon0 = 0x80;
if (ch>7)
{
adgsel0=1; // select upper channel set ad8-11
ch=ch-4; // adjust AD0-AD2 bits
}
adcon0 = adcon0 + ch; // init channel number
adcon1 = 0x28; // init adcon1 reg, 10-bits mode
adst = 1; // conversion start
while((adst == 1) && (i<50000)) i++; // wait end of conversion, or timeout
return ad; // output AD value
}
/* ne pas oublier de configurer les ports AN utilisés en entrée dans la fonction main :
par exemple : */
main ()
{
pd1 = 0b11000000; // Set Port 1 direction (0 = IN)
prc2 = 1; // protect off
pd0 = 0b11111101 // port 0 direction
}
