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

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程序
(关于 STC 的 PCA 高速模式输入 40kHz 方波的程序.../* PWM 高速方波输出 7 6 5 4 3 2 1 0 //...C8051F040的PCA程序1 6页 免费 C8051F040的PCA程序...
基于STC单片机实现多位软件PWM
基于STC单片机实现多位软件PWM_电子/电路_工程科技_专业资料。基于 STC 单片机...(PCA)模块,可用于软件定时器、外部脉冲捕捉、高速输出以及 PWM 输出四 种工作...
c8051f410 PCA十六位脉宽调节程序
c8051f410 PCA十六位脉宽调节程序_工学_高等教育_教育专区。#include <C8051F410...C8051f410定时器PCA设... 4页 免费 用PCA实现16位PWM 18页 免费 pca算法...
基于C8051F020单片机的PID控制系统设计论文
基于C8051F020单片机的PID控制系统设计论文_信息与通信...模块完成了人机交互, 使用 WT588D-16P 语音模块实现...PWM 输出信号的频率取决于 PCA0 计数器/定时器的...
基于51单片机的物体追踪和遥控车的实现
PCA 模块的时钟源可以选择是来自于系统时钟的几分频还是来 源于定时器,系统中采用了 1/12 的系统时钟源频率。因为 PWM 是 8 位的,所以 PWM 输出的波形频率是...
8EC8051F单片机的直流无刷电机转速控制系统
8EC8051F单片机的直流无刷电机转速控制系统_电子/...(PCA) 5 个通用 16 位计数器/定时器 专用的...通过脉宽调制 PWM 来控制电动机电枢电压可以实现调速...
C8051F020实验1
定时器 (6) 具有 5 个捕捉/比较模块的可编程计数器/定时器阵列 2 (7) ...PCA0 测量方波的周期 PCA1 输出 8 位 PWM UART0 方式 0 实现同步串行移位...
c8051介绍
同步输出,用于产生无抖动波形 4、两个模拟比较器 5..., 有 5 个捕捉/ 比较模块//可用于产生 PWM ? ...实时时钟方式(用定时器 3 或 PCA) ; 10、供电...
C8051F020 学习笔记——基本概念
C8051F020 学习笔记——基本概念 C8051F020 是完全...定时器、高速输出、频率输出、8 位脉冲宽度调制器和...PCA 捕捉/比较模块的 I/O 和外部时钟输入可以通过...
寻迹小车报告
比较模块的可编程计数/定时器阵列、看 门狗定时器...C8051F020 单片机支 持双时钟,其工作电压范围为 ...PCA0CPM1---电机 2PWM 输出,工作在 8 位定时器...
更多相关标签: