I'm writing a program which will read an analog input, use that as the input to the PID Library, and then I need to get it into a 10-bit number that will go out on SPI to a DAC.
I'm having trouble wrapping my brain around the different number formats. I'm envisioning the "number" read on the analog channel as being a floating point number (representing a 0 to 5v input), that is then going to get processed with the PID Library and it will output a floating point number, but I then need to get that number into a 10-bit word to go out to the DAC. How do I do that last conversion? What if I want my output to represent a number from zero to 2.5v, instead of zero to 5v?
Go easy on me please. I'm great at "programming" in Matlab, but have virtually no C or Java experience.
Microcontrollers generally avoid "floating point." The numbers you get from the ADC (and send to most DACs) are essentially the numerators of fractions whose denominators are implied (usually by the reference or supply voltage) and involve the number of steps.
So if you read a number N from the ADC, that means that the actual voltage is
N* Vref/2nbits
So a reading of 512 means that the voltage is half of Vref, because 512/210 = 512/1024 = 1/2
You can also think of it as a proportion:
N/2nbits = V/Vref
(N in the ADC reading, V is the actual voltage.)
(and you thought you'd never use your Middle School Algebra again!)
The number you get from the analogue in will be an integer between 0 and 1023 (0 = 0V and 1023 = 5V). And for all those playing at home, 1023 = 2^10 - 1 (a 10 bit number).