PWM motor control

Discussion in 'The Projects Forum' started by The_Fleertz, Oct 23, 2012.

  1. The_Fleertz

    Thread Starter New Member

    Apr 23, 2012
    8
    0
    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

    Code ( (Unknown Language)):
    1. //Stephen Ehle
    2. //PWM programming
    3.  
    4. #define _LEGACY_HEADERS
    5. #include <htc.h>
    6.  
    7. __CONFIG(INTIO & WDTDIS & PWRTDIS & BORDIS & BORDIS & LVPDIS & DEBUGEN & DUNPROTECT & UNPROTECT);
    8.  
    9. volatile unsigned int d_cycle=800;
    10. volatile char pwm_init=0;
    11.  
    12. void main( void ){
    13.  
    14. TRISC=0xFF;
    15. TRISD=0xFF;
    16. PR2=0xFF;
    17. PEIE=1;
    18. GIE=1;
    19. CCP1CON = 0b11001100;
    20.  
    21. CCPR1L = d_cycle>>2;
    22. CCP1CON = CCP1CON | ( ( d_cycle & 0b00000011 ) << 4 );
    23.  
    24. TMR2IF=0;
    25. T2CON=0b00000100;
    26.  
    27. while(pwm_init != 1);
    28.  
    29. TRISC=0x00;
    30. TRISD=0x00;
    31.  
    32. while(1);
    33.  
    34. }
    35.  
    36. void interrupt isr( void ){
    37.  
    38.    if( TMR2IF ){
    39.       pwm_init = 1;
    40.    }
    41.  
    42. }
     
Loading...