PIC10F322でLチカの第2弾 せっかくだからPWMも使ってみる。
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 |
#include <xc.h> // CONFIG #pragma config FOSC = INTOSC // Oscillator Selection bits (INTOSC oscillator: CLKIN function disabled) #pragma config BOREN = OFF // Brown-out Reset Enable (Brown-out Reset disabled) #pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled) #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled) #pragma config MCLRE = OFF // MCLR Pin Function Select bit (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 LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming) #pragma config LPBOR = OFF // Brown-out Reset Selection bits (BOR disabled) #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.) #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) #define _XTAL_FREQ 16000000 void interrupt intr(void) { INTCONbits.TMR0IF = 0;//Timer0 Overflow Interrupt Flag Clear TMR0 = 222; RA2 = ~RA2; } int main(void) { OSCCONbits.IRCF = 0b111;//16MHz ANSELA = 0b000;//for Digital I/O TRISAbits.TRISA0 = 0;// TRISAbits.TRISA1 = 0;// TRISAbits.TRISA2 = 0;// //Timer0 OPTION_REGbits.PSA = 1;//Prescaler is disabled //OPTION_REGbits.PS = 111;//1:256 OPTION_REGbits.T0CS = 0;//TMR0 Clock Source is Internal instruction cycle clock (FOSC/4) INTCONbits.GIE = 1;//Global Interrupt Enabled INTCONbits.PEIE = 1;//Peripheral Interrupt Enabled INTCONbits.TMR0IE = 1;//Timer0 Overflow Interrupt Enabled INTCONbits.TMR0IF = 0;//Timer0 Overflow Interrupt Flag Clear TMR0 = 222; //PWM1 PR2 = 104; T2CONbits.TOUTPS = 0b0000;//Timer2 Output Postscaler = 1 T2CONbits.T2CKPS = 0b00;//Timer2 Clock Prescale is 1 T2CONbits.TMR2ON = 1;//Timer2 is on PWM1DCH = 0b00110100; PWM1DCL = 0b10; PWM1CONbits.PWM1EN = 1;//PWM Module Enabled PWM1CONbits.PWM1OE = 1;//Output to PWMx pin is enabled PIR1bits.TMR2IF = 0; while(1){ RA1 = 1; __delay_ms(300); RA1 = 0; __delay_ms(300); } } |
RA0はPWM1を使ってデューティー比50%の約38kHzのパルス。 RA1は__