configuring analog and digital pins

trebla

Joined Jun 29, 2019
542
Start conversion:
GO_bit =1;

Wait until conversion is done:
while(GO_bit == 1);

Read AD result:
wlevel = ((int)((ADRESH << 8) + ADRESL));
 

Thread Starter

abo zaynab

Joined May 1, 2018
35
To be sure that result will be contain both registers information concatenated to 16 bit integer is better to add casting operator :

wlevel = ((int)((ADRESH << 8) + ADRESL));

where wlevel should be declared as int. If you want display result as float you better use different float-type variable for it.
wlevel is declared as int;
trisa=0x3f;
trisb=0x00;portb=0x00;
trisc=0xff;
trisd=0xff;
trise=0x07;
InitTimer0();
cmcon=0x07;
ADCON0=0b11000001;
ADCON1=0b10001110;also i changed 7th bit in both 0 & 1
delay_ms(2000);
GO_bit=1;delay_ms(1);
if(GO_bit==1){ wlevel = ((int)((ADRESH << 8) + ADRESL));}
AD converter module starts conversion if the Go bit is set, if this bit is clear then conversion is done. You must check this.
When ADON = 1:
1 = A/D conversion in progress (setting this bit starts the A/D conversion which is automatically
cleared by hardware when the A/D conversion is complete)i think its cleared by hardware as in datasheet or should i check it?
 

trebla

Joined Jun 29, 2019
542
should i check it?
You set this bit already by initializing ADCON0 register (it is bit0 in this register). This bit will stay on until you disable it. Only thing you need for starting conversion is setting the GO_bit (bit2 in same register). If AD conversion is done GO_bit is cleared automatically by MCU.
 

Thread Starter

abo zaynab

Joined May 1, 2018
35
When ADON = 1:
1 = A/D conversion in progress (setting this bit starts the A/D conversion which is automatically
cleared by hardware when the A/D conversion is complete)i think its cleared by hardware as in datasheet or should i check it?
ive solved it i used
wlevel=ADC_Get_Sample(0);
instead of adc_read
 
Top