//****************************************************************************************** // Project : Wave Generater // Program Name : // Programer : // Create date : Version 1.0 H22.03.10 // History : // Laungage : SDCC C Language //****************************************************************************************** // (1)Description // //****************************************************************************************** #include #include #include int at 0x2007 __CONFIG = _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOD_OFF & _IESO_OFF & _FCMEN_OFF ; void INITCMP(void); void SETCMP(unsigned char); void WAIT01(void); int main(void) { //********************************** // 1.初期化処理 //********************************** // (1)オシレータの設定(内部8MHzにする) OSCCON = OSCCON | (1 << 6) | (1 << 5) | (1 << 4); // (2)ポートの設定1(アナログ/デジタル設定) // GP0(AN0)をアナログ入力, GP1(AN1)をアナログ入力, GP2(AN2)をデジタル, GP4(AN3)をデジタルにする ANSEL = ANSEL | (1 << 0) | (1 << 1) | (0 << 2) | (0 << 3); // (3)ポートの設定2(ディレクション設定) TRISIO = 0xFB; //0b11111011 // (4)コンパレータの設定 INITCMP(); //********************************** // 2.メインプログラム //********************************** while(1){ SETCMP(7); WAIT01(); SETCMP(9); WAIT01(); SETCMP(11); WAIT01(); SETCMP(13); WAIT01(); SETCMP(14); WAIT01(); SETCMP(14); WAIT01(); SETCMP(14); WAIT01(); SETCMP(13); WAIT01(); SETCMP(11); WAIT01(); SETCMP(9); WAIT01(); SETCMP(7); WAIT01(); SETCMP(5); WAIT01(); SETCMP(3); WAIT01(); SETCMP(1); WAIT01(); SETCMP(0); WAIT01(); SETCMP(0); WAIT01(); SETCMP(0); WAIT01(); SETCMP(1); WAIT01(); SETCMP(3); WAIT01(); SETCMP(5); WAIT01(); } } //************************************************************************************ // Function Name : INITCMP // Title : // Input : // Output : //************************************************************************************ void INITCMP(void) { // // (1)Initialized CMCON0 Register ( COMPARATOR CONTROL REGISTER 0 ) // CMCON0[7] Unimplemented: Read as ‘0’ // CMCON0[6] COUT: Comparator Output bit // When CINV = 0: // 1 = VIN+ > VIN- // 0 = VIN+ < VIN- // When CINV = 1: // 1 = VIN+ < VIN- // 0 = VIN+ > VIN- // CMCON0[5] Unimplemented: Read as ‘0’ // CMCON0[4] CINV: Comparator Output Inversion bit // 1 = Output inverted // 0 = Output not inverted // CMCON0[3] CIS: Comparator Input Switch bit // When CM<2:0> = 110 or 101: // 1 = VIN- connects to CIN+ // 0 = VIN- connects to CIN- // CMCON0[2:0]: Comparator Mode bits // See the Comparator modes and CM<2:0> bit settings. CMCON0 = 0x4D; //0b01001101 // // (2)Initialized VRCON Register ( VOLTAGE REFERENCE CONTROL REGISTER ) // VRCON[7] VREN: CVREF Enable bit // 1 = CVREF circuit powered on // 0 = CVREF circuit powered down, no IDD drain and CVREF = VSS // VRCON[6] Unimplemented: Read as ‘0’ // VRCON[5] VRR: CVREF Range Selection bit // 1 = Low range // 0 = High range // VRCON[4] Unimplemented: Read as ‘0’ // VRCON[3-0] VR<3:0>: CVREF Value Selection 0 <= VR <3:0> <= 15 // When VRR = 1: CVREF = (VR<3:0>/24) * VDD // When VRR = 0: CVREF = VDD/4 + (VR<3:0>/32) * VDD VRCON = 0x80; //0b10000000 } //************************************************************************************ // Function Name : SETCMP // Title : // Input : W-reg // Output : //************************************************************************************ void SETCMP(unsigned char in_data ) { // // (1)Initialized VRCON Register ( VOLTAGE REFERENCE CONTROL REGISTER ) // VRCON[7] VREN: CVREF Enable bit // 1 = CVREF circuit powered on // 0 = CVREF circuit powered down, no IDD drain and CVREF = VSS // VRCON[6] Unimplemented: Read as ‘0’ // VRCON[5] VRR: CVREF Range Selection bit // 1 = Low range // 0 = High range // VRCON[4] Unimplemented: Read as ‘0’ // VRCON[3-0] VR<3:0>: CVREF Value Selection 0 <= VR <3:0> <= 15 // When VRR = 1: CVREF = (VR<3:0>/24) * VDD // When VRR = 0: CVREF = VDD/4 + (VR<3:0>/32) * VDD in_data = in_data | 0x80 ; //0b10000000 VRCON = in_data; } //************************************************************************************ // Function Name : WAIT01 // Title : Wait 0.001[S] // Input : // Output : //************************************************************************************ void WAIT01(void) { // // 1.Set Register // (1)T1CON Register // T1CON[7] T1GINV: Timer1 Gate Invert bit(1) // 1 = Timer1 gate is inverted // 0 = Timer1 gate is not inverted // T1CON[6] TMR1GE: Timer1 Gate Enable bit(2) // If TMR1ON = 0: This bit is ignored. // If TMR1ON = 1: // 1 = Timer1 is on if Timer1 gate is not active // 0 = Timer1 is on // T1CON[5-4] T1CKPS<1:0>: Timer1 Input Clock Prescale Select bits // 11 = 1:8 Prescale Value // 10 = 1:4 Prescale Value // 01 = 1:2 Prescale Value // 00 = 1:1 Prescale Value // T1CON[3] T1OSCEN: LP Oscillator Enable Control bit // If INTOSC without CLKOUT oscillator is active: // 1 = LP oscillator is enabled for Timer1 clock // 0 = LP oscillator is off // Else: This bit is ignored. // T1CON[2] T1SYNC: Timer1 External Clock Input Synchronization Control bit // TMR1CS = 1: // 1 = Do not synchronize external clock input // 0 = Synchronize external clock input // TMR1CS = 0: // This bit is ignored. Timer1 uses the internal clock. // T1CON[1] TMR1CS: Timer1 Clock Source Select bit // 1 = External clock from T1CKI pin (on the rising edge) // 0 = Internal clock (FOSC/4) // T1CON[0] TMR1ON: Timer1 On bit // 1 = Enables Timer1 // 0 = Stops Timer1 // T1CON =0x05; //0b00000101 // (2)Set TMR1 Value // 8000000Hz(Int_OSC)/4/8(Prescale)/10(0.1[S]) => 0d25000(0x61A8) // 0xFFFF(-)0xF424 => 0d40535(0x9E57) // 8000000Hz(Int_OSC)/4/1(Prescale)/1000(0.001[S]) => 0d2000(0x7D0) // 0xFFFF(-)0x07D0 => 0d63535(0xF82F) // TMR1H = 0xF8; TMR1L = 0x2F; // 2.Timer PIR1 = PIR1 & 0xfe; while((PIR1 & 0x01) == 0); }