Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Question: PWM or CTC modes can be used to create a 50% duty cycle wave. In the below, I used CTC mode to create square
Question:
PWM or CTC modes can be used to create a 50% duty cycle wave. In the below, I used CTC mode to create square waves with frequencies of musical notes. Now I am going to revisit that design challenge using PWM. Think about how the two modes differ in the waveforms they create. In CTC mode I only had to change the compare register to change the period. In PWM mode you have to change 1 register to change the period and another register to change the pulsewidth to maintain 50% duty cycle. Rewrite the below code using PWM mode.
REFRENCES
Below is the code done before:
#define LA 36364
#define LAS 34335
#define LB 32389
#define LC 30534
#define LCS 28881
#define LD 27211
#define LDS 25723
#define LE 24242
#define LF 22923
#define LFS 21622
#define LG 20408
#define LGS 19277
#define MA 18182
#define MAS 17167
#define MB 16194
#define MC 15296
#define MCS 14440
#define MD 13629
#define MDS 12862
#define ME 12140
#define MF 11461
#define MFS 10811
#define MG 10204
#define MGS 9627
#define HA 9091
#define WHOLE 1000
#define DHALF 750
#define HALF 500
#define QUARTER 250
void setup() {
DDRD = 0x01;
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
TCCR1A |= (1
TCCR1B |= (1
}
void loop() {
playNote(LDS, WHOLE);
}
void playNote(int note, int timeNote) {
TCCR1B |= (1 1. Given 25 notes of the musical scale below: LA LAS LB LC LCS LD LDS LE LF LFS LG LGS MA MAS MB MC MCS MD MDS ME MF MFS MG MGS HA They are each produced by a square wave with frequency as defined by the following formula P/12 note 440* 2 re P ranges from -12 (low A - LA) to 12 (high A - HA) The period of the square wave is illustrated in figure 1 below inote -- note 2 2 fnote note Figure 1: Equations for Period and h Period of a note square wave 16MHz Clock cycle in /2 Tnote- 2note 16MHz-2note = (OCRIA + 1) For the 25 notes (fnote- 220 through fnote -880) determine the OCR1A values needed to output the square wave of each note using Timer1 in CTC mode. Hint: write a program to do the math for vou and print the results to the serial monitor
OCR1A = note;
delay(timeNote);
TCCR1B |= (0
delay(50);
The image depicts what the code is all about:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started