#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Red face 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.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,851
    Rep Power
    481
    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]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by b49P23TIvg
    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.
    Thanks for your reply.
    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.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,158
    Rep Power
    2222
    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.
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,110
    Rep Power
    1803
    Construct a single 16-bit value from two 8-bit values thus:
    Code:
    short adc = adc_low | (adc_high << 8) ;
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Smile


    Originally Posted by dwise1_aol
    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.
    Thanks for your help, I did not realise that was possible but I have tried it and it works. Thanks again

IMN logo majestic logo threadwatch logo seochat tools logo