Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Develop a UNO sketch that reads two positive integers, frequency and duty cycle, from the PC keyboard and then, based on the fast PWM mode,

Develop a UNO sketch that reads two positive integers, frequency and duty cycle, from the PC keyboard and then, based on the fast PWM mode, generates on digital pin 5 a square wave with that frequency and duty cycle. When an input frequency cannot be achieved, use one that is as close as you can get.There is no need to consider (near-)zero or (near-)100% duty cycle. Use an oscilloscope to observe the waveforms. Identify the range of frequency values that you are able to generate. Note that pin 5 is connected to the timer 0 channel B output compare pin.

int freq; int cycle;

void setup() { Serial.begin(9600); Serial.flush(); double max_freq = 16000000.00 / (256.00 * 1024.00); Serial.println(max_freq); OCR0A = 100; OCR0B = 50; { while(!Serial.available()); freq = Serial.parseInt(); cycle = Serial.parseInt(); Serial.println("frequency = "); Serial.println(freq, DEC); Serial.println("duty cycle = "); Serial.println(cycle, DEC);

pinMode(5, OUTPUT); TCCR0A = TCCR0B = 0; TCCR0A = 0x23; //Timer/Counter Control Register A bit 5 long LHS = 16000000 / freq; int prescalars[5] = {1, 8, 64, 256, 1024}; for(int i = 0; i < 5; i++) { if(LHS / prescalars[i] < 256 && LHS / prescalars[i] > 0) switch(i) { case 0: TCCR0B = 0x09; //Timer/Counter Control Register B OCR0A = LHS / prescalars[i]; //Output Compare Register A break; case 1: TCCR0B = 0x0A; OCR0A = LHS / prescalars[i]; break; case 2: TCCR0B = 0x0B; OCR0A = LHS / prescalars[i]; break; case 3: TCCR0B = 0x0C; OCR0A = LHS / prescalars[i]; break; case 4: TCCR0B = 0x0D; OCR0A = LHS / prescalars[i]; break; } } double percent_duty = 100.00 / (double) cycle; double temporary = OCR0A; OCR0B = (int)(temporary / percent_duty); //Output Compare Register B = TOP * Percent duty cycle } }

Im having trouble produce the waveform. if someone could help me thanks

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Inference Control In Statistical Databases From Theory To Practice Lncs 2316

Authors: Josep Domingo-Ferrer

2002nd Edition

3540436146, 978-3540436140

More Books

Students also viewed these Databases questions

Question

What is a client?

Answered: 1 week ago