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
I created the following code from **broken link removed**. 4.11 EXAMPLE 9. I had to convert the code to c. I am sure that I have made a mistake somewhere.
When I simulate it using "real pic simulator" it uses 100% of the MCU and the segments run at such a speed I cannot see what is going on. I have looked through the code so many times and I cannot figure out how to fix it. I know it has something to do with the interrupt trigger time.
I created the following code from **broken link removed**. 4.11 EXAMPLE 9. I had to convert the code to c. I am sure that I have made a mistake somewhere.
When I simulate it using "real pic simulator" it uses 100% of the MCU and the segments run at such a speed I cannot see what is going on. I have looked through the code so many times and I cannot figure out how to fix it. I know it has something to do with the interrupt trigger time.
Code:
int x = 0;
int digit, portd_index, shifter, number;
int portd_array[4];
void interrupt(void) {
PORTA = 0; // clear porta
PORTD = portd_array [portd_index]; // send correct value to portd
PORTA = shifter;
shifter = shifter << 1;
if (shifter > 8){
shifter = 1;
}
portd_index++; // Increment portd_index
if (portd_index > 3){
portd_index = 0; //' Turn on 1st, turn off 4th 7segment display
}
TMR0 = 0; //' Reset TIMER0 value
T0IF_bit = 0; //' Clear Timer0 interrupt flag
}
mask(int val){
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:0x3f;
}
}
void main()
{
TMR0 = 0; //Clear timer 0
OPTION_REG = 8;
GIE_bit = 1; //Enable Global Interrupt
T0IF_bit = 0; //clear interrupt flag
T0IE_bit = 1; //Enable TMR0 interrupt
PEIE_bit = 1; //Peripheral Interrupt Enable bit
OPTION_REG = 80;
T0CS_bit = 0; //Internal instruction cycle clock =0
PSA_bit = 0; //Prescaler is assigned to the Timer0
TRISD = 0; //PORT set to output
PORTA = 0; //Clear PORTA
TRISA = 0; //Set PORTA as output
PORTD = 0; //Clear PORTD
TRISD = 0; //set PORTD as output
digit = 0; //' Initial value of variable digit
portd_index = 0; //' Turn on 1st LED display
shifter = 1; // ' Initial value of variable shifter
number = 6789; //' Some initial value on LED display
while(1)
{
digit = number / 1000; //' Extract thousands //6.789
portd_array[3] = mask(digit); //' and store it to PORTD array || mask(0xf4) get value from sub function
digit = (number / 100);// mod 10; //' Extract hundreds //67.89
portd_array[2] = mask(digit); //' and store it to PORTD array
digit = (number / 10); //mod 10; //' Extract tens //678.90
portd_array[1] = mask(digit); //' and store it to PORTD array
digit = number ;//mod 10; //' Extract ones //6789
portd_array[0] = mask(digit); //' and store it to PORTD array
Delay_ms(1000); //' One second delay
number++; //' Increment number number++;
if (number > 9999) { //' Start to count from zero
number = 0;}
}
}