EmbeddedRelated.com
Forums
Memfault Beyond the Launch

Fastest way to increment a 4-bit value in a register - 8051

Started by mik3ca 5 years ago3 replieslatest reply 5 years ago380 views

I own multiple AT89C4051's, its a basic 8051 with flash rom and 128 bytes of ram of which most is used by other resources.

I have just enough free to store 40 bytes but I need to store counts of 4 items that belong to 20 separate categories. For simplicity, let's say each category is fruit, color, numbers, letters, etc. and that each item is "red", "1", "C", "mango" etc. 

But in reality, I have 20 units (boards) each with 4 different sensors, and I need to be able to tally which sensor was hit and which board hit it.

I added code that can do the job, but I need a way to optimize it. The only thing I can think of off the top of my head is to jump to using the higher speed AT89LP4052 micro and making the board for it, but if I can get away with simply changing a few lines of code here to make the code function faster,  then I'd rather do that.

Any ideas?

and POS0 and POS1 are two low bits in the high nibble derived from another register if that helps. At this time, I don't need the count to exceed 15. so I thought the nibble approach is wise, but then again I should probably just do the byte approach on AT89LP4052 since it has 256 bytes of total ram, but that requires me to build a programming board for that IC.

I'm still welcome to code optimizations here.

P.S. I can't forget to reserve at least a good 8-16 bytes for the stack for interrupts that I'll later implement.

      ;POS0 = bit 0 of sensor that got hit
      ;POS1 = bit 1 of sensor that got hit
      ;HITC = 30h = Start of ram address for hit counts

      ;memory format: aaaabbbb ccccdddd aaaabbbb ccccdddd...
      ;where a,b,c,d=each sensor count from 0-15 for each unit

      mov A,REMOTEUNIT ;unit that hit me (#1 to 24)
      rl A             ;shift left
      mov C,POS1       ;memory byte determined 
      addc A,#HITC     ;by high bit of sensor #
      mov R1,A         ;R1=final memory location
      ;I wonder if I can optimize the following lines....
      mov A,@R1        ;Load whats in location
      jb POS0,nosw     ;See what sensor we got and..
        swap A         ;make low nibble the sensor we want
      nosw:
      mov B,A          ;Save high count of wrong sensor
      anl B,#0F0h      
      inc A            ;increment count of correct sensor
      anl A,#0Fh       ;but dont overflow
      orl A,B          ;merge two sensor counts back in
      jb POS0,nosw2
        swap A         ;swap back if we swapped for our sensor
      nosw2:
      mov @R1,A        ;and store result
[ - ]
Reply by jorickJanuary 17, 2019

If you can guarantee that the count won't exceed 15 for either nybble, you can go with the following:

      ;POS0 = bit 0 of sensor that got hit
      ;POS1 = bit 1 of sensor that got hit
      ;HITC = 30h = Start of ram address for hit counts

      ;memory format: aaaabbbb ccccdddd aaaabbbb ccccdddd...
      ;where a,b,c,d=each sensor count from 0-15 for each unit

      mov A,REMOTEUNIT ;unit that hit me (#1 to 24)
      rl A             ;shift left
      mov C,POS1       ;memory byte determined
      addc A,#HITC     ;by high bit of sensor #
      mov R1,A         ;R1=final memory location
      mov A,@R1        ;Load whats in location
      jb POS0,add1     ;Sensor is in high nybble
        add A,#16         ;increment the high nybble
        sjmp add2         ;go store it
      add1:            ;Sensor is in low nybble 
        inc A             ;increment the low nybble
      add2:     
      mov @R1,A        ;Store result

[ - ]
Reply by jorickJanuary 17, 2019

Or,

      mov A,REMOTEUNIT ;unit that hit me (#1 to 24)
      rl A             ;shift left
      mov C,POS1       ;memory byte determined
      addc A,#HITC     ;by high bit of sensor #
      mov R1,A         ;R1=final memory location
      mov a,#1         ;Assume low nybble incrementor
      jb POS0,add1     ;If sensor is in high nybble
        swap a         ;Move incrementor to the high nybble
      add1:             
      add A,@R1        ;Increment the proper nybble
      mov @R1,A        ;Store result

[ - ]
Reply by mik3caJanuary 17, 2019
I should try that. (Im due for a drink)
Memfault Beyond the Launch