Replies: 7 comments 9 replies
-
Like I asked when you emailed me, please post the contents of update_adc_key() - because I think there might be a much more graceful way to do this with a lot less code. The type B timers are excellejt for tasks like this - something like this is the general structure of a TCB and it's all covered in the dataheet
Off the top of my head, equivalent code for TCB1 would look something like
but again, what does update_adc_key() do? There may be an easier way..... |
Beta Was this translation helpful? Give feedback.
-
Transferred to the megaTinyCore discussions section; the modern ATtiny parts are supported by this core, not ATTinyCore. |
Beta Was this translation helpful? Give feedback.
-
BTW, the reason I asked whether that's all the ADC is used for is that therecould be better ways to implement it if so - especia,ly if you'd kind of like the timer for something else. You put the ADC into "free running" mode. on that one ADC pin. Kick it off once (you might even want to slow down the ADC clock so the readings don't come in so fast, they come in damned fast on 2-series parts because of the improved ADC) and it will continue sampling until you explicitly tell it not to.... I'll bet something like this would perform a lot better than a direct translation WITHOUT USING A SEPARATE TIMER
Another thing you could do using freerun mode which is FAR less extreme would be to simply do
either way, that ungodly mess you have there should be taken behind the woodshed and reimplemented - you can only have 1 button pressed at a time right? And there are two variables there that are 1's or 0's, one per button. if there are 8 buttons or less, that should be a single byte for each (otherwise an appropriately sized unsigned int). If you could safely set all except the bitfor the button you now know is pressed isn't pressed, then it's just var = 1<<i or something like that. shrug but I should go do something more useful. like getting 2.4.0 readty for release - if it worked on classic generation, the differences between them are such that the analogous implementation to what you had on classic will worst case be comparable3 and ;likely faster. |
Beta Was this translation helpful? Give feedback.
-
No prob, glad that was useful And as I said, all the rest of the code in that ISR sucks too (as far as I'm concerned, analog read for buttons is fundamentally a lame approach. I bet if you did like a 4 sample accumulate, and compared ADC0.SAMP with ADC0.RESULT, and then took a ton of data like that you might be able to debounce cleanmly tat way. Or if not, keep a short history of readings, or whatever/ But the real core issue is that analog read is the wrong way to do button input (shrug) To be fair I did occasionally see it when I was scrapping consujmer electronics just to see what design decisions they made (very good exercise!) - though I guess with a 1626, you can at most only get 4 more I/O pins by going with a 1627 - above that you're in 4808 or dx28 or dx32 land |
Beta Was this translation helpful? Give feedback.
-
Oh for sure, yeah - the crystal pins are only for 32.768kHz watch crystal for the RTC. (though they can take an external system clock input on PA3). You thought you needed crystal for uart? No, no you don;'t. we're living in the future now the oscillators on these are miles better than classic AVRs. I have literally never had any problems with the factory cal, even without using the oscillator error term from the tools submenu. Like, the oscillators are nothing like what you[re used to from the classic AVRs. (38400? so slow! These parts will do 115200 baud at (16 MHz / 16 =) 1 MHz from internal oscillator. I in the debug tooling for my tuning stuff that's going in to 2.4.0 I have a 20 MHz sketch that I run it at 921600 baud because it's an SPI slave that takes in debugging output and data for curve fitting over SPI from it's source (which can't use USART because it's cycling MCLKCALIBA register through every option, while timing a stream of 1 ms pulses. AVRs run the SPI bus at fairly high speeds (ie, maximum SPI clock divider, if the chip is lucky and got through it's high side cal range, would exceed 300 kHz), and I did not want to have to implement a buffer do avoid losing data, so I just cranked the USART baud way the hell up (just being a little faster wasn't enough, but I forget if I was using the Arduino serial library, that is slow enough that if you were barely going to keep up based on the baud, the delays from the bloat and inefficiency in HardwareSerial will kill it) - and since it only goes one direction, as long as it can get data out much faster than new data will ever come in, we're golden. |
Beta Was this translation helpful? Give feedback.
-
Hmmmmm if you could get all three pins onto the same port and use them like this you can ditch analog shit alltogether! select - one diode from btnpin1. then it's all digital reads. In fact if you can maneuver them all to be adjacent on the same port, (say porta pins 1-3) you could
Now you have 3 bit number that is 0 when nothing is pressed, and a different number dpeending on what button was pressed, in 4 clock cycles per read with 3 pins and a dozen el cheapo signal diodes ("small signal" diodes. I have worked with some low-forward-drop optimized diodes for the t43, the part with the integrated boost converter to run from an a single alkaline battery - but I wanted it to be efficient, so I chose the lowest drop diode I could. Well, it never went into the low power mode, because even holding back 3v, the reverse leakage through the diode was enough that it never went into active low current mode even with fresh batteries, meaning it ws leaking over a third of a milliamp in the reverse direction wehen only holding back 3V. And the datasheet explicitly warns about this and I ignored it too! Worked beautifully with proper diodes once I knew what I had to look for :-P But my point in mentioning it is that those diodes, while great at high current wouldn't actually act like diodes in this application hence why I specificaklky mentioned use of "small signal" diodes../ Heck, imagine strategically chosen combinations? up - one diode from btnpin1. (1) and the two combinations with btnpin1 and 2 are declared invalid You could do things like
|
Beta Was this translation helpful? Give feedback.
-
The ADC is baller, way better than a normal 10-bit one on classic AVR, or the 12-bit fake differential one on the Dx-series. If the numbers you get are unexpected, either it is configured wrong or your test setup is wired incorrectly and the actual analog voltages are not what you expect. You can resolve this mystery for certain with a any DMM - just check the voltage between analog pin and ground. Does it match with what your math predicts for that combination of resistors (I'm assuming this is one supply rail to a bunch of NO switches each of which connects to one side of a common resistor, and the other end of that is connected to the opposite supply rail, such that when nothing it pressed, it's always at either ~0 or ~Vcc, depending on which way around you wired it, and when you press a button, it will take on an intermediate voltage based on ratio of those resistors). You should be able to run simple code to print analog values out and measure the same voltage on your scope that is reflected at the pin (Hopefully at this point, it's not a wiring problems that is now reflected in the board you sent out at 5AM. I have seen people \post just about every permutation of that circuit, even though only one is sane. Also make sure I didn't set default resolution or reference voltage wrong - I don't think I do, but it's new (and when using RESULT directly alongside freerun mode, making sure you get the right resolution is your job, not in the core's control anymore. The hardware natively will give you 8 or 12 bits, and megaTinyCore defaults to emulatiing 10 bit ADC of older parts (from a 12 bit reading) until you instruct otherwise, but one would normally put it into 12-bit mode in setup, (see analogResoliution() in readme, and also be on guard for any default parameter like the reference voltage to be wrong on startup too. Any bug like that should be reported as I am not aware of any. |
Beta Was this translation helpful? Give feedback.
-
Hi,
I have a library that uses Timer2 on an uno. An 8 bit timer. The ATtiny 1626 uses 16 bit timers and has a split mode.
By doing some reading Timer1 on the 1626 is used for tone() which I do not use. Are there any settings I need to change under Tools in the Arduino IDE such as mills() and micros() ?
Assuming a 16Mhz crystal, can someone give me an example code showing timer1 that fires its ISR every 4 - 6ms?
The ISR calls a function that reads the value of an analog pin.
Thank you.
Here is the original code -
The ISR
That is really code to me, no idea.
Beta Was this translation helpful? Give feedback.
All reactions