I am feeling lazy so I will do an article later. For now here is some stuff to play with. I found that I am out of 684s so I did this with a 676. Should work with the 684.
For now we will look at 8 bit ADC. Using 8 bit the signal is divided into 256 steps, 0 - 255.
The ADC will return 0 - 255. If you use this to read a voltage and you are using 5 volts for the VDD/reference, 1 volt will return 51, 2 volts will return 102, 3 volts.....
You will not find this way of doing the ADC in the manual, but if you follow along with the data sheet you will see what is happening with the registers.
I wrote the examples for the PicKit1 test board. Using the on-board POT and LEDs.
Example #1
Code:
'FL PIC16F676
'16F676 ADC ON PICKIT_1
DEFINE OSC 4
@ __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _CP_OFF
ANSEL = %00000001 'CHANNEL AN0
ADCON1 = %00010000 'FOSC/8
TRISA = %00000001
CMCON = %00000111
CHAN0 VAR BYTE 'VAR TO HOLD ADC0 READING
CHECK:
GOSUB ADC_0
IF CHAN0 > 128 THEN
GOSUB BLINK
ELSE
PORTA = %00000000
ENDIF
GOTO CHECK
BLINK:
D0_HIGH:
TRISA = %11001111
PORTA = %00010000
PAUSE 500
D1_HIGH:
TRISA = %11001111
PORTA = %00100000
PAUSE 500
D2_HIGH:
TRISA = %11101011
PORTA = %00010000
PAUSE 500
D3_HIGH:
TRISA = %11101011
PORTA = %00000100
PAUSE 500
RETURN
ADC_0: 'READ AN0
ADCON0 = %00000001 'TURNS ADC ON, SET FOR 8 BIT
GOSUB READ_AD
CHAN0 = ADRESH
RETURN
READ_AD: 'DOES THE ADC CONVERSION
PAUSE 50
ADCON0.1=1
WHILE ADCON0.1=1:WEND
RETURN
Example #2
Code:
'FL PIC16F676
'16F676 ADC ON PICKIT_1 WITH CASE SELECT
DEFINE OSC 4
@ __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _CP_OFF
ANSEL = %00000001 'CHANNEL AN0
ADCON1 = %00010000 'FOSC/8
TRISA = %00000001
CMCON = %00000111
CHAN0 VAR BYTE 'VAR TO HOLD ADC0 READING
CHECK:
GOSUB ADC_0
GOSUB BLINK
GOTO CHECK
BLINK:
SELECT CASE CHAN0
CASE IS > 254
TRISA = %11001111
PORTA = %00010000
CASE IS > 192
TRISA = %11001111
PORTA = %00100000
CASE IS > 128
TRISA = %11101011
PORTA = %00010000
CASE IS > 64
TRISA = %11101011
PORTA = %00000100
CASE ELSE
PORTA = %00000000
END SELECT
RETURN
ADC_0: 'READ AN0
ADCON0 = %00000001 'TURNS ADC ON, SET FOR 8 BIT
GOSUB READ_AD
CHAN0 = ADRESH
RETURN
READ_AD: 'DOES THE ADC CONVERSION
PAUSE 50
ADCON0.1=1
WHILE ADCON0.1=1:WEND
RETURN
Bookmarks