//********************************************************************** /*   <簡易ADロガー(シリアル出力)> */ //********************************************************************** //■マクロ定義 sbit LED at RA5_bit; #define LED_ON 1 #define LED_OFF 0 // sbit SW at RA3_bit; #define SW_ON 0 #define SW_OFF 1 // #define BYTE unsigned short #define WORD unsigned int #define DWORD unsigned long //********************************************************************** //■関数宣言 extern void main(); extern void interrupt(); extern void init_ccp_compare(); extern WORD adc_read_ex(BYTE channel); extern void adc_init_ex(); //********************************************************************** //■メイン関数です。 void main() { static short cnt; // OSCCON = 0b01110000; //clock=32MHz TRISA = 0b00011001; APFCON = 0b00100000; // Soft_UART_Init(&PORTA, 4, 1, 115200, 0); adc_init_ex(); // for (cnt = 0; cnt < 10; cnt++) { LED = LED_ON; Delay_ms(100); LED = LED_OFF; Delay_ms(100); } // init_ccp_compare(); //割り込みを許可します。 INTCON.PEIE = 1; INTCON.GIE = 1; // while (1) { if (SW == SW_ON) { LED = LED_ON; } else { LED = LED_OFF; } } } //********************************************************************** //■割り込み関数です。 double ad; char *buf = "00000\r\n"; // void interrupt() { PIR1.CCP1IF = 0; // // ad = adc_read_ex(0); ad = ADC_Get_Sample(0); //ad *= 4.8828125; //性能改善のため電圧値への換算はパソコン側で行います。 WordToStr(ad, buf); if (SW == SW_ON) { Soft_UART_Write(buf[1]); Soft_UART_Write(buf[2]); Soft_UART_Write(buf[3]); Soft_UART_Write(buf[4]); Soft_UART_Write('\r'); Soft_UART_Write('\n'); } } //********************************************************************** //■CCP初期化関数です。(1msecのインターバルタイマ発生用) void init_ccp_compare() { // CCPの設定 PIE1.CCP1IE = 1; PIR1.CCP1IF = 0; CCP1CON.CCP1M3 = 1; CCP1CON.CCP1M2 = 0; CCP1CON.CCP1M1 = 1; CCP1CON.CCP1M0 = 1; CCPR1L = 0x40; // 1msec...(1÷32000000)*4*0x1F40 CCPR1H = 0x1F; // 2msec...(1÷32000000)*4*0x3E80 // TIMER1の設定 PIE1.TMR1IE = 0; PIR1.TMR1IF = 0; TMR1L = 0; TMR1H = 0; T1CON.T1CKPS0 = 0; T1CON.T1CKPS1 = 0; T1CON.TMR1ON = 1; } //********************************************************************** //■ADCを初期化する関数です。 void adc_init_ex() { ADC_Init(); // ANSELA.ANSA4 = 0; ANSELA.ANSA2 = 0; ANSELA.ANSA1 = 0; ANSELA.ANSA0 = 1; ADCON1.ADFM = 1; ADCON1.ADCS2 = 1; ADCON1.ADCS1 = 1; ADCON1.ADCS0 = 0; ADCON1.ADPREF1 = 0; ADCON1.ADPREF0 = 0; } //********************************************************************** //■ADCから読み込む関数です。 WORD adc_read_ex(BYTE channel) { static int ad; // ADCON0.GO = 0; ADCON0.ADON = 1; ADCON0.CHS4 = channel.B4; ADCON0.CHS3 = channel.B3; ADCON0.CHS2 = channel.B2; ADCON0.CHS1 = channel.B1; ADCON0.CHS0 = channel.B0; ADCON0.GO = 1; while (ADCON0.GO == 1) { } ad = ADRESH & 0b00000011; ad <<= 8; ad = ad | ADRESL; return (ad); } //********************************************************************** void switch_on_check() { while (Button(&PORTA, 3, 1, 0) == 0) ; while (Button(&PORTA, 3, 1, 1) == 0) ; } //**********************************************************************