main.c
程序代码如下:
//--------------------
//PWM测试程序
//--------------------
#include <p24Fxxxx.h>
_CONFIG3( WPFP_WPFP0 & SOSCSEL_IO & WUTSEL_LEG & WPDIS_WPDIS & WPCFG_WPCFGDIS & WPEND_WPENDMEM )
_CONFIG2( FNOSC_PRIPLL & POSCMOD_HS )//& PLL96MHZ_ON & PLLDIV_DIV5 )
//_CONFIG1( GCP_ON & JTAGEN_OFF & FWDTEN_OFF & ICS_PGx2 )
#if 1
/* INT is processor specific in length may vary in size */
typedef signed int INT;
typedef signed char INT8;
typedef signed short int INT16;
typedef signed long int INT32;
/* UINT is processor specific in length may vary in size */
typedef unsigned int UINT;
typedef unsigned char UINT8;
typedef unsigned short int UINT16;
/* 24-bit type only available on C18 */
typedef unsigned long int UINT32; /* other name for 32-bit integer */
/* MPLAB C Compiler for PIC18 does not support 64-bit integers */
#endif
void oc1_inint(UINT16 PR2_num,UINT16 OC1R_num,UINT16 OC1RS_num);
void oc1_inint(UINT16 PR2_num,UINT16 OC1R_num,UINT16 OC1RS_num)
{
TRISBbits.TRISB13=0;
RPOR6bits.RP13R =18;// OC1_IO; //RP15作为OC1输出
T2CON = 0;
TMR2 = 0;
//-----6,25kHZ----------
PR2 = PR2_num;//1060;
OC1R = OC1R_num;//530;
OC1RS = OC1RS_num;//530;
//----------------
OC1CON1 = 0x0006; //PWM
T2CON = (1 << 15)|(0 << 5)|(0 << 4); //启动时钟,1分频
IEC0 |= 0x0100; //允许T2,中断
}
//-------------------------
// main ()函数
//
//-------------------------
int main(void)
{
// Set up clock settings
CLKDIV = 0x0000;
OSCCON = 0x3302; // Enable secondary oscillator, use HS oscillator
TRISB = 0x0FFF; //设置LED为输出RB15-RB12
RPOR6bits.RP13R = 18; //RP15作为OC1输出
T2CON = (1 << 15)|(0 << 5)|(0 << 4); //启动时钟,1分频
oc1_inint(1060,530,530); //PWM 初始化,6,25kHZ
while(1)
{
;
}
return 0;
}
我的程序,pwm出来的方波是6.25kHZ(160us)
但是和datasheet上面的方法,无法算出来这个值;
外部晶振20M
T2CON = (1 << 15)|(0 << 5)|(0 << 4); //启动时钟,1分频
TCY=TCY/2=(1000/20M)/2=25ns;
pwm周期=(PR2+1)*TCY*(Timer2预分频值)
pwm周期=1061*25ns*1
pwm周期=26.5us
而实际是160us
请问是哪里的问题?
(原文件名:pwm.jpg)
以我的水准看 没什么问题
只是
TCY=TCY/2=(1000/20M)/2=25ns;??
跟下面的pdf不够清楚 我对不上来
一周热门 更多>