WStevens_sa
Member level 2
- Joined
- Jan 5, 2011
- Messages
- 47
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,286
- Location
- South Africa
- Activity points
- 1,695
Hi all
I have some code I created to count 0 to 9 on a 7 segment LED. Everything works fine except when it gets to 9 it shows a zero. The issue at hand is that my function returns an integer instead of the value 0x6F that I pass to PORTD. If I pass it directly to PORTA = 0x6F then it shows a 9. I would like to know if this code is efficient or is there a simpler way.
I have some code I created to count 0 to 9 on a 7 segment LED. Everything works fine except when it gets to 9 it shows a zero. The issue at hand is that my function returns an integer instead of the value 0x6F that I pass to PORTD. If I pass it directly to PORTA = 0x6F then it shows a 9. I would like to know if this code is efficient or is there a simpler way.
Code:
int x=0;//Counter to loop through 0 to 9
getnumber(int val) //function to return number as a byte
{
switch (val) {
case 0: return 0x3f; break; // 0 = 0x3f // 1 = 0x06 // 2 = 0x5B // 3 = 0x4F // 4 = 0x66 // 5 = 0x6D
case 1: return 0x06; break; // 6 = 0x7D // 7 = 0x07 // 8 = 0x7F // 9 = 0x6F
case 2: return 0x5B; break;
case 3: return 0x4F; break;
case 4: return 0x66; break;
case 5: return 0x6D; break;
case 6: return 0x7D; break;
case 7: return 0x07; break;
case 8: return 0x7F; break;
case 9: return 0x6F; break;
//default:return 0x3f;
}
}
void main()
{
TRISD = 0; // PORTA set to output
ADCON1 = 7; //Disable ADC so that PORTA can be used for digital purpose
while(1)
{
for (x = 0; x < 9; x++)
{
Delay_ms(1000);
PORTD = getnumber(x); //Call function with parameter
}
};
}