Must be getting rusty with C
Beavis3215
Posts: 229
in Propeller 1
#include "simpletools.h" #include "adcDCpropab.h" int main() // Main function { adc_init(21, 20, 19, 18); set_directions(15, 0, 0b1111111111111111); // pins 15 through 0 are outputs int d[] = {0b1110111, 0b1000100, 0b1101011, 0b1101110, 0b1011100, //digits 0 through 9 0b0111110, 0b0011111, 0b1100100, 0b1111111, 0b1111100}; while(1) { for (int sample = 0; sample <= 1000; sample++) { float i = adc_volts(2); float j = adc_volts(2); if (j > i) { i = j; } } int a = (int)i; // intger part float b = (10 * (i - a)); int c = (int)b; // frac part //print("%f, %d, %d\n", i, a, c); set_outputs(15, 9, d[c]); // c is the decimal value set_output(1,1); // pin 1 is the decimal digit driver pause(8); set_output(1,0); set_outputs(15, 9, d[a]); // a is the integer value set_output(2,1); // pin 2 is the integer digit driver pause(8); set_output(2,0); } }
I Cant figure out why this won't compile. When attempting, I becomes undeclared after I = j;
any Ideas?
Comments
Solution: move "float i;" above the for-loop (right underneath the opening brace for the while-loop).
int main(I)
This would be passing an argument to main and would have local scope to the main function and not global to the entire file and other functions in the file. But, would it compile as is?
To make it global to the entire file and any functions in the file, you would declare it above the main under the includes. To ensure the compiler does not optimize the value out, you could add volatile to the variable.
That speeds up the code, especially on a ARM as it's a load-and-store type mcu.
Registers in C are temporary and will be trashed on exit of function.