Embedded C program - ADC and timer0 on PIC18

danadak

Joined Mar 10, 2018
4,057
Basically think of a graph x-y straight line, where x varies from 0 - 255,
y your desired PWM values. Then just solve for the slope and constant
in -




Then use the equation in your conversion/mapping from A/D to PWM values.
You can do the values as floats and round, then convert back to integers with
casting.


Regards, Dana.
 

Thread Starter

anon1

Joined Jan 25, 2018
13
For better precision, you could read all 10 bits from the ADC rather than just 8.
Sorry, I may have used the word precision incorrectly. I didn't mean that the ADC reading wasn't picked up accurately, I meant that I wasn't getting the range that I was after. My program doesn't need to be able to distinguish between 1.01V and 1.04V. The issue is that the number that I need to feed into timer0(), needs to be between 26473 (1000ms) and 55770 (250ms) but I can't pick these up directly from the potentiometer so I somehow need to scale up the reading so that the smallest number that is fed into timer0 is 26473 and the largest 55770 (and they need to correspond with the lowest and highest pot setting). I tried multiplying by 1000 but that only works when the pot reading happens to be between 0 and about 65 - so multiplying by 1000 isn't giving me the range that I want (between 26473 (1000ms) and 55770 (250ms)). I had already tried >>4 but it didn't solve my problem.
 

Thread Starter

anon1

Joined Jan 25, 2018
13
Basically think of a graph x-y straight line, where x varies from 0 - 255,
y your desired PWM values. Then just solve for the slope and constant
in -




Then use the equation in your conversion/mapping from A/D to PWM values.
You can do the values as floats and round, then convert back to integers with
casting.


Regards, Dana.
The teacher hasn't brought up PWM at all (unless there's a different term for it) and none of the previous lab exercises have referred to it. The previous lab exercises asked me to use timers and ADCs separately and I managed to do those but now that he's asking us to combine them, I seem to be missing a step. How would pulse-width modulation fit into this picture, conceptually?
 

MrChips

Joined Oct 2, 2009
30,805
Did you read the suggestion provided in post #2?
The applicable equation is:

y = mx + b

You need to determine the values of m and b.

The solution you seek has nothing to do with PWM or MCU. This is purely mathematics.

Also, how did you arrive at these values: (between 26473 (1000ms) and 55770 (250ms)) ?
Do they make any sense?
 

djsfantasi

Joined Apr 11, 2010
9,163
Do you remember any algebra? Given the desired equation
y = mx + b​
What is the lowest value returned by the potentiometer? Using this info, what variable corresponds to the lowest value? This question should be pretty easy...

Then what does the variable m represent? I’ll give you a hint in this one. It’s the slope. Do you remember the equation for slope? It’s the change in y, your desired equation, divided by the change in x, the pot readings.

And what is change? Using two points, subtract one y value the other y value and divide by... the change is x (you should be able to figure this out on your own. )

You have two known values, (0,26) and (255,56) (are you sure the maximum is 255?) So do the calculations yourself and post your results (I.e. post detailed, step by step calculations)
 
Top