/*File: FanAlm.c * Author: S-Tatsuno * Created on 2021/09/20, 13:07 */ // PIC12F1840 Configuration Bit Settings // 'C' source line config statements // CONFIG1 #pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin) #pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled) #pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled) #pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR) #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled) #pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled) #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled) #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) #pragma config IESO = ON // Internal/External Switchover (Internal/External Switchover mode is enabled) #pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled) // CONFIG2 #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) #pragma config PLLEN = ON // PLL Enable (4x PLL enabled) #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset) #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.) #pragma config LVP = ON // Low-Voltage Programming Enable (Low-voltage programming enabled) #include #include #include #include typedef unsigned char BYTE; typedef unsigned int WORD; volatile BYTE Count1m; // 1msec counter volatile BYTE Count10m; //10msec counter BYTE Pwm_frwq; /* T1Gに入れる周波数をつくる。1msec単位 */ WORD T1data; /* FAN PWM信号1周期時間 clk=32M/8 */ float T1data_LPF; /* 平均化 */ void init_timer1(void); void init_timer2(void); void PIN_MANAGER_Initialize(void); void __interrupt() Isr(void); // LEVEL /* ****************************************************************** */ void init_timer1(void) { T1CON = 0x35; /* 00:Fosc/4,11:1/8,0,0:nonSync,0,1:T1enabl 32MHz/4/8=1MHz */ T1GCON= 0xF8; /* 1:T1Genable,1:ActivH,1:Togle,1:single,1:T1Ggo 000 */ /* Singleモードは計測後停止,T1Ggo=1でエッジ待ち */ TMR1 = 0; PIR1bits.TMR1GIF = 0; PIE1bits.TMR1GIE = 1; /* T1G割込有効 */ TMR1ON = 1 ; // } /* ------------------------------------------------------------------ */ void init_timer2(void) { IRCF0 = 0; IRCF1 = 1; IRCF2 = 1; IRCF3 = 1; // INTOSC = 8MHz SPLLEN = 1; // PLL x4 = 32MHz Configで定義しているのでなくても動く /* Intrrupt TIMER2 */ T2CON = 0b00111101; // Post 1/8,Pre_1/4 32M/4/8/4=250kHz PR2 = 249; // Timer2 250k/250=1000Hz PIR1bits.TMR2IF = 0; // Timer2 PIE1bits.TMR2IE = 1; // Timer2 TMR2ON = 1 ; // PEIE = 1 ; // GIE = 1 ; // } /* ------------------------------------------------------------------ */ void PIN_MANAGER_Initialize(void) { LATA = 0x00; TRISA = 0x1B; // 0:output RA2 RA5 0001 1011 ANSELA = 0x00; // 0:digital 1:analog WPUA = 0x1B; // Weak pllup 1:enable OPTION_REGbits.nWPUEN = 0; APFCON = 0x00; // ペリフェラルpinを変える時使用。変えないので0 bit3 RA4 is T1G1 APFCONbits.T1GSEL = 0; // RA4=T1G } /* ------------------------------------------------------------------ */ void __interrupt() Isr(void) //One LEVEL Only { // T1G割込でパルス1周期の時間を計測する if(PIR1bits.TMR1GIF) // Timer1G T1G割込 { PIR1bits.TMR1GIF = 0; // Clear TMR1GIF T1data = (WORD)TMR1; // Tiner1 DATA 読込 T1data_LPF = ((T1data_LPF * 0.99)+ (float)T1data*0.01 ); // LPF LATA2 ^=1; // 周波数計測 TMR1 = 0; // Timer Counter Clear T1GGO = 1; // T1Ggo=1でエッジ待ち READY状態にする } /* END PIR1bits.TMR1GIF */ /* ------ 1msec interrupt ------------ */ if(PIR1bits.TMR2IF) // Timer2 { PIR1bits.TMR2IF = 0; // Reset Interrupt /* FAN パルス信号模擬 */ if(++Pwm_frwq <= 4) { LATA5 = 0; // 1,2,3,4:4msecLOW出力 }else if(Pwm_frwq <=8 ) { LATA5 = 1; // 5,6,7,8,0:5msecHigh出力 9msec周期=111Hz }else { Pwm_frwq = 0; } if(++Count1m >= 10) // 10msec { Count1m = 0; if(++Count10m >= 10) // 100msec { Count10m = 0; } } } /* END PIR1bits.TMR2IF */ } /* ------------------------------------------------------------------ */ void main(void) { PIN_MANAGER_Initialize(); init_timer2(); init_timer1(); Pwm_frwq = 0; for(;;) { } return; }