PWM motor control

Thread Starter

The_Fleertz

Joined Apr 23, 2012
8
Hey,
Trying to get some PWM code working. I really don't know much about how this works and just followed the steps in the PIC16f887 datasheet. I'm just trying to run a motor in one direction for now.
Thanks

Rich (BB code):
//Stephen Ehle
//PWM programming

#define _LEGACY_HEADERS
#include <htc.h>

__CONFIG(INTIO & WDTDIS & PWRTDIS & BORDIS & BORDIS & LVPDIS & DEBUGEN & DUNPROTECT & UNPROTECT);

volatile unsigned int d_cycle=800;
volatile char pwm_init=0;

void main( void ){

TRISC=0xFF;
TRISD=0xFF;
PR2=0xFF;
PEIE=1;
GIE=1;
CCP1CON = 0b11001100;

CCPR1L = d_cycle>>2;
CCP1CON = CCP1CON | ( ( d_cycle & 0b00000011 ) << 4 );

TMR2IF=0;
T2CON=0b00000100;

while(pwm_init != 1);

TRISC=0x00;
TRISD=0x00;

while(1);

}

void interrupt isr( void ){

   if( TMR2IF ){
      pwm_init = 1;
   }

}
 
Top