当前位置:首页 >> 信息与通信 >>

c8051的PCA模块使用定时器实现双路40KHz的PWM波形输出


//-----------------------------------------------------------------------------
// F00x_PCA0_8Bit_PWM_Output.c
//-----------------------------------------------------------------------------
// Copyright 2006 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// This program sends a PWM waveform out of an I/O pin, using the PCA's
// 8-bit PWM Output Mode. The duty cycle of the waveform is modified
// periodically within the main loop.
//
// In this example, PCA Module 0 is used to generate the waveform, and the
// crossbar is configured to send the CEX0 pin out on P0.0.
//
// How To Test:
//
// 1) Download code to a 'F00x device which has an oscilloscope monitoring P0.0
// 2) Run the program - the waveform should be visible on the oscilloscope.
// 3) Verify that the duty cycle of the waveform varies smoothly between
// minimum and maximum values.
//
//
// FID: 00X000032
// Target: C8051F00x
// Tool chain: Keil C51 7.50 / Keil EVAL C51
// Command Line: None
//
//
// Release 1.0
// -Initial Revision (BD)
// -10 MAR 2006
//

//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------

#include <c8051f000.h> // SFR declarations
#include<intrins.h>


//-----------------------------------------------------------------------------
// Global Constants
//-----------------------------------------------------------------------------

#define SYSCLK 22118400 // External oscillator frequency in Hz
sbit LED = P1^6; // LED='1' means ON

//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------

void OSCILLATOR_Init (void);
void PORT_Init (void);
void PCA2_Init (void);
void PCA3_Init (void);
//-----------------------------------------------------------------------------
// main() Routine
//-----------------------------------------------------------------------------

void main (void)
{
unsigned int delay_count; // Used to implement a delay
bit duty_direction = 0; // 0 = Decrease; 1 = Increase

// Disable watchdog timer
WDTCN = 0xde;
WDTCN = 0xad;
EA=1;
PORT_Init (); // Initialize crossbar and GPIO
OSCILLATOR_Init (); // Initialize oscillator
PCA2_Init (); // Initialize PCA0
PCA3_Init ();
while (1)
{
// Wait a little while
_nop_();
_nop_();
_nop_();
_nop_();
//PCA0CPH0 = 256 - (256 * 0.4);
TH0 = 254;
_nop_();
_nop_();
_nop_();
_nop_();

}

}

//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// OSCILLATOR_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function initializes the system clock to use the external crystal
// oscillator with a crystal of 22.1184 MHz.
//
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void)
{
int i; // Delay counter

OSCXCN = 0x67; // Start external oscillator with
// 22.1184MHz crystal

for (i=0; i < 256; i++) ; // Wait for oscillator to start

while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle

OSCICN = 0x88; // Select external oscillator as SYSCLK
// Source and enable missing clock
// detector
}

//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function configures the crossbar and GPIO ports.
//
// P0.0 digital push-pull PCA0 CEX0
//
//-----------------------------------------------------------------------------
void PORT_Init (void)
{

XBR0 = 0x25; // Route CEX0 to P0.0
XBR1 = 0x00;
XBR2 = 0x40; // Enable crossbar and weak pull-ups

PRT0CF |= 0xf1; // Set CEX0 (P0.0) to push-pull

}
//-----------------------------------------------------------------------------
// PCA3_Init
//-----------------------------------------------------------------------------
void PCA3_Init (void)
{
// Configure Timer 0 for 8-bit auto-reload mode, using SYSCLK as time base
TMOD &= 0xF0; // Clear all T0 control bits
TMOD |= 0x02; // 8-bit auto-reload timer
CKCON |= 0x08; // T0 uses SYSCLK
TH0 = 200; // Set up reload value
TL0 = 200; // Set up initial value

// configure PCA time base; overflow interrupt disabled
PCA0CN = 0x40; // Stop counter; clear all flags
PCA0MD = 0x04; // Use t0 as time base
// PCA0MD = 0x02; //Use SYSCLK/4 as time base
PCA0CPM3 = 0x43; // Module 0 = 8-bit PWM mode

PCA0L = 0x00; // Reset PCA Counter Value to 0x0000
PCA0H = 0x00;
// Configure initial PWM duty cycle = 50%
PCA0CPH3 = 256 - (256 * 0.4);

EIE1 |= 0x08; // Enable PCA interrupts
// Start PCA counter
CR = 1;
TR0=1;
}
//-----------------------------------------------------------------------------
// PCA2_Init
//-----------------------------------------------------------------------------
void PCA2_Init (void)
{
// Configure Timer 0 for 8-bit auto-reload mode, using SYSCLK as time base
TMOD &= 0xF0; // Clear all T0 control bits
TMOD |= 0x02; // 8-bit auto-reload timer
CKCON |= 0x08; // T0 uses SYSCLK
TH0 = 200; // Set up reload value
TL0 = 200; // Set up initial value

// configure PCA time base; overflow interrupt disabled
PCA0CN = 0x40; // Stop counter; clear all flags
PCA0MD = 0x04; // Use t0 as time base
// PCA0MD = 0x02; //Use SYSCLK/4 as time base
PCA0CPM2 = 0x43; // Module 0 = 8-bit PWM mode

PCA0L = 0x00; // Reset PCA Counter Value to 0x0000
PCA0H = 0x00;
// Configure initial PWM duty cycle = 50%
PCA0CPH2 = 256 - (256 * 0.4);

EIE1 |= 0x08; // Enable PCA interrupts
// Start PCA counter
CR = 1;
TR0=1;
}
//-----------------------------------------------------------------------------
// PCA0_ISR
//-----------------------------------------------------------------------------

void PCA0_ISR (void) interrupt 9
{

if (CCF2||CCF3) // If Module 0 caused the interrupt
{
CCF2 = 0;
CCF3=0; // Clear module 0 interrupt flag.
PCA0CPH2 =256 - (256 * 0.4);
PCA0CPH3 =256 - (256 * 0.4);
LED = ~LED; // Invert the LED pin
}

else // Interrupt was caused by other bits.
{
PCA0CN &= ~0x86; // Clear other interrupt flags for PCA
}
}

//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------

赞助商链接
相关文章:
STC12C5A60S2内部PWM应用
PWM 输出 CR=1; //启动 PCA 计数器 while(1) ...PCA 定时寄存器 低位 sfr CH = 0xF9; //PCA ...40; //模块 0 输出 占空因数为 75% void PWM_...
C8051F020的PCA配置小结
C8051F020的PCA配置小结_信息与通信_工程科技_专业资料...(PCA0CN.7) 置位时允许 PCA0 计数器/定时器...PCA0CPMn:PCA 捕捉/比较寄存器 PWM16n ECOMn ...
STC12C5A60S2单片机PWM应用
PCA/PWM 的时钟源是 SYSclk,不是定时器 0,PWM ...当 MAT0=1 时, 计数值与模块比较/捕获寄存器的值...Pulse Width modulation)是一种使用程序来控制波形...
PCA程序
(关于 STC 的 PCA 高速模式输入 40kHz 方波的程序.../* PWM 高速方波输出 7 6 5 4 3 2 1 0 //...C8051F040的PCA程序1 6页 免费 C8051F040的PCA程序...
STC单片机PCA模块示例
PCA 的捕获、高速输出、16bit 定时器PWM 实现 *...184Hz~46KHz * 返回值:无 */ void Set_PWM_...C8051f单片机的PCA模块介... 6页 免费 STC单片机(...
基于C8051F330单片机的可调波形输出实验的程序(三种波...
C8051F330程序定时器实例 2页 1财富值 c8051f330单片机...基于C8051F330单片机的可调波形输出实验的程序(三种波形...{ PCA0MD &= ~0x40; //关闭看门狗 OSCICN ...
C8051F5xx
PCA ) ,有 6 个捕捉/ 比较模块和 enhanced PWM ...VDD 监视器、看门狗定时器和时钟振荡器的 C8051F50...F = 200 kHz —— 95 ——μ A VDD = 2.1...
C8051F单片机PWM输出程序
C8051F单片机PWM输出程序_信息与通信_工程科技_专业资料。关键字:C8051F, PWM,...// PCA0MD PCA0MD } &= 0x40; = 0x01; //---...
PWM部分
系列单片机 PCA/PWM 应用 STC12C5A60S2 系列单片机集成了两路可编程计数器阵列(PCA)模块,可用于软件定时 器、外部脉冲的捕捉、高速输出以及脉宽调制(PWM)输出。 ...
STC5410AD内部PWM应用
{ //PWM 当 D/A 输出,PWM 口经过 RC 电路后 可以从示波器看到输出波形为:...由于只有一对 CH 和 CL 寄存器,所有模块共享 PCA 定时器 和频率。输出的...
更多相关标签: