1

I'm working on a simple LED project using Atmega2560 microcontroller. I don't know how I can adjust the led brightness to the level I want.

I need to do all the processing with digital signals. In this case, PWM and Analog signals not allowed.

DEF consts:

LEDS : Which LEDs will be work

LED_DATA : I will use it to out DDRC

Theory

To get 50% brightness:

  1. Turn on the LED (logic-1)
  2. Delay 5ms
  3. Turn off the LED (logic-0)
  4. Delay 5ms

Main Loop

.def LEDS = R16

.def LED_DATA = R21

.org 0
    rjmp MAIN

MAIN:
    ldi LEDS, 0xFF          ; 0xFF = 1111 1111 

    out DDRC, LEDS          ; make PORTC's all pins to output
    sbi PORTB, 0
    sbi PORTB, 1
    sbi PORTB, 2

LOOP_MAIN:
    out PORTC, LED_DATA
    call DELAY
    out PORTC, 0x00
    call DELAY
    rjmp LOOP_MAIN

My wait700ms command:

wait700ms:
   push r17

   ldi r16,0x40     ; run 0x400000 times
   ldi r17,0x00     ; ~12 million cycle
   ldi r18,0x00     ; for 16Mhz: ~0.7s delay
_w0:
   dec r18          
   brne _w0         
   dec r17          
   brne _w0         
   dec r16          
   brne _w0         

   pop r17          

   ret

As you can see above, my delay command is insufficient even when i set the ldi r16,0x01 instead of ldi r16,0x40. With 0x01, It happens so fast, but it's not enough. I can see it with my eye and it flashes in full brightness. It's a situation we don't want. On average, it should blink between 90 Hz and 120 Hz to adjust brightness.

P.S: Creating duty-cycle with analog pulse not allowed.

Questions

  1. How can I generate a 5ms delay?

  2. Can we solve this problem by using (ISR) timer interrupts? *(If possible, how can i generate 5ms delay with timer? (TCCR, OCR0 (for presaceler), ...))*

Dentrax
  • 574
  • 8
  • 22
  • Using a timer is not that much different from a PWM signal. Consider putting your LED in series with an array of resistors in parallel. They may or may not be all the same value, but if you connect each to a different digital pin, then by writing a high to the that/those pin(s) would in essence ground it and you'd have 256 combinations of resistance, which would, in turn, change the intensity of the LED. – Shift_Left Dec 19 '18 at 22:28
  • Are you saying you can see the flashing with your eye? I believe you, and I think you should make it flash a lot faster. Instead of using a 100 Hz blink rate (5 ms on, 5 ms off), how about trying a few kilohertz? Just make your delay a lot shorter. Or are you actually saying that you don't know how to make a 5 ms delay? What delay are you currently getting with the code that you wrote? Did you measure it with an oscilloscope? Please post the source code of `DELAY`; the source code of `wait700ms` is irrelevant because you don't call it and you don't want to delay for that long. [mcve] – David Grayson Dec 21 '18 at 19:03
  • Yes, I actually saying: I don't know how to make a 5 ms delay. For `call DELAY` It actually represents `delay700ms` It's my mistake. :) – Dentrax Dec 21 '18 at 20:28
  • I'm not interested an answering #2 since it is a very different question from #1 and there is basically no overlap in the answers. Is it OK if I just answer #1? If you have two radically different questions like this, you really should post them separately, and show the efforts you have put forward to solve the problem yourself (i.e. the code you used to attempt to do a delay using a timer). – David Grayson Dec 22 '18 at 01:21
  • Of course it's okay to asnwer Q1. The reason I wrote the Q2 is that if Q1 cannot be solve, it should be an alternative. Actually, that's my second mistake. I'm asking one question. (Q1) :) – Dentrax Dec 22 '18 at 09:12

0 Answers0