8051 PCA changing duty cycle
Discussion in "Project Help" started by nmcleod1993 Jul 10, 2014.
Thu Jul 10 2014, 07:05 pm
Hey this is my first post.
I'm using the 8051 to control a DC motor. I'm using the 8 bit PCA for PWM. In the future I'm going to have a feed back loop to control the speed but for now I have two switches. For some reason, when either switch is pressed, the duty cycle changes to the expected value, but immediately after releasing it reverts back to the original duty cycle. My code is:
I'm using the 8051 to control a DC motor. I'm using the 8 bit PCA for PWM. In the future I'm going to have a feed back loop to control the speed but for now I have two switches. For some reason, when either switch is pressed, the duty cycle changes to the expected value, but immediately after releasing it reverts back to the original duty cycle. My code is:
//----------------------------------------------------------------------------- #include <c8051f020.h> // SFR declarations //----------------------------------------------------------------------------- // Global CONSTANTS //----------------------------------------------------------------------------- #define uchar unsigned char sbit SW1 = P1^0; sbit SW2 = P1^1; sbit LED = P1^6; //----------------------------------------------------------------------------- // Function PROTOTYPES //----------------------------------------------------------------------------- void main (void); void config(void); void desired_voltage (uchar out_of_twelve); void increment_voltage (void); void decrement_voltage (void); void delay(void); //----------------------------------------------------------------------------- // MAIN Routine //----------------------------------------------------------------------------- void main (void) { WDTCN = 0xDE; // Disable watchdog timer WDTCN = 0x07; WDTCN = 0xAD; OSCICN = 0x07; // set SYSCLK to 16MHz, config(); while(1){ if(!SW1){ increment_voltage(); while(SW1==0){ delay(); } } if(!SW2){ decrement_voltage(); while(SW2==0){ delay(); } } } } void delay(void) { int i, j; for (i=0; i <200; i++) { for (j = 0; j < 200; j++){ } } } void config (void){ XBR0 = 0x0c; // enable CEX0 at P0.0 XBR2 = 0x44; // enable crossbar and weak P0MDOUT = 0x15; // set P0.0 output state to P1MDOUT = 0x40; // configure the PCA PCA0CN = 0x40; // enable PCA counter PCA0MD = 0; // disable CF interrupt PCA0CPM0 = 0x42; // CCM0 in 8-bit PWM mode PCA0CPL0 = 0x80; // initialize PCA PWM value desired_voltage(5); //mid SW1 = 1; SW2 = 1; } void desired_voltage(uchar out_of_twelve){ PCA0CPH0 = 256-((256*out_of_twelve)/12); } void increment_voltage (void){ PCA0CPH0 -= 60; } void decrement_voltage (void){ PCA0CPH0 += 60; }
[ Edited Thu Jul 10 2014, 09:11 pm ]
Sat Jul 12 2014, 01:14 am
Your basic logic seems okay.
If the PWM returns to the midpoint I see two things to check.
Either decrement_voltage() and increment_voltage() are both being called or the device is resetting.
Give decrement_voltage() and increment_voltage() different values to show the difference.
Add LED flashing code to the start of the program to show resets.
If the PWM returns to the midpoint I see two things to check.
Either decrement_voltage() and increment_voltage() are both being called or the device is resetting.
Give decrement_voltage() and increment_voltage() different values to show the difference.
Add LED flashing code to the start of the program to show resets.
Powered by e107 Forum System