给开发板接了个LED和开关,简单写了个测试程序:
/*
* 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会发光。全程使用到了基本的输入输出的基本的寄存器的操作。