#include "p33FJ32MC204.h"
#include "dsp.h"
#include <xc.h>
/*****************Config bit settings****************/
// FBS
#pragma config BWRP = WRPROTECT_OFF // Boot Segment Write Protect (Boot Segment may be written)
#pragma config BSS = NO_FLASH // Boot Segment Program Flash Code Protection (No Boot program Flash segment)
// FGS
#pragma config GWRP = OFF // General Code Segment Write Protect (User program memory is not write-protected)
#pragma config GSS = OFF // General Segment Code Protection (User program memory is not code-protected)
// FOSCSEL
#pragma config FNOSC = FRC // Oscillator Mode (Internal Fast RC (FRC) with divide by N)
#pragma config IESO = ON // Internal External Switch Over Mode (Start-up device with FRC, then automatically switch to user-selected oscillator source when ready)
// FOSC
#pragma config POSCMD = XT // Primary Oscillator Source (Primary Oscillator Disabled)
#pragma config OSCIOFNC = OFF // OSC2 Pin Function (OSC2 pin has clock out function)
#pragma config IOL1WAY = OFF // Peripheral Pin Select Configuration (Allow Only One Re-configuration)
#pragma config FCKSM = CSECME // 必须要使得时钟使能 Clock Switching and Monitor (Both Clock Switching and Fail-Safe Clock Monitor are disabled)
// FWDT
#pragma config FWDTEN = OFF // Watchdog Timer Enable (Watchdog timer always enabled)
// FPOR
#pragma config FPWRT = PWR128 // POR Timer Value (128ms)
#pragma config ALTI2C = OFF // Alternate I2C pins (I2C mapped to SDA1/SCL1 pins)
#pragma config LPOL = ON // Motor Control PWM Low Side Polarity bit (PWM module low side output pins have active-high output polarity)
#pragma config HPOL = ON // Motor Control PWM High Side Polarity bit (PWM module high side output pins have active-high output polarity)
#pragma config PWMPIN = ON // Motor Control PWM Module Pin Mode bit (PWM module pins controlled by PORT register at device Reset)
// FICD
#pragma config ICS = PGD3 // Comm Channel Select (Communicate on PGC1/EMUC1 and PGD1/EMUD1)
#pragma config JTAGEN = OFF // JTAG Port Enable (JTAG is Disabled)
void delay1(unsigned int n);
unsigned int j,k;
int main(void)
{
// //Perform a clock switch to 40MIPS (80Mhz)
// PLLFBD = 38; //M=PLLFBD+2 = 40
// CLKDIVbits.PLLPOST = 0; //N2=2(PLLPOST+1) = 2
// CLKDIVbits.PLLPRE = 0; //N1=PLLPRE+2 = 2
//
// //unlock OSCCON register
// __builtin_write_OSCCONH(0x03);//切换到带PLL的主振荡器模式 经过测试 应该切换成功了
// __builtin_write_OSCCONL(0x01);
// //wait for clock to stabilize
// while(OSCCONbits.COSC != 0b011);
// //wait for PLL to lock
// while(OSCCONbits.LOCK != 1) {};
// //clock switch finished
TRISB = 0X03FF;
TRISC = 0X003F;
//循环流水灯
while(1)
{
//RB10-RB15
PORTBbits.RB15 =1;
delay1(1000);
PORTBbits.RB15 =0;
delay1(100);
PORTBbits.RB14 =1;
delay1(1000);
PORTBbits.RB14 =0;
delay1(100);
PORTBbits.RB13 =1;
delay1(1000);
PORTBbits.RB13 =0;
delay1(100);
PORTBbits.RB12 =1;
delay1(1000);
PORTBbits.RB12 =0;
delay1(100);
PORTBbits.RB11 =1;
delay1(1000);
PORTBbits.RB11 =0;
delay1(100);
PORTBbits.RB10 =1;
delay1(1000);
//RC6,RC7
PORTBbits.RB10 =0;
delay1(100);
PORTCbits.RC7 =1;
delay1(1000);
PORTCbits.RC7 =0;
delay1(100);
PORTCbits.RC6 =1;
delay1(1000);
PORTCbits.RC6 =0;
}
}
void delay1(unsigned int n)
{
for(j=0;j<n;j++)
for (k=335;k>0;k--)
{ Nop();
Nop();
Nop();
Nop();
}
}
MPLAB软件程序-简单的流水灯程序(附上源代码和注释)
于 2023-07-10 21:35:04 首次发布