Reputation: 1725
So I have a PIC 16F88, and I managed to do a couple things with it, but now I was wondering how to get analog to digital conversion?
I've tried many examples out there in the internet, but no success :( if anyone could give a light on how to use the ADCON0/1 and stuff like that, I will appreciate.
Thanks a lot.
Edit: this is my code:
START
banksel TRISA
clrf TRISB
movlw 0xff
movwf TRISA
movlw b'11000111'
movwf OPTION_REG
movlw b'00000001'
movwf ADCON1
banksel PORTB
clrf PORTB
LOOP
btfss PIR1,ADIF
goto LOOP
bsf ADCON0,GO
WAIT
btfsc ADCON0,GO
goto WAIT
movf ADRESH,W
movwf PORTB,W
END
Upvotes: 0
Views: 5200
Reputation: 989
From the PIC datasheet:
The ADRESH:ADRESL registers contain the 10-bit result of the A/D conversion. When the A/D conversion is complete, the result is loaded into this A/D result register pair, the GO/DONE bit (ADCON0<2>) is cleared and the A/D interrupt flag bit ADIF is set. The block diagram of the A/D module is shown in Figure 11-1.
After the A/D module has been configured as desired, the selected channel must be acquired before the conversion is started. The analog input channels must have their corresponding TRIS bits selected as inputs. To determine sample time, see Section 11.1. After this acquisition time has elapsed, the A/D conversion can be started. The following steps should be followed for doing an A/D conversion:
- Configure the A/D module: • Configure analog pins / voltage reference / and digital I/O (ADCON1) • Select A/D input channel (ADCON0) • Select A/D conversion clock (ADCON0) • Turn on A/D module (ADCON0)
- Configure A/D interrupt (if desired): • Clear ADIF bit • Set ADIE bit • Set GIE bit 3. Wait the required acquisition time.
- Start conversion: • Set GO/DONE bit (ADCON0)
- Wait for A/D conversion to complete, by either: • Polling for the GO/DONE bit to be cleared OR • Waiting for the A/D interrupt
- Read A/D Result register pair (ADRESH:ADRESL), clear bit ADIF if required.
- For next conversion, go to step 1 or step 2 as required. The A/D conversion time per bit is defined as TAD. A minimum wait of 2TAD is required before next acquisition starts.
You are not waiting the acquisition time before starting the conversion. Everything else seems right.
Upvotes: 0