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

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

相关文章:
PWM和PCA
模块与之相连,由高字节(PCA0H)和低字节(PCA0PCA...定时器溢出 CF 中断的屏蔽位 0: 禁止 CF 中断 1...它是把每一脉冲宽度均相等的脉冲列作为 PWM 波形,...
STC12C5A60S2单片机PWM应用
PCA/PWM 的时钟源是 SYSclk,不是定时器 0,PWM ...当 MAT0=1 时, 计数值与模块比较/捕获寄存器的值...Pulse Width modulation)是一种使用程序来控制波形...
PWM部分
系列单片机 PCA/PWM 应用 STC12C5A60S2 系列单片机集成了两路可编程计数器阵列(PCA)模块,可用于软件定时 器、外部脉冲的捕捉、高速输出以及脉宽调制(PWM)输出。 ...
STC单片机PCA模块示例
PCA 的捕获、高速输出、16bit 定时器PWM 实现 *...184Hz~46KHz * 返回值:无 */ void Set_PWM_...C8051f单片机的PCA模块介... 6页 免费 STC单片机(...
C8051F320八位PWM输出
C8051F320八位PWM输出_信息与通信_工程科技_专业...PCA 模块 8 位 PWM 输出 DEMO //PWM 输出脚为 ...{ PCA0CN = 0x40; PCA0CPM0 = 0x46; PCA0...
基于C8051F120的全数字单相逆变器
基于 C8051F120 的 SPWM 波形产生及软件编程 ......通过片内 A/D 转换模块实现输出电压的采样, 经过...比如定时器、PWM 电路、可编程计数 器阵列(PCA)等...
C8051F系列单片机的SPWM算法全分析
详细介绍了通过C8051单片机的可编程计数器列阵PCA实现SPWM控制波形的原理和方法,...位计数器/定时器和 6 个 16 位捕捉/比较模块, 可以输出 6 路 PWM 波形。...
关于STC15F2K60S2的脉冲捕获应用
改变定时 器 0 的溢出率,可以实现可调频率的 PWM ...工作在 PCA 高速输出模式,PCA 计数器的值与模块的...是一种使用程序来控制波形占空比、 周期、相位波形的...
用STC单片机的PCA功能产生PWM_脉冲
SST 单片机带有 5 路 PCA 模块,PCA 功能比普通的...在 PWM 模式,输出的频率决定于 PCA 定时器的源。...(最大) 5.9KHz 3.9KHz 11.8KHz 15.5Hz 0....
c8051f410 PCA十六位脉宽调节程序
c8051f410 PCA十六位脉宽调节程序_工学_高等教育_教育专区。#include <C8051F410...C8051f410定时器PCA设... 4页 免费 用PCA实现16位PWM 18页 免费 pca算法...
更多相关标签: