button pwm

This commit is contained in:
Christoph J. Scherr 2023-10-31 11:24:34 +01:00
parent 21b4a3e479
commit b85b77b88d
2 changed files with 57 additions and 1 deletions

57
btn-pwm/main.c Normal file
View File

@ -0,0 +1,57 @@
/*
* Use the timers pwm to dim a light on demand.
*/
#define F_CPU 16000000UL
#define __AVR_ATmega328P__
#include <avr/interrupt.h>
#include <avr/io.h>
#include <util/delay.h>
#define PWM_LOW 24
#define PWM_HIGH 192
int state = 0;
// code for INT0
ISR(INT0_vect) {
OCR0A = PWM_HIGH; // max value for timer
PORTB |= (1<<PORTB5);
}
// code for INT1
ISR(INT1_vect) {
OCR0A = PWM_LOW; // max value for timer
PORTB &= ~(1<<PORTB5);
}
int main(void) {
// read from D2 and D3, write to D6
DDRD |= (1 << PORTD6);
DDRB = 0xff; // used for internal led only
PORTD |= (1 << PORTD2) | (1 << PORTD3); // pull up D2 and D3
// enable external interruprs
EIMSK |= (1 << INT0);
EIMSK |= (1 << INT1);
// trigger when anything changes
EICRA |= (1 << ISC00); // for INT0
EICRA |= (1 << ISC10); // for INT1
// Timer 0
// Mode: Fast PWM
// Prescaler: /8
// max: dynamic depending on pushed button
TCCR0A |= (1 << WGM01) | (1 << WGM00); // Fast PWM Mode
OCR0A = PWM_HIGH; // max value for timer
TCCR0A |= (1 << COM0A1); // set non inverting mode, start initialized
TCCR0B |= (1 << CS01); // enable with prescaler 8
sei(); // magic interrupt macro
while (1) {
// do nothing
}
}

View File

@ -18,7 +18,6 @@ int main(void) {
DDRD = 0; // read from D DDRD = 0; // read from D
PORTD |= (1 << PORTD2) | (1 << PORTD3); // pull up D2 and D3 PORTD |= (1 << PORTD2) | (1 << PORTD3); // pull up D2 and D3
PORTB = 0; PORTB = 0;
// enable external interruprs // enable external interruprs