RedEagle
03.01.2007, 13:53
Ich möchte mit einem ATmega32 (bei 16MHz) eine LED blinken lassen.
Dazu habe ich den code von Roboternetz genommen und leicht verändert.
Allerdings blinken die LEDs nicht im Sekundentakt:
#include <avr/io.h>
#include <avr/interrupt.h>
#ifndef F_CPU
#define F_CPU (1000000*16) //Die *16 wurden von mir hinzugefügt
#endif
#define IRQS_PER_SECOND 2000 /* 500 µs */
#define IRQS_PER_10MS (IRQS_PER_SECOND / 100)
///////////////////////////////////////////////////////////////////////////////////////////////
static volatile uint8_t timer_10ms;
void timer1_init();
void wait_10ms (const uint8_t);
////////////////////////////////////////////////////////////////////////////////////////////////
void main()
{
uint16_t i,j,k,l; //zähler
DDRC = 0xFF;//An C liegen die LEDs
PORTC = 0x02;//LEDs aus
DDRA = 0xFF;
PORTA= 0x00;
DDRB = 0xFF;
PORTB= 0x00;
DDRD = 0xFF;
PORTD= 0x00;
timer1_init();
sei();
while (1)
{
// LED an
PORTC |= 0xF0;
// 1 Sekunde warten
for(l=0; l<0xFFFF; l++){
for(k=0; k<0xFFFF; k++){
for(j=0; j<0xFFFF; j++){
for(i=0; i<0xFFFF; i++){ wait_10ms (255);
}}}}
// LED aus
PORTC &= 0x0F;
// 1 Sekunde warten
for(l=0; l<0xFFFF; l++){
for(k=0; k<0xFFFF; k++){
for(j=0; j<0xFFFF; j++){
for(i=0; i<0xFFFF; i++){ wait_10ms (255);
}}}}
}
return;
}
//-----------------------------------------------------------------------------------------------
void timer1_init()
{
TCCR1A = 0;
#if defined (CTC1) && !defined (WGM12)
TCCR1B = (1 << CTC1) | (1 << CS10);
#elif !defined (CTC1) && defined (WGM12)
TCCR1B = (1 << WGM12) | (1 << CS10);
#endif
OCR1A = (unsigned short) ((unsigned long) F_CPU / IRQS_PER_SECOND-1);
#if defined (TIMSK1)
TIMSK1 |= (1 << OCIE1A);
#elif defined (TIMSK)
TIMSK |= (1 << OCIE1A);
#endif
}
//-----------------------------------------------------------------------------------------------
void wait_10ms (const uint8_t t)
{
timer_10ms = t;
while (timer_10ms);
}
/////////////////////////////////////////////////////////////////////////
SIGNAL (SIG_OUTPUT_COMPARE1A)
{
static uint8_t interrupt_num_10ms;
if (++interrupt_num_10ms == IRQS_PER_10MS)
{
interrupt_num_10ms = 0;
if (timer_10ms != 0)timer_10ms--;
}
return;
}
wo istz das Problem??
Dazu habe ich den code von Roboternetz genommen und leicht verändert.
Allerdings blinken die LEDs nicht im Sekundentakt:
#include <avr/io.h>
#include <avr/interrupt.h>
#ifndef F_CPU
#define F_CPU (1000000*16) //Die *16 wurden von mir hinzugefügt
#endif
#define IRQS_PER_SECOND 2000 /* 500 µs */
#define IRQS_PER_10MS (IRQS_PER_SECOND / 100)
///////////////////////////////////////////////////////////////////////////////////////////////
static volatile uint8_t timer_10ms;
void timer1_init();
void wait_10ms (const uint8_t);
////////////////////////////////////////////////////////////////////////////////////////////////
void main()
{
uint16_t i,j,k,l; //zähler
DDRC = 0xFF;//An C liegen die LEDs
PORTC = 0x02;//LEDs aus
DDRA = 0xFF;
PORTA= 0x00;
DDRB = 0xFF;
PORTB= 0x00;
DDRD = 0xFF;
PORTD= 0x00;
timer1_init();
sei();
while (1)
{
// LED an
PORTC |= 0xF0;
// 1 Sekunde warten
for(l=0; l<0xFFFF; l++){
for(k=0; k<0xFFFF; k++){
for(j=0; j<0xFFFF; j++){
for(i=0; i<0xFFFF; i++){ wait_10ms (255);
}}}}
// LED aus
PORTC &= 0x0F;
// 1 Sekunde warten
for(l=0; l<0xFFFF; l++){
for(k=0; k<0xFFFF; k++){
for(j=0; j<0xFFFF; j++){
for(i=0; i<0xFFFF; i++){ wait_10ms (255);
}}}}
}
return;
}
//-----------------------------------------------------------------------------------------------
void timer1_init()
{
TCCR1A = 0;
#if defined (CTC1) && !defined (WGM12)
TCCR1B = (1 << CTC1) | (1 << CS10);
#elif !defined (CTC1) && defined (WGM12)
TCCR1B = (1 << WGM12) | (1 << CS10);
#endif
OCR1A = (unsigned short) ((unsigned long) F_CPU / IRQS_PER_SECOND-1);
#if defined (TIMSK1)
TIMSK1 |= (1 << OCIE1A);
#elif defined (TIMSK)
TIMSK |= (1 << OCIE1A);
#endif
}
//-----------------------------------------------------------------------------------------------
void wait_10ms (const uint8_t t)
{
timer_10ms = t;
while (timer_10ms);
}
/////////////////////////////////////////////////////////////////////////
SIGNAL (SIG_OUTPUT_COMPARE1A)
{
static uint8_t interrupt_num_10ms;
if (++interrupt_num_10ms == IRQS_PER_10MS)
{
interrupt_num_10ms = 0;
if (timer_10ms != 0)timer_10ms--;
}
return;
}
wo istz das Problem??