📄 dg128pwm.c
字号:
/*-------------------------------------------------------
PWM Module
Donald
Apr 16, 2007
-------------------------------------------------------*/
#include <hidef.h> /* common defines and macros */
#include <mc9s12dg128.h> /* derivative information */
#include "DG128PWM.h"
void Pwm16Init(void) {
// PWM67
PWME_PWME6 = 0;
PWME_PWME7 = 0;
PWMCTL = 0X80; // Use PWM6 PWM7
PWMPRCLK = 0X30; // B = bus / 8 = (8M?) / 8 = 1MHz
PWMSCLB = 5; // SB = B / (2 * PWMSCLB) = 100 kHz,( pulse is 10us )
PWMCLK_PCLK7 = 1; // 16bit PWM67 clock source is SB
PWMPOL = 0X80;
PWMDTY6 = 0X00;
PWMDTY7 = 0X98; // PWM duty is 1520us, and S3010 is 0 degree
PWMPER6 = 0X07; // T is 20000us (20ms)
PWMPER7 = 0XD0;
PWME_PWME7 = 1;
}
///////////////////////////////////////////////////////////
// Turn a given degree
// nWise: 0 - CW, 1 - CCW
// nDegree: degree from 0 to 60.
void ActionDegree(int nWise, int nDegree){
wordnbyte nTmp;
if (nWise == 0){
nTmp.wordW = 152 + nDegree;
}
else {
nTmp.wordW = 152 - nDegree;
}
PWMDTY2 = nTmp.wordWS.byteH;
PWMDTY3 = nTmp.wordWS.byteL;
}
void PwmInit(void) {
// DDRP = 0XFF;
PWME_PWME0 = 0; // Disable PWM Ch0
PWMPRCLK = 0x06; // A = bus / 64 = 8M / 64 = 125kHz
PWMSCLA = 10; // Clock SA = 1000Hz
PWMCLK_PCLK0 = 1; // Ch0 clock source is SA
PWMPOL |= 0x01;
PWMDTY0 = 50;
PWMPER0 = 255; // T is 40ms
PWME_PWME0 = 1; // Enable PWM Ch0
PWME_PWME1 = 0; // Disable PWM Ch1
// PWMPRCLK = 0x06; // A = bus / 64 = 8M / 64 = 125kHz
// PWMSCLA = 10; // Clock SA = 1000Hz
PWMCLK_PCLK1 = 1; // Ch1 clock source is SA
PWMPOL |= 0x02;
PWMDTY1 = 50;
PWMPER1 = 255; // T is 40ms
PWME_PWME1 = 1; // Enable PWM Ch1
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -