Lab 08
Lab 08
Rasham
Saqib B21F0476EE019
sbi DDRB, 5 ;
ldi R18, 1<<5 ; R16 = 0b_0010_0000 (Pin 5 of PORTB)
ldi R17, 0
out PORTB, R17
BEGIN:
rcall DELAY ;3
eor R17, R18 ;1
out PORTB, R17 ;1
rjmp BEGIN ;2
DELAY:
ldi R16, 0b_0000_0001 ; 1; FOC0A, FOC0B, -, -, WGM02, CS02, CS01, CS00 = TCCR0B
out TCCR0B, R16 ; 1; counter starts running after execution of this
instruction
AGAIN:
in R20, TIFR0 ;1
sbrs R20, TOV0 ;1/2
rjmp AGAIN ;2
ldi R16, 0b_0000_0001 ; we are unmasking the interrupt for Timer 0 overflow
Lab Task 3: Timer 0 CTC Mode.
What is the maximum frequency you can generate on OC0A pin using the Timer 0 in
CTC mode on ATMEGA328P using the Arduino board available in the lab? Refer to
datasheet for finding out frequency in CTC mode. Show your calculation and
implementation to the lab instructor. A sample code is given below:
ldi R16, 0b_0000_0001 ; FOC0A, FOC0B, -, -, WGM02, CS02, CS01, CS00 = TCCR0B
out TCCR0B, R16
;FAST PWM
.org 0x0000
rjmp start
.org 0x0100
start:
ldi R16, 0b_0000_0001 ; FOC0A, FOC0B, -, -, WGM02, CS02, CS01, CS00 = TCCR0B
out TCCR0B, R16
.org 0x0100
start:
ldi R16, 0b_0000_0001 ; FOC0A, FOC0B, -, -, WGM02, CS02, CS01, CS00 = TCCR0B
out TCCR0B, R16
; ;EXTERNAL PULSE
.org 0x0000
rjmp start
.org 0x0100
start:
ldi R16, 0b_0000_0111 ; FOC0A, FOC0B, -, -, WGM02, CS02, CS01, CS00 = TCCR0B
out TCCR0B, R16
here:
in R17, TCNT0
out PORTB, R17
rjmp here
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as an output
TCCR1A = 0b10100001; // Configure Timer1 for Fast PWM mode
TCCR1B = 0b00001001; // Set prescaler to 1
}
void loop() {
// Increase brightness from minimum to maximum
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(ledPin, brightness); // Set the LED brightness
delay(10); // Delay for smoother transition
}