|
|
View previous topic :: View next topic |
Author |
Message |
Dale Stewart
Joined: 21 Apr 2005 Posts: 8 Location: Wollongong, AUSTRALIA
|
Help: Can't Get Function to Work for PWM |
Posted: Fri Jul 08, 2005 10:08 pm |
|
|
Hi
I am a newbie to PICs and learning CCS C.
My project is a small robot, and I want to write a simple function to control the speed and direction of DC motors using PWM.
The code I wrote before worked fine. However when I tried to encapsulate this code as a function to control the motors - it won't work.
I already tried using integer variables as arguments instead of constants when calling the function.
The problem doesn't seem to be a simple logic error, but something to do with CCS C coding ???
Any thoughts are most welcome.
Here is the code - please just focus on the function prototype, call, and definition and ignore the superfluous code, as it already works otherwise and has been tested thoroughly.
Code: |
// Motors_Main.c
//
// Date: 09 July 05
//
// Author: Dale T Stewart
//
// Purpose:
//
// The main code template for motors
//
//
// Test Notes:
//
//
// 1. Robot motors running at ~4.8V supply via Texas
// Instruments SN754410NE motor driver.
//
//
//
//
// Hardware notes:
//
// 1. Oscillator value affects frequency of PWM output
// to motors.
//
// 2. Motor direction control bits. Motors orientation and wiring
// need to be taken into consideration. See associated notes on
// Project #1.
#include <16F876A.h>
#fuses HS, NOWDT, NOPROTECT, NOLVP
#use delay( clock = 8000000 ) // 8MHz oscillator
//------------------------------------------------------------
//
// Motors Section
//
//------------------------------------------------------------
#define PERIOD 255 // 255 is maximum period?
#define POSTSCALE 1 // 1 ( fastest ) to 3 ( slowest )
// Select and define custom register names
//
#byte motors_Port_C = 7 // Select Port C address register
//----------------------------------------------------------
//
// Constants for motors speed
//
// Motor1 = RHS Motor2 = LHS
//
// SPEED Dec ~DUTY CYCLE ( % ) Relative Speed
#define SPEED_1 77 // 30 Minimum
#define SPEED_2 102 // 40 |
#define SPEED_3 128 // 50 |
#define SPEED_4 153 // 60 |
#define SPEED_5 179 // 70 |
#define SPEED_6 204 // 80 |
#define SPEED_7 230 // 90 v
#define SPEED_8 255 // 100 Maximum
//------------------------------------------------------
//
// DIRECTION - CONTROL BITS FOR MOTOR DIRECTION
//
// These depend on motors orientation and wiring.
// See associated notes on Project #1
#define FORWARD 0b01010110
#define REVERSE 0b10100110
#define TURN_LEFT 0b10010110
#define TURN_RIGHT 0b01100110
#define COAST 0b00000000
#define BRAKE_1 0b11110110
#define BRAKE_0 0b00000110
#define LEFT_FORWARD 0b01000010
#define LEFT_REVERSE 0b10000010
#define RIGHT_REVERSE 0b00100100
#define RIGHT_FORWARD 0b00010100
//------------------------------------------
//
// Motors prototype
void motors( int dir, int spd );
//**************************************************************
//
// START MAIN
//
//***********************************************************
void main()
{
//**********************************************************
//
// Motors Setup
//
//**********************************
// Motors: Setup data direction registers
//
set_tris_c( 0x00 ) ; // Set Port C data direction register to // output
//********************************
//
// Motors: Setup CCPx pins for PWM mode
//
setup_ccp1( CCP_PWM ) ; // Configure CCP1/RC2 as a PWM
setup_ccp2( CCP_PWM ) ; // Configure CCP2/RC1 as a PWM
//************************************
//
// Motors: Setup Timer2 times and frequencies
//
//
// The cycle time will be (1/clock)*4*t2div*(period+1)
// In this program clock=8000000 and period = 255 (below)
// For the three possible selections the cycle time is:
//
// (1/8000000)*4 * 1 * 256 = 128 us or 781.3 kHz
// (1/8000000)*4 * 4 * 256 = 512 us or 195.3 kHz
// (1/8000000)*4 * 16 * 256 = 2.05 ms or 488.3 Hz
setup_timer_2( T2_DIV_BY_1 , PERIOD, POSTSCALE ) ;
// End motors setup
//
//**************************************************************
while( 1 == 1 ) // Chase your tail - woof!
{
//******************************************
//
// GO FORWARD
motors( FORWARD, SPEED_7 );
} // end while
} // end main
//**********************************************************
//
// END MAIN
//
//**********************************************************
//***********************************************************
//
// Function Definitions
//
//**************************************************************
//------------------------
//
// Function: void motors( int direction, int speed )
//
// Purpose: Control motors speed & direction
//
void motors( int direction, int speed )
{
int d, s;
motors_Port_C = d ;
set_pwm1_duty( s ) ;
set_pwm2_duty( s ) ;
}
|
:-]
Cheers
Dale |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jul 08, 2005 10:49 pm |
|
|
In the function below, you're not using the passed parameters of
direction and speed. Instead, you're using a couple un-initialized
local variables for Port C and the duty cycle functions. You need
to get rid of the locals, and use your function parameters.
Quote: |
void motors( int direction, int speed )
{
int d, s;
motors_Port_C = d ;
set_pwm1_duty( s ) ;
set_pwm2_duty( s ) ;
} |
Also, I would reduce the number of asterisk lines. To me, they distract
attention from the code. Before I looked at your code, I copied it to
MPLAB and deleted all those lines. Then I saw the problem right away. |
|
|
Dale Stewart
Joined: 21 Apr 2005 Posts: 8 Location: Wollongong, AUSTRALIA
|
|
Posted: Fri Jul 08, 2005 11:12 pm |
|
|
Hi PCM
Thanks heaps for your help and effort!
Cool - it works!
I am just learning C and am rusty on functions- it was a dumb mistake.
Yeah I guess I do use a lot of asterisks to try and organise the code into sections - do you use another method?
:-]
Cheers
Dale |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jul 08, 2005 11:50 pm |
|
|
If you want to see my commenting style, you can see it in my post
near the end of this link. I separate functions with dashed lines.
http://www.ccsinfo.com/forum/viewtopic.php?t=23359
Everyone has their own style and I realize that when you're starting
out, you need more comments. My only reservation is that, for me,
using lines asterisks between lines of code is distracting. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|