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

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
//-----------------------------------------------------------------------------

相关文章:
PCA捕获模块
上位机输出的 PWM 调制信号的频率为 1 kHz,而且系...该单片机有 2 路可编程计数器阵列(PCA) /PWM,...c8051的PCA模块使用定时... 3页 免费喜欢此文档的...
AVR16定时器T1相频PWM的使用程序
c8051的PCA模块使用定时... 3页 免费 MSP430程序库之定时器TA... 9页 免费 AVR使用范例--定时器实现... 6页 免费 定时器pwm 2页 免费 定时器和单个按键...
STC12C5A60S2输出PWM波形示例程序-IO及PCA
STC12C5A60S2输出PWM波形示例程序-IO及PCA_电子/电路_工程科技_专业资料。STC...这里使用定时器最大值从 0 开始计数一直到 65535 溢出 TL0=0x00; EA=1; /...
STC单片机PCA模块示例
路 PCA 的捕获、高速输出、16bit 定时器PWM ...184Hz~46KHz * 返回值:无 */ void Set_PWM_...C8051f单片机的PCA模块介... 6页 免费 STC单片机(...
C8051F单片机PWM输出程序
C8051F单片机PWM输出程序_信息与通信_工程科技_专业资料。关键字:C8051F, PWM,...// PCA0MD PCA0MD } &= 0x40; = 0x01; //---...
STC12C2052AD单片机的PCA捕获模式
定时器, 它可与 2 个 16 位捕获/比较模块相连...上位机输出的 PWM 调制信号的频率为 1 kHz 左右。...C8051f单片机的PCA模块介... 6页 免费 基于MSP430...
STC12C5A60S2单片机PWM应用
PCA/PWM 的时钟源是 SYSclk,不是定时器 0,PWM ...当 MAT0=1 时, 计数值与模块比较/捕获寄存器的值...Pulse Width modulation)是一种使用程序来控制波形...
用STCC单片机内置PWM模块产生可调频率的PWM
;PCA 模块 1 PWM 寄存器。 ;PCA_PWMn: 2 ; ...= 1:将 CEXn 设置PWM 输出。 ;ECCFn = 1...; 0x49 ; 0x4d ; 0x42 1 0 16 位软件定时器...
用STCC单片机内置PWM模块产生可调频率的PWM
;PCA 模块 0 的捕捉/比较寄存器 ;PCA 模块 1 的...;PWMn = 1:将 CEXn 设置PWM 输出。 ;ECC...; 1 0 0 1 16 位软件定时器。 ; 1 0 0 1...
基于STC单片机实现多位软件PWM
内部集成 MAX810 专 用复位电路,2 路 PWM,8 路高速 10 位 A/D 转换、双...(PCA)模块,可用于软件定时器、外部脉冲捕捉、高速输出以及 PWM 输出四 种工作...
更多相关标签:
c8051f020定时器程序 | c8051f 定时器 | c8051f410定时器 | c8051f020定时器 | c8051f340定时器 | c8051f410定时器1s | c8051f020 pwm程序 | c8051f340 pwm |