My first project! Reading and Generating PWM signals with Attiny10

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all,

 

I’m designing a code for the Attiny10. Since I’m new in microcontrollers, the past few weeks I’ve been learning about them from the amazing information on this forum, from the basics, practicing with simple examples and dealing with interrupts, timers, etc. Now I feel confident to start programming the project that I wanted to do, but I’d like to share with you how I’m going to do it and hear your opinions that will make me learn for sure.

First, let me explain the project. It’s simple but challenging also (at least for me, hehe). The micro receives a PWM signal on pin 3 (it’s ICP0 so it’s possible to set up interrupts for changes in the input signal). The PWM input signal is a RC signal, i. e. duty cycle between 1000 and 2000 microseconds and frequency about 50Hz. Depending on the duty cycle of the input, a LED (on Pin 1 – OC0B) will be on/off with different patterns.

The tricky part is that the LED patterns will use PWM modes in order to fade in/out the LED, so we have one timer and 2 PWM signals to deal with. As far as I know, the Attiny10 can’t be reading the PWM input signal and producing a PWM output at the same time (there is only one timer!), so I have three options:

 

1 - Use timer  for reading the PWM input and generate the PWM output signal by software

2 - Use timer for generating the PWM output and read the PWM input by software

3 - Alternate the use of the timer for reading and generating the PWM input/output

 

Option 3 is possible because I don’t need an immediate response on the output, a delay of 1s it’s not a problem, and the LED pattern will be repeated every second, so I can set up the timer to read the input -> read the input -> set up the timer to generate the PWM output -> generate one pattern (1 seccond) -> set up the timer to read the input -> LOOP

 

From my point of view, all three options are possible and are good, but I’d like option 3 the most since It’ll be reducing software and use more hardware. What do you think? Any other option?

 

Thank you in advance!

Manuel

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Why not set up the timer for an IRQ every 10us, to take a measurement of the PWM (this would give you about 100 possible different results for the 1000us variation).  Also during the 10us (100KHz) IRQ you can skip led on cycles  to give the led any brightness you want...if done over 100 irq's, you can control the brightness to 100 levels at a 1KHz refresh, or 1000levels at 100Hz refresh. 

==================

On the other hand, the OCRnA  can be set to generate a PWM, which just uses the free-running TCNT count as the tickbase, so just adjust OCRxA to give the desired LED PWM

 

The Input capture  (ICR) can also be capturing the TCNT as external events happen .. read it and compare to the previous capture reading to determine pulse width (duty cycle)...that assumes you know the signal's freq [Duty=hi_time*freq]    If not, you might be able to measure the high pulse width (as just mentioned), and set an opposite ICR trigger to measure the lowtime...with both numbers, the duty can be found , even if the freq is not originally known.  [ Duty= high_time/(high_time+low_time) ]...this assume not much changes between these separate measurements, which might not be completed on the same wave cycle (but very close).

 

When in the dark remember-the future looks brighter than ever.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I like first option, pretty straightforward and easy to implement, but I like more the second one, I think is more effective. I didn’t know it’s possible to set up two (or even more) interrupts and use the timer for PWM generation and Input capture at the same time.

 

I measure the dutycycle enabling the Input Capture Interrupt for rising edge, I save the TCNT and change to falling edge capture, and measure the difference between the two events. The problem that I see here is when the timer overflows I’ll have a mistaken measure, so somehow in the code I should prevent it.

 

Thank you very much for your answer, very helpful.  

 

Manuel

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A brain dead T10 would not be my choice for a first AVR, why not use an Arduino UNO, much easier to program and LOTS of example code to learn from, not to mention it's cheaper then the combo of mpu + plugboard adapter + power supply + programmer + any thing else I may not have thought of.....

 

Jim

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:
A brain dead T10 would not be my choice for a first AVR,
+1
ki0bk wrote:
why not use an Arduino UNO, much easier to program
+1

 

My thoughts entirely. I guess people pick things like tiny10 because they look "small and simple"? Too small and too simple! The mega328p on an Arduino board is a great chip for learning AVRs (which is presumably why the Arduino folks eventually concentrated on that one).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
ki0bk wrote: A brain dead T10 would not be my choice for a first AVR, +1 ki0bk wrote: why not use an Arduino UNO, much easier to program +1
avrcandies wrote:
Why not set up the timer for an IRQ every 10us,

I'm not a Tiny10 guru, but in any AVR8 a repetitive 10us interrupt, while not drop-dead, is at least tricky in any real app with other interrupt sources as any latency greater than 20us causes a lost "tick".

 

M3one wrote:
As far as I know, the Attiny10 can’t be reading the PWM input signal and producing a PWM output at the same time (there is only one timer!),

As mentioned, I'm not a Tiny10 expert.  In an AVR8 with a typical 16-bit timer, ICP can be used along with PWM.  Some caveats, as ICR1 then cannot be the TOP for the PWM and a different TOP register will be needed.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:

A brain dead T10 would not be my choice for a first AVR, why not use an Arduino UNO, much easier to program and LOTS of example code to learn from, not to mention it's cheaper then the combo of mpu + plugboard adapter + power supply + programmer + any thing else I may not have thought of.....

 

Jim

 

 

I also have the arduino UNO and I've been dealing with it before. Now I use it as programmer for little T10. No complaints for the T10, it's challenging :D

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Starting small, I see. I love Tiny 10s, but that's just because I'm weird and love making tiny things.

 

A whole, silly computer in a SOT-23! Amazing!

 

Of course, I also like Arduini and xMegas too.

 

277,232,917 -1 The largest known Mersenne Prime

Measure twice, cry, go back to the hardware store

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:

As mentioned, I'm not a Tiny10 expert.  In an AVR8 with a typical 16-bit timer, ICP can be used along with PWM.  Some caveats, as ICR1 then cannot be the TOP for the PWM and a different TOP register will be needed.

 

I'll let you know if I succed!

 

Thanks for the tips

 

Manuel

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi again,

 

Just finished the code. It works like a charm!! I set up timer as fast PWM 9bits and I use it for reading PWM from ICP0 pin and generating PWM output.

 

I read PWM in the interrupt when the ICP0 signal rises/falls (taking into account when the timer overflows). Depending on this read, the duty cycle of the PWM output is changed in the main routine. The overflow timer interrupt is only used to update the duty cycle.

 

I'm really impressed how well it's working, this tiny thing is amazing!!

 

Thanks for the help.

 

Manuel