PIC 12f675 analog input help/tutorial?

Discussion in 'The Projects Forum' started by vane, Mar 25, 2010.

  1. vane

    Thread Starter Active Member

    Feb 28, 2007
    181
    0
    I am doing a project for my college and for the circuitry I need a potentiometer to change a variable (a speed control for a motor).

    I feel best coding in C and i have the mikroC for PICs demo.

    I am wondering if anyone can link me to a tutorial which explains the basics of how to read the analog inputs and how to set them up.

    Peter
     
  2. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
  3. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    Peter,
    Did you download the free MPLAB software from Microchip?

    There are samples supplied with it.

    The demo.c and demo.h for the PICKit 1 uses the ADC input to sense the position of a pot, and that changes the speed of the LEDs flashing on the PICkit 1 board.
     
  4. CVMichael

    Senior Member

    Aug 3, 2007
    416
    17
    Reading analog is very easy in mikroC, I think the better question is how do you plan to change the speed of the motor when you have only digital outputs ? Do you plan using PWM ?
    Actually the better question is, what kind of motor ? DC or Stepper or Servo ?

    I'm at work now, but from what I remember to read the ADC goes like this:

    Code ( (Unknown Language)):
    1.  
    2. void main() {
    3.      unsigned int adc0;
    4.      
    5.      do {
    6.           adc0 = ADC_Read(0);
    7.  
    8.           // output the adc0 here
    9.      } while(1);
    10. }
    11.  
     
  5. vane

    Thread Starter Active Member

    Feb 28, 2007
    181
    0
    Yeah PWM, DC motor.

    So does it reply with a number of the resistance? like 10,000 or something like that?
     
  6. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    It's a 10-bit number, so the result is 0 to 1,023.

    Think of it as a ratio of the reference voltage. Vin=(adc0/1023)*Vref. If you're powering the PIC with 5v and have not set a particular reference voltage (Vref), then it's the PIC supply voltage. Don't expect great results if your Vref isn't steady.
     
  7. vane

    Thread Starter Active Member

    Feb 28, 2007
    181
    0
    Don't suppose you could explain in more detail that equation, i.e what they all mean, not familliar with Vin etc

    Also, off topic, do PICs get damaged in close proximity to small rare earth magnets?
     
  8. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    Vin=(adc0/1023)*Vref
    where:
    Vin = the voltage on the selected ADC input.
    adc0 = the result returned from the ADC_Read(0) function.
    1023 = the highest decimal number that can be represented by 10 bits.
    Vref = the ADC's reference voltage. It can be Vdd, or set to a pin connected to an external voltage reference. See section 9 in the datasheet. If you want better accuracy, use the external voltage reference. If accuracy isn't very important, just use Vdd.

    No.
     
  9. CVMichael

    Senior Member

    Aug 3, 2007
    416
    17
    The Pwm_Change_Duty function that is included in mikroC is only 8 bit, so you will actually have to convert the 10 bit from ADC to 8 bit (divide by 4, or right shift by 2)

    You can see that in the mikroC manual, on page 304
    http://www.easypsoc.com/pdf/mikroc/mikroc_manual.pdf

    So then your code would be something like:
    Code ( (Unknown Language)):
    1.  
    2. void main() {
    3.      unsigned int adc0;
    4.  
    5.      Pwm_Init(5000);  // put here the frequency you need
    6.      Pwm_Start();
    7.  
    8.      do {
    9.           adc0 = ADC_Read(0);
    10.          
    11.           Pwm_Change_Duty(adc0 >> 2);
    12.  
    13.           // output the adc0 here
    14.      } while(1);
    15. }
    16.  
    [edit]
    .... that is if your PIC would have PWM hardware integrated ! but I checked the data sheet, and PIC12F675 does not have PWM.
    This means you have to simulate PWM with software using interrupts
     
    Last edited: Mar 25, 2010
  10. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    If you want to use hardware PWM, you'll need to use a PIC12F683.

    You can perform PWM in the software, but you need to be mindful of where your code is executing. It's easy to wander off someplace and get lost in a section of code - which will naff your PWM.
     
  11. vane

    Thread Starter Active Member

    Feb 28, 2007
    181
    0
    I was thinking more of:

    enabling motor output
    waiting set PWM time
    disabling motor output
    waiting 100% - first wait value

    So effectively it is measuring the voltage drop over the Vref pin and the Analog pin?

    Can anyone think of a way of maybe using optoelectrics to measure an RPM of a small OS 15LA 0.15 cubic inch model engine which would probably rev anywhere from 1000 to 9000 rpm. I am thinking maybe making a decoder like thing (like you find in rollerball mice)

    My project is a fuel pump which i want to automatically shut off if the rpm becomes 0

    Here is the engine: http://www.mysheldonshobbies.com/product_info.php?manufacturers_id=19&products_id=45&osCsid=kezrhyzv
     
    Last edited: Mar 26, 2010
  12. vane

    Thread Starter Active Member

    Feb 28, 2007
    181
    0
    I am thinking a rearward offset flanged encoder which slips behind the prop, the a sensor bar comes forward over it.
     
  13. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    5,694
    904
    Now that caught my eye.:)

    Are you simply trying to control motor speed of a DC motor in an airplane or boat? Have you considered looking at the back emf?

    John
     
  14. vane

    Thread Starter Active Member

    Feb 28, 2007
    181
    0
    well yes, its a DC motor, but in a fuel pump to aid fuel run in a model IC engine
     
  15. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    John,
    It's a gas powered high-RPM single cylinder engine. Our OP just wants to sense if the engine has stopped in order to turn off the fuel pump.

    Trying to make something that will be in balance at high RPM will be a challenge. Even a small imbalance will be a big load on the crank journals at high RPM, leading to high-frequency vibrations and extra wear.

    I'm thinking that if you could use a hole punch on some aluminum foil to make a couple of same-size reflectors, and use a tiny bit of cryo glue to secure them to the back of the prop or inside of the prop spinner, and use a reflective IR emitter/detector pair, you'd have a stream of pulses once the engine is running.

    Couple that with a missing pulse detector, and you'd have your pump cutoff circuit.
     
  16. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    Is/are the crank counterbalance(s) magnetic, or is a magnet attracted to it/them?

    Just a "blue sky" idea :)D) but you might be able to use a Hall-effect sensor backed up by a small magnet to sense the rotation of the counterbalance - perhaps without making any mods to the engine or props whatsoever. Just glue the Hall-effect sensor using something like JB-Weld to the crankcase.
     
  17. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    5,694
    904
    Sorry, I didn't pick up that bit of info from the rest of the thread.

    If the question is just whether the engine is on or off, and if it is gas vs. fuel, then the ignition signal might be used. The ignition stops when the prop stops. If the engine is glow, then obviously that won't work.

    Alternatively, a light sensor or IR emitter/sensor could be used without having to add any reflector at all. We measure prop speeds all the time with just ambient light. I have not done it, but I am pretty sure IR bounce off of the back of the prop would also be adequate

    John
     
  18. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    Sure, even a phototransistor in a piece of black shrink tubing behind the prop could work - as long as our OP wasn't into night flying ;)

    They'd need something to keep crud from getting in the tube and blocking the light.
     
  19. vane

    Thread Starter Active Member

    Feb 28, 2007
    181
    0
    Well I will be doing it on a rapid prototyping 3d printer thing so it will probably be near enough balanced, I can always hand balance it with a prop balancer.
     
  20. vane

    Thread Starter Active Member

    Feb 28, 2007
    181
    0
    You know the movement decoders on roller ball mice, are they infra red LEDs and infrared LDRs or something?
    So i will need another analog input to read that.
     
Loading...