8bit Hex to 7-segment

7 posts / 0 new
Author
Message

Hey guys, this is the first thing I have done with any microcontrollers so far. I have some knowledge in C and lots in electronics. I wrote a piece of code to convert 8bits of binary to be displayed in a BCD fashion. IE:

00001111 would display 15 on the display. I have written a fairly large chunk of code using if statements. When I program onto the chip and power up it just displays:

00 the the a bit is not lit on the left 7seg

and when I put logic highs on any of my PORTB pins nothing happens. The 7-seg in common anode so all the define have to be inverse of the actual letters.

any help would be much appreciated. I'm using a ATMEGA8L.

```#include

#define zero  0b00000001
#define one   0b01001111
#define two   0b00010010
#define three 0b00000110
#define four  0b01001100
#define five  0b00100100
#define six   0b00100000
#define seven 0b00001111
#define eight 0b00000000
#define nine  0b00001100

int main(void)
{
// Set Port B pins as all inputs
DDRB = 0b00000000;

// Set all port C as outputs
DDRC = 0b11111111;

// Set all port D as outputs
DDRD = 0b11111111;

//Set all outputs high to start
PORTC = 0b11111111;
PORTD = 0b11111111;

while (1)
{
if (PORTB == 0b00000000)
{
(PORTC =  zero);
(PORTD =  zero);
}

if (PORTB == 0b00000001)
{
(PORTC =  zero);
(PORTD =  one);
}

if (PORTB == 0b00000010)
{
(PORTC =  zero);
(PORTD =  two);
}

if (PORTB == 0b00000011)
{
(PORTC =  zero);
(PORTD =  three);
}

if (PORTB == 0b00000100)
{
(PORTC =  zero);
(PORTD =  four);
}

if (PORTB == 0b00000101)
{
(PORTC =  zero);
(PORTD =  five);
}

if (PORTB == 0b00000110)
{
(PORTC =  zero);
(PORTD =  six);
}

if (PORTB == 0b00000111)
{
(PORTC =  zero);
(PORTD =  seven);
}

if (PORTB == 0b00001000)
{
(PORTC =  zero);
(PORTD =  eight);
}

if (PORTB == 0b00001001)
{
(PORTC =  zero);
(PORTD =  nine);
}

if (PORTB == 0b00001010)
{
(PORTC =  one);
(PORTD =  zero);
}

if (PORTB == 0b00001011)
{
(PORTC =  one);
(PORTD =  one);
}

if (PORTB == 0b00001100)
{
(PORTC =  one);
(PORTD =  two);
}

if (PORTB == 0b00001101)
{
(PORTC =  one);
(PORTD =  three);
}

if (PORTB == 0b00001110)
{
(PORTC =  one);
(PORTD =  four);
}

if (PORTB == 0b00001111)
{
(PORTC =  one);
(PORTD =  five);
}

if (PORTB == 0b00010000)
{
(PORTC =  one);
(PORTD =  six);
}

if (PORTB == 0b00010001)
{
(PORTC =  one);
(PORTD =  seven);
}

if (PORTB == 0b00010010)
{
(PORTC =  one);
(PORTD =  eight);
}

if (PORTB == 0b00010011)
{
(PORTC =  one);
(PORTD =  nine);
}

if (PORTB == 0b00010100)
{
(PORTC =  two);
(PORTD =  zero);
}

if (PORTB == 0b00010101)
{
(PORTC =  two);
(PORTD =  one);
}

if (PORTB == 0b00010110)
{
(PORTC =  two);
(PORTD =  two);
}

if (PORTB == 0b00010111)
{
(PORTC =  two);
(PORTD =  three);
}

if (PORTB == 0b00011000)
{
(PORTC =  two);
(PORTD =  four);
}

if (PORTB == 0b00011001)
{
(PORTC =  two);
(PORTD =  five);
}

if (PORTB == 0b00011010)
{
(PORTC =  two);
(PORTD =  six);
}

if (PORTB == 0b00011011)
{
(PORTC =  two);
(PORTD =  seven);
}

if (PORTB == 0b00011100)
{
(PORTC =  two);
(PORTD =  eight);
}

if (PORTB == 0b00011101)
{
(PORTC =  two);
(PORTD =  nine);
}

if (PORTB == 0b00011110)
{
(PORTC =  three);
(PORTD =  zero);
}

if (PORTB == 0b00011111)
{
(PORTC =  three);
(PORTD =  one);
}

if (PORTB == 0b00100000)
{
(PORTC =  three);
(PORTD =  two);
}

if (PORTB == 0b00100001)
{
(PORTC =  three);
(PORTD =  three);
}

if (PORTB == 0b00100010)
{
(PORTC =  three);
(PORTD =  four);
}

if (PORTB == 0b00100011)
{
(PORTC =  three);
(PORTD =  five);
}

if (PORTB == 0b00100100)
{
(PORTC =  three);
(PORTD =  six);
}

if (PORTB == 0b00100101)
{
(PORTC =  three);
(PORTD =  seven);
}

if (PORTB == 0b00100110)
{
(PORTC =  three);
(PORTD =  eight);
}

if (PORTB == 0b00100111)
{
(PORTC =  three);
(PORTD =  nine);
}

if (PORTB == 0b00101000)
{
(PORTC =  four);
(PORTD =  zero);
}

if (PORTB == 0b00101001)
{
(PORTC =  four);
(PORTD =  one);
}

if (PORTB == 0b00101010)
{
(PORTC =  four);
(PORTD =  two);
}

if (PORTB == 0b00101011)
{
(PORTC =  four);
(PORTD =  three);
}

if (PORTB == 0b00101100)
{
(PORTC =  four);
(PORTD =  four);
}

if (PORTB == 0b00101101)
{
(PORTC =  four);
(PORTD =  five);
}

if (PORTB == 0b00101110)
{
(PORTC =  four);
(PORTD =  six);
}

if (PORTB == 0b00101111)
{
(PORTC =  four);
(PORTD =  seven);
}

if (PORTB == 0b00110000)
{
(PORTC =  four);
(PORTD =  eight);
}

if (PORTB == 0b00110001)
{
(PORTC =  four);
(PORTD =  nine);
}

if (PORTB == 0b00110010)
{
(PORTC =  five);
(PORTD =  zero);
}

if (PORTB == 0b00110011)
{
(PORTC =  five);
(PORTD =  one);
}

if (PORTB == 0b00110100)
{
(PORTC =  five);
(PORTD =  two);
}

if (PORTB == 0b00110101)
{
(PORTC =  five);
(PORTD =  three);
}

if (PORTB == 0b00110110)
{
(PORTC =  five);
(PORTD =  four);
}

if (PORTB == 0b00110111)
{
(PORTC =  five);
(PORTD =  five);
}

if (PORTB == 0b00111000)
{
(PORTC =  five);
(PORTD =  six);
}

if (PORTB == 0b00111001)
{
(PORTC =  five);
(PORTD =  seven);
}

if (PORTB == 0b00111010)
{
(PORTC =  five);
(PORTD =  eight);
}

if (PORTB == 0b00111011)
{
(PORTC =  five);
(PORTD =  nine);
}

if (PORTB == 0b00111100)
{
(PORTC =  six);
(PORTD =  zero);
}

if (PORTB == 0b00111101)
{
(PORTC =  six);
(PORTD =  one);
}

if (PORTB == 0b00111110)
{
(PORTC =  six);
(PORTD =  two);
}

if (PORTB == 0b00111111)
{
(PORTC =  six);
(PORTD =  three);
}

if (PORTB == 0b01000000)
{
(PORTC =  six);
(PORTD =  four);
}

if (PORTB == 0b01000001)
{
(PORTC =  six);
(PORTD =  five);
}

if (PORTB == 0b01000010)
{
(PORTC =  six);
(PORTD =  six);
}

if (PORTB == 0b01000011)
{
(PORTC =  six);
(PORTD =  seven);
}

if (PORTB == 0b01000100)
{
(PORTC =  six);
(PORTD =  eight);
}

if (PORTB == 0b01000101)
{
(PORTC =  six);
(PORTD =  nine);
}

if (PORTB == 0b01000110)
{
(PORTC =  seven);
(PORTD =  zero);
}

if (PORTB == 0b01000111)
{
(PORTC =  seven);
(PORTD =  one);
}

if (PORTB == 0b01001000)
{
(PORTC =  seven);
(PORTD =  two);
}

if (PORTB == 0b01001001)
{
(PORTC =  seven);
(PORTD =  three);
}

if (PORTB == 0b01001010)
{
(PORTC =  seven);
(PORTD =  four);
}

if (PORTB == 0b01001011)
{
(PORTC =  seven);
(PORTD =  five);
}

if (PORTB == 0b01001100)
{
(PORTC =  seven);
(PORTD =  six);
}

if (PORTB == 0b01001101)
{
(PORTC =  seven);
(PORTD =  seven);
}

if (PORTB == 0b01001110)
{
(PORTC =  seven);
(PORTD =  eight);
}

if (PORTB == 0b01001111)
{
(PORTC =  seven);
(PORTD =  nine);
}

if (PORTB == 0b01010000)
{
(PORTC =  eight);
(PORTD =  zero);
}

if (PORTB == 0b01010001)
{
(PORTC =  eight);
(PORTD =  one);
}

if (PORTB == 0b01010010)
{
(PORTC =  eight);
(PORTD =  two);
}

if (PORTB == 0b01010011)
{
(PORTC =  eight);
(PORTD =  three);
}

if (PORTB == 0b01010100)
{
(PORTC =  eight);
(PORTD =  four);
}

if (PORTB == 0b01010101)
{
(PORTC =  eight);
(PORTD =  five);
}

if (PORTB == 0b01010110)
{
(PORTC =  eight);
(PORTD =  six);
}

if (PORTB == 0b01010111)
{
(PORTC =  eight);
(PORTD =  seven);
}

if (PORTB == 0b01011000)
{
(PORTC =  eight);
(PORTD =  eight);
}

if (PORTB == 0b01011001)
{
(PORTC =  eight);
(PORTD =  nine);
}

if (PORTB == 0b01011010)
{
(PORTC =  nine);
(PORTD =  zero);
}

if (PORTB == 0b01011011)
{
(PORTC =  nine);
(PORTD =  one);
}

if (PORTB == 0b01011100)
{
(PORTC =  nine);
(PORTD =  two);
}

if (PORTB == 0b01011101)
{
(PORTC =  nine);
(PORTD =  three);
}

if (PORTB == 0b01011110)
{
(PORTC =  nine);
(PORTD =  four);
}

if (PORTB == 0b01011111)
{
(PORTC =  nine);
(PORTD =  five);
}

if (PORTB == 0b01100000)
{
(PORTC =  nine);
(PORTD =  six);
}

if (PORTB == 0b01100001)
{
(PORTC =  nine);
(PORTD =  seven);
}

if (PORTB == 0b01100010)
{
(PORTC =  nine);
(PORTD =  eight);
}

if (PORTB == 0b01100011)
{
(PORTC =  nine);
(PORTD =  nine);
}

}

return 1;
}
```
Total votes: 0

you read the port state with PINB, not PORTB.

And I would suggest using a lookup table with a loop instead of hard-coding every possible value, as it is more elegant and the code size will be A LOT smaller.

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it"

Total votes: 0

A more natural way to do this would be

``` char val = PINB;
PORTD = segments[val/10];
PORTC = segments[val%10];```

where val must be guaranteed to be <= 99, and segments is a 10 byte array of the segments for each digit.

Total votes: 0

Like so

```#include
#include

uint8_t lut_segments[] = {
0b00000001,
0b01001111,
0b00010010,
0b00000110,
0b01001100,
0b00100100,
0b00100000,
0b00001111,
0b00000000,
0b00001100
};

void SetSegment(uint8_t value)
{
if (value > 99)
return;
PORTC = lut_segments[value/10];
PORTD = lut_segments[value%10];
}

int main(void)
{
DDRB = 0;
DDRC = 0xff;
DDRD = 0xff;

while (1)
{
SetSegment(PINB);
}
}```

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it"

Total votes: 0

Quote:
I wrote a piece of code to convert 8bits of binary to be displayed in a BCD fashion. IE:

00001111 would display 15 on the display.

BCD for 15 would be 00010101, not 00001111, which is just binary.

If the value is BCD coded, the SetSegment function would look like this :

```void SetSegment(uint8_t value)
{
PORTC = lut_segments[value >> 4];
PORTD = lut_segments[value & 0x0f];
}
```

don't forget to do bounds checking in that function, or add 6 more elements to the array to display an error.

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it"

Total votes: 0

The code works like a charm, you're the man.

I'm still having issues with the "a" segment on the left seven segment not lighting up, would it be because pin6 of port c is the reset pin of the microcontroler? That's the pin being used to control segment a

Total votes: 0

No worries fixed the problem, thanks for the help again.