embedded - C - Increment 18 bits in C 8051 -


i have been programming 8051 2 months , of newbie c language. working flash memory in order read, write, erase, , analyze it. working on write phase @ moment , 1 of tasks need specify address location , fill location data increment next location , fill complementary data. on , forth until reach end.

my dilemma have 18 address bits play , have 3 bytes allocated 18 bits. there anyway combine 18 bits int or unsigned int , increment that? or option increment first byte, when byte rolls on 0x00 increment next byte , when 1 rolls over, increment next?

i have:

void inc_address(void) {     p6=address_byte1;     p7=address_byte2;     p2=address_byte3;     p5=data_byte;      while(1)     {         p6++;         if(p6==0x00){p7++;}         else if(p7==0x00){p2++;}         else if(p2 < 0x94){break;} //hex 9 values dealing flash chip         p5=~data_byte;     } } 

is there anyway combine 18 bits int or unsigned int , increment that?

sure. supposing int , unsigned int @ least 18 bits wide on system, can this:

unsigned int next_address = (hi_byte << 16) + (mid_byte << 8) + low_byte + 1; hi_byte = next_address >> 16; mid_byte = (next_address >> 8) & 0xff; low_byte = next_address & 0xff; 

the << , >> bitwise shift operators, , binary & bitwise "and" operator.

it bit safer , more portable not make assumptions sizes of types, however. avoid that, include stdint.h, , use type uint_least32_t instead of unsigned int:

uint_least32_t next_address = ((uint_least32_t) hi_byte << 16)         + ((uint_least32_t) mid_byte << 8)         + (uint_least32_t) low_byte         + 1;  // ... 

Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -