📄 example_2_dc.c
字号:
//-----------------------------------------------------------------------------
// Example 2
// DC Motor Control with Reversing
//-----------------------------------------------------------------------------
// Copyright 2004 Silicon Laboratories Inc.
//
// AUTH: KAB
// DATE: 12MAR04
//
// This program provides DC motor control with hard reversing using the PCA
// 8-bit PWM Mode. A single PCA module is used to generate an 8-bit PWM. The
// pin skip register XBR0 is used to multiplex the PWM between two outputs
// P0.0 and P0.1. These two outputs are used to drive the lower transistors in
// an H-Bridge configuration. P0.2 and P0.3 are used to drive the upper
// transistors.
//
// The ADC is used to read the poteniometer voltage on P0.6. The ADC uses polled
// mode and 64 sample averaging.
//
// When the pushbutton is pressed and held the PWM duty cycle will be set to
// zero and all transistors will be turned off. This will allow the motor to
// coast. Releasing the switch will cause the motor to reverse. If the switch
// is released before the motor comes to a complete stop, the motor will
// abruptly stop and reverse directions.
//
// Target: C8051F30x
//
// Tool chain: KEIL Eval 'c'
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051f300.h> // SFR declarations
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void SYSCLK_Init (void); // Initialize SYSCLK
void PORT_Init (void); // Initialize XBR and Port Pins
void PCA0_Init (void); // Initialize PCA0
void ADC0_Init (void); // Initialize ADC
unsigned char readVin(void); // read ADC using polling
unsigned char avgVin(void); // returns avg ADC reading
void coast (void);
void reverse (void);
//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------
bit Fwd;
sbit SW1 = P0^5;
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void)
{
PCA0MD &= ~0x40; // Disable Watchdog Timer
SYSCLK_Init (); // Initialize system clock
PORT_Init (); // Initialize crossbar and GPIO
ADC0_Init(); // Initialize ADC for polled mode
PCA0_Init (); // PCA0 for 8-bit PWM
Fwd = 1; // start in forward direction
EA = 1; // enable global interrupts
while (1)
{
if(SW1) // if not pressed
{
PCA0CPH0 = avgVin(); // get avg reading and output to PWM
}
else
{
coast(); // if pressed, coast
while(!SW1); // wait for button to be released
reverse(); // reverse directions
}
}
}
//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
//
// This routine initializes the system clock to use the internal 24.5MHz
// oscillator as its clock source. Also enables missing clock detector reset
// and the VDD Monitor.
//
void SYSCLK_Init (void)
{
OSCICN = 0x07; // Configure internal oscillator for
// highest frequency.
RSTSRC = 0x06; // Enable missing clock detector
// and VDD Monitor.
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Configure the Crossbar and GPIO ports.
// P0.0 - /Abot - active low PWM signal drives bottom transistor
// P0.1 - /Bbot - active low PWM signal drives bottom transistor
// P0.2 - /Atop - active low signal drives top transistor
// P0.3 - /Abot - active low signal drives top transistor
// P0.4 -
// P0.5 - Switch
// P0.6 - Vin
// P0.7 -
//
void PORT_Init (void)
{
XBR0 = ~0x01; // skip all except P0.0
XBR1 = 0x40; // Enable CEX0 on P0.0
P0MDOUT = 0x0f; // P0.0 - P0.3 are push-pull outputs
P0MDIN = ~0x40; // Configure P0.6 for analog input
P0 = ~0x08; // P0.3 low
XBR2 |= 0x40; // Enable crossbar
}
//-----------------------------------------------------------------------------
// PCA0_Init
//-----------------------------------------------------------------------------
//
//
void PCA0_Init (void)
{
PCA0MD = 0x02; // PCA uses sysclk/4, no interrupt
PCA0CPM0 = 0x42; // Module 0, 8-bit PWM Mode
PCA0L = 0x00; // reset the timer
PCA0H = 0x00;
PCA0CPL0 = 0x00; // Initialize to minimum duty
PCA0CPH0 = 0x00;
CR = 1; // Start PCA0 timer
}
//-----------------------------------------------------------------------------
// ADC0_Init
//-----------------------------------------------------------------------------
void ADC0_Init (void)
{
ADC0CN = 0x40; // Low-power tracking mode;
// ADC0 conversions are initiated
// on writes to AD0BUSY;
AMX0SL = 0xf6; // select P0.6, single-ended
ADC0CF = 0x81; // AD0SC=4 gain =1
REF0CN = 0x0a; // ADC uses Vdd for full scale
EIE1 &= ~0x04; // disable ADC0 EOC interrupt
AD0EN = 1; // enable ADC
}
unsigned char readVin(void)
{
AD0INT = 0; // clear ADC0 end-of-conversion
AD0BUSY = 1; // initiate conversion
while (!AD0INT); // wait for conversion to complete
return ADC0;
}
unsigned char avgVin(void)
{
unsigned char i, result;
unsigned int sum;
sum = 0;
for (i = 64; i != 0; i--) // repeat 64 times
{
sum += readVin(); // read ADC and add to sum
}
result = (unsigned char)(sum>>6); // divide by 64 and cast to uchar
return result; // return average reading
}
void coast(void)
{
PCA0CPH0 = 0; // disable PWM
P0 = 0xff; // force all outputs high
}
void reverse (void)
{
Fwd=!Fwd; // toggle direction flag
CR = 0; // Stop PCA0 timer
PCA0CPH0 = 0; // clear duty cycle
PCA0CPM0 = 0; // disable PWM
P0 = 0xff; // force all outputs high
XBR2 &= ~0x40; // disable Crossbar
if (Fwd)
{
XBR0 = ~0x01; // don't skip P0.0
P0 = ~0x08; // P0.3 low
}
else
{
XBR0 = ~0x02; // don't skip P0.1
P0 = ~0x04; // P0.2 low
}
XBR2 |= 0x40; // enable Crossbar
PCA0CPM0 = 0x42; // Module 0, 8-bit PWM Mode
CR = 1; // restart PCA0 timer
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -