October 21st, 2013, 04:00 AM
16 bit arithmatic from 8 bit variables
I am using basic ansi C on an 8 bit processor.
I have two 14 bit numbers stored in 8 bit variables, top 6 bits in one variable and lower 8 bits in another. I need to compare the second number with the first and if it is larger I need to increment the first by one. If it is smaller I need to decrement the first by one. Exit if they are the same. I am having problems handling the carry and borrow from one variable to the next.
October 21st, 2013, 08:10 AM
What data types are available with your standard ANSI c compiler?
I'm pretty sure the "short" type is guaranteed to hold at least 16 bits.
[/code] are essential for python code and Makefiles!
October 21st, 2013, 10:03 AM
Thanks for your reply.
Originally Posted by b49P23TIvg
I am using a mikroC compiler for 8 bit PIC microcontrollers.
The first 14 bit number is generated from a AD converter, the second number is the duty cycle of a PWM generator. I need to slowly ramp the duty cycle up or down to match the AD output.
October 21st, 2013, 10:34 AM
The point was that ANSI C makes the software mostly independent of the hardware. Even though you have an 8-bit microcontroller, there is nothing restricting all your program's variables to only 8 bits. It should only be when you have to apply those variables' values to the hardware (eg, peripherals' control and data registers) that you need to fiddle with bit sizes.
October 21st, 2013, 02:07 PM
Construct a single 16-bit value from two 8-bit values thus:
short adc = adc_low | (adc_high << 8) ;
October 21st, 2013, 02:49 PM
Thanks for your help, I did not realise that was possible but I have tried it and it works. Thanks again
Originally Posted by dwise1_aol