Go Down

Topic: Question about timer1 (Read 91 times) previous topic - next topic

piuhis3

Sep 21, 2019, 09:45 pm Last Edit: Sep 21, 2019, 10:13 pm by piuhis3
Hi!
I have made a code that uses the timer 1 to create a 60hz signal on pin 10 with the duty cycle of 0.03%
Code: [Select]

  void setup() {
   TCCR1A =  _BV(COM1B1) | _BV(WGM11) | _BV(WGM10);
   TCCR1B = _BV(WGM12) |_BV(WGM13) | _BV(CS12);
   OCR1A = 1025; //16000000hz/256hz/60hz  ≈ 1025
   pinMode(10, OUTPUT);
  }
  void loop() {
  }
   

But for some reason I can't even get any kind of signal out of that port. Can someone help me with my code?
Edit: Include the basic absolute minimum amount of code for an arduino project.

PerryBebbington


Quote
But for some reason I can't even get any kind of signal out of that port. Can someone help me with my code?
You're more likely to get something out of the port if you actually write some code....


piuhis3

#2
Sep 21, 2019, 10:01 pm Last Edit: Sep 21, 2019, 10:04 pm by piuhis3
You're more likely to get something out of the port if you actually write some code....


What kinda code? Shouldn't enabling COM1B1 Register manipulate the port 10?

PerryBebbington

What is the basic, absolute minimum code for an Arduino project?

piuhis3


DrAzzy

You have WGM set to 16, fast PWM with OCR1A as TOP.

You have COM1B1 set to 1,

Clear OC1A/OC1B on Compare Match, set
OC1A/OC1B at BOTTOM (non-inverting mode)


But you haven't set OCR1B, so it's at it's default value of 0.

I suspect what's happening is that the compare match happens at a count of 0, so it spends all it's time with the bit cleared (low).

if you want a 50% duty cycle square wave, set OCR1B to half of what you set OCR1A (TOP) to.
ATTinyCore for x4/x5/x61/x7/x8/x41/1634/828/x313 megaTinyCore for the megaavr ATtinies - Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts, mosfets, awesome prototyping board in my store http://tindie.com/stores/DrAzzy

PerryBebbington

Edited the main post.
Right, that's better.

Please post ALL your code in future, you are far more likely to get quick and helpful answers that way instead of wasting both your time and the time of people trying to help you.

I don't know the answer to your question.


piuhis3

You have WGM set to 16, fast PWM with OCR1A as TOP.

You have COM1B1 set to 1,

Clear OC1A/OC1B on Compare Match, set
OC1A/OC1B at BOTTOM (non-inverting mode)


But you haven't set OCR1B, so it's at it's default value of 0.

I suspect what's happening is that the compare match happens at a count of 0, so it spends all it's time with the bit cleared (low).

if you want a 50% duty cycle square wave, set OCR1B to half of what you set OCR1A (TOP) to.
Good theory, but unfortunately it doesn't seem to work. Here is my new code.
Code: [Select]

   TCCR1A =  _BV(COM1B1) | _BV(WGM11) | _BV(WGM10);
   TCCR1B = _BV(WGM12) |_BV(WGM13) | _BV(CS12);
   OCR1A = 1025; //16000000hz/256hz/60hz  ≈ 1025
   OCR1B = 512;
   pinMode(10, OUTPUT);

DrAzzy

hmmmmm..... I don't see the issue, then. And this is an atmega328p? (eg, uno/nano/pro mini/other '328p based board)

(btw - your comment should be written //16000000hz/256/60hz  - the prescaler has no units, and this way the hz's cancel so your result is, as you want, dimensionless - ofc this has nothing to do with your problem, just something that bugs me as someone whose education was very heavy on the importance of dimensional analysis for sanity validation)
ATTinyCore for x4/x5/x61/x7/x8/x41/1634/828/x313 megaTinyCore for the megaavr ATtinies - Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts, mosfets, awesome prototyping board in my store http://tindie.com/stores/DrAzzy

johnwasser

You aren't going to get an exact 60Hz since 16000000 doesn't divide evenly by 60. When I do the timer math I get a prescale of 8 and a count of 33,333 and 1/3rd.  If you want fewer steps of PWM you can use a prescale of 256 and count of 1041.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

DrAzzy

Well, he says he's getting *nothing* not that he's getting wrong output. So we should figure that out first :-P
ATTinyCore for x4/x5/x61/x7/x8/x41/1634/828/x313 megaTinyCore for the megaavr ATtinies - Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts, mosfets, awesome prototyping board in my store http://tindie.com/stores/DrAzzy

piuhis3

#11
Sep 21, 2019, 11:31 pm Last Edit: Sep 21, 2019, 11:32 pm by piuhis3
Ok, I got it to work, it turns out there was just a loose connection in my circuit. Thanks for all of your help! And it doesn't matter that it isn't 100% exact 60hz.

Go Up