MPLAB软件程序-简单的流水灯程序(附上源代码和注释)

#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();
        }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清漠233

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值