Math question relating to AD input on PIC12F675


Results 1 to 13 of 13

Threaded View

  1. #6
    Join Date
    Oct 2010
    Posts
    12


    Did you find this post helpful? Yes | No

    Default

    OK.... that was a long night!!

    I've finished the code and tested the circuit on a breadboard. After getting my head on straight, I was able to revise the main code quite easily, even for me!!

    The headache and long hours occurred when I just couldn't get the A/D working correctly. I spent hours going over the data sheet and dozens of forum messages, tried different combination's of Defines and register settings but never got it working... UNTIL... I saw that the PAUSE statement was expecting a 16 bit argument. At least that is the way that I understand it. So, based upon that, I changed my code from being setup for 8 bit resolution to 10 bit and everything purred like a kitten. Got to bed at 6am with a smile on my face!!

    Bruce, if you happen to read this, I strongly believe that your proposed book would have saved me many hours of head-scratching by translating the data sheet into easier to understand terminology. So, don't give up on it.

    Thanks to all who post on this forum for giving me the use of your knowledge and helping us all learn from your own trials and your successes.

    My extremely complex, yet simple code is available for scrutiny.

    p.s. Special thanks to Louie and Al for responding to my original post and helping me much more than they probably realize. THANKS!!!

    Code:
    '****************************************************************
    '*  Name    : Train_Auto-Reverser.pbp                           *
    '*  Author  :                                                   *
    '*  Notice  : Copyright (c) 2010                                *
    '*          : All Rights Reserved                               *
    '*  Date    : 10/27/2010                                        *
    '*  Version : 1.0                                               *
    '*  Notes   : Model train auto-reversing circuit using          *
    '*          : a PIC12F675 and reed switch sensors.              *
    '****************************************************************
    
    
    '   ---------Configuration--------
    
    ' PICBasic Pro v2.6a default configuration for PIC12F675
    '   __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_ON & _CP_OFF
    ' Changed MCLR function to Input Pin during programming of PIC.
    
    
    '   ---------Defines/Constants-------
    
    DEFINE OSCCAL_1K 1              ' Calibrate internal oscillator
    
    DEFINE ADC_BITS 10              ' Set number of bits in A/D result
    DEFINE ADC_CLOCK 3              ' Set clock source (3=rc)
    DEFINE ADC_SAMPLEUS 50          ' Set sampling time in uS
    
    RY1 CON 4                       ' Relay 1 (Power) to GPIO.4
    RY2 CON 5                       ' Relay 2 (Voltage reversing) to GPIO.5
    
    
    '   ---------Variables---------
    
    SW1 VAR GPIO.0                  ' Reed switch 1 to GPIO.0
    SW2 VAR GPIO.1                  ' Reed switch 2 to GPIO.1
    adval VAR WORD                  ' Store result of each A/D conversion
    delay VAR WORD                  ' Store result of math conversion
    
    
    '   ---------Initialization--------
    
    ANSEL = %00000100               ' Set AN2 analog, all others digital
    CMCON = 7                       ' Turn off analog comparators
    ADCON0.7 = 1                    ' Right justify A/D conversion output
    
    
    '   ---------Main Code---------
    
    PAUSE 2000                      ' Let everything stabalize for 2 seconds.
    LOW RY2                         ' Make sure RY2 is off for forward movement.
    
                                
    mainloop:
    
    HIGH RY1                        ' Turn on Power relay to start forward movement.
    
      WHILE SW1 = 1                 ' Keep RY1 active until reed switch SW1
        HIGH RY1                    ' is closed.
      WEND
                          
    LOW RY1                         ' Removes power and stops movement.
    
    GOSUB delayloop                 ' Keep stopped for x number of seconds.(0-63)
        
    HIGH RY2                        ' Prepare RY2 for reverse movement.
    PAUSE 500                       ' Let RY2 settle before activate RY1.
    HIGH RY1                        ' Turn on power relay to start reverse movement.
        
      WHILE SW2 = 1                 ' Keep RY1 active until reed switch SW2
        HIGH RY1                    ' is closed.
      WEND
    
    LOW RY1                         ' Removes power and stops movement.
    LOW RY2                         ' Prepare RY2 for next forward movement.
        
    GOSUB delayloop                 ' Keep stopped for x number of seconds.(0-63)
      
    GOTO mainloop                   ' Keep repeating until user terminated.
        
    END
      
        
    delayloop:
    ADCIN 2, adval                  ' Read A/D channel 2 into adval variable.        
    delay = adval * 60              ' Creates values for PAUSE command that                     
    PAUSE delay                     ' allow delays from approx. 0 to 61 seconds.
    RETURN
    Last edited by rfetech; - 28th October 2010 at 17:58.

Members who have read this thread : 0

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