# How to use output PWM in AVR

I have this code to blink an array (8-led) of led one after one:

#include <avr/io.h>
#include <avr/delay.h>

#define dela 500

int main (void)
{
DDRC = 0xFF;
for(int i=0; ; i++,i%=8)
{
PORTC |=(1<<i);
delay_ms(dela);
PORTC &= ~(1 << i);
delay_ms(dela);
}
}


I want to light up those led partially, like dimmed version of this, something like PWM

Now my question is how can I do that, how can I write analog output ?

Micro-controller : ATmega8A - PDIP

• you would require a DAC for analog output ... that is different from PWM ... an analog output is not necessary for varying the brightness of an LED ... a PWM signal is preferrable because it does not involve analog voltage levels – jsotola Sep 28 '19 at 21:18
• Have you considered using larger resistors? You only need one resistor since there will only be one running at a time. – Caleb Reister Sep 28 '19 at 21:20
• If you want to minimize software overhead, I would recommend getting a 3-to-8 decoder like the 74HC138 and connecting the PWM out to one of the enable lines. This would require only four pins and greatly reduce software overhead. – Caleb Reister Sep 28 '19 at 21:32
• PWM is solved , I just need how can I write analogWrite() like thing in avr as in arduino – Maifee Ul Asad Sep 30 '19 at 17:19

Modulate the LED "on" time.

Something like this.

for(int i=0; ; i++,i%=8)
{
for(int k=0; k<26; k++)
{
PORTC |=(1<<i);
delay_ms(10);
PORTC &= ~(1 << i);
delay_ms(10);
}
delay_ms(dela);
}

• I couldn't find any usage of k, What i want is like instead of output true of false, setting or resetting,0 volt or 5 volt. I want to write an analog output. – Maifee Ul Asad Sep 28 '19 at 16:39
• what does this mean? ... I couldn't find any usage of k – jsotola Sep 28 '19 at 19:01
• You are using k in the loop and iterating 25 times. And then using the old code, no usage of k. So will it actually dim the light? I haven't tested yet. – Maifee Ul Asad Sep 29 '19 at 11:41
• Now I have got it after testing it.. Can you edit the answer and help me with the analogWrite() type thing – Maifee Ul Asad Sep 30 '19 at 17:17
• that is a different question ... post another question – jsotola Sep 30 '19 at 23:07