Starting and stopping Phase Correct PWM

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

How do I actually turn on PD6 when PWM is enabled(on ATMega88PA)? Once I have the WGM and COMM bits set to select the type of PWM and which pin it is enabled for, lets say I want to start the motor, do I simply enable the CSxx bits and to stop the motor (turn the pin completely off) clear them? Or do I continue to use PORTD |= (1<<6); and PORTD &= ~(1<<6); to turn the motor on and off?

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

Quote:

lets say I want to start the motor, do I simply enable the CSxx bits

Starting the timer sounds good. You might want to set OCR to a "known" value if that is important to you.

Quote:

to stop the motor (turn the pin completely off) clear them? Or do I continue to use PORTD |= (1<<6); and PORTD &= ~(1<<6); to turn the motor on and off?

Neither. And I guess there is an asterisk to my "sounds good" above.

Once you "connect" the pin to the timer with the COM bits, PORTD has no effect. So to stop the motor, you stop the timer with the CS bits (optional), disconnect the pin with the COM bits, then use PORT to set the pin to the desired "off" level--probably low.

(You can leave the pin connected if you ensure/wait for the pin being low. By the time you do that logic you might as well do the straightforward sequence that I listed.)

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

Then is the following correct?

#include 
#include 
#include 
#include 

volatile bool TS;

int main()
{
static bool TF;
volatile uint8_t value;

//Sets pin direction, PD6 Motor Drive, PB1 Motor Brake
DDRD |= 0x40;
DDRB |=	0x02;	

//Sets initial state of PD6 and PB1
PORTD &= ~0x40;	//Motor Drive off
PORTB &= ~0x02;	//Motor Brake off

//Enable Pull Up Resistors
PORTC |= 0x05;	//PC0(Gbx. Pos.),PC2(Trig.) 
PORTD |= 0xA0;	//PD5,PD7	
PORTB |= 0x05;	//PB0,PB2

//Enable Global Interrupts and PCINT8
sei();				
PCICR  |= 0x02;		
PCMSK1 |= 0x01;			

//Sets TC0 for Phase Correct PWM opt. 5
TCCR0A |= 0x01;
TCCR0B |= 0x08;
OCR0A = 0xD0; //192 (75% duty cycle)

	//Main Loop
	while(1){

		//Fire Gun when PINC2 is low
		if( ((PINC&_BV(PINC2)) == 0) ){
			TCCR0A |= _BV(COM0A0);
			TCCR0B |= _BV(CS00);}
		else{
			TCCR0B &= ~_BV(CS00);
			TCCR0A &= ~_BV(COM0A0);
			PORTD &= ~_BV(PIND6);}

		if( TF != TS ){
			if( TS ){
				value++;
			}
		TF=TS;
		}
		
	}//End Main Loop
		
}

//Main Interrupt
ISR(PCINT1_vect)
{	
	//Reads PINC0 and sets TS True or False
	TS = ((PINC&_BV(PINC0)) != 0) ? true : false; 

}//End Main Interrupt

PIND6 is the motor drive, PINC2 is the trigger, PINC0 is the gearbox position sensor. Ignore the interrupt and the if( TF... statement as it is arbitrary for this particular test, I just left it in. Normally value is a conditional for the Fire Gun if statement. However at the moment I am concerned with testing PWM functionality only before I move further.