Question
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
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