PIC 12f675 analog input help/tutorial?

Thread Starter

vane

Joined Feb 28, 2007
189
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
 

SgtWookie

Joined Jul 17, 2007
22,230
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.
 

CVMichael

Joined Aug 3, 2007
419
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:

Rich (BB code):
void main() {
     unsigned int adc0;
     
     do {
          adc0 = ADC_Read(0);

          // output the adc0 here
     } while(1);
}
 

Thread Starter

vane

Joined Feb 28, 2007
189
Yeah PWM, DC motor.

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

SgtWookie

Joined Jul 17, 2007
22,230
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.
 

Thread Starter

vane

Joined Feb 28, 2007
189
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?
 

SgtWookie

Joined Jul 17, 2007
22,230
Don't suppose you could explain in more detail that equation, i.e what they all mean, not familliar with Vin etc
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.

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

CVMichael

Joined Aug 3, 2007
419
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:
Rich (BB code):
void main() {
     unsigned int adc0;

     Pwm_Init(5000);  // put here the frequency you need
     Pwm_Start();

     do {
          adc0 = ADC_Read(0);
          
          Pwm_Change_Duty(adc0 >> 2);

          // output the adc0 here
     } while(1);
}
[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:

SgtWookie

Joined Jul 17, 2007
22,230
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.
 

Thread Starter

vane

Joined Feb 28, 2007
189
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:

Thread Starter

vane

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

jpanhalt

Joined Jan 18, 2008
11,087
I am thinking a rearward offset flanged encoder which slips behind the prop, the a sensor bar comes forward over it.
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
 

SgtWookie

Joined Jul 17, 2007
22,230
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.
 

SgtWookie

Joined Jul 17, 2007
22,230
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.
 

jpanhalt

Joined Jan 18, 2008
11,087
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.
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
 

SgtWookie

Joined Jul 17, 2007
22,230
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.
 

Thread Starter

vane

Joined Feb 28, 2007
189
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.
 

Thread Starter

vane

Joined Feb 28, 2007
189
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.
 
Top