Task: To generate pulses, use the configured timer Timer 0, register for comparison A. Also, the pulse frequency is 1290 Hz, the duty-cycle is 81%, the PWM generation method is Normal PWM, the number of pulses is 5500.
How to replace the delayMicroseconds function with something else? Is it possible to somehow set the duty-cycle is 81%?
Code:
const uint8_t buttonPin = 11;
const int pulsePin = 6;
volatile bool buttonPressed = false;
volatile int pulseCount = 0;
void buttonISR() {
buttonPressed = true;
}
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(pulsePin, OUTPUT);
Serial.begin(9600);
TCCR0A = 0;
TCCR0B = 0;
TCNT0 = 0;
OCR0A = 194 - 1;
TCCR0A |= (1 << WGM01);
TCCR0B |= (1 << CS01) | (1 << CS00);
TIMSK0 |= (1 << OCIE0A);
}
void loop() {
bool pinHigh = (digitalRead(buttonPin) == HIGH);
if (pinHigh && buttonPressed) {
while (pulseCount < 5500) {
digitalWrite(pulsePin, HIGH);
delayMicroseconds(105);
digitalWrite(pulsePin, LOW);
delayMicroseconds(25);
pulseCount++;
}
pulseCount = 0;
buttonPressed = false;
} else if (!pinHigh && !buttonPressed) {
buttonPressed = true;
}
}
ISR(TIMER0_COMPA_vect) {
if (buttonPressed && pulseCount < 5500) {
pulseCount++;
}
}
Do you mean "duty-cycle"?
6v6gt
April 23, 2023, 10:26am
6
I guess "Task" here means school assignment. It is not cleat from your code if you are trying to generate the wave form in the loop or using interrupts and has a sort of ChatGPT look to it..
Look at this example for specifying the frequency and duty cycle of a Uno class board.
It uses timer1 and Fast PWM though. https://www.gammon.com.au/forum/?id=11504&reply=6#reply6
how to generate pulses with time interrupt?
6v6gt
April 23, 2023, 10:42am
8
Are you free to chose which arduino pin the wave form appears on ?
I can choose 6, 9 or 11 pin
Is my code generating pulses incorrectly?
philipsusers:
6, 9 or 11 pin
I would use 9 (OC1A) or 11 (OC2A). Timer0 is used for the Arduino core so changing it will cause millis() and micros() to fail.
1290 Hz is 12403.1 clock cycles so the closest you can get is 1290.0105Hz.
81% of 12403 is 10046.43 so the closest you can get is 80.9965%
TCCR1A = 0;
TCCR1B = 0;
TCNT1= 0;
// Use WGM 14: FastPWM with TOP in ICR1
TCCR1A |= _BV(WGM11);
TCCR1B |= _BV(WGM13) | _BV(WGM13);
ICR1 = 12402; // Frequency = 1290 Hz (16 MHz / (TOP + 1))
// Set 81% duty cycle
OCR1A = 10046;
// Enable PWM on OC1A
TCCR1A |= _BV(COM1A1);
// Enable the interrupt to count pulses
TIMSK1 = OCIE1A;
// Start the timer at 16 MHz
TCCR1B |= _BV(CS10);
If I need to generate pulses when I press a button
Start and stop the timer upon button press by modifying the appropriate timer control register, clock select bits.
can you give a code example
Did you not see post #11 ?
// Start the timer at 16 MHz
TCCR1B |= _BV(CS10);
with the push of a button?
That line will work anywhere you put it, including within code that detects a button push and takes some action.
and how to count the number of pulses
Very similar to your existing code except for TIMER1_COMPA_vect instead of TIMER0_COMPA_vect.
ISR(TIMER1_COMPA_vect) {
if (pulseCount < 5500)
pulseCount++;
else
// Stop the timer
TCCR1B &= ~(_BV(CS12 | _BV(CS11) | _BV(CS10)); // Clear clock select field
}
Can this be done with timer0?