#avr Logs

Oct 20 2019

#avr Calendar

05:38 AM vishwin60 is now known as vishwin
07:25 AM joakimk: I've set up Timer1 on an ATMEGA16 to run in CTC mode (I think) with overflow interrupt, so every time the counter reaches the value in `OCR1A` the ISR is executed. Next, I'm trying to transmit a "tempo byte" over serial, but I'm having problems scaling from 8-bit to 16-bit
07:27 AM joakimk: Here's what's happening in the ISR: `OCR1A = (1-(payload / 256)) * 65536;`, where `payload` is an unsigned char.
07:27 AM cehteh: what is a 'tempo byte"
07:27 AM joakimk: hehe, just my name for it :)
07:27 AM joakimk: I want to be able to type/transmit the tempo
07:28 AM joakimk: so `payload` will be [0, 255]
07:28 AM joakimk: I guess I _could_ change to transmit a 16-bit "tempo byte", but then I have to figure out the details in python all over again
07:30 AM cehteh: i dont really get you, this are now multiple things at once
07:30 AM cehteh: first timer overflow, at MAX or TOP ... and do you want to change the timer speed?
07:31 AM joakimk: yes
07:31 AM joakimk: but the timer is 16-bit, right?
07:31 AM joakimk: timer1
07:31 AM cehteh: second compare match at OCR1A .. looks totally unrelated to me now
07:31 AM cehteh: third some serial stuff
07:32 AM joakimk: well, sorry about that then...
07:32 AM djph: break it into parts
07:32 AM joakimk: OCR1A is TOP for timer1, isn't it?
07:32 AM cehteh: look at the datasheet i dont have the ds for the mega16 open here
07:32 AM djph: joakimk: "maybe"
07:32 AM joakimk: I think it is
07:32 AM cehteh: no depends on mode
07:32 AM djph: ^
07:32 AM joakimk: but if I toggle it between 25000 and 65000, then I get two tempos
07:32 AM cehteh: there are plenty ways to configure that, can also be the ICP register
07:33 AM joakimk: so I think this is what I want to set (I have looked into the datasheets when I set this up)
07:34 AM joakimk: thing is -- and this seems to me to be a legit question -- the TOP value for timer1 is 16-bit. If I receive an 8-bit value, how can I "scale" that
07:34 AM joakimk: does that make more sense?
07:34 AM cehteh: there are subtle differences when oyu use the OCR or the overflow interrupt btw
07:34 AM cehteh: you can just look at the high byte
07:35 AM joakimk: of what? :)
07:35 AM cehteh: loosing 8 bytes of precision .. if i understand you right
07:36 AM djph: joakimk: send two bytes, update the high/low of the compare register
07:36 AM joakimk: so, scaling is not a good approach?
07:37 AM cehteh: you can make some relation 0 == 25000 and 1 == 65000, .... sending 0 or 1 (or whatever you defined) when you only need few values and want to preserve acuracy
07:38 AM joakimk: any idea why my computation/arithmetic doesn't work? Is it something to do with using uint8, and not floating points?
07:38 AM cehteh: i dont get why you need to scale it, why do you need a 16 bit timer, why dont you send 16 bit then, whats the actualy thing you try to archive?
07:40 AM cehteh: dont use floating point
07:40 AM cehteh: and in integer you should do multiplication before division and ideally optimize division out
07:40 AM joakimk: sure I can send two bytes. I just spent a lot of time setting the python script (on the PC) up to pack/encode etc etc the bytes properly, so I just continued from what I had working. And then it occurred to me that sending one byte (256 resolution tempo) would be sufficient for my purposes
07:41 AM joakimk: but sure I can send two bytes. It seems doing math in the ISR is just wasting CPU :)
07:41 AM cehteh: you cant compress 16bit lossless to 8 bit
07:41 AM joakimk: well no
07:41 AM cehteh: scaling would be just sending the upper half of the 16 bit
07:42 AM cehteh: (or value/256)
07:42 AM joakimk: I don't understand your point
07:42 AM joakimk: here's what I'm trying to achieve;
07:42 AM joakimk: I have LEDs blinking, controlled by timer1. The tempo for that (OCR1A) is 16 bit. Can't change that
07:42 AM joakimk: I have a serial connection, and I wanted to transmit a byte for the tempo.... using one byte
07:43 AM joakimk: just for fun
07:43 AM cehteh: ah
07:43 AM cehteh: then its multiplied by 256
07:43 AM cehteh: other way around
07:43 AM joakimk: aaa ha!!
07:43 AM joakimk: that's great
07:43 AM joakimk: hadn't thought of it that simple
07:43 AM joakimk: haha
07:43 AM cehteh: TOP = byte_from_serial*256;
07:43 AM joakimk: funny
07:44 AM joakimk: thanks!
07:44 AM joakimk: problem with this hobby is the complexity of the hardware stuff makes me forget all common sense
07:45 AM cehteh: well .. i recommend to send text and parse that, bit more work, but easier to handle/debug in the long run
07:45 AM djph: or just whack it into the OCR1AH byte directly ... no multiplying necessary
07:45 AM joakimk: nice!
07:45 AM cehteh: for example as soon you start to send more data or 16bit values you need to have some protocoll to define where the message boundaries are
07:46 AM joakimk: well, there is a protocol... I'm using the ESP8266
07:46 AM djph: well, read the datasheet on how to do that, but you can usually just slam it right into the compare register ...
07:46 AM cehteh: sending "set led_tempo 65512\n" would be the easiest
07:46 AM joakimk: familiar with that? the wifi module?
07:46 AM cehteh: i thought you use mega16
07:47 AM joakimk: yes. ESP8266 connected via serial to ESP
07:47 AM joakimk: sorry to ATMEGA16
07:47 AM djph: the esp-whatever is an atmega and esp82(?) on the same board
07:47 AM cehteh: why that?
07:47 AM djph: because wifi!
07:47 AM djph: (also, eww wifi)
07:47 AM cehteh: the esp is a microcontroller on its own which is vastly more powerful than the mega16
07:48 AM joakimk: but it doesn't have more than -- what -- 2 output pins?
07:48 AM cehteh: you can set timers and blink leds on the gpio pin of the esp already
07:48 AM cehteh: yes of course
07:49 AM cehteh: https://en.wikipedia.org/wiki/ESP8266
07:49 AM cehteh: look at the data ...
07:50 AM joakimk: Btw... any chance you might take a quick look at how I'm setting up the timer...? Seems to work as it should, but I'm confident it's not 100% correct. Here's a paste: https://pastebin.com/ecsMD5ZG
07:50 AM cehteh: often not all gpio's are routed out, but usually enough to be useful for some projects
07:51 AM joakimk: but it says, GPIO 0, General-purpose input/output No. 0
07:51 AM joakimk: 2 right?
07:51 AM joakimk: I have the AI-Thinker version
07:51 AM cehteh: Input 16 GPIO pins
07:51 AM joakimk: definitely not on mine ...
07:51 AM joakimk: not on the breakout at least
07:52 AM joakimk: the breakout has a header of only 8 pins
07:52 AM joakimk: including VCC and GND
07:52 AM cehteh: you want to clear the ISR flag in case one was pending by writing a 1 to it
07:52 AM cehteh: and setting the prescaler starts the counter, after that you better dont touch it, when you need to clear TCNT then do that first
07:52 AM joakimk: I do cli before, and sei after
07:53 AM joakimk: ok
07:53 AM cehteh: yes esp because that you need to clear the isr flag
07:53 AM joakimk: but I do:
07:54 AM cehteh: imagine the counter is running, you call cli, just in that moment the counte flags a pending interrupt, then you reconfigure the counter, once you finish and call sei() the pending interrupt will be called
07:54 AM joakimk: well yes...
07:54 AM cehteh: corner case/race condition
07:55 AM joakimk: but I didn't quite understand your point (when you said, "yes esp because that you need to clear the isr flag")
07:55 AM cehteh: put cli/sei into your setupTimer
07:56 AM joakimk: but setupTimer is run only once, before the main loop
07:57 AM cehteh: in rare cases you get spurious interrupt when you dont clear the interrupt flag while reconfiguring the timer (in some other cases you may actually handle that pending interrupt, but thats up to you to understand and determine whats the right thing)
07:57 AM joakimk: do you mean reconfigure as in when I *change* the tempo?
07:57 AM joakimk: here's my ISR:
07:57 AM cehteh: usually in 99% of all cases you want to cancel all traces left from the old hardware config
07:57 AM cehteh: yes
07:58 AM joakimk: do you mean, `cli(); OCR1AH = payload; sei();`?
07:58 AM cehteh: no whole setup timer
07:59 AM cehteh: https://pastebin.com/xRyJ1TMc
07:59 AM cehteh: changing OCR1A is latched you dont need to care /disable interrupts there
08:00 AM joakimk: ok :) uh, sorry about this but: I realize now, while discussing all this (which I greatly appreciate!) that I'm really not doing any overflow interrupt. My ISR is `ISR (TIMER1_COMPA_vect)`
08:00 AM cehteh: yeah you can use either
08:00 AM joakimk: this is not overflow, is it?
08:00 AM cehteh: there is a slight difference
08:01 AM cehteh: COMPA happens when the timer hits TOP
08:01 AM cehteh: (actualy OCR1A .. but you configuredthat to be TOP)
08:01 AM joakimk: yes
08:02 AM joakimk: right :) So, here's my ISR: ISR (TIMER1_COMPA_vect)
08:02 AM cehteh: and overflow happens slightly later on the next step when the timer overflows (reset to 0)
08:02 AM joakimk: sorry to paste inline here...
08:02 AM cehteh: just a subtle difference
08:02 AM joakimk: yeah -- I think that makes no difference to me. As you might see, I have an array of bytes, containing a "pattern of bits" to blink. And I step through that program on PORTB (LEDs)
08:03 AM joakimk: ... and I want to be able to change the tempo while it's running
08:04 AM cehteh: should be right
08:06 AM joakimk: thanks again!
08:06 AM joakimk: :)
12:43 PM rue_mohr: it would be good to take one of the existing libraries and clean/ammend it from one end to the other
12:43 PM rue_mohr: there have been a lot of good ones done
01:01 PM nohit: what lins
01:01 PM nohit: *libs
01:10 PM rue_mohr: http://homepage.hispeed.ch/peterfleury/avr-software.html
01:11 PM rue_mohr: flurrys i2c library is a great start, there are other things there,
01:11 PM rue_mohr: cant recall the name of the other one
01:11 PM rue_mohr: prycon
01:11 PM rue_mohr: http://www.procyonengineering.com/embedded/avr/avrlib/
01:12 PM rue_mohr: not much is worth mentioning after prycon
01:13 PM rue_mohr: our guess is that prycon was the base of what became arduino
01:14 PM rue_mohr: most of it will need a heavy rewrite now, gcc changed
01:17 PM rue_mohr: https://github.com/dreamiurg/avr-liberty
01:17 PM rue_mohr: heh, not much newer
01:18 PM rue_mohr: https://github.com/synic/avrlib
01:18 PM rue_mohr: I wonder what happened 6 years ago
01:24 PM djph: arduino got better?
01:25 PM rue_mohr: I watched it from its start
01:26 PM rue_mohr: it was a completely silly system for programming microcontrollers
01:26 PM rue_mohr: but thats what the world wanted
01:33 PM nohit: this "great flurrys i2c library" is yet another bit-bang library :D
01:34 PM nohit: nothing special about it
01:35 PM rue_mohr: actaully, ecasue its software it works on anythign
01:37 PM nohit: that is true. but its true about every i2c bit bang lib
01:46 PM rue_mohr: flurrys is different
01:47 PM rue_mohr: his is as tight as software i2c acn be
01:47 PM rue_mohr: not an oz of bloat on that
01:47 PM rue_mohr: there are not many peices of code I back in the way I'll back that
02:01 PM samantaz__ is now known as samantaz
02:22 PM nabil__ is now known as nabil
02:33 PM twnqx: rue_mohr: LUFA
02:33 PM twnqx: though yes, slightly bloated / overheady
03:18 PM nohit: and in 2019 completely useless
03:21 PM julius: hi
03:22 PM julius: is there some common led driver chip thats easy to get on aliexpress? 12v input, 900ma output
03:24 PM rue_mohr: yea, not gonna say its all usable
03:25 PM rue_mohr: julius, maybe, the popularchips aren't listed cuase their like china internal
03:25 PM julius: china internal?
03:25 PM julius: you mean not for outsiders?
03:28 PM rue_mohr: usually there isnt' even an engligh version of the datasheet
03:30 PM julius: but they sell everything else...why not led chips?
03:31 PM julius: i mean drivers...they sell leds alright
03:35 PM djph: 12v input on an IC is a bit much ...
03:35 PM julius: output i mean, for the led
03:35 PM julius: the led wants 900ma at 9-12v
03:42 PM nohit: https://www.banggood.com/Wholesale-LED-Drivers-c-3466.html
03:45 PM julius: yes thanks. i was more looking for something that is controllable via code for dimming. texas instruments got some nice chips. but i avent found much information about it, code for example
03:48 PM nohit: i have written a driver for thus https://www.diodes.com/assets/Datasheets/AP3156.pdf
03:55 PM julius: this one: https://www.diodes.com/assets/Datasheets/AP3156.pdf got a pdf with 44 pages....jesus
03:56 PM julius: nohit, nice. but that one outputs 20ma per channel?
03:57 PM nohit: if the ds says so
04:12 PM rue_shop1: polprog, I still get the make install for the stm32 hanging every second time...
04:19 PM polprog: i havent had the time to look into it
04:19 PM polprog: sorry :(
04:21 PM rue_shop1: hmm I thought we'd worked it out and fixed it...
04:22 PM rue_shop1: something to do with the reset time or soemthing and thought we adjusted it
04:24 PM rue_shop1: ah hello (unrelated)
04:26 PM rue_shop1: is 0x200 ~1/10 of 0x1000 ?
04:26 PM rue_shop1: yes!
04:27 PM rue_shop1: ok I got input capture working!
04:28 PM djph: yay!
05:05 PM polprog: rue_shop1: ill check that out as soon as i lay my hands on the stm32 again, ive got a ton of other stuff i need to do
05:05 PM polprog: uni mostly
05:05 PM polprog: was planning to do electronics today but had to set up VMs
05:06 PM polprog: nighters
09:34 PM rue_mohr: I got freq measuring working
09:44 PM Thrashbarg_ is now known as Thrashbarg
11:42 PM day__ is now known as day