// PIC12 F1572 主时钟 32MHz, RA2->
WM3_Out, RA4->T1G+边沿中断Input, RA5开关输出
// Configuration bits: selected in the GUI
// CONFIG1
#pragma config BOREN = ON
#pragma config PWRTE = OFF
#pragma config FOSC = INTOSC
#pragma config MCLRE = ON
#pragma config CP = OFF
#pragma config WDTE = OFF
#pragma config CLKOUTEN = OFF
// CONFIG2
#pragma config WRT = OFF
#pragma config LPBOREN = OFF
#pragma config LVP = OFF
#pragma config STVREN = ON
#pragma config BORV = LO
#pragma config PLLEN = OFF
#include <xc.h>
#include <stdint.h>
#include <stdbool.h>
#define PWM_PR (0xbb7f) // 决定总周期数值
#define PWM_PS (1) // (7)表示1:128预分频。(1 => 1:2)
#define DC_MAX (0xffff)
/*
Main application
*/
void main(void)
{
// initialize the device
// SCS INTOSC;IRCF 8MHz_HF = 0x72; 16Mhz = 0x78; 32Mhz = 0xF0;
OSCCON = 0xF0;
// HFIOFR disabled; HFIOFS not0.5percent_acc; LFIOFR disabled;
OSCSTAT = 0x00;
// Set the secondary oscillator
LATA = 0x20; // RA5 空闲输出为高电平。
TRISA = 0x1B; // RA5为输出,RA4为输入,RA2为Pwm3输出。
ANSELA = 0x03; // 关闭RA0,RA1的数字输入。
WPUA = 0x1B; // 上拉RA4(RA3),RA1,RA0。
OPTION_REGbits.nWPUEN = 0x00;
APFCON = 0x00;
// =======================
// Intialize PWM3 - RA2 OUT
// =======================
PWM3PH = 0; // 相位寄存器(3PHH、3PHL)
PWM3DC = 0x3e80; // 占空比(输出的宽度)(主周期x预分频数值)
PWM3PR = PWM_PR; // 周期寄存器(xPRH、xPRL)
PWM3CLKCON = 0; // Fosc, No prescalar
PWM3CLKCONbits.PS = PWM_PS; // PWM period prescalar
PWM3CON = PWM3CON & 0xF3; // standard mode
PWM3CONbits.PWM3OE = 1; // turn on output enable.
PWM3CONbits.PWM3POL = 0; // (0=负脉冲)(1=正脉冲)
PWM3LDCONbits.PWM3LD = 1; // Load OF, PH, DC, and PR buffers
PWMEN = 0x04; // enable all PWM's as once = 0x07.
while (1)
{
// Add your application code
 
ORTA=0xDF;
 
ORTA=0xFF;
 
ORTA=0xDF;
 
ORTA=0xFF;
// IO口 翻转 重复50次
}
}
/**
End of File
*/