0
\$\begingroup\$

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

\$\endgroup\$
4
  • 1
    \$\begingroup\$ 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 \$\endgroup\$
    – jsotola
    Commented Sep 28, 2019 at 21:18
  • \$\begingroup\$ Have you considered using larger resistors? You only need one resistor since there will only be one running at a time. \$\endgroup\$ Commented Sep 28, 2019 at 21:20
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Commented Sep 28, 2019 at 21:32
  • \$\begingroup\$ PWM is solved , I just need how can I write analogWrite() like thing in avr as in arduino \$\endgroup\$ Commented Sep 30, 2019 at 17:19

1 Answer 1

1
\$\begingroup\$

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);
}
\$\endgroup\$
6
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Sep 28, 2019 at 16:39
  • \$\begingroup\$ what does this mean? ... I couldn't find any usage of k \$\endgroup\$
    – jsotola
    Commented Sep 28, 2019 at 19:01
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Sep 29, 2019 at 11:41
  • \$\begingroup\$ Now I have got it after testing it.. Can you edit the answer and help me with the analogWrite() type thing \$\endgroup\$ Commented Sep 30, 2019 at 17:17
  • \$\begingroup\$ that is a different question ... post another question \$\endgroup\$
    – jsotola
    Commented Sep 30, 2019 at 23:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.