PIC24F16KL402开发板使用笔记二: 基本输入输出实验

给开发板接了个LED和开关,简单写了个测试程序:

  1. /*
  2. * File:   key_led.c
  3. * Author: Administrator
  4. *
  5. * Created on 2015?3?23?, ??6:40
  6. */
  7. #include "xc.h"
  8. _FOSCSEL
  9. (
  10.      FNOSC_FRC &  // Oscillator Select (8MHz FRC with Postscaler (FRCDIV))
  11.    //  SOSCSRC_ANA &   // SOSC Source Type (Analog Mode for use with crystal)
  12.    //  LPRCSEL_HP &    // LPRC Power and Accuracy (High Power/High Accuracy)
  13.      IESO_ON         // Internal External Switch Over bit (Internal External Switchover mode enabled (Two-speed Start-up enabled))
  14. )
  15. _FOSC
  16. (
  17.     // POSCMD_HS &   // Primary Oscillator Mode (Primary oscillator disabled)
  18.      OSCIOFNC_ON &   // CLKO Enable Configuration bit (CLKO output signal enabled)
  19.      POSCFREQ_MS &   // Primary Oscillator Frequency Range Configuration bits (Primary oscillator/external clock frequency between 100kHz to 8MHz)
  20.      SOSCSEL_SOSCHP &// SOSC Power Selection Configuration bits (Secondary Oscillator configured for high-power operation)
  21.      FCKSM_CSECME    // Clock Switching and Monitor Selection (Clock Switching and Fail-safe Clock Monitor Enabled)
  22. )
  23. void delay(unsigned int uiCnt)
  24. {
  25.      int i,j;
  26.      for(i=0;i<uiCnt;i++)
  27.          for(j=0;j<110;j++);
  28. }
  29. int main(void) {
  30.      //ANSA = 0b0000000000000000;
  31.      //ANSB = 0b0000000000000000;
  32.      ANSAbits.ANSA1 = 0;  //选通为数字输入
  33.      TRISBbits.TRISB3 = 0; //IO的方向为输出
  34.      TRISAbits.TRISA0 = 0; 
  35.     TRISAbits.TRISA1 = 1; //IO的方向设为输入
  36.      TRISBbits.TRISB14 = 1;
  37.      
  38.     while(1)
  39.      {
  40.          if(PORTAbits.RA1) //如果按下,则拉高电平.
  41.          {
  42.             // LATAbits.LATA0 = 1;
  43.              LATBbits.LATB3 = 1;
  44.              delay(1000);
  45.          }
  46.          else
  47.              //LATAbits.LATA0 = 0;
  48.              LATBbits.LATB3 = 0;
  49.          delay(500);
  50.         LATAbits.LATA0 ^= 1; //心跳DEBUG灯,开发板自带的.
  51.          
  52.      }
  53.      
  54.     return 0;
  55. }



/*
* File:   key_led.c
* Author: Administrator
*
* Created on 2015?3?23?, ??6:40
*/

#include "xc.h"
_FOSCSEL
(
    FNOSC_FRC &  // Oscillator Select (8MHz FRC with Postscaler (FRCDIV))
  //  SOSCSRC_ANA &   // SOSC Source Type (Analog Mode for use with crystal)
  //  LPRCSEL_HP &    // LPRC Power and Accuracy (High Power/High Accuracy)
    IESO_ON         // Internal External Switch Over bit (Internal External Switchover mode enabled (Two-speed Start-up enabled))
)
_FOSC
(
   // POSCMD_HS &   // Primary Oscillator Mode (Primary oscillator disabled)
    OSCIOFNC_ON &   // CLKO Enable Configuration bit (CLKO output signal enabled)
    POSCFREQ_MS &   // Primary Oscillator Frequency Range Configuration bits (Primary oscillator/external clock frequency between 100kHz to 8MHz)
    SOSCSEL_SOSCHP &// SOSC Power Selection Configuration bits (Secondary Oscillator configured for high-power operation)
    FCKSM_CSECME    // Clock Switching and Monitor Selection (Clock Switching and Fail-safe Clock Monitor Enabled)
)
void delay(unsigned int uiCnt)
{
    int i,j;
    for(i=0;i<uiCnt;i++)
        for(j=0;j<110;j++);
}
int main(void) {
    //ANSA = 0b0000000000000000;
    //ANSB = 0b0000000000000000;
    ANSAbits.ANSA1 = 0;  //选通为数字输入
    TRISBbits.TRISB3 = 0; //IO的方向为输出
    TRISAbits.TRISA0 = 0; 
    TRISAbits.TRISA1 = 1; //IO的方向设为输入
    TRISBbits.TRISB14 = 1;
    
    while(1)
    {
        if(PORTAbits.RA1) //如果按下,则拉高电平.
        {
           // LATAbits.LATA0 = 1;
            LATBbits.LATB3 = 1;
            delay(1000);
        }
        else
            //LATAbits.LATA0 = 0;
            LATBbits.LATB3 = 0;
        delay(500);
       LATAbits.LATA0 ^= 1; //心跳DEBUG灯,开发板自带的.
        
    }
    
    return 0;
}
当接在PORTA1上的按钮,按下时,接到PORTB3上的LED会发光。全程使用到了基本的输入输出的基本的寄存器的操作。

粽子糖果 发表于10-26 10:40 浏览65535次
分享到:

已有0条评论

暂时还没有回复哟,快来抢沙发吧

添加一条新评论

只有登录用户才能评论,请先登录注册哦!

话题作者

粽子糖果
粽子糖果(总统)
金币:41631个|学分:51991个
立即注册
畅学电子网,带你进入电子开发学习世界
专业电子工程技术学习交流社区,加入畅学一起充电加油吧!

x

畅学电子网订阅号