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

AntoniaRoons
Fri Apr 19 2024, 09:59 pm
carpinteyrowrl
Fri Apr 19 2024, 02:51 pm
DonaldJAX
Fri Apr 19 2024, 01:08 pm
Lewisuhakeply
Thu Apr 18 2024, 06:00 pm
Darrellciz
Thu Apr 18 2024, 11:07 am
Charlessber
Thu Apr 18 2024, 09:29 am
BartonSem
Thu Apr 18 2024, 04:56 am
DonaldKnown
Thu Apr 18 2024, 12:24 am