[solved] Timer1 on Attiny45

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

Hello,

I need to generate short pulses (around 10us) with a reasonable precision, let say about 20%.

I follow many tutorials but I still do not understand why the following code does not work completely:

byte pin=0;

void loop() {
digitalWrite(pin,HIGH);
delay(100);
digitalWrite(pin,LOW);
timer(200);
delay(3000);
}

void timer(byte b) {
byte portb1mask=1<<PORTB1;

TCCR1=B00000000;
PORTB|=portb1mask;
TCNT1=0;
TCCR1=B00001111;
while(TCNT1<=b);
PORTB&=~portb1mask;
TCCR1=B00000000;
}

void setup() {
pinMode(pin,OUTPUT);
pinMode(PB1,OUTPUT);
}

For each loop() iteration, I expect pin 0 pulse shortly, and normally pin 1 should pulse immediately after using timer 1 comparison.
But it works only for half of the loop iteration:

Iteration n: pin 0 pulse, then pin 1 pulse
Iteration n+1: only pin 0
Iteration n+2: pin 0 then pin 1
Iteration n+3: only pin 0

Does anybody have a suggestion about this ?

Best regards,

Grégoire.

This topic has a solution.
Last Edited: Sat. Jan 24, 2015 - 10:37 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

First, learn how to use the "code tags" -- <> on the toolbar.  It is very hard to follow unformatted code, and many readers quickly lose interest.

 

If you are indeed working in the Arduino environment, then say so.  Tell AVR model, and AVR clock speed.

 

There is an Arduino forum, where experts visit?  For me, I cannot tell where Arduino ends and "native" coding begins (the mention of PB1).

 

For me, post the smallest complete test program that demonstrates your symptoms.

 

 

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

Hello, thank you for your answer. You are right, I am working with Arduino environment, I forgot to mention this. I use an usb dongle to program an Attiny 45 running at 8mhz.
the program here is extracted from my real application, this is the smallest set of lines to illustrate it.
The "native" code is entirely declared in function timer(byte): it only uses C and registers access. Do you think "Arduino" libraries can impact "native" code?

Regards,
Grégoire.

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

The "native" code is entirely declared in function timer(byte): it only uses C and registers access. Do you think "Arduino" libraries can impact "native" code?

First, I'd like more background.  Can you post a complete test program that demonstrates the behaviour?  For example, I have no idea what a  "B00000000" is.

 

Also, repost the code using formatting block, <> on the editing toolbar.  It is too difficult to work with unformatted code.

 

Gee, that is what I said when I replied earlier...

 

Can you use PB1 as a parameter to pin_mode()?  PB1 is just a 1.  What is the Arduino pin mapping on a '45?

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

Do you think "Arduino" libraries can impact "native" code?

http://forum.arduino.cc/index.ph...

According to responses in that thread, the millis() function uses timer1 on a '45.  So there may be bits set in GTCCR that might affect your code?

By default, for the ATtiny85 family, timer 1 is used for millis.  There is a compile-time switch available to move millis to timer 0.

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

Can you use PB1 as a parameter to pin_mode()?

Yes, it just might not give you the result you wanted.

Regards,
Steve A.

The Board helps those that help themselves.

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

The t45 isn't supported by the stock Arduino IDE, you need to install a 3rd party 'core'.  Different cores have different pin-mappings.  Which one do you have?
 

"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."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

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

"Fast.  Cheap.  Good.  Pick two."

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

 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,
I found the solution: in order to have the timer1 working, a value must be set into ocr1a.
I don't know why, because I do not use it, but that works perfectly ! Value can be 0 or anything else.
I can't find any reason for this in user manual in section timer1.

Thanks for your help,

Regards,

Grégoire.

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

Can you show your code?  Show the code that 'didn't work', and the fix.

"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."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

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

"Fast.  Cheap.  Good.  Pick two."

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