[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [avr-gcc-list] problems with timer0 on atmega128
From: |
Volkmar Dierkes |
Subject: |
Re: [avr-gcc-list] problems with timer0 on atmega128 |
Date: |
Thu, 30 Jan 2003 08:19:40 +0100 |
User-agent: |
40tude_Dialog/2.0.3.1 Hamster/2.0.0.0 |
Josh,
I tried to simulate your example in VMLAB and I can't find any
problem which results in that way as you describe. I get the
behavior as you want to have.
Only some hints. I am not sure which gcc version you use, but I
changed the #include-section to this:
#include <avr/io.h>
#include <avr/signal.h>
#include <avr/interrupt.h>
You should not include an avr/ioxxx.h file. It is recommend to use
avr/io.h. The correct MCU is declared via the makefile. Oh, I see I
used a makefile which I took from another project. Maybe there is a
difference, but I have only less experience on the options to
avr-gcc. Here is what my makefile does:
avr-gcc -c -g -Os -funsigned-char -funsigned-bitfields -fpack-struct
-fshort-enums -Wall -Wstrict-prototypes -Wa,-ahlms=tmr_test.lst -mmcu=atmega128
-I. tmr_test.c -o tmr_test.o
tmr_test.c:20: warning: function declaration isn't a prototype
tmr_test.c:43:2: warning: no newline at end of file
avr-gcc -Wl,-Map=tmr_test.map,--cref -mmcu=atmega128 tmr_test.o -lm --output
tmr_test.elf
avr-objcopy -O ihex -R .eeprom tmr_test.elf tmr_test.hex
avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load"
--change-section-lma .eeprom=0 -O ihex tmr_test.elf tmr_test.eep
avr-objdump -h -S tmr_test.elf > tmr_test.lss
And also, it is recommend to use
TCCR0 = 5; // set timer0 prescaler
OCR0 = 0xff; // set compare0 to 0xff
TIMSK |= _BV(OCIE0); // interrupt on compare
TIMSK &= ~_BVOCIE0);
instead of the outp(), sbi(), cbi() stuff.
Volkmar
On Wed, 29 Jan 2003 19:46:36 -0500, Josh Thompson wrote:
> Hi,
>
> I am trying to figure out how to use a timer to run code while my main
> program loop is doing something else. I have 2 LEDs connected to an
> atmega128, one to PORTD, the other to PORTA. When running the attached
> code, I would expect this to happen:
>
>
>
> at start, LED on PORTA should be on steady
> LED on PORTD should be flashing rapidly
>
> after some amount of time,
> LED on PORTA should go off and
> LED on PORTD should be flashing slower
>
>
>
>
> All that happens is that LED on PORTA comes on.
>
> Here is how I'm compiling it:
>
> avr-gcc -mmcu=atmega128 main.c -o main
>
> Thanks for your help; this is driving me crazy!!
> Josh
>
>
> ---------------------------------------------------
> #include <avr/iom128.h>
> #include <sig-avr.h>
> #include <interrupt.h>
> #include <io.h>
>
> #define TIMERCOUNTER_PERIODIC_TIMEOUT 100
>
> volatile static unsigned char timerCounter;
>
> void initTimer(void) {
> outb(TCCR0, 5); // set timer0 prescaler
> outb(OCR0, 0xff); // set compare0 to 0xff
> sbi(TIMSK, OCIE0); // interrupt on compare
> timerCounter = 0;
> }
>
> SIGNAL(SIG_OUTPUT_COMPARE0) {
> timerCounter++;
> }
>
> int main() {
> volatile static unsigned int count;
> volatile static unsigned int max;
>
> DDRD = 0xff; PORTD = 0X00;
> DDRA = 0xff; PORTA = 0x01;
>
> max = 1000;
> sei();
> initTimer();
>
> for(;;) {
> PORTD = 0x60;
> for(count = 0; count < max; count++);
> PORTD = 0x00;
> for(count = 0; count < max; count++);
>
> if (timerCounter > TIMERCOUNTER_PERIODIC_TIMEOUT) {
> cbi(TIMSK, OCIE0);
> PORTA = 0x00;
> max = 5000;
> }
> }
> }
> avr-gcc-list at http://avr1.org
avr-gcc-list at http://avr1.org