Skip to content

Instantly share code, notes, and snippets.

@avr-programmierung
Created May 16, 2019 11:04
Show Gist options
  • Save avr-programmierung/243e56c948a29aa9103cf6051850d989 to your computer and use it in GitHub Desktop.
Save avr-programmierung/243e56c948a29aa9103cf6051850d989 to your computer and use it in GitHub Desktop.
ATmega88 @ 8MHz LED fading 02 with 10bit valuetable and phasecorrect pwm
/*
* LED_fading_02.c
* with 10bit valuetable and phasecorrect pwm
* Controller: ATmega88 @ 8MHz
*/
#include <avr/io.h>
#include <util/delay.h>
const unsigned int pwm_table_10[64] =
{
0, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8, 9, 10,
11, 12, 13, 15, 17, 19, 21, 23, 26, 29, 32, 36, 40, 44, 49, 55,
61, 68, 76, 85, 94, 105, 117, 131, 146, 162, 181, 202, 225, 250,
279, 311, 346, 386, 430, 479, 534, 595, 663, 739, 824, 918, 1023
};
int main(void)
{
DDRB |= (1<<PB1); // OC1A = output
ICR1 = 1023; // ICR1 = Top Value
// Mode 10: PWM, Phase Correct, Prescaler = 1, Clear OC1A on compare match
TCCR1A = (1 << COM1A1) + (1 << WGM11);
TCCR1B = (1 << WGM13) + (1 << CS10);
while(1)
{
for(int i=0; i<=63; i++) // PWM inkrementieren
{
OCR1A = pwm_table_10[i]; // Tabellenwert i dem OCR1A zuweisen
_delay_ms(50);
}
for(int i=63; i>= 1; i--) // PWM dekrementieren
{
OCR1A = pwm_table_10[i]; // Tabellenwert i dem OCR1A zuweisen
_delay_ms(50);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment