PIC16F886でRA1に接続したLEDを点滅させる。
まずは、単純に__delayで1秒間隔で点滅させる。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
// PIC16F886 Configuration Bit Settings // 'C' source line config statements #include <xc.h> // #pragma config statements should precede project file includes. // Use project enums instead of #define for ON and OFF. // CONFIG1 #pragma config FOSC = INTRC_NOCLKOUT// Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN) #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register) #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled) #pragma config MCLRE = OFF // RE3/MCLR pin function select bit (RE3/MCLR pin function is digital input, MCLR internally tied to VDD) #pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled) #pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled) #pragma config BOREN = OFF // Brown Out Reset Selection bits (BOR disabled) #pragma config IESO = OFF // Internal External Switchover bit (Internal/External Switchover mode is disabled) #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled) #pragma config LVP = OFF // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming) // CONFIG2 #pragma config BOR4V = BOR40V // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V) #pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off) #define _XTAL_FREQ 8000000 int main(void) { OSCCONbits.IRCF = 0b111;//8Mhz TRISAbits.TRISA1 = 0;//Out ANSELbits.ANS1 = 0;//Digital PORTAbits.RA1 = 1; while(1){ PORTAbits.RA1 = ~PORTAbits.RA1; __delay_ms(1000); } } |
AN0(RA0)で半固定抵抗からの電圧の変化を読み取り、このAD変換値に応じて待ち時間を変え、RA1のLEDの点滅速度を変化させる。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
// PIC16F886 Configuration Bit Settings // 'C' source line config statements #include <xc.h> // #pragma config statements should precede project file includes. // Use project enums instead of #define for ON and OFF. // CONFIG1 #pragma config FOSC = INTRC_NOCLKOUT// Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN) #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register) #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled) #pragma config MCLRE = OFF // RE3/MCLR pin function select bit (RE3/MCLR pin function is digital input, MCLR internally tied to VDD) #pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled) #pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled) #pragma config BOREN = OFF // Brown Out Reset Selection bits (BOR disabled) #pragma config IESO = OFF // Internal External Switchover bit (Internal/External Switchover mode is disabled) #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled) #pragma config LVP = OFF // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming) // CONFIG2 #pragma config BOR4V = BOR40V // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V) #pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off) #define _XTAL_FREQ 8000000 void delay(unsigned char i){ unsigned char j; for(j=0; j<i; j++){ __delay_us(5000); } } int main(void) { OSCCONbits.IRCF = 0b111;//8Mhz TRISAbits.TRISA0 = 1;//Input TRISAbits.TRISA1 = 0;//Output ANSELbits.ANS0 = 1;//Analog ANSELbits.ANS1 = 0;//Digital PORTAbits.RA1 = 1; //ADC Settings ADCON0bits.ADCS = 0b00;//A/D Conversion Clock is FOSC/2 ADCON0bits.CHS = 0b0000;//Analog Channel is AN0 ADCON0bits.ADON = 1;//ADC Enabled ADCON1bits.ADFM = 1;//Right justified while(1){ __delay_us(1); ADCON0bits.GO_DONE = 1; while(ADCON0bits.GO_DONE == 1); delay(ADRESL); //delay(100); PORTAbits.RA1 = ~PORTAbits.RA1; } } |