Discussion in "Project Help" started by    nmcleod1993    Jul 10, 2014.
Thu Jul 10 2014, 07:05 pm
#1
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:

//-----------------------------------------------------------------------------
#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
#2
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.

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

gtaletrzua
Fri May 03 2024, 10:55 am
Clydehet
Wed May 01 2024, 06:44 pm
Davidoried
Wed May 01 2024, 06:11 pm
KevinTab
Sun Apr 28 2024, 05:35 am
Tumergix
Sun Apr 28 2024, 12:59 am
StevenDrulk
Sat Apr 27 2024, 08:47 pm
StephenHauct
Sat Apr 27 2024, 09:38 am
Adamsaf
Sat Apr 27 2024, 07:12 am