avr pwm 6 leds fade in/out

avr pwm 6 leds fade in/out
Çeşme:
1. https://www.youtube.com/watch?v=iP_f0KY7lx8
1.1. http://playground.arduino.cc/Code/PwmFrequency
1.2. http://forum.arduino.cc/index.php?topic=16612#msg121031

2. https://facelesstech.wordpress.com/2015/09/13/avr-led-candle-flicker-atmega328/

3.
https://www.youtube.com/watch?v=Xki0CD3uy0Y&list=PLJTlt64jBcCtQ99qqW-n-hraDkSJVMBCR&index=18
https://www.youtube.com/watch?v=UXjEHhLlHHQ&index=19&list=PLJTlt64jBcCtQ99qqW-n-hraDkSJVMBCR
  1. Arduino UNO
uint8_t i; void setup() { pinMode(3, OUTPUT); pinMode(5, OUTPUT); pinMode(6, OUTPUT); pinMode(9, OUTPUT); pinMode(10, OUTPUT); pinMode(11, OUTPUT); } void loop() { for (i = 0; i < 255; i++) { writeAnalog(); } for (i = 255; 0 < i; i--) { writeAnalog(); } } void writeAnalog() { analogWrite(3, i); analogWrite(5, i); analogWrite(6, i); analogWrite(9, i); analogWrite(10, i); analogWrite(11, i); delay(5); }
#define F_CPU 16000000UL #include <avr/io.h> #include <stdlib.h> #include <util/delay.h> #define DELAY_MS 5 uint8_t i; int main(void) { // --------------------------------------------------------------------------------------- // OCR0B or OCR0A starts TCCR0A = _BV(WGM01)|_BV(WGM00); // Waveform Generation Mode = Fast PWM OCRA BOTTOM TOP // OCR0B DDRD |= _BV(PD5); // OC0B = ARD 5 = MCU 11 TCCR0A |= _BV(COM0B1); // Compare Output Mode for OCR0B pin Clear OC0B on Compare Match. // OCR0A DDRD |= _BV(PD6); // OC0A = ARD 6 = MCU 12 TCCR0A |= _BV(COM0A1); // Compare Output Mode for OCR0A pin Clear OC0A on Compare Match when up-counting. Set OC0A on Compare Match when down-counting. TCCR0B = _BV(CS02); // clkI/O/256 (From prescaler) // OCR0B or OCR0A ends // --------------------------------------------------------------------------------------- // OCR1A or OCR1B starts TCCR1A = _BV(WGM10); // Waveform Generation Mode = Fast PWM, 8-bit 0x00FF BOTTOM TOP // OCR1A DDRB |= _BV(PB1); // OC1A = ARD 9 = MCU 15 TCCR1A |= _BV(COM1A1); // Clear OC1A/OC1B on Compare Match, set OC1A/OC1B at BOTTOM (non-inverting mode) // OCR1B DDRB |= _BV(PB2); // OC1B = ARD 10 = MCU 16 TCCR1A |= _BV(COM1B1); // Clear OC1A/OC1B on Compare Match, set OC1A/OC1B at BOTTOM (non-inverting mode) TCCR1B = _BV(WGM12)|_BV(CS12); // clkI/O/256 (From prescaler) // OCR1A or OCR1B ends // --------------------------------------------------------------------------------------- // OCR2A or OCR2B starts TCCR2A = _BV(WGM20); // Waveform Generation Mode -> Fast PWM 0xFF BOTTOM MAX // OCR2B DDRD |= _BV(PD3); // OC2B = ARD 3 = MCU 5 TCCR2A |= _BV(COM2B1); // Clear OC2B on Compare Match // OCR2A DDRB |= _BV(PB3); // OC2A = ARD 11 = MCU 17 TCCR2A |= _BV(COM2A1); // Clear OC2B on Compare Match. TCCR2B = _BV(CS22)|_BV(CS21); // clkI/O/256 (From prescaler) // OCR2A or OCR2B ends // --------------------------------------------------------------------------------------- while (1) { for (i = 0; i < 255; i++) { OCR2B = i; // PD3 = OC2B = ARD 3 = MCU 5 OCR0A = i; // PD6 = OC0A = ARD 6 = MCU 12 OCR0B = i; // PD5 = OC0B = ARD 5 = MCU 11 OCR1A = i; // PB1 = OC1A = ARD 9 = MCU 15 OCR1B = i; // PB2 = OC1B = ARD 10 = MCU 16 OCR2A = i; // PB3 = OC2A = ARD 11 = MCU 17 _delay_ms(DELAY_MS); } for (i = 255; 0 < i; i--) { OCR2B = i; // PD3 = OC2B = ARD 3 = MCU 5 OCR0A = i; // PD6 = OC0A = ARD 6 = MCU 12 OCR0B = i; // PD5 = OC0B = ARD 5 = MCU 11 OCR1A = i; // PB1 = OC1A = ARD 9 = MCU 15 OCR1B = i; // PB2 = OC1B = ARD 10 = MCU 16 OCR2A = i; // PB3 = OC2A = ARD 11 = MCU 17 _delay_ms(DELAY_MS); } } }
.nolist .include "./m328Pdef.inc" .list .def temp = r16 jmp start start: ldi temp, low(RAMEND) out SPL, temp ldi temp, high(RAMEND) out SPH, temp ; --------------------------------------------------------------------------------------- ; OCR0B or OCR0A starts ldi temp, (1<<PD5)|(1<<PD6); out DDRD, temp ldi temp, (1<<WGM01)|(1<<WGM00)|(1<<COM0B1)|(1<<COM0A1) out TCCR0A, temp ldi temp, (1<<CS02) out TCCR0B, temp ; OCR0B or OCR0A ends ; --------------------------------------------------------------------------------------- ; OCR1A or OCR1B starts ldi temp, (1<<PB1)|(1<<PB2); out DDRB, temp ldi temp, (1<<WGM10)|(1<<COM1A1)|(1<<COM1B1) sts TCCR1A, temp ldi temp, (1<<WGM12)|(1<<CS12) sts TCCR1B, temp ; OCR1A or OCR1B ends ; --------------------------------------------------------------------------------------- ; OCR2A or OCR2B starts in temp, DDRD sbr temp, (1<<PD3) out DDRD, temp in temp, DDRB sbr temp, (1<<PB3) out DDRB, temp ldi temp, (1<<WGM20)|(1<<COM2B1)|(1<<COM2A1) sts TCCR2A, temp ldi temp, (1<<CS22)|(1<<CS21) sts TCCR2B, temp ; OCR2A or OCR2B ends loop: clr r0 ldi temp, 0 while0: out OCR0A, temp out OCR0B, temp sts OCR1AL, temp sts OCR1AH, r0 sts OCR1BL, temp sts OCR1BH, r0 sts OCR2A, temp sts OCR2B, temp rcall delay inc temp cpi temp, 0xff brne while0 ldi temp, 0xff while1: out OCR0A, temp out OCR0B, temp sts OCR1AL, temp sts OCR1AH, r0 sts OCR1BL, temp sts OCR1BH, r0 sts OCR2A, temp sts OCR2B, temp rcall delay dec temp brne while1 rjmp loop delay: push r16 ; Cycles: 1 ldi r16, 1 ; 31 lp1: ; Cycles: 1 ldi r30, 0x00 ; Cycles: 1 ldi r31, 0x5F lp2: ; Cycles: 2 sbiw r31:r30, 1 ; Cycles: 1 if condition is false ; 2 if condition is true brne lp2 ; Cycles: 1 dec r16 ; Cycles: 1 if condition is false ; 2 if condition is true brne lp1 pop r16 ; Cycles: 4 ret
#chip mega328p, 16 DDRB = b'00001110' DDRD = b'01101000' ' --------------------------------------------------------------------------------------- ' OCR0B or OCR0A starts 'TCCR0A = b'10100011' 'TCCR0B = b'00000100' Set WGM01 ON Set WGM00 ON Set PD5 ON Set COM0B1 ON Set PD6 ON Set COM0A1 ON Set CS02 ON ' --------------------------------------------------------------------------------------- ' OCR1A or OCR1B starts TCCR1A = b'10100001' TCCR1B = b'00001100' ' --------------------------------------------------------------------------------------- ' OCR2A or OCR2B starts TCCR2A = b'10100001' TCCR2B = b'00000110' ' --------------------------------------------------------------------------------------- start: For inc = 0 to 255 SetInc(inc) Next For inc = 255 to 0 SetInc(inc) Next goto start ' --------------------------------------------------------------------------------------- sub SetInc(inc) OCR0A = inc OCR0B = inc OCR1AH = inc_H ' PB1 = OC1A = ARD 9 = MCU 15 OCR1AL = inc OCR1BH = inc_H ' PB2 = OC1B = ARD 10 = MCU 16 OCR1BL = inc OCR2A = inc OCR2B = inc wait 5 ms end sub