12f683 ADC help


Closed Thread
Results 1 to 4 of 4

Thread: 12f683 ADC help

  1. #1

    Default 12f683 ADC help

    My code is below and turns on/off a led/load on output 0 depending on the pic supply voltage. It was designed using a picaxe chip which works well but I want to migrate to pbp and non picaxe chips.

    The adc part does not seem to be working I suspect my config/defines. Can someone have a look for me please.

    Code:
    '************************* Slave PIC12F683 Pinouts ****************************
    
    '                                    Top					
    '                                   _____
    '(+ Cell Supply)             +Ve -1|  ^  |8- -Ve               (- Cell Supply)
    '(Program In)                Rxd -2|  6  |7- Output 0        (Bypass Load Out)
    '(Slave Data Bus Out)   Output 4 -3|  8  |6- Input 1        (RefAdc In 1.235V) 
    '(Slave Data Bus In)     Input 3 -4|  3  |5- Output 2    (Master Data Bus Out)
    '                                   ----- 
    
    '************************ Slave Module Specification ***************************
    
    'Supply/Cell Voltage 1.75 - 5.00V DC (Pic limits)
    'Average Supply Current at 3.35v <1ma
    'Voltage Ref LM 385 1.235V accuracy 1%
    'Supply/Cell Voltage sensing maximum accuracy +/- 20mv
    'Maximum balancing/bypass current with 15R resistor 333ma at 5.00V 
    'Maximum serial Bus data rate 4800 baud (Picaxe 08M limit at 8mhz)
    'Maximum cell Capacity 650ah (65,000) (Limit of 16bit Word Master Variable)
    'Maximum 128 Slave Modules per Master Module (Master Pic Scratchpad Ram limit)
    'CPU Speed 31khz - 8mhz with internal resonator (Lower speed used for power saving) 
    'Permitted Working Cell Voltage Range 1.75 - 4.30V
    'RefVADC should be 63200 but can be adjusted to compensate for variations. 
    'Note Slave Opto's are Sink driven therefore (Pic Output Low = Opto On, High = Opto Off)
    
    '*******************************************************************************
    '********************** Program Size 376 out of 2048 Words *********************
    '*******************************************************************************
    @ DEVICE PIC12F683,MCLR_OFF
    @ DEVICE PIC12F683,BOD_OFF
    @ DEVICE PIC12F683,PROTECT_OFF
    @ DEVICE PIC12F683,WDT_ON
    
        Define OSC 4			'set processor speed to 4 Mhz  
        INTCON = %10001000      'internal oscillator
        OSCCON = %01100111		'sets internal osc to 4 Mhz and stable
        CMCON0 = 7              'Comparators off
        GPIO = %00000000        'outputs low
        TRISIO = %00001010      'Set Pins GPIO.1 & GPIO.3 as inputs
        ANSEL = %00110010       'AN1 Analog Input
        ADCON0 = %10000101		'turns A/D on, A/D reads pin 6(AN1), right justify and Vref=Vdd
    
    'Define ADCIN parameters
    
        Define ADC_BITS 10 ' Set number of bits in result
        Define ADC_CLOCK 3 ' Set clock source (3=rc)
        Define ADC_SAMPLEUS 50 ' Set sampling time in uS
    
    
    
    '------------------------------ General configuration --------------------------
    	include "bs1defs.bas"		'allows the use basic stamp variables and "modedefs.bas" for n2400 command
        	
    '*******************************************************************************
    
    
    'Variables Constants and I/O definitions
    
    '------------------------ Variables 16bit --------------------------------------
    
    CellV	var	Word 	'Corrected Cell voltage (16bit value)
    RefADC	var	Word 	'Raw Adc input data variable range 0-1023 10bit
    CellA	var	Word 	'Cell average voltage last 10 measurements
    
    '----------------- Variables 8bit --------------------------------------------
    
    VData	var	CellV.byte0     'VData is low byte of Word Var CellV 
    CountB  var byte			'General 0-255 byte ADC loop counter variable
    
    '------------------- Constants -----------------------------------------------
    
    RefVADC	con	63200	'Fixed Ref Voltage Calibration LM385 1.235v * 1023 * 100 / 2 = 63200
    CutInV	con	365		'Balancing load/bypass cut in Voltage  (This must match value set in Master)
    CutOutV	con	360		'Balancing load/bypass cut out Voltage (This must match value set in Master)
    Delay	con	5		'Data delay time in milliseconds 
    DLow	con	175		'Cell correction value 175 or 1.75V subtracted from CellVoltage 
    DHigh	con	430		'Cell correction value 430 or 4.30V (If Cell V>430 or <175 then error)
    
    '-------------- Pins used for I/O and designations -------------------------
    
    '---------------- Digital high/low Outputs ------------------------
    
    Load		var	GPIO.0	'2W 15R Transistor driven load/bypass resistor & optional led indicator on Output 0 (pin 7)
    MasterBus	var GPIO.2	'Master Data Bus Output Baud4800 on Output 2 (pin 5)
    SlaveBusIn	var	GPIO.3	'Slave Data Bus Input Baud4800 on Input 3 (pin 4)
    SlaveBusOut	var GPIO.4	'Slave Data Bus Output Baud4800 on Output 4 & optional led indicator (pin 3)
    
    '---------------- Analogue ADC Inputs ------------------------------
    
    RefInput	var	GPIO.1	'LM385 1.235V RefAdc on Input 1 (pin 6)
    
    '*******************************************************************************
    
    Start:							'Main program start
    
    	high SlaveBusOut			'Turn off interrupt signal for next Slave (SlaveBusOut)
    	high MasterBus				'Turn off MasterBus Optocoupler (Reqd due to sink driven)
    	low Load					'Turn off by-pass resistor
    
    Main:							'Main program loop label
    	
    	CellA = 0 					'Set Cell Average word variable to 0 (Zero)
    	
    	for CountB = 1 to 10		'10x ADC Oversampling loop counter
    	adcin RefInput, RefADC		'Measure cell voltage on RefInput pin and store in RefADC
    	CellA = CellA + RefADC		'Add latest ADC reading to running total
    	next CountB					'Repeat loop until 10 ADC readings obtained
    	
    	CellA = CellA / 10			'Calculate average ADC reading for last 10 readings
    
    	CellV = RefVADC / CellA     'Calculate Cell Voltage from the average ADC LM385 VRef 1.235v reading
    	
    	CellV = CellV * 2           'Calculate Cell Voltage from the average ADC LM385 VRef 1.235v reading     
    	
    	if CellV > CutInV then		'If Cell V > CutInV then
    	high Load					'Turn on bypass resistor and bypass led
    	endif
    	
    	if CellV < CutOutV then		'If Cell V < CutOutV then
    	low Load					'Turn off bypass resistor and bypass led
    	endif
    
        if slavebusIn = 0 then Main 'If no interrupt signal goto Main
    
    	if CellV >DHigh or CellV < DLow then	'If V >4.30V or V <1.75V set out of range (b0=0)	
    	VData = 0							'Set VData = 0 Cell error out of Voltage range condition
    	else
    	CellV = CellV - DLow			 	'Convert Word (CellV) data into Byte (VData) for output
    	endif                       	
    
    Loop1:
    	if slavebusIn = 1 then Loop1 	    'Wait until interrupt signal (pin 4) is low before begining transmission
    	serout MasterBus,T9600,[VData]		'Send VData (b0) to Master at Baud9600
        low SlaveBusOut						'Turn on interrupt signal for next Slave (SlaveBusOut) 
    	Pause Delay							'Allow time for next Slave and Master to be ready
    	high SlaveBusOut					'Turn off interrupt signal for next Slave (SlaveBusOut)
        
        goto Main					'Goto start of Main loop
    My schematic is here if it helps.

    www.solarvan.co.uk/bms/DigitalSlave080908.jpg

  2. #2
    Join Date
    Apr 2006
    Location
    GearSweaterMountain, The Netherlands
    Posts
    52


    Did you find this post helpful? Yes | No

    Default

    Hi,

    I sometimes have problems with adc_in. Try this piece of code where you want to adc_in:

    Code:
    ADCON0.1 = 1   'Start ADC conversion                                   
    while ADCON0.1 = 1 :wend   'Wait for ADC DONE
    
    RefADC.highbyte = ADRESH   'Move HIGH byte of result to adcVAL
    RefADC.lowbyte = ADRESL   'Move LOW byte of result to adcVAL
    CellA = CellA + RefADC		'Add latest ADC reading to running total
    Succes,
    UB

  3. #3


    Did you find this post helpful? Yes | No

    Default Thanks

    Quote Originally Posted by ultiblade View Post
    Hi,

    I sometimes have problems with adc_in. Try this piece of code where you want to adc_in:

    Code:
    ADCON0.1 = 1   'Start ADC conversion                                   
    while ADCON0.1 = 1 :wend   'Wait for ADC DONE
    
    RefADC.highbyte = ADRESH   'Move HIGH byte of result to adcVAL
    RefADC.lowbyte = ADRESL   'Move LOW byte of result to adcVAL
    CellA = CellA + RefADC		'Add latest ADC reading to running total
    Succes,
    UB
    That seems to be doing the trick! Cheers.

  4. #4
    Join Date
    Apr 2006
    Location
    GearSweaterMountain, The Netherlands
    Posts
    52


    Did you find this post helpful? Yes | No

    Smile

    Great !

    Regards,
    UltiBlade

Similar Threads

  1. Stable Adc Reading Routine
    By gebillpap in forum General
    Replies: 27
    Last Post: - 13th May 2015, 02:18
  2. 10 bit ADC display on LCD using 16f873
    By pr2don in forum mel PIC BASIC
    Replies: 3
    Last Post: - 6th March 2010, 18:29
  3. Can't get ADC to loop
    By TravisM in forum mel PIC BASIC
    Replies: 2
    Last Post: - 11th October 2009, 15:33
  4. ADC value with 2 decimals on an LCD
    By Squibcakes in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 2nd December 2005, 15:54
  5. 12F675 ADC 'Issues'
    By harrisondp in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 31st March 2005, 01:55

Members who have read this thread : 2

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts