📄 f300pwm8_1.c
字号:
/* ************************************************************************** * * Module : PWM of C8051F300 * File Name : F300PWM8_1.c * Designed by : James Chan * Coded by : James Chan * Tested by : * Creation Date : 16 Dec 2005 * Descriptions : PWM demo. * PWM function setting: 1. 8-Bit Pulse Width Modulator * 2. 1 channel output * 3. Base frequency is 1 kHz * * Copyright (c) 2005 MAXTEK Corporation Taiwan. * All rights reserved. * ************************************************************************** *//* ************************************************************************** * * Revision History * ================ * * Rev Date Who Description * ------------------------------------------------------------------------ * ************************************************************************** *//* ************************************************************************** * Include Files ************************************************************************** */#include <c8051F300.h> // SFR declarations/* ************************************************************************** * Gobal Constants ************************************************************************** */#define PWM_START 0x01 // starting value for the PWM high timesbit PWM_OUT = P0^0; // define PWM output port pinsbit SW = P0^3; // button switch input pin/* ************************************************************************** * Variables ************************************************************************** */unsigned int PWM = PWM_START; // Number of PCA clocks for waveform // to be high // duty cycle = PWM / 256 // Note: this is a 8-bit value////*************************************************************************//// Main Code////*************************************************************************////------------------------------------------------------------------------------------// Function PROTOTYPES//------------------------------------------------------------------------------------void Reset_Source_Init(void){ // disable watchdog timer PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer enable)}void Oscillator_Init (void){ OSCICN = 0x06; // Use internal oscillator to 3.125MHz(SYSCLK / 8)}void Port_IO_Init (void){ XBR1 = 0x40; // Enable crossbar and weak pull-ups XBR2 = 0x40; // Enable CEX0 at P0.0 P0MDOUT = 0x01; // P0.0as push-pull output}void PCA_Init (void){ // configure the PCA PCA0CN = 0x40; // enable PCA counter PCA0MD |= 0x00; // disable CF interrupt // PCA time base = SYSCLK / 12 // CCM0 //PCA0CPL0 = 0x8F; PCA0CPH0 = 0x80; PCA0CPM0 = 0x42; // CCM0 in 8-Bit Pulse Width Modulator Mode
// CCM0 //PCA0CPL0 = 0x8F; PCA0CPH1 = 0x80; PCA0CPM1 = 0x42; // CCM0 in 8-Bit Pulse Width Modulator Mode}void Device_Init (void){ Reset_Source_Init(); Oscillator_Init(); Port_IO_Init(); PCA_Init();}//------------------------------------------------------------------------------------// MAIN Routine//------------------------------------------------------------------------------------void main (void){ Device_Init(); while (1) { // Press the button switch to change to Hi pulse width if(SW == 0) { while(SW == 0); //PCA0CPL0 = 0x00; PCA0CPH0 = PWM++;
PCA0CPH1 = PWM++; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -