SIMPLE timer trouble

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

Hi - I'm just trying to get a PWM waveform going.  I have a ATMega328P (on an Arduino Nano) running at 16MHz.  I've set OCR0A to various values to no avail - output PD6 just stays low, looking with an o'scope.  See anything obviously stupid here?

void Timer_Setup(){	
	DDRD = PORTD6;						// set PD6 as timer output
	TCCR0A |= ((1<<WGM00) | (1<<WGM01) | (1<<COM0A1));	// Fast PWM mode
	TCCR0B |= ((1<<CS00) | (1<<WGM02));			// no prescaler
}

thanks! paul

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

oops I did change the port direction to DDRD = (1<<PORTD6) - still no joy

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

Try this:

 

DDRD = 0x40;  //set portD,6 to output
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 62.500 kHz
// Mode: Fast PWM top=OCR0A
// OC0A output: Non-Inverted PWM
// OC0B output: Disconnected
// Timer Period: 2.512 ms
// Output Pulse(s):
// OC0A Period: 2.512 ms Width: 2.512 ms
TCCR0A=(1<<COM0A1) | (0<<COM0A0) | (0<<COM0B1) | (0<<COM0B0) | (1<<WGM01) | (1<<WGM00);
TCCR0B=(1<<WGM02) | (1<<CS02) | (0<<CS01) | (0<<CS00);
TCNT0=0x00;
OCR0A=0x9C;
OCR0B=0x00;

Untested

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

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

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

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

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

Is this part of an Arduino sketch?  Arduino does default setup of timers.  All of your |= just adds on to how the timer is already set, and indeed you may not get what you expect.

 

 

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

Ahh thanks gentlemen, I will try Jim's setup tonight.  No, this is straight C - the hardware is Arduino clone, tho... paul

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

I don't think you want mode 7, do you?  If you do mode 7 with OCR0A as TOP, then OCOA output may well be uninteresting.  You could set up for PWM on the second channel, using OCR0B as the duty cycle for the PWM on OC0B.

 

Perhaps start with mode 3.  (fixed TOP of 0xff)  And indeed get rid of the |= to make us respondents happier, especially when only showing a fragment.

 

 

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 - good point, will try it tonight - paul

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

Major Deuippe here - I wasn;t running my Timer_Setup code!!  Thanks all for your help, paul