#avr Logs

May 02 2019

#avr Calendar

08:26 AM leachim6: hi o/
08:30 AM leachim6: quick question regarding timers on the tiny4313
08:30 AM leachim6: I set TCCR0B to 0x06 which should trigger the interrupt on the falling edge of PD4, should my LED not blink when PD4 falls to ground?
08:30 AM leachim6: https://pb.l1nx.pw/LjTFsnjUSt
09:20 AM leachim6: nvm I got it, I forgot that counters existed, whoops
01:04 PM jancoow: Hey guys; I might see something I'm doing wrong. I'm trying to use the compare channels of timer A on my attiny 414. However, I'm seeing odd/unexpected behaviour..
01:04 PM jancoow: What is actually the correct way to set the compare value? And to reset the timer?
01:05 PM jancoow: I currently set the values for the compare channels with TCA0.SINGLE.CMP0 and TCA0.SINGLE.CMP1
01:05 PM jancoow: and restarting the timer by setting the TCA0.SINGLE.CNT to 0
01:25 PM cehteh: dat strage api :D
01:25 PM jancoow: yep
01:25 PM cehteh: do not set the counter manually to 0
01:26 PM cehteh: let the hardware do that
01:26 PM jancoow: why not?
01:26 PM cehteh: because it not as precise
01:26 PM cehteh: interrupt may be delayed and counted on
01:26 PM jancoow: I've disabled the overflow timer, because I need to reset it on each zero cross from my 50hz AC
01:26 PM cehteh: why that?
01:27 PM jancoow: my ZC is generating a pulse every .. zero cross. Which is triggered in a falling edge interupt
01:28 PM jancoow: I measure the CNT of the timer, which is the timing of the ZC (time between the ZC)
01:28 PM jancoow: then reset it
01:28 PM cehteh: dont you rather want some PLL locked on the AC frequency?
01:28 PM jancoow: and I calculate 2 compare values, because I've two triacs, where the triac should turn ON
01:28 PM cehteh: i mean what do you want to do with zc?
01:28 PM jancoow: so that 0-100% of the time between the zero cross
01:28 PM jancoow: uhm; light dimming and a softstart for a motor
01:29 PM cehteh: do you filter the zc input not to get false impulses?
01:29 PM jancoow: yes, the ZC circuit works perfectly
01:29 PM jancoow: but I encounter some timer issues..
01:30 PM jancoow: for example, when both compare channels are the same value
01:30 PM jancoow: none of the interupts are triggered..
01:30 PM cehteh: wel read the datasheet for that avoid these situations
01:31 PM jancoow: yeah I can't find anything about that
01:31 PM cehteh: since its zero crossing, when you are close to zero the last few bits dont matter anyway, so for eample use only 0-98% of the time
01:32 PM cehteh: i'd prolly do some PLL to lock on AC freq and keep the timer running
01:32 PM jancoow: my ZC circuit has a fixed rate of 5ms pulled down
01:33 PM jancoow: on 50HZ
01:33 PM cehteh: that doesnt matter
01:33 PM cehteh: start the timer with some 'good guess' what 50hz would be
01:34 PM cehteh: then adjust the TOP value until you are locked to the phase
01:34 PM jancoow: this is the current code btw :P https://jancokock.me/f/fb4f3 (cmp 0 disabled for testing)
01:34 PM cehteh: you may even use the input capture for that
01:34 PM jancoow: ah okay, yeah that might be better
01:34 PM cehteh: gives you shitloads of time to handle the interrupt without being time critical
01:35 PM jancoow: problem is when I set the compare channel to this: 24760 - 24735 - 24735 (where the first number is the ZC timing, last two numbers are the compare values)
01:35 PM jancoow: my light bulb starts flickering like hell
01:35 PM jancoow: which I don't get why..
01:35 PM cehteh: but dont expect that you can locxk perfectly on the phase, you have some aliasing error, thus handle that in some sensibel way, allow some slight deadband or accept jitter
01:36 PM jancoow: while this doesn't flicker: 24758 - 24735 - 24638 (this is 1% "on")
01:37 PM cehteh: mhm no incput capture for you, you need both channels :)
01:38 PM cehteh: dunno your chip but on many avr's the timer can be configured to use the IRC register as TOP, that allows you for using both compare match units for pwm
01:39 PM jancoow: maybe I should just turn the compare channel off if it's 0%.. same for 100%
01:39 PM jancoow: that will solve the issues to I think
01:42 PM cehteh: yes good idea
01:43 PM cehteh: and keep timers running if possible
01:43 PM cehteh: usually thats less pain
01:43 PM jancoow: why?
01:44 PM cehteh: consitency, no jitter, compensating for errors, less interrupt load,
01:44 PM cehteh: plenty reasons
01:44 PM jancoow: so just setting the CNT to 0 will be okay?
01:44 PM cehteh: no
01:44 PM cehteh: keep the timer running
01:44 PM jancoow: huh
01:45 PM cehteh: PLL
01:45 PM cehteh: yes you can reset it every time, will works as long your ZC is 100.000% exact
01:46 PM jancoow: why wouldn't my ZC be perfect
01:46 PM cehteh: but when you lock your timer to generate a 50hz signal syncronized with the zc then it just works even when missing a zc due to noise
01:46 PM cehteh: becauzse AC has shitlods of noise on the line
01:47 PM jancoow: yeah especially when driving motors with this board LOL
01:47 PM jancoow: due that flicker on 0% I killed 2 triacs..
01:47 PM cehteh: might be just your neighbour turning on a vaccum cleaner or drilling machine
01:48 PM jancoow: my neighbours have ~9 * 3kwh fans running ^^
01:48 PM cehteh: also you may add a snubber to the triacs
01:48 PM jancoow: yeah it's to late for snubbers :/
01:48 PM cehteh: lol
01:49 PM cehteh: then expect flickering and killed triacs
01:49 PM jancoow: I can't drive my reversable shutter with this board anyways. It's putitng around 500 volt on the second widning of the motor
01:49 PM jancoow: which kills the triac imediatly
01:49 PM jancoow: so mostly light dimming
01:49 PM cehteh: protection diode
01:50 PM jancoow: on ac
01:50 PM cehteh: yes
01:50 PM jancoow: varistor?
01:50 PM cehteh: no diode .. two zeners facing each other
01:50 PM cehteh: --->|-----|<---
01:50 PM jancoow: I moved the approach to one triac and a relay to change directions :)
01:50 PM jancoow: which works fine
01:51 PM jancoow: anyhow
01:51 PM cehteh: thats readily available with 1.5kw surpessing capabilities
01:51 PM cehteh: 40 cent or so
01:51 PM jancoow: you are suggesting setting my timer overflow to the 50HZ frequency?
01:51 PM cehteh: yes
01:52 PM jancoow: but then I need a second timer to measure the time between the zero cross, right
01:52 PM cehteh: using the ICP as overflow .. then you can still use both compare/match units
01:52 PM cehteh: no
01:52 PM cehteh: only one timer
01:53 PM cehteh: on startup you set the overflow to some 'good guess' for 50hz
01:53 PM cehteh: then on the first zero cross pulse you start the timer
01:54 PM jancoow: and then how can I adjust the timer
01:54 PM jancoow: to match if it's out of sync
01:54 PM cehteh: and on each subsequent pulse you check how close it is to the overflow, perhaps adjusting it to get locked on the phase
01:54 PM cehteh: and of course you keep the pll state somewhere and only operate the triacs when in sync
01:55 PM cehteh: https://en.wikipedia.org/wiki/Phase-locked_loop
01:56 PM cehteh: in your case its pretty much easier than the article looks like :)
01:57 PM jancoow: but in that case I'm just hardcoding the timing between the ZC
01:57 PM cehteh: in your case?
01:57 PM jancoow: that would be 24750 as overflow value
01:57 PM cehteh: i ownt do that
01:57 PM cehteh: will drift out of phase
01:58 PM jancoow: yes but how can I measure how much time there is between a ZC ,, if the timer resets at a specific top value
01:58 PM cehteh: ok unless you reset the timer on each zc ... if thats 100% reliable :)
01:58 PM jancoow: yeah
01:58 PM cehteh: you dont need to measure
01:58 PM jancoow: why not 0.o
02:00 PM cehteh: on zc you just check if its liek say 2% smaller than 24750 ... then 24750 was too high. decrement it, or 0 to 2% of the range (timer just overflown before the ZC) then your timer is too fast, increment it
02:00 PM cehteh: also .. important you have 2 variables, frequency and phase
02:01 PM cehteh: thus you need some way to nudge it into phase
02:01 PM cehteh: (thats temporary incrementing/decrementing the top but not alter the freq globally)
02:02 PM jancoow: and why... would this be more accurate then just measuring the time between each ZC and resetting the timer? :P
02:02 PM jancoow: I don't really get that
02:03 PM cehteh: because pll can run even over noise, averaging it out, while your reset may work .. as long noise dont disturb it
02:04 PM jancoow: but that's why I especially choose this circuit: https://cdn.hackaday.io/files/1597066832861504/SimpleIsolatedZeroCrossDetector.pdf
02:04 PM jancoow: which would elimate all noise ! :D
02:04 PM cehteh: ok then go on in your way
02:05 PM jancoow: but yeah, if it's a noisy ZC, I can understand that that would result in flickers
02:05 PM jancoow: but that didn't happen so far; only when two compare values are the same or close togheter
02:06 PM jancoow: or at 0%
02:06 PM jancoow: (so compare channel is close to max..)
02:06 PM cehteh: with the weird api your chip has i cant help you, read datasheet again, dont skip details
02:07 PM cehteh: there are some nasty details around 0% 100% and such corner cases
02:07 PM jancoow: yeah no worries you already helped me so much last time :D
02:07 PM jancoow: I really appreciate that
02:07 PM cehteh: also you may just ensure that the values are never the same if(ocra==ocrb)++orca; ....
02:08 PM jancoow: cehteh: maybe fun to watch the working led dimmer project: https://photos.app.goo.gl/qHbwWsppvHTipuVa8
02:08 PM cehteh: i doubt that a +/-1 difference matters
02:08 PM jancoow: yeah maybe that's a dirty work around.. I won't probably notice it on dimming levels indeed. Same goes for 0 and 100%. I should just disable the compare and let the GPIO pin stay high or low all the time
02:08 PM cehteh: but looks more like there is some more grave bug, check timer mode etc
02:09 PM cehteh: iirc you need to do that anyway .. see datasheet, one end (0% or 100% wasnt perfectly available)
02:10 PM gsi__ is now known as gsi_
02:10 PM cehteh: thats some hardware limitation, compare match toggles or changes the output and it only gets reset on the next timer tick or so
02:11 PM * cehteh forgotten the details
02:11 PM jancoow: mmh okay okay
02:11 PM jancoow: yeah that's definitly happening at 0%
02:11 PM jancoow: 100% is working fine; but it isn't actually 100% as in timer max value, but 100% as in close to the ZC
02:12 PM jancoow: or well; at exactly the ZC
02:37 PM jancoow: cehteh: okay the 0 and 100% check are working, the big flickers are gone
02:39 PM cehteh: nice
02:39 PM jancoow: but..
02:39 PM jancoow: now I pay close attention
02:39 PM jancoow: I guess you are right
02:39 PM jancoow: I see really small jittering
02:45 PM jancoow: cehteh: It's gotten worse with the new code lol. I think there is to much happening now in the ZC interupt
02:46 PM cehteh: do you have a scope or logic analyzer
02:46 PM jancoow: nope :(
02:47 PM cehteh: get a $12 logic analyzer at least
02:47 PM cehteh: then time the ZC output precisely and see if there is any jitter
02:48 PM jancoow: Wanna buy a RIGOL DS1054Z
02:49 PM jancoow: cehteh: this is currently the code. https://jancokock.me/f/cdf7b/ pretty.......... ugly lol
02:50 PM cehteh: doesnt matter much for what you are doing any reasonable china dso will do .. and a logic analyzer is just something you need to have, buy it anyway that doent compete with a dso, it complement its
02:51 PM cehteh: there are plenty things you can only do with logic analyzer which are hard to impossible with a 40x as expensive scope
02:51 PM cehteh: and yes dont put that all in the isr
02:52 PM cehteh: steps_between_zc / 255 * (dimmer_channel_1_level/DIMMER_SCALE_VALUE);
02:52 PM cehteh: .. calculate the other way around
02:52 PM jancoow: what?
02:52 PM cehteh: steps_between_zc * (dimmer_channel_1_level/DIMMER_SCALE_VALUE) / 255 ; (dont you need /256?)
02:53 PM jancoow: I'll move the 0 & 100% check to the main loop, that hasn't be checked every time in the ZC
02:53 PM cehteh: prolly needs 32bith math, but divide first will be pretty lossy
02:54 PM cehteh: you can prolly move almost everything into the main lool
02:54 PM cehteh: loop
02:54 PM jancoow: yeah, but the dimming will be lot faster then
02:54 PM jancoow: because it runs faster
02:54 PM cehteh: also that channl_0/1 caclulations .. just protect it with cli/sei
02:54 PM jancoow: maybe I simply make a slow timer
02:54 PM cehteh: it wont be faster
02:55 PM jancoow: yeah, it will, this only runs at 1/00
02:55 PM cehteh: ?
02:55 PM jancoow: I currently increment with X value every zero cross
02:55 PM jancoow: so 100 times per second
02:55 PM jancoow: right
02:55 PM jancoow: ?
02:55 PM jancoow: so main loop runs a lot quicker
02:56 PM cehteh: timer ftw ... and sleep
02:56 PM jancoow: yeah that might be better
02:56 PM cehteh: mainloop can laizily update all kinds and stuff and go into sleep when done
02:56 PM cehteh: interrupt with load the values and wake up from sleep
02:57 PM cehteh: when you have other interrupt sources of course you need to sync that somehow (and/or go instantly to sleep again)
02:57 PM jancoow: the new math isn't working tho
02:57 PM jancoow: like you suggesting
02:58 PM cehteh: could be a single bool indicationg if the calculates values got consumed
02:58 PM jancoow: yeah that's what I'm doing now
02:58 PM cehteh: you need 32bit math
02:58 PM cehteh: bit slower but gives you the precision
02:58 PM jancoow: https://jancokock.me/f/0406b/ this is the whole thingy :P
02:58 PM jancoow: sorry for bad code..
02:59 PM jancoow: oh nvm your math does work :P
02:59 PM cehteh: well i dont want to read it all :) .. want to do my stuff here
02:59 PM cehteh: my math needs 32bit
02:59 PM cehteh: but more precise
02:59 PM cehteh: and you most likely need to /256 not /255 or?
03:00 PM jancoow: yeah cool no more flickering at least now more things are removed from the ZC :D
03:00 PM jancoow: it's butter smooth
03:00 PM cehteh: its more because of the math change which had huge aliasing errors
03:00 PM jancoow: ah damn I see...
03:00 PM cehteh: change the math to your way dividing first :)
03:00 PM jancoow: It's much more accurate now
03:01 PM jancoow: was the math so bad?
03:01 PM jancoow: and why 256? My max number is 0xFF
03:02 PM cehteh: 24750 / 255 = approx. 97.058824
03:02 PM cehteh: so you had only 97 discrete steps
03:02 PM cehteh: and since the A is sine its even pretty non-linear
03:02 PM cehteh: AC
03:03 PM cehteh: thats pretty coars esp on the dim end+
03:04 PM jancoow: mmh
03:04 PM cehteh: / 256 is much easier on the compiler, thats just a shift (or in this case jist accesing the higher word 1 address shifted)
03:05 PM jancoow: well on 0xFF the compare will be turned of anyways and the I/O will stay high
03:05 PM jancoow: but I'll change it :D
03:06 PM cehteh: and you wanted to divide to 256 discrete values 1-256 not 0-255 -- but maybe finally you need to substract one, havent checked your whole code but just a hunch that makes more sense
03:06 PM jancoow: my input value is from a UART char, so 0x00 to 0xFF
03:06 PM cehteh: foo * scaling / 256 -1 ?
03:07 PM cehteh: foo * (scaling+1) / 256 -1
03:08 PM jancoow: huh
03:08 PM cehteh: heh just ceck it dunn owaht you exactly need
03:11 PM jancoow: me neither anymore lol
03:11 PM jancoow: I'm just feeling a noob right now
03:12 PM cehteh: actually: ((24750 + (24750 / 255)) * 255) / 256 = 24750
03:12 PM cehteh: .. note that you can precalculate the (24750 / 255)
03:13 PM cehteh: no need to do that in the isr
03:13 PM jancoow: where did you get the 24750
03:13 PM jancoow: yeah..
03:13 PM cehteh: you saied that earlier
03:13 PM cehteh: whatever you need there .. time bewtween zc
03:13 PM jancoow: yeah exactly
03:14 PM cehteh: anyway that abive is exact (but linear)
03:14 PM jancoow: yeah speaking about linearity..
03:15 PM jancoow: this is just the simple version.. I also have a current sensor on the board
03:15 PM cehteh: in reality you prolly want some approximation which accounts for the sine AND for human perception, thus that formula is moot
03:15 PM jancoow: where I want to also liniar dim led bulbs
03:15 PM jancoow: because led bulbs totally don't dim nicely
03:16 PM jancoow: so that's an other thing I want to achieve this this board :D
03:20 PM cehteh: there you have to take even more things into account
03:20 PM cehteh: ideally you find some simple approximation formula which matches it nicely
03:24 PM jancoow: the problem is that no led bulb dims the same
03:25 PM jancoow: that's why measuring and some kind of calibrate process would be awesome :P
03:25 PM cehteh: add a light sensor and do a PID control loop :D
03:25 PM jancoow: yeah that could do the job to
03:25 PM jancoow: I've already a ldr :P
03:44 PM jancoow: cehteh: tested it for a while now; it looks really stable!! no flickering, works great
03:44 PM jancoow: dimming looks good
03:45 PM jancoow: wait I'll made a small vid :P
03:45 PM cehteh: i douibt a vid will show the difference
03:45 PM jancoow: yea fair :P[
03:45 PM jancoow: nvm then
04:03 PM jancoow: i've to go byebye!
11:57 PM day__ is now known as day