/*
* Filename: Timers_Exercise3
* Author: George Howard
* Date: 12/05/2024
* Description: PURPOSE
*/
// Task: Use the FastPWM Mode to produce a square wave with a duty cycle
// which is adjustable by using switches on the red shield.
// You can change only one switch to high and the rest to zero.
// For each case mention the duty cycle value and your calculations.
// Draw a timing diagram to explain how it works
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
// Workings:
void dutyCycle() {
switch (PORTB) {
case 0x01:
OCR1A = 32;
break;
case 0x02:
OCR1A = 64;
break;
case 0x04:
OCR1A = 96;
break;
case 0x08:
OCR1A = 128;
break;
case 0x10:
OCR1A = 160;
break;
case 0x20:
OCR1A = 192;
break;
case 0x40:
OCR1A = 224;
break;
case 0x80:
OCR1A = 256;
break;
default:
OCR1A = 0;
break;
}
}
int main(void)
{
Serial.begin(9600);
cli(); // Disable interupts
DDRA = 0x80; // Sets PIN7 as output
PORTA = 0x00; // Resistors low
DDRB = 0x00; // All PORTB Pins to input
PORTB = 0xFF; // Enable Pull up resistors
TCCR1A = 0x83;
TCCR1B = 0x04; // Sets Mode 3 (PWM) and 256 prescaler
OCR1A = 0;
sei(); // Enable interupts
while (1)
{
dutyCycle();
}
}