cmsp430code-composer

Converting decimal to BCD


I'm working on an assignment currently that uses a microcontroller and a circuit to display a decimal in binary, through LED's. I have to use three push buttons: one to increment, one to decrement, and the last to reset. I have my wiring and configuration complete. My code on the other hand has some glitches that I cannot fix with only an intro to C class under my belt. I am using Code Composer. Problem number one: My counter on the "Tens" section(MSB) does not stop at 9 but rather begins to display binary 10-15. The first four bits are the ones(right) and the second four bits, tens(left) ex: 1010 0001 but the max value is 1001 1001. Problem number two: starting from zero if I decrement the value, the counter displays 95 rather than 99. ex: starting from 0000 0000 decrement displays 1001 0101. I tried using an if statement to make a condition for MSB to stop if greater than ten but the code doesn't run. Pretty much any modification I did to my code prevents it from working properly.

#include <msp430.h>

unsigned int dec2bcd(unsigned int num);
void delay(void);

int main(void) {
WDTCTL = WDTPW | WDTHOLD;    // Stop watchdog timer

P1DIR = 0xFF;
P2DIR = 0xF0;
unsigned int c;
unsigned int count = 0x00;

while(1){
    if((P2IN & BIT0) == BIT0){
        count++;
        c = dec2bcd(count);
    }

    else if((P2IN&BIT1) == BIT1){
        count--;
        c = dec2bcd(count);
    }

    else if((P2IN&BIT2) == BIT2){
        count = 0x00;
        c = dec2bcd(count);
    }

    delay();
    P1OUT = c;

}

}

unsigned int dec2bcd(unsigned int num)
{
    unsigned int ones = 0;
    unsigned int tens = 0;
    unsigned int temp = 0;

    ones = num%10;
    temp = num/10;
    tens = temp<<4;
    return (tens + ones);
}

void delay(void)
{
    volatile unsigned int i, j;
    for(i=10000; i>0; i--)
    {
        for(j=3; j>0; j--){
    }
}
}

Solution

  • Why you see 95?

    As mentioned by @Olaf, msp430 uses 16bit integer.

    When you do count = 0u -1, count acutally wraps to 65535;

    unsigned int dec2bcd(unsigned int num) // num is now 65535
    {
        unsigned int ones = 0;
        unsigned int tens = 0;
        unsigned int temp = 0;
    
        ones = num%10; // 65535%10 = 5
        temp = num/10; // 65535/10 = 6553
        tens = temp<<4;  // what's displayed is by tens is actually the lower
                         // 4 bits of tens, so tens is 6553%16=9
        return (tens + ones);// so the result is 95
    }
    

    Why tens can to beyond 10

    Same problem, because your input is greater than 99.

    unsigned int dec2bcd(unsigned int num) // say num is now 100
    {
        unsigned int ones = 0;
        unsigned int tens = 0;
        unsigned int temp = 0;
    
        ones = num%10; // 100%10 = 0
        temp = num/10; // 100/10 = 10, or 0x0A
        tens = temp<<4;  
        return (tens + ones);// so the result is A0
    }
    

    What you should do?

    In your code, limit the range to 0-99, you have the choice of either wrap-around (99+1=0, and 0-1=99)or saturation (99+1=99, 0-1=0). But whatevenr you need, you need to write it yourself: C language does not provide it.