#avr Logs

Oct 21 2020

#avr Calendar

12:47 AM [1]MrMobius is now known as MrMobius
02:36 AM nohit: rue_bed: dont worry about it, its black magic to you guys
02:05 PM joakimk: I need some help with the 16-bit timer (timer1) on an ATMEGA88. I'm working on a hobby project where I want to use timer1 to "blink" a set of LEDs, in a specific program/pattern (scroll, etc) and at a user-changeable rate/tempo
02:05 PM joakimk: no fading
02:06 PM joakimk: so far, I've set the timer up in CTC mode, but I'm having problems when the tempo changes. I've configured the prescaler wrt. the crystal s.t. the max rate is 0.1s and min rate is 1s
02:06 PM joakimk: https://pastebin.com/x2XKwR0V
02:07 PM joakimk: if the blinking is going at min speed (1s) and I increment the tempo (the tempo is set by sending 0-255), then the lights flicker every time the tempo is changed
02:10 PM joakimk: if the tempo is set to 1s (OCRA1 = 57600) and I receive 254, then the ISR will change it to 576+(254*223) = 57218. Let's say this happens as the counter is at 25000. Will changing OCR1A while in CTC mode cause the counter to RESET immediately?
02:11 PM joakimk: I tried changing the code s.t. the new tempo is only "set" (to OCR1A) when the counter actually "overflows" -- i.e. inside the `ISR (TIMER1_COMPA_vect)` -- but this didn't seem to affect the behavior
02:12 PM joakimk: so: What's a good way to use timer1 to blink some LEDs, at a variable rate, where the rate can be changed continuously during runtime -- and I want to observe an as fluent as possible response in the LED blinking rate
02:13 PM joakimk: I want to slowly increase the tempo from 1s to 0.1s, over say 5s period, and just see the blinking flawlessly going ever faster :)
02:13 PM joakimk: maybe CTC mode is wrong?
02:14 PM joakimk: I tried to paste the relevant code in the paste above. The crystal is `#define F_CPU 3686400UL`
02:15 PM cehteh: i'd try to stay with a constant speed counter if possible and read the datasheet, some wgm modes latch the TOP, but iirc changes to the prescaler is not/never latched
02:27 PM joakimk: Hi cehteh :)
02:28 PM joakimk: I did the maths, and got the timer set up for the desired interval
02:28 PM Igloo: joakimk: Do you mean PORTB &= (~(command << 3)); on line 27?
02:29 PM cehteh: joakimk: i just use one timer (8 bit when slow enough) run as wall clock and do all such timing from that
02:29 PM joakimk: Igloo the thing is. I'm using the three LEDs to display a blinking pattern. And the 7 others to display "debug" /state data. I want to toggle the pattern without changing the debug/state bits
02:30 PM Igloo: 'PORTB =' is going to set the value of all 8 PORTB pins
02:30 PM joakimk: cehteh I just don't see how I can have the blinking going at variable rates if the timer is running at constant speed
02:30 PM cehteh: yeah its a bit more complex :D
02:30 PM Igloo: Which pin is the flickering LED on?
02:30 PM joakimk: I think all...
02:31 PM cehteh: joakimk: just 'schedule' things at given times
02:31 PM joakimk: I'll hook up my STK and brb
02:31 PM Igloo: OK, I'm a little lost. When you do "PORTB = (~(command << 3));", what might command be, and what do you want that to do?
02:32 PM Igloo: Ah, command is 1, right?
02:32 PM Igloo: So that line turns PB0, PB1, PB2, PB4, PB5, PB6 and PB7 off, and PB3 on. Would that explain the flickering you see?
02:33 PM Igloo: Sorry, other way round. 3 off, the rest on.
02:47 PM joakimk: aah
02:48 PM joakimk: Igloo so
02:48 PM joakimk: line 27 displays the command received (i.e. byte, 0x01, 0x02, 0x03 or 0x04) "across" LEDs 0-6
02:49 PM joakimk: sorry, I mean 0-5
02:49 PM joakimk: while LEDs 6-7-8 are reserved for displaying the running "program" (blinking). I only use 3 lights for the pattern, since this is a prototype for a traffic lights controller project
02:50 PM joakimk: so I guess you're right
02:50 PM joakimk: `PORTB = (~(command << 3));` will obviously switch off all LEDs, except for the "command" bits
02:52 PM joakimk: if I wanted to JUST show the command, without "disturbing" (changing) the current pattern shown in LEDs 5-6-7 (hehe, I keep messing up the indices here) I need to do something like in line 20-21
02:52 PM joakimk: where I first "read" out the current pattern, and set that along with the command bits, in line 27
02:56 PM Igloo: joakimk: You can do PORTB |= bits_to_turn_on, or PORTB &= ~bits_to_turn_off
02:57 PM Igloo: That's short for e.g. PORTB = PORTB | bits_to_turn_on
02:58 PM Igloo: Oh, right, but you want to set a set of bits to paritular values. Yeah, then something like 20,21 sounds good.
03:02 PM joakimk: but now it works :D
03:02 PM joakimk: haha
03:03 PM joakimk: you were dead on with line 27 (bluuuush9
03:04 PM joakimk: now I can step the tempo 0-255 as wildly as I want, and the response (in the observed blinking pattern) is just that it's steadily going faster and faster :D
03:04 PM joakimk: thanks!
03:04 PM joakimk: so CTC mode isn't WRONG for this purpose...?
03:05 PM joakimk: cehteh I'd like to learn more about what you were saying, with the constant speed timer which still -- somehow -- lets you change rate
03:06 PM joakimk: do you have some links?
03:10 PM joakimk: Igloo so, something like `uint8_t mem = (~PORTB & 0b00000111);
03:18 PM joakimk: thanks again!
07:13 PM rue_mohr: ...
11:45 PM day_ is now known as day