Please help with this arduino code. It is intended to generate a new value for t
ID: 3816761 • Letter: P
Question
Please help with this arduino code.
It is intended to generate a new value for the sine wave every 10 milliseconds and send it out as a PWM signal on pin 10. Note we will be using the Timer1 library to generate the PWM output and MsTimer2 to run an ISR ever 10 milliseconds. To generate next time and next output value x use Time += 10e-3; // Advance time by sample interval if( Time > Period ) // if time goes past Period = 1/frequency Time -= Period; // Wrap by Period, not allowing Time to grow to large. // Using time and frequency generate next sine value // and shifting it to go from approximately 1 to 1023 x = 511*sin( 6.2831853*Frequency*Time) + 512;
#include <math.h>
#include <TimerOne.h>
#include <MsTimer2.h>
Global variables of Time, Frequency and Period
ISR():
{
Turn on the LED (pin 13)
Generate next Time, and then next output value x
Send x out as a PWM signal on pin 10.
Turn off the LED
}
Setup():
{
Set up Timer2 to run an ISR every 10 milliseconds
Set Timer1 to have a 500 microseconds period and a PWM on pin 10.
}
Loop: Nothing is here.
Explanation / Answer
#include <avr/io.h>
#include <avr/interrupt.h>
uint8_t count = 0; // global counter
// initialize timer, interrupt and variable
void timerX_init()
{
// set up timerX with suitable prescaler and CTC mode
// initialize counter
// initialize compare value
// enable compare interrupt
// enable global interrupts
}
// process the ISR that is fired
ISR (TIMERx_COMPA_vect)
{
// do whatever you want to do here
// say, increment the global counter
count++;
// check for the global counter
// if count == odd, delay required = 11 ms
// if count == even, delay required = 9 ms
// thus, the value of the OCRx should be constantly updated
if (count % 2 == 0)
OCRx = 9999; // calculate and substitute appropriate value
else
OCRx = 10999; // calculate and substitute appropriate value
}
int main(void)
{
// initialize the output pin, say PC0
DDRC |= (1 << 0);
// initialize timerX
timerX_init();
// loop forever
while(1)
{
// do nothing
}
}
#include <avr/io.h>
#include <util/delay.h>
void pwm_init()
{
// initialize TCCR0 as per requirement, say as follows
TCCR0 |= (1<<WGM00)|(1<<COM01)|(1<<WGM01)|(1<<CS00);
// make sure to make OC0 pin (pin PB3 for atmega32) as output pin
DDRB |= (1<<PB3);
}
void main()
{
uint8_t duty;
duty = 115; // duty cycle = 45% of 255 = 114.75 = 115
// initialize timer in PWM mode
pwm_init();
// run forever
while(1)
{
OCR0 = duty;
}
}
// program to change brightness of an LED
// demonstration of PWM
#include <avr/io.h>
#include <util/delay.h>
// initialize PWM
void pwm_init()
{
// initialize timer0 in PWM mode
TCCR0 |= (1<<WGM00)|(1<<COM01)|(1<<WGM01)|(1<<CS00);
// make sure to make OC0 pin (pin PB3 for atmega32) as output pin
DDRB |= (1<<PB3);
}
void main()
{
uint8_t brightness;
// initialize timer0 in PWM mode
pwm_init();
// run forever
while(1)
{
// increasing brightness
for (brightness = 0; brightness < 255; ++brightness)
{
// set the brightness as duty cycle
OCR0 = brightness;
// delay so as to make the user "see" the change in brightness
_delay_ms(10);
}
// decreasing brightness
for (brightness = 255; brightness > 0; --brightness)
{
// set the brightness as duty cycle
OCR0 = brightness;
// delay so as to make the user "see" the change in brightness
_delay_ms(10);
}
// repeat this forever
}
}