PDA

View Full Version : Can't get ADC to loop



TravisM
- 9th October 2009, 01:37
'The following program is written in PICBasic for the PIC16F690.
'The microcontroller monitors the analog voltage on AN0. The equivalent binary output
'is placed in bytes B0 and B1. The problem is that the program converts the analog
'voltage on AN0 one time and stops. Sometimes, after power up, the program loops through
'the code as it should for a couple of seconds, then stops. Seting the RESET low causes
'the program to loop a couple of times, then stops. I have included code to set RB6 low,
'then high to test the program looping. I know that I have included more code than
'necessary, but in desperation, I have thrown in everthing I can think of. But apparently,
'I have not included the right thing. Please help!!


symbol TRISA = 133
SYMBOL TRISB = 134
SYMBOL TRISC = 135
SYMBOL PORTA = 05
SYMBOL PORTB = 06
SYMBOL PORTC = 07
symbol ANSEL = 286
SYMBOL ANSELH = 287
SYMBOL WPUA = 149
SYMBOL IOCA = 150
SYMBOL WPUB = 277
SYMBOL IOCB = 278
SYMBOL ADCON0 = 31
SYMBOL ADCON1 = 159
symbol OSCCON = 143
symbol ADRESH = 30
SYMBOL ADRESL = 158
SYMBOL CCP1CON = 23
SYMBOL SSPCON = 20
SYMBOL SSPBUF = 19
SYMBOL SSPSTAT = 148
SYMBOL OPTION_REG = 129
SYMBOL INTCON = 11
SYMBOL PIE1 = 140
SYMBOL PIR1 = 12
SYMBOL T2CON = 18
SYMBOL CCPR1L = 21

POKE T2CON, 0
POKE CCP1CON, %00000000 'Turn off capture, compare, pwm
POKE OSCCON, %01100111 'selects 4Mz clock, sets internal oscillator,
poke SSPCON, %00010000 'DISABLES SSP, CONFIG FOR I/O monitors clock status
POKE WPUB, %11110000 'ENABLE WPU FOR PORTB
POKE IOCB, 0 'DISABLE INTERUPT ON CHANGE
poke trisA, %00111111 'set PORTA as inputs
poke trisb, %00000000 'set PORTB & C as outputs
POKE TRISC, %00000000
poke ANSEL, %00000001 'SET RA0 TO ANALOG, THE REST TO DIGITAL INPUTS
POKE ANSELH, 0 'SET ALL OF THE ANALOG SELECT HIGH REGISTERS TO DIGITAL INPUTS
POKE OPTION_REG, %00000111 'PRESCALLER IS ASSIGNED TO timer0
POKE WPUA, %00110111 'ENABLE WEAK PULL-UPs for PORTA
poke IOCA, 0 'disable interupt on change
POKE PIE1, 0 'disable periphial interupts
poke PIR1, 0 'clear all interrupts
poke portb, 1 '!!NOTE!! set bit 0 of port b for testing

POKE ADCON1, %00010000 'sets ADC conversion clock to Fosc/8; Fosc = 4, (4Mhz/8)

CONVERT:

poke INTCON, 0
poke PIR1, 0 'CLEAR ADIF BIT
POKE PIE1, 0
low 6
poke ADCON0, %10000001 'right justified, Vdd reference, bits 2-5 selects AN0 as analog
pause 30 'input pin, ADC enabled, bit 1 must be set
'(on a seperate cmd line) to start conversion.

POKE ADRESL, 0 'CLEAR ADRESL & H
POKE ADRESH, 0
poke ADCON0, %10000011 'set bit 1 to start conversion, bits 2 - 5 selects analog input
'bit 0 enables ADC, bit 7 selects right justified
pause 30 'wait for ADC to convert
ChkConv:
PEEK ADCON0, B2 'Check for bit 1 cleared to see if conversion is complete
if bit1 = 0 then ReadADC 'conversion is complete if bit 1 = 0, go read ADC output
pause 30
goto ChkConv 'conversion not complete, check again

ReadADC:
peek ADRESH, B1 'read hi byte of result (only bits 0 & 1 valid)
B1 = B1 & %00000011 'null out all bits other than bits 0 & 1
peek ADRESL, B0 'read low byte of result
pause 30

poke portc, B0 'NOTE!!TESTING ADC, READ THE LO BYTE
high 6 '!!TEST
pause 30 '!!TEST
goto CONVERT 'NOTE!!TESTING ADC

end

Darrel Taylor
- 9th October 2009, 04:17
ChkConv:
PEEK ADCON0, B2 'Check for bit 1 cleared to see if conversion is complete
if bit1 = 0 then ReadADC 'conversion is complete if bit 1 = 0, go read ADC output
pause 30
goto ChkConv 'conversion not complete, check again

The predefined symbol BIT1 is located in the B0 variable.
But you've read ADCON0 into B2, so it's not looking at the correct bit for GO/DONE.
<br>

TravisM
- 11th October 2009, 15:33
Thanks Darrel;

I changed the variable from B2 to B0 and now the program works fine.

Thanks again, TravisM