Steves
- 30th May 2009, 07:13
Hi, I am very new to all of this, hope someone can set me straight. I started a project with a 16F628 and was goin OK but decided I needed to read a battery voltage and swapped to a 16F88. To test the A/D conversion I used the following code.
I am using PBP V2.45 but have just ordered upgrade.
INCLUDE "modedefs.bas" ' Include serial modes
' port A used for LCD
Define LCD_DREG PORTA ' RA0 to RA3 is data pin for LCD
Define LCD_DBIT 0
Define LCD_RSREG PORTA ' RA6
Define LCD_RSBIT 6
Define LCD_EREG PORTA ' RA4
Define LCD_EBIT 4
Define LCD_BITS 4
DEFINE OSC 4
'define A/D setup
Define ADC_BITS 8
define ADC_SAMPLEUS 100
@ DEVICE pic16F88, INTRC_OSC_NOCLKOUT ' system clock options
Ai var PORTB.6 'input to chanel 6 A/D
bat var word 'battery voltage off load
bat1 var word 'battery voltage on load
led var PORTB.0 'speaker/led output
OSCCON = %01101110 'internal clock, is primary clk @ 4 Mhz
CMCON =7 ' Comparator off
TRISA = 0 ' Port A all outputs
TRISB = %11111110 'Port B all inputs 0
ANSEL = %01100000 'all digital except chanel 5&6 which are inputs (analog)
ADCON0 = %00101001 'Frc internal clock, chanel 5 is A/D (RB6/AN5)
ADCON1 = %01000000 'left justify,divide clk by 2, Vref is VDD and VSS
low led 'turn off led
' Allow time for LCD startup
pause 2000
start:
LCDOut $FE,1 ' Clear LCD screen
LCDOut "Goto Bat_Volts" ' See where we are
pause 1000
'test that we loop
toggle led
pause 250
toggle led
pause 250
'Start program ***************************************8
goto Bat_Volts
'check battery off load
Bat_Volts:
LCDOut $FE,1 ' Clear LCD screen
LCDOut "ADCON0 = "
LCDOut $FE,$C0
lcdout #ADCON0 'Display ADCON0
pause 2000
LCDOut $FE,1 ' Clear LCD screen
LCDOut "Before ADC" 'Check where we are
pause 1000
ADCIN Ai, bat 'read A/D chanel 6 and store value to bat
while ADCON0.1=1:WEND ' Check to make sure ADC conversion finished..
LCDOut $FE,1 ' Clear LCD screen
LCDOut "After ADC" 'Check where we are
pause 1000
LCDOut $FE,1 ' Clear LCD screen
LCDOut "ADCON0 = "
LCDOut $FE,$C0
lcdout #ADCON0 'Display ADCON0
pause 2000
LCDOut $FE,1 ' Clear LCD screen
LCDOut "Battery = ",#bat ' Display Voltage
LCDOut $FE,$C0
lcdout "Volts"
pause 2000
goto start
end 'End of program
When I run it I only read 255 whether inputing VDD or VSS, and ADCON0 is 41 before the ADCIN command and 193 after.
Have been thru the data sheet and thought I had all registers set up correctly but maybe not.
This is a lot of fun but I don't get much sleep these days.Thanks for any help.
Steve
I am using PBP V2.45 but have just ordered upgrade.
INCLUDE "modedefs.bas" ' Include serial modes
' port A used for LCD
Define LCD_DREG PORTA ' RA0 to RA3 is data pin for LCD
Define LCD_DBIT 0
Define LCD_RSREG PORTA ' RA6
Define LCD_RSBIT 6
Define LCD_EREG PORTA ' RA4
Define LCD_EBIT 4
Define LCD_BITS 4
DEFINE OSC 4
'define A/D setup
Define ADC_BITS 8
define ADC_SAMPLEUS 100
@ DEVICE pic16F88, INTRC_OSC_NOCLKOUT ' system clock options
Ai var PORTB.6 'input to chanel 6 A/D
bat var word 'battery voltage off load
bat1 var word 'battery voltage on load
led var PORTB.0 'speaker/led output
OSCCON = %01101110 'internal clock, is primary clk @ 4 Mhz
CMCON =7 ' Comparator off
TRISA = 0 ' Port A all outputs
TRISB = %11111110 'Port B all inputs 0
ANSEL = %01100000 'all digital except chanel 5&6 which are inputs (analog)
ADCON0 = %00101001 'Frc internal clock, chanel 5 is A/D (RB6/AN5)
ADCON1 = %01000000 'left justify,divide clk by 2, Vref is VDD and VSS
low led 'turn off led
' Allow time for LCD startup
pause 2000
start:
LCDOut $FE,1 ' Clear LCD screen
LCDOut "Goto Bat_Volts" ' See where we are
pause 1000
'test that we loop
toggle led
pause 250
toggle led
pause 250
'Start program ***************************************8
goto Bat_Volts
'check battery off load
Bat_Volts:
LCDOut $FE,1 ' Clear LCD screen
LCDOut "ADCON0 = "
LCDOut $FE,$C0
lcdout #ADCON0 'Display ADCON0
pause 2000
LCDOut $FE,1 ' Clear LCD screen
LCDOut "Before ADC" 'Check where we are
pause 1000
ADCIN Ai, bat 'read A/D chanel 6 and store value to bat
while ADCON0.1=1:WEND ' Check to make sure ADC conversion finished..
LCDOut $FE,1 ' Clear LCD screen
LCDOut "After ADC" 'Check where we are
pause 1000
LCDOut $FE,1 ' Clear LCD screen
LCDOut "ADCON0 = "
LCDOut $FE,$C0
lcdout #ADCON0 'Display ADCON0
pause 2000
LCDOut $FE,1 ' Clear LCD screen
LCDOut "Battery = ",#bat ' Display Voltage
LCDOut $FE,$C0
lcdout "Volts"
pause 2000
goto start
end 'End of program
When I run it I only read 255 whether inputing VDD or VSS, and ADCON0 is 41 before the ADCIN command and 193 after.
Have been thru the data sheet and thought I had all registers set up correctly but maybe not.
This is a lot of fun but I don't get much sleep these days.Thanks for any help.
Steve