/***********************************
功能:PWM测试程序
MCU:dsPIC30F4011
TOOLS:MPLAB X IDE V3.15 + XC16 V1.25
震荡器:外部4M晶振
芯艺设计室 http://www.chipart.cn
2015-12-08
***********************************/
#include "xc.h"
#include <stdint.h>
#define XTFREQ 4000000 // xtal = 4Mhz;
#define PLLMODE 16 // PLLx16
#define FCY XTFREQ*PLLMODE/4 // Instruction Cycle Frequency
#include <libpic30.h>
#define LED_PORT_INIT TRISBbits.TRISB6=0
#define LED_ON LATBbits.LATB6=1
#define LED_OFF LATBbits.LATB6=0
#define LED_TOOGLE LATBbits.LATB6^=1
_FOSC(CSW_FSCM_OFF & XT_PLL16);
_FWDT(WDT_OFF);
#define DelayMs __delay_ms
//溢出频率10khz
void Timer2Init(void)
{
PR2 = 1600; // Timer2 周期寄存器
T2CONbits.TON = 1; // Timer2 启动计数
}
/*
void __attribute__((interrupt,no_auto_psv)) _T2Interrupt( void )
{
IFS0bits.T2IF = 0;
}
*/
//输出比较初始化
void OC2Init(void)
{
OC2CON=6; //PWM模式,TIMER2为时钟源
OC2RS=800;
}
int main( void )
{
LED_PORT_INIT;
OC2Init();
Timer2Init();
TRISFbits.TRISF5=1; //???????
while(1)
{
if(PORTFbits.RF5)
LED_ON;
else
LED_OFF;
}
return 0;
}
|
|