at90usb1287 timer 1 second

Go To Last Post
31 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

help create timer 1 second on board at90usb1287.

This code not run.

#include <avr/io.h>

#include <avr/interrupt.h>

int main(void)
{
    DDRD = 0xFF;
    PORTD = 0xFF;
        TCNT1H = 0; //setup
        TCNT1L = 0;
    // set timer 1 compare A
    OCR1AH = 0x1E;
    OCR1AL = 0x84; // 1 second
    // set timer 1 prescaler to clck / 1024
    TCCR1A = 0x00;
    TCCR1B = 0x0D;
        
    TIMSK1 |= (1 << OCIE1A);
    
    sei();
    while(1)
    {   };

}


ISR(TIMER1_COMPA_vect)
{
        TCNT1H = 0xE1;
        TCNT1L = 0x7C;
     PORTD ^= _BV(PD5);
}

 

Last Edited: Thu. Jun 25, 2015 - 09:55 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Wrong forum..moved to Mega/Tiny forum

Jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you want constant delay of 1 second then why are you writing TCNT1H/L in ISR()? Do all calculations for 1 second delay, set prescaler bits accordingly and write TCNT1H/L=0 and OCR1H/L registers in main() only.

Last Edited: Thu. Jun 25, 2015 - 10:13 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

#include <avr/io.h>

#include <avr/interrupt.h> 
 

volatile int ctr=0;

int main(void)
{
	DDRD = 0xFF;
	PORTD = 0xFF;
        TCNT1H = 0; //setup
        TCNT1L = 0; 
	// set timer 1 compare A
	OCR1AH = 0x1E;
	OCR1AL = 0x84; // 1 second
	// set timer 1 prescaler to clck / 1024
	TCCR1A = 0x00;
	TCCR1B = 0x0D;
        
	TIMSK1 |= (1 << OCIE1A); // activate the compa interupt
	
	sei();
	while(1)
	{
		
	
	};

}


ISR(TIMER1_COMPA_vect)
{
      PORTD ^= _BV(PD5);
}

does not work

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I guess the starting point in any of this is what the frequency of the CPU is. For USB use I ugess the 1287 has either an 8MHz or a 16MHz crystal attached.

 

After that it's simply a question of doing the sums for CTC mode.

 

Judging by your most recent post you are not using CTC mode - this is a mistake - it is the "best" mode to get a consistent 1s interrupt.

 

When I plug the numbers into avrcalc.exe (google it - Jack Tidwell).

 

It says that on 8MHz you want a /256 prescaler and an OCR=0x7A11 and you will get an exact 1000ms period.

 

It says that on 16MHz you want a /256 prescaler and an OCR=0xF423 for the same, exact period.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have 8MHz and prescaler=1024 therefore (1/8000000sec)*1024=0.000128sec; after 1sec/0.000128sec=7812.5 (or 8000000/1024).

if converted from decimal to hexadecimal 7813 => 1E85.

Тhus     OCR1AH = 0x1E;     OCR1AL = 0x85;

I hope correctly?

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Clawson's recommendation is more accurate if you want exactly 1 second, not much reason to use a larger prescaler as long as the timer isn't overflowing for the delay desired.

And why are you setting OCR1AH and OCR1AL separately? You can just set OCR1A to a 16 bit value and gcc takes care of setting the low and high registers.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcalc.exe shown OCR1A=0x1E83 (exact value);

 

But code does not work

#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 8000000UL

int main(void)
{
    DDRD = 0xFF;
    PORTD|= _BV(PD5);
        //setup
        TCNT1H = 0;
        TCNT1L = 0;
         // activate the compa interupt
    TIMSK1 |= (1 << OCIE1A);
     // set timer 1 compare A
    OCR1AH = 0x1E;
    OCR1AL = 0x83; // 1 second
    // set timer 1 prescaler to clck / 1024
    TCCR1A = 0x00;
    TCCR1B = 0x05;
       
    
    sei();
    while(1)
    {    };

}


ISR(TIMER1_COMPA_vect)
{
      PORTD ^= _BV(PD5);
}

that it is necessary to make the program to work

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But that isn't CTC mode?

 

Can I suggest you don't use meaningless numbers like 0x05 when setting TCCR1B etc but use (1 << bitnames) to document the bits you are setting?

 

CTC mode (with OCR1A to set the frequency) is WGM mode 4. That means bit WGM12 needs to be set. It is bit 3 of TCCR1B. So maybe try:

TCCR1B = (1 << WGM12) | (1 << CS12); // WGM mode 4 = CTC and /256 prescaler

As I said above for an accurate 1000.0ms you want to use /256 (as I've shown here) and then the OCR1A value is 0x7A11 so also use:

OCR1A = 0x7A11; // 1000ms with /256 on 8MHz

I don't see a lot of point in setting OCR1AH and OCR1AL separately when they can both be set in one operation like this.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#include <avr/io.h>
#include <avr/interrupt.h> 
#define F_CPU 8000000UL

int main(void)
{
	DDRD = 0xFF;
	PORTD|= _BV(PD5);
        //setup
        TCNT1H = 0; 
        TCNT1L = 0; 
         // activate the compa interupt
	TIMSK1 |= (1 << OCIE1A); 
        TIFR1 |= (1<<OCF1A);
	// set timer 1 compare A
	OCR1A = 0x7A11;  // 1 second
	
	TCCR1A = 0x00;
	TCCR1B = (1 << WGM12) | (1 << CS12);
	
	sei();
	while(1)
	{ };
}
ISR(TIMER1_COMPA_vect)
{
      PORTD ^= _BV(PD5);
}

Code does not work.

 

Clawson thanks for the answers. I'm learning programming avr microprocessor.

Please help.

 

 

compiling the program is successful:

[avran@localhost p_avr]$ avr-gcc -mmcu=at90usb1287 -Os -c avr9.c -o avr9.o
[avran@localhost p_avr]$ avr-objcopy -I elf32-avr -O ihex avr9.o avr9.hex
[avran@localhost p_avr]$ dfu-programmer at90usb1287 erase
[avran@localhost p_avr]$ dfu-programmer at90usb1287 flash avr9.hex
Validating...
96 bytes used (0.08%)

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What does "does not work" mean?  What do you expect to happen?  What >>is<< happening?  How are you testing?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

Can I suggest you don't use meaningless numbers like 0x05 when setting TCCR1B etc but use (1 << bitnames) to document the bits you are setting?

#include <avr/io.h>
#include <avr/interrupt.h> 
#define F_CPU 8000000UL

int main(void)
{
	DDRD = 0xFF;
	PORTD|=(1<<PD5);
        TIMSK1 = (1 << OCIE1A); 
       	OCR1A = 0x7A11; 
	TCCR1A = 0x00;
	TCCR1B = (1 << WGM12) | (1 << CS12);
	
	sei();
	while(1)
	{ };
}
ISR(TIMER1_COMPA_vect)
{
  PORTD ^=(1<<PD5);
  PORTD|=(1<<PD6);

}

It does not work

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:

What does "does not work" mean?  What do you expect to happen?  What >>is<< happening?  How are you testing?

 

LED must flash at a frequency of once per second.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You only answered one question, "what do you expect to happen".  Please answer the others.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:

You only answered one question, "what do you expect to happen".  Please answer the others.

 

The green LED lights up(PD5).
All the time lights.

 

adding led PD6

ISR(TIMER1_COMPA_vect)
{
  PORTD ^=(1<<PD5);
  PORTD|=(1<<PD6);

}

but led PD6 do not light up.

It seems interrupt does not work

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And you are positive that you have the LED connected to PD5?

 

If you do manual blinking in the main loop, does it blink then?

 

#include <util/delay.h>
...
while (1)
    {
        _delay_ms(234);
        PORTD ^= (1<<PD5);
    }

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The code looks fine to me...what're your fuse settings, and what are you using for an oscillator?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:

And you are positive that you have the LED connected to PD5?

 

Yes, after

PORTD|=(1<<PD6);

LED lights up. I have at90usbkey board http://www.atmel.com/tools/AT90USBKEY.aspx

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:

If you do manual blinking in the main loop, does it blink then?

 

#include <util/delay.h>
...
while (1)
    {
        _delay_ms(234);
        PORTD ^= (1<<PD5);
    }

 

 

I know but  need a timer counter.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I know bun  need a timer counter.

I understand that.  The purpose of the exercise is to ensure that the LED is really on PD5 and connected properly.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Rezurok wrote:

The code looks fine to me...what're your fuse settings, and what are you using for an oscillator?

 

how read fuse at90usb1287 using avrdude

avrdude   -c  ?????   -p usb1287 -U hfuse:r:hfuse.txt:h -U lfuse:r:lfuse.txt:h

-c programmer-id-c programmer-id    what to write after -с?

I have  dfu-programmer at90usbkey.

 

 

Last Edited: Fri. Jun 26, 2015 - 07:58 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:

I know bun  need a timer counter.

I understand that.  The purpose of the exercise is to ensure that the LED is really on PD5 and connected properly.

 

The training objective is to work with timer

– Two 8-bit timer/counters with separate prescaler and compare mode
– Two16-bit timer/counter with separate prescaler, compare- and capture mode

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Rezurok wrote:

The code looks fine to me...what're your fuse settings, and what are you using for an oscillator?

 

internal oscillator at 8Mhz

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So, did you do the test I asked, as a "sanity check" to ensure that you are really driving the correct pin?

 

It is interesting that your build result is 96 bytes.  When I built your code, it was about 250 bytes.

 

96 bytes is not even enough for the vector table, is it?

 

Based on that, I think that you are building the wrong file.  Perhaps look at the .LSS to see what is actually being processed.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:

So, did you do the test I asked, as a "sanity check" to ensure that you are really driving the correct pin?

 

 

yes, all pins are correct

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:

Based on that, I think that you are building the wrong file.  Perhaps look at the .LSS to see what is actually being processed.

 

avr-objdump -S avr9.o > avr9.lss
cat avr9.lss

avr9.o:     file format elf32-avr


Disassembly of section .text:

00000000 <__vector_17>:
   0:	1f 92       	push	r1
   2:	0f 92       	push	r0
   4:	0f b6       	in	r0, 0x3f	; 63
   6:	0f 92       	push	r0
   8:	11 24       	eor	r1, r1
   a:	8f 93       	push	r24
   c:	9f 93       	push	r25
   e:	9b b1       	in	r25, 0x0b	; 11
  10:	80 e2       	ldi	r24, 0x20	; 32
  12:	89 27       	eor	r24, r25
  14:	8b b9       	out	0x0b, r24	; 11
  16:	5e 9a       	sbi	0x0b, 6	; 11
  18:	9f 91       	pop	r25
  1a:	8f 91       	pop	r24
  1c:	0f 90       	pop	r0
  1e:	0f be       	out	0x3f, r0	; 63
  20:	0f 90       	pop	r0
  22:	1f 90       	pop	r1
  24:	18 95       	reti

Disassembly of section .text.startup:

00000026 <main>:
  26:	8f ef       	ldi	r24, 0xFF	; 255
  28:	8a b9       	out	0x0a, r24	; 10
  2a:	5d 9a       	sbi	0x0b, 5	; 11
  2c:	82 e0       	ldi	r24, 0x02	; 2
  2e:	80 93 6f 00 	sts	0x006F, r24
  32:	81 e1       	ldi	r24, 0x11	; 17
  34:	9a e7       	ldi	r25, 0x7A	; 122
  36:	90 93 89 00 	sts	0x0089, r25
  3a:	80 93 88 00 	sts	0x0088, r24
  3e:	10 92 80 00 	sts	0x0080, r1
  42:	8c e0       	ldi	r24, 0x0C	; 12
  44:	80 93 81 00 	sts	0x0081, r24
  48:	78 94       	sei
  4a:	00 c0       	rjmp	.+0      	; 0x4c <__SREG__+0xd>

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:

It is interesting that your build result is 96 bytes.  When I built your code, it was about 250 bytes.

96 bytes is not even enough for the vector table, is it?

 avr-gcc -mmcu=at90usb1287 -c avr9.c -o avr9.o
 avr-objcopy -I elf32-avr -O ihex avr9.o avr9.hex
 dfu-programmer at90usb1287 erase
 dfu-programmer at90usb1287 flash avr9.hex
Validating...
174 bytes used (0.14%)

key -Os in avr-gcc removed.

 

-Os Optimize for size.  -Os enables all -O2 optimizations that do not
           typically increase code size.  It also performs further
           optimizations designed to reduce code size.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Now

ISR(TIMER1_COMPA_vect)
{
  PORTD ^=(1<<PD5);
  }

blinks once

 

ISR(TIMER1_COMPA_vect)
{
  PORTD|=(1<<PD5);
  }

all the time ligths up

 

an interrupt is triggered once

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why are you skipping the link stage? It will never work that way.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Sat. Jun 27, 2015 - 08:28 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch, clawson, Rezurok

Сompiled hex file in Atmel Studio 6,  made once blinking in 7-8 second.

Where mode /256 took 8 seconds?

The question I myself decide.

The main....... i have blinking led

Thank you.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

joeymorin wrote:

Why are you skipping the link stage? It will never work that way.

 

Yes,  Atmel Studio 6 compilition

avr-gcc.exe" -o GccApplication3.elf  GccApplication3.o   -Wl,-Map="GccApplication3.map" -Wl,--start-group -Wl,-lm  -Wl,--end-group  -mmcu=at90usb1287

avr-objcopy.exe" -O ihex -R .eeprom -R .fuse -R .lock -R .signature  "GccApplication3.elf" "GccApplication3.hex"

avr-objcopy.exe" -j .eeprom  --set-section-flags=.eeprom=alloc,load --change-section-lma .eeprom=0  --no-change-warnings -O ihex "GccApplication3.elf" "GccApplication3.eep" || exit 0

avr-objdump.exe" -h -S "GccApplication3.elf" > "GccApplication3.lss"

avr-objcopy.exe" -O srec -R .eeprom -R .fuse -R .lock -R .signature  "GccApplication3.elf" "GccApplication3.srec"

avr-size.exe" "GccApplication3.elf"

 

Program Memory Usage     :    300 bytes   0,2 % Full
Data Memory Usage         :    0 bytes   0,0 % Full