Snabb hjälp med AVR önskas!
Hej igen.. Nya problem med interrupt..
Jag har fått externa interrupt från pinne PD2 och PD3 att fungera, med dessa
så ändrar jag tiden på min klocka som ska visas på LCD (hh:mm:ss).
Sedan ska timer0 användas för att räkna upp min klocka med en sekund.. varje sekund (såklart).
Men jag vet inte riktigt hur jag använder timer0 riktigt..
När jag har timer0 overflow interrupt aktiverat så verkar uD befinna sig lite väl länge i " SIGNAL(SIG_OUTPUT_COMPARE0) "-vektorn.
Fast det funkar att komma in i mina andra två interrupt-vektorer under tiden.
jaja.. någon som kan hjälpa mig lite med detta? har försökt tyda databladet.
Men det är ju lika lättförstått som en arabisk bibel baklänges..
Här är min kod:
Jag har fått externa interrupt från pinne PD2 och PD3 att fungera, med dessa
så ändrar jag tiden på min klocka som ska visas på LCD (hh:mm:ss).
Sedan ska timer0 användas för att räkna upp min klocka med en sekund.. varje sekund (såklart).
Men jag vet inte riktigt hur jag använder timer0 riktigt..
När jag har timer0 overflow interrupt aktiverat så verkar uD befinna sig lite väl länge i " SIGNAL(SIG_OUTPUT_COMPARE0) "-vektorn.
Fast det funkar att komma in i mina andra två interrupt-vektorer under tiden.
jaja.. någon som kan hjälpa mig lite med detta? har försökt tyda databladet.
Men det är ju lika lättförstått som en arabisk bibel baklänges..
Här är min kod:
Kod: Markera allt
#include <io_atmega16.h>
#include <stdio.h>
#include <avr/delay.h>
#include <delay_loop.h>
#include <lcd4.h>
#include <interrupt.h>
#include <signal.h>
volatile int hour=0, min=0, sek=31;
// --- Globala objekt ---
lcd4 theDisplay;
delay_loop theDelay;
//--- Main ---
int main(void)
{
char s[20];
//--- Intitiering av ports ---
DDRA = 0xFF; // Alla utgångar
DDRB = 0xFF; // Alla utgångar
DDRD = 0xF3; // PD2,PD3 ingångar, resten utgångar
MCUCR = 0x0F; // Positiv flank på INT0 genererar interrupt
GICR = 0xC0; // INT0 enable
TCCR0 = 0x05; // Prescaler (clk/1024)
TIMSK = 0x01; // Timer0 overflow interrupt enable
OCR0 = 255;
//PORTB = 0x0F;
SREG = 0x80; // Global aktiveing av interrupt
//--- Initiering av objects ---
lcd4_init(&theDisplay,&PORTC, &DDRC, 4000,50);
delay_loop_init(&theDelay, 5000, DELAY_LOOP_8MHZ );
while(1)
{
sprintf(s, " %02d:%02d:%02d", hour, min, sek);
lcd4_cup_row1(&theDisplay);
lcd4_write_string(&theDisplay, s);
delay_loop_delay(&theDelay);
}
return 0;
}
SIGNAL(SIG_OUTPUT_COMPARE0)
{
sek++;
if(sek == 60)
{
sek = 00;
}
TCNT0 =0;
}
SIGNAL(SIG_INTERRUPT1)
{
sek =0;
PORTB=0x0F;
hour++;
if(hour==25)
{
hour =0;
}
}
SIGNAL(SIG_INTERRUPT0)
{
sek =0;
PORTB = 0xF0;
min++;
if(min==60)
{
min =0;
}
}
Jag måste säga att jag inte riktigt greppar programidén, men en grej jag reagerar på är att du sätter TOIE0 i TIMSK, men du har ingen interrupt-hanterare för overflow, däremot för SIG_OUTPUT_COMPARE0. Den är dock inte satt i TIMSK (0x02), så jag undrar lite om sek någonsin ändras, jag tvivlar på att interrupthanteraren körs ordentligt, såvida du inte har fel i någon #include eller nåt i den stilen.
Edit, TIMSK, inte TIMSK0 i mega16, fy på mig.
Edit2, jag skrev inte ett rätt idag.
Edit, TIMSK, inte TIMSK0 i mega16, fy på mig.
Edit2, jag skrev inte ett rätt idag.
-
thepirateboy
- EF Sponsor
- Inlägg: 2109
- Blev medlem: 27 augusti 2005, 20:57:58
- Ort: Borlänge
Är det overflow du vill köra bör du ändra SIGNAL(SIG_OUTPUT_COMPARE0) till SIGNAL(SIG_OVERFLOW0) eller ännu bättre ISR(TIMER0_OVF_vect)
http://www.sax.de/~joerg/avr-libc-user- ... rupts.html
http://www.sax.de/~joerg/avr-libc-user- ... rupts.html
Upptäckte att jag tydligen valt fel interrupt i TIMSK.
Ahaa.. tack! Då vet jag hur man även kommer åt overflow interrupt..
Den sidan var mycket smidig, det är ju omöjligt att veta egentligen vad
alla typer av interupter och sånt heter, läser man i de include filer man har
att tillgå, är det inget man direkt kan läsa ut från dom.
Stort tack till alla som hjälpt mig.
Ahaa.. tack! Då vet jag hur man även kommer åt overflow interrupt..
Den sidan var mycket smidig, det är ju omöjligt att veta egentligen vad
alla typer av interupter och sånt heter, läser man i de include filer man har
att tillgå, är det inget man direkt kan läsa ut från dom.
Stort tack till alla som hjälpt mig.
Peka gärna på vart du ser det i dessa filer..
signal.h
------------
Interrupt.h
-----------
signal.h
------------
Kod: Markera allt
/* Copyright (c) 2002, Marek Michalkiewicz
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or oth :er materials provided with the
distribution.
* Neither the name of the copyright holders nor the names of
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE. */
/* $Id: signal.h,v 1.8 2004/11/01 21:19:54 arcanum Exp $ */
#ifndef _AVR_SIGNAL_H_
#define _AVR_SIGNAL_H_
/** \name Macros for writing interrupt handler functions */
/*@{*/
/** \def SIGNAL(signame)
\ingroup avr_interrupts
\code#include <avr/signal.h>\endcode
Introduces an interrupt handler function that runs with global interrupts
initially disabled. */
#ifdef __cplusplus
#define SIGNAL(signame) \
extern "C" void signame(void); \
void signame (void) __attribute__ ((signal)); \
void signame (void)
#else
#define SIGNAL(signame) \
void signame (void) __attribute__ ((signal)); \
void signame (void)
#endif
/** \def INTERRUPT(signame)
\ingroup avr_interrupts
\code#include <avr/signal.h>\endcode
Introduces an interrupt handler function that runs with global interrupts
initially enabled. This allows interrupt handlers to be interrupted. */
#ifdef __cplusplus
#define INTERRUPT(signame) \
extern "C" void signame(void); \
void signame (void) __attribute__ ((interrupt)); \
void signame (void)
#else
#define INTERRUPT(signame) \
void signame (void) __attribute__ ((interrupt)); \
void signame (void)
#endif
/** \def EMPTY_INTERRUPT(signame)
\ingroup avr_interrupts
\code#include <avr/signal.h>\endcode
Defines an empty interrupt handler function. This will not generate
any prolog or epilog code and will only return from the ISR. Do not
define a function body as this will define it for you.
Example:
\code EMPTY_INTERRUPT(SIG_ADC);\endcode */
#ifdef __cplusplus
#define EMPTY_INTERRUPT(signame) \
extern "C" void signame(void); \
void signame (void) __attribute__ ((naked)); \
void signame (void) { __asm__ __volatile__ ("reti" ::); }
#else
#define EMPTY_INTERRUPT(signame) \
void signame (void) __attribute__ ((naked)); \
void signame (void) { __asm__ __volatile__ ("reti" ::); }
#endif
/*@}*/
#endif /* _AVR_SIGNAL_H_ */
Interrupt.h
-----------
Kod: Markera allt
/* Copyright (c) 2002, Marek Michalkiewicz
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of the copyright holders nor the names of
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE. */
/* $Id: interrupt.h,v 1.12 2004/11/01 21:19:54 arcanum Exp $ */
#ifndef _AVR_INTERRUPT_H_
#define _AVR_INTERRUPT_H_
#include <avr/io.h>
/** \name Global manipulation of the interrupt flag
The global interrupt flag is maintained in the I bit of the status
register (SREG). */
/*@{*/
/** \def sei()
\ingroup avr_interrupts
\code#include <avr/interrupt.h>\endcode
Enables interrupts by clearing the global interrupt mask. This function
actually compiles into a single line of assembly, so there is no function
call overhead. */
#define sei() __asm__ __volatile__ ("sei" ::)
/** \def cli()
\ingroup avr_interrupts
\code#include <avr/interrupt.h>\endcode
Disables all interrupts by clearing the global interrupt mask. This function
actually compiles into a single line of assembly, so there is no function
call overhead. */
#define cli() __asm__ __volatile__ ("cli" ::)
/*@}*/
#ifdef __cplusplus
extern "C" {
#endif
/** \name Allowing specific system-wide interrupts
In addition to globally enabling interrupts, each device's particular
interrupt needs to be enabled separately if interrupts for this device are
desired. While some devices maintain their interrupt enable bit inside
the device's register set, external and timer interrupts have system-wide
configuration registers.
Example:
\code
// Enable timer 1 overflow interrupts.
timer_enable_int(_BV(TOIE1));
// Do some work...
// Disable all timer interrupts.
timer_enable_int(0);
\endcode
\note Be careful when you use these functions. If you already have a
different interrupt enabled, you could inadvertantly disable it by
enabling another intterupt. */
/*@{*/
/** \ingroup avr_interrupts
\def enable_external_int(mask)
\code#include <avr/interrupt.h>\endcode
This macro gives access to the \c GIMSK register (or \c EIMSK register
if using an AVR Mega device or \c GICR register for others). Although this
macro is essentially the same as assigning to the register, it does
adapt slightly to the type of device being used. This macro is
unavailable if none of the registers listed above are defined. */
/* Define common register definition if available. */
#if defined(EIMSK)
#define __EICR EIMSK
#endif
#if defined(GIMSK)
#define __EICR GIMSK
#endif
#if defined(GICR)
#define __EICR GICR
#endif
/* If common register defined, define macro. */
#if defined(__EICR) || defined(DOXYGEN)
#define enable_external_int(mask) (__EICR = mask)
#endif
/** \ingroup avr_interrupts
\code#include <avr/interrupt.h>\endcode
This function modifies the \c timsk register.
The value you pass via \c ints is device specific. */
static __inline__ void timer_enable_int (unsigned char ints)
{
#ifdef TIMSK
TIMSK = ints;
#endif
}
/*@}*/
#ifdef __cplusplus
}
#endif
#endif
Du tittar i fel fil. den filen du kollar i är för Alla avr, du måste kolla i den filen som är spesefik för den AVR du använder. Titta i exempel vis i iom16.h för Atmega16. ^^
Sedan vill jag slå ett slag för AVRstudio4 (med winavr), lätt att konfugurera (inget pillande i makefile). Vissar alla använda filer i ett "träd"... en fröjd att använda ^^
Sedan vill jag slå ett slag för AVRstudio4 (med winavr), lätt att konfugurera (inget pillande i makefile). Vissar alla använda filer i ett "träd"... en fröjd att använda ^^
