#avr Logs

Jan 02 2019

#avr Calendar

08:06 AM rue_bed: there have been more people here with problems with the dragon than I think any other programmer
06:58 PM FozzWorth: Hi everyone. Happy New Year.
06:59 PM FozzWorth: Hi everyone. I built an 8 bit R2R DAC to be driven by a 328p with 16Mhz oscillator to perform simple signal generation. My goal was to then attach a rotary encoder (built from a scavenged 3 lead hdd motor) and program the uC to switch between waveforms (and eventually make other adjustments) based on the encoder input triggering external interrupts on a rising edge. And now, the dilemna...
06:59 PM FozzWorth: and hi again
07:01 PM FozzWorth: PORTD appears to be the only full 8 bit port available to attach the DAC. It also happens to be where the external interrupt pins reside as well as UART pins, both of which I'd like to utilize. Am I hosed?
07:23 PM cehteh: FozzWorth: you need uart?
07:28 PM FozzWorth: cehteh: I was hoping to keep it, but I suppose it won't be completely necessary. I've got 2x16 LCD on the way
07:29 PM cehteh: you could use some 328pb board and use the 2nd uart or some other ways to connect the display
07:30 PM FozzWorth: a couple of folks have suggested splitting the 8 bit output across ports, but I don't know if that will slow things down too much for this purpose or if implementing that with the sine table will be too deep for my current knowledge
07:31 PM FozzWorth: I'm not building for fast, just to teach myself stuff
07:31 PM cehteh: can work too, when done right it should not have much impact on the speed
07:31 PM cehteh: litlte bit more messy code
07:32 PM cehteh: and a few cycles difference in latching the output, which shouldnt matter much
07:33 PM cehteh: you have a lowpass on the output to filter aliasing? would improve this anyway
07:36 PM FozzWorth: I don't. Still getting my feet wet. Just got basic sine and saw output working.
07:37 PM cehteh: avr's are not perfectly suited for the task, but still should work put somehow
07:37 PM FozzWorth: the aliasing being the steps since the AVR is so slow?
07:38 PM cehteh: no, always happens when you switch outputs, ringing etc
07:39 PM FozzWorth: oh, how far in on the scope do you suppose I'd need to be to observe that? The waveform looked fairly clean to my untrained eye
07:39 PM cehteh: ah good then
07:39 PM cehteh: with the load you entended on the output?
07:40 PM cehteh: i never really tried to generate audio/sine waves
07:41 PM FozzWorth: ah, good point. No load on the DAC output, just a scope
07:42 PM cehteh: actually if i had to i would rather generate a square wave of variable frequency with the timer in hardware and use another timer/pwm or simple dac (few bits might be enough) to tune a filter for the output frequency
07:42 PM cehteh: far less processing needed then
07:43 PM cehteh: but entirely depends on what you actually need
07:44 PM FozzWorth: I supposed there won't be a need for a load until I learn enough to build something suitable for generating (and using) audio frequencies.
07:45 PM cehteh: most likely you want to keep it high impedance and drive a opamp with it only
07:45 PM FozzWorth: My thought is that I can't justify spending any money on equipment until I know what the hell I'm doing with it.
07:45 PM cehteh: haha
07:53 PM FozzWorth: cehteh: thanks for the tips. Off to figure how to split the byte across ports. Seems like the best (i.e cheap) solution to move forward..
07:56 PM cehteh: you lay as well just start with a less than 8 bit output
07:57 PM cehteh: surely not so nice, but good enough for a test
07:57 PM cehteh: and from that on you then can eventually work on combining more ports to create more than 8 bit DAC too
08:05 PM FozzWorth: I think I'm already at the more than 4-bit stage. I've got the encoder triggering output of a counter variable to UART correctly. Am I correct to think that to split the 8 bits, I'd need to evaluate the current array position's value on each loop iteration, do the math operations accordingly to split it in to 4 bit pieces and output to each port?
08:05 PM FozzWorth: https://paste.debian.net/1058395/
08:06 PM FozzWorth: and obviously break out the port's assigned value in to the appropriate pieces
08:07 PM FozzWorth: I was shockingly surprised by how lean that sine code ended up being.
08:07 PM cehteh: haha
08:08 PM rue_mohr: dac
08:09 PM rue_mohr: mmm audio dac
08:09 PM rue_mohr: what avr?
08:09 PM FozzWorth: 328p at 16Mhz
08:10 PM cehteh: you dont need the & 0xff ... and better use uint8_t than unsingned char for portability (actually the same)
08:10 PM rue_mohr: do you just wnt square wave?
08:10 PM cehteh: also ram is a bit limited, you may rather put the table in progmem .. and when split the port use 2 tables
08:11 PM rue_mohr: er, I guess not
08:11 PM cehteh: he wants sine
08:11 PM cehteh: well yes otherwise that code isnt so great
08:11 PM cehteh: because timing will be off when you use interrupts and handle other things
08:11 PM rue_mohr: an avr isn't really fast enough for 8 bits at 60Hz...
08:11 PM rue_mohr: at 256 samples/cycle
08:12 PM rue_mohr: so, where are you at now?
08:12 PM cehteh: isnt? i didnt do the math
08:12 PM rue_mohr: hows what you have working out?
08:12 PM FozzWorth: I've got the sine and saw code completed and I figured I'd use the encoder to add control when I get advanced enough
08:12 PM rue_mohr: I wanted to make an avr power inverter, but its not fast enough
08:12 PM rue_mohr: k
08:13 PM FozzWorth: I'm satisfied with the quality of the waveforms it produces currently
08:13 PM cehteh: well i wont do it that way either, but for learning its good
08:13 PM rue_mohr: k
08:13 PM FozzWorth: this is strictly for learning purposess
08:13 PM cehteh: whats the output freq currently?
08:13 PM rue_mohr: so, lets try this
08:14 PM rue_mohr: take the & 0xFF off and see if its any faster
08:14 PM cehteh: i bet the compiler optimized that already away
08:14 PM cehteh: with warning enabled it would prolly warn about "statement has no effect" or somesuch
08:15 PM FozzWorth: pretty sure I got 16Khz on a saw. I want to say less than half that with the sine, but I don't have a screen cap to confirm. Wiring it all back up now
08:15 PM FozzWorth: also making the suggested edits
08:16 PM cehteh: addressing, loads, store, loop ... maybe you dump the asm should be pretty efficient and as fast as you can do
08:16 PM cehteh: that loop may be less than 10 cycles ... so 16mhz/10/256
08:17 PM cehteh: my bets
08:17 PM cehteh: but it will become messy when you want to modulate the frequency
08:17 PM cehteh: and do other things along
08:18 PM cehteh: thus: much easier to output square wave and filte that to a sine
08:19 PM cehteh: can be done on hardware with the timers, the AVR will become bored then
08:20 PM cehteh: also needs only 2 pins for the output one for the square wave the other for controlling the filter (pwm)
08:21 PM cehteh: someome who is good with analog stuff may even figure out how to make a self tuning filter
08:22 PM rue_mohr: oh, you CAN overclock the avr too
08:22 PM cehteh: technically they can run at 20Mhz
08:23 PM rue_mohr: I'v heard of like 50
08:23 PM rue_mohr: iirc
08:23 PM cehteh: yeah
08:23 PM rue_mohr: but, your definitly on your own up there
08:23 PM cehteh: but then lots of the hardware gives up, uart, ADC,
08:23 PM cehteh: and still that wont help you much in modulating the frequency
08:24 PM cehteh: youi may use a attiny85, because that one can run at 16mhz from internal oscillator which is tuneable :=
08:25 PM cehteh: little dirty secret of the tinys with PLL
08:25 PM cehteh: iirc no mega can do that
08:26 PM cehteh: (i mean officially, not overclocked) with overclocking you get it way faster
08:33 PM FozzWorth: ok. 6.25Khz on the sine and 15.6Khz on the saw
08:34 PM cehteh: different array lengths?
08:35 PM FozzWorth: I believe I can see the ringing you mentioned, cehteh.
08:35 PM cehteh: no array at all?
08:35 PM cehteh: for the saw
08:35 PM FozzWorth: no array for the saw, just a counter
08:35 PM cehteh: you pretty always want a lowbass at nyquist frequency on a DAC
08:38 PM FozzWorth: adding that to the list. I knew I left a few spare through holes for a something, heh
08:41 PM FozzWorth: I breadboarded an TL07x-based wave generator, but I think I was generating the square there too and not utilizing PWM. I don't recall why opted for making the DAC
08:42 PM FozzWorth: I seem to recall getting a cleaner wave from the DAC on a breadboard
08:43 PM cehteh: better matched resistors, maybe it had filtering etc
08:44 PM cehteh: the way you do it is somewhat educational, but not much practical :)
08:44 PM FozzWorth: heh, story of my life!
08:45 PM cehteh: well .. i would be curious how my suggestion would work .. you may try that next :D
08:45 PM FozzWorth: I've got a AD9835 in TSSOP that I managed to solder up to a board I etched. Figure I need some more coding and hardware lessons before I dive in there though
08:45 PM cehteh: also learning how to use the hardware effeiciently is the road to success
08:46 PM cehteh: do you have any plans in mind what to build? synthesizer? or so?
08:46 PM FozzWorth: Yea eventually for synthesis, guitar effects and the like
08:46 PM cehteh: dunno if AVR is a good choice for that
08:46 PM cehteh: actuaaly i am pretty sure its a bad choice :D
08:47 PM cehteh: but avrs are damn easy to use and learn
08:47 PM FozzWorth: yea I got you, it was more of an oh, this is what I have available and will use it
08:47 PM cehteh: yeah nothing wrong then
08:47 PM FozzWorth: yea my C was pretty much non-existent prior
08:48 PM FozzWorth: I've programmed for the web for several years but chose AVR to get more familiar with C for the myriad of uses those chips have
08:48 PM FozzWorth: I just happened to choose the wrong use!
08:49 PM cehteh: well learn how to use the hardware stuff, timers, uart, adc, comparator etc
08:49 PM FozzWorth: I plan to utilize one to drive a menu and such for that AD chip
08:50 PM FozzWorth: yea, that's mainly what this is all about
08:50 PM cehteh: thats where it really becomes interesting
08:51 PM FozzWorth: your original suggestion for the signal gen was to utilize PWM and shape it off-chip yes?
08:51 PM rue_mohr: lots of epopel do that
08:51 PM FozzWorth: would it be something similar to this?
08:51 PM FozzWorth: http://www.learningaboutelectronics.com/images/Function-generator-circuit.png
08:51 PM cehteh: kinda ..
08:52 PM rue_mohr: you cold also use ax external dac/ram
08:52 PM FozzWorth: that's what I originally breadboarded with the TL072
08:52 PM rue_mohr: or overclock a cpu
08:52 PM rue_mohr: or do an R-2R
08:52 PM cehteh: he does a r2r
08:53 PM rue_mohr: I have an R-2R thats modified, 1 less resistor
08:53 PM rue_mohr: you do a pi to delta convrsion on the last stage, then drop the resistor that goes from the bit to ground
08:53 PM FozzWorth: I've got an external wave gen chip, but my goal is to familiarize myself with the avr and C and hardware that I can drive the chip and build menus and stuff
08:53 PM rue_mohr: :)
08:53 PM cehteh: still analog stuff when its more than monitor supply voltage temperature change or other slow things is really not the domain of AVR's
08:53 PM rue_mohr: I'm the only person in the world I know to have thought of it
09:06 PM FozzWorth: cehteh: sorry to ask again but is the method I was thinking of using for splitting the byte across two ports suitable, based on the code I have with the sine table?
09:08 PM cehteh: hu i didnt seen any code
09:09 PM cehteh: when you want speed, then you need 2 tables (gigantic waste of memory)
09:09 PM FozzWorth: where I have to evaluate the value of the array index on each iteration and break it out with the necessary math operations
09:09 PM FozzWorth: ah
09:09 PM cehteh: or clevery arrange the pins so that you can mask them by constant
09:09 PM FozzWorth: https://paste.debian.net/1058395/
09:09 PM rue_mohr: keep it to oneport if you can
09:10 PM rue_mohr: 1 sec
09:10 PM cehteh: he cant
09:10 PM cehteh: only portd is 8 bit bit he wants INT's and UART wich is therre too
09:10 PM FozzWorth: rue_mohr: I'm utilizing uart and external interrupts on portD
09:11 PM cehteh: well you may use pcint instead that works on any port
09:11 PM cehteh: and anyway using interrupts will destroy your waveform generatiion
09:11 PM cehteh: that code doesnt split bits, i've seen that
09:11 PM rue_mohr: I have a suggested split with source
09:12 PM cehteh: ideally you arrange it that at least you can mask it different bits on each ports
09:12 PM rue_mohr: looking for it...
09:12 PM FozzWorth: yea, my logic for the encoder rotation would have to be adjusted in that case. I'm detecting on rising edge and checking status of the other phase of signal
09:13 PM cehteh: useing the analog comparator for the encoder?
09:13 PM FozzWorth: I'm using a scavenged hdd 3-lead motor as the encoder, cause you know, I won't spend a dollar on equipment if I don't know how it works :-P
09:13 PM rue_mohr: DDRB = (OUTPUT << PB0 | OUTPUT << PB1 | INPUT << PB2 | INPUT << PB3 | INPUT << PB4 |INPUT << PB5 | INPUT << PB6 | INPUT << PB7);
09:13 PM rue_mohr: DDRD = (INPUT << PD0 | INPUT << PD1 | OUTPUT << PD2 |OUTPUT << PD3 |OUTPUT << PD4 |OUTPUT << PD5 |OUTPUT << PD6 |OUTPUT << PD7);
09:14 PM rue_mohr: I split over B/D
09:14 PM rue_mohr: PORTD = ~(a);
09:14 PM rue_mohr: PORTB = ~(a);
09:14 PM cehteh: woaaah
09:14 PM rue_mohr: because thats all I use them for, I dont need to play with the bit positions
09:14 PM cehteh: that looks expensive when the compiler doesnt optimize it properly
09:14 PM rue_mohr: the ~ was casue I was using it with leds that were backwards
09:15 PM rue_mohr: and cehteh the DDRB is reduced to a constant by the compiler
09:15 PM rue_mohr: #define OUTPUT 1
09:15 PM rue_mohr: #define INPUT 0
09:15 PM rue_mohr: http://paste.debian.net/1058403/
09:16 PM rue_mohr: thats the source I'm pulling it from
09:16 PM cehteh: ah my bad
09:17 PM cehteh: but moment you cant set a on both ports without masking when using the other pins except when you use alternate functions where thats hidden, still not pretty
09:18 PM rue_mohr: unless their inputs or other
09:18 PM cehteh: PORTD = a & MASKD; PORTB = a & MASKB; is what i would aim for
09:18 PM rue_mohr: in which case PORTx = wont change them
09:18 PM cehteh: well depends on what you do therre
09:18 PM FozzWorth: cehteh: the bit splitting method I haven't written yet, just trying to figure out if I'm going down the wrong rabbit hole.
09:19 PM rue_mohr: its fast tho, and I already didn't like the time diff between writing
09:19 PM FozzWorth: ok got it. Thanks for that
09:19 PM cehteh: yeah when it works its the fastest
09:20 PM cehteh: does the pin toggle thin (writing to PINx) also toggle pullups or is that only active when the pin is configured as output?
10:28 PM davor_ is now known as davor
11:13 PM day__ is now known as day
11:59 PM FozzWorth: cehteh, rue_mohr thanks for the bit mask tips. It knocked my frequencies down a couple Khz but works just fine for my ill-conceived project!