Pages: [1]   Go Down
Author Topic: full adder/subtracter with carry  (Read 1143 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have 2 8bit bytes that come in from the SPI. I need to add or subtract a 1 to the bytes. So I need help with a code that can add to the lower byte and be able to carry into the high byte when necessary. I also have no idea how to subtract.

The other option is a increment/decrement system for the 16bits.

This is simple using chips. smiley-roll
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thats a simple if statement. After you add, check if the new value is 0. If so, add 1 to the high byte. Alternatively, you can use a union.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25820
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
This is simple using chips
Quicker too.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Edison Member
*
Karma: 43
Posts: 1556
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just construct a 16-bit integer with the hi and lo bytes, add 1, and you're done.

Pete
Logged

Where are the Nick Gammons of yesteryear?

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12568
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The other option is a increment/decrement system for the 16bits.

In my opinion this is the only approach that makes sense. From the description you've given it's apparent that the two bytes are conceptually holding a 16-bit value, so if you just store that value in a 16-bit int then manipulating the value becomes trivial.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The 2 bytes is incremented up or down by 1 then sent back out on the SPI lines. As far as I know SPI wants 8bit binary bytes.
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The 2 bytes is incremented up or down by 1 then sent back out on the SPI lines. As far as I know SPI wants 8bit binary bytes.

Then use a union.
Logged

USA
Offline Offline
Jr. Member
**
Karma: 4
Posts: 89
If you can't fix it with a hammer, it must be an electrical problem.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

add:
Code:
unsigned char low, high;

void setup() {                
}

void loop() {

  //add 1 with carry
  asm(
    "lds r24, (low)  \n\t"  //low byte
    "lds r25, (high) \n\t"  //high byte
    "ldi r23, 1      \n\t"  
    "add r24, r23    \n\t"  //increment low byte by 1
    "adc r25, 0x00   \n\t"  //add carry to high byte
    "sts (low), r24  \n\t"  //save results
    "sts (high), r25 \n\t"
    ::: "r23","r24", "r25"
  );

  //subtract 1 with carry
  asm(
    "lds r24, (low)  \n\t"
    "lds r25, (high) \n\t"
    "ldi r23, 1      \n\t"
    "sub r24, r23    \n\t"
    "sbc r25, 0x00   \n\t"
    "sts (low), r24  \n\t"
    "sts (high), r25 \n\t"
    ::: "r24", "r25"
  );
}

Edit note: for completeness I included the subtraction code. My original thought was that the OP was requesting an assembly language routine.
« Last Edit: June 30, 2013, 09:47:42 am by JimEli » Logged


UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12568
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The 2 bytes is incremented up or down by 1 then sent back out on the SPI lines. As far as I know SPI wants 8bit binary bytes.

So, put them into an int, modify the int and then take them out again. Converting between a pair of bytes and an int is quick and easy.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

add:
Code:
unsigned char low, high;

void setup() {                
}

void loop() {
  asm(
    "lds r24, (low)  \n\t"  //low byte
    "lds r25, (high) \n\t"  //high byte
    "ldi r23, 1      \n\t"  
    "add r24, r23    \n\t"  //increment low byte by 1
    "adc r25, 0x00   \n\t"  //add carry to high byte
    "sts (low), r24  \n\t"  //save results
    "sts (high), r25 \n\t"
    ::: "r23","r24", "r25"
  );
}

The compiler can handle adding one to an integer. You don't need to drop into assembler for that. smiley-wink
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I have 2 8bit bytes that come in from the SPI. I need to add or subtract a 1 to the bytes.

Perhaps show your existing code?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 
Quote
highbyte = B00000000; //cmd0 NOP to read control SDO
        lowbyte = B00000000;
        digitalWrite(SSP, LOW);
        rdacsethigh = SPI.transfer(highbyte); //sent cmd0
        rdacsetlow = SPI.transfer(lowbyte);   //and read SDO
        digitalWrite(SSP, HIGH);


Sorry I am an electrical engineer, im not into software. How do I convert into and out of the integer?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12568
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int tmp = word(highbyte, lowbyte);
tmp++;
highbyte = highByte(tmp);
lowbyte = lowByte(tmp);

It could be made more compact but you get the idea.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you PeterH, what you did makes sense.
Logged

Pages: [1]   Go Up
Jump to: