[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Something I don't understand (loops and interrupts)
From: |
Jim Jackson |
Subject: |
Re: Something I don't understand (loops and interrupts) |
Date: |
Fri, 3 Apr 2020 11:38:18 +0100 (BST) |
User-agent: |
Alpine 2.22 (DEB 394 2020-01-19) |
Not looked too closely, but ...
On Fri, 3 Apr 2020, BERTRAND Joël wrote:
> ISR(TIMER1_COMPA_vect, ISR_BLOCK)
> {
> uint8_t i;
>
> interrupt_counter = (interrupt_counter + 1) % 25;
>
> if (interrupt_counter == 0) // 1 s
> {
> gpio_toggle(&i_led[i_work]);
> }
>
> // Switch debug leds off
> for(i = 0; i < i_max; i++)
> {
> if (interrupt_counter == 0) serial_send_byte(i);
This gets called every time the for loop iterates, so will get called i_max
times, all but the first call will cuase serial_send_byte(i) to loop
waiting for the transmit buffer to empty. You do NOT want to be calling
serial_send_byte(i) in an ISR anyway. Check for interrupt_counter == 0 in
the main loop, and make sure it's declared volatile.
> if (i_led[i].timer > 0)
> {
> i_led[i].timer--;
>
> if (i_led[i].timer == 0)
> {
> gpio_off(&i_led[i]);
> }
> }
> }
>
> return;
> }
>
> serial_send_byte is a trivial function:
>
> void
> serial_send_byte(uint8_t byte)
> {
> while(!(UCSR0A & (1 << UDRE0)));
> UDR0 = byte;
> gpio_on(&i_led[i_comm_tx], 5);
>
> return;
> }
>
> MCU enters in this interrupt (25 times each second). I have checked
> that gpio_toggle(&i_led[i_work]) generates 1 Hz signal. But I don't
> understand why for(i = 0; i < i_max; i++) doesn't run as expected. I
> obtain on serial line (115200, 8N1):
>
> 10:23:00: 7
> 10:23:00: 8
> 10:23:00: 9
> 10:23:00: 4
> 10:23:01: 5
> 10:23:01: 6
> 10:23:01: 7
> 10:23:01: 8
> 10:23:01: 9
> 10:23:01: 4
> 10:23:02: 5
> 10:23:02: 6
> 10:23:02: 7
> 10:23:02: 8
> 10:23:02: 9
> 10:23:02: 0
> 10:23:03: 4
> 10:23:03: 5
> 10:23:03: 6
> 10:23:03: 7
> 10:23:03: 8
> 10:23:03: 9
> 10:23:03: 4
> 10:23:04: 5
> 10:23:04: 6
> 10:23:04: 7
> 10:23:04: 8
> 10:23:04: 9
> 10:23:04: 4
> 10:23:05: 5
> 10:23:05: 6
> 10:23:05: 7
> 10:23:05: 8
> 10:23:05: 9
>
> Value of i_max: 10. But why i doesn't contains values between 0 and 3 ?
>
> Best regards,
>
> JB
>