//----------------------------------------------------------------------------- // Note // 測定した電圧をシリアル通信で送信する。 // PICの周波数を32Mhzに設定 //----------------------------------------------------------------------------- #include #include #include // コンフィグレーションBIT1 設定 #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 = ON // Power-up Timer Enable (PWRT enabled) #pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input) #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 = OFF // Brown-out Reset Enable (Brown-out Reset disabled) #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) #pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is disabled) #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled) // コンフィグレーションBIT2 設定 #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 = HI // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), high trip point selected.) #pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming) #define _XTAL_FREQ 32000000 #define TX_UART_SIZE 3 //#define VOLTAGE_REFERENCE 5000 #define VOLTAGE_REFERENCE 4930 #define VOLTAGE_RESOLUTION 1024 //#define R1_OHM 10000 #define R1_OHM 9960 //#define R2_OHM 10000 #define R2_OHM 9960 volatile unsigned char uart_tx_msg[TX_UART_SIZE] = {0}; volatile unsigned char uart_tx_cnt = 0; void interrupt isr_ctrl( void ) { if( TXIF ) { if( uart_tx_cnt < TX_UART_SIZE ) { TXREG = uart_tx_msg[ uart_tx_cnt ]; uart_tx_cnt++; } else { TXIE = 0; } if( uart_tx_cnt >= TX_UART_SIZE ) { uart_tx_cnt = 0; TXIE = 0; } TXIF = 0; } } int main(int argc, char** argv) { unsigned short ad_data; long ad_volt,input_volt; short i,work1,work2,work3; OSCCON = 0b01110000 ; // 内部クロック8MHz ANSELA = 0b00010000 ; // AN3をアナログにし、他はデジタルI/Oに割当てる TRISA = 0b00011010 ; // RA1,3,4を入力にし、他は出力に割当てる PORTA = 0b00000000 ; // 出力ピンの初期化(全てLOWにする) ADCON1 = 0b10100000 ; // 右寄せ、Fosc/32,VDDをリファレンスにする ADCON0 = 0b00001101 ; // AN3を使用する __delay_us(5); // ch設定後、反映待ち TXSTA = 0x24; // 送信情報設定:非同期モード 8ビット・ノンパリティ RCSTA = 0x90; // 受信情報設定 BAUDCON = 0x08; // 16bit SPBRG = 0x40; // ボーレートを9600(高速モード)に設定 SPBRGH = 0x03; RXDTSEL = 0; TXCKSEL = 0; TXIE = 0; TXIF = 0; PEIE = 1; GIE = 1; for(;;) { // AD 変換 GO_nDONE = 1; // 変換開始 // アナログをデジタルに変換 while( GO_nDONE ) { // wait } ad_data = ADRESH; ad_data = (ad_data << 8) | ADRESL; // 計算 ad_volt = ((long)VOLTAGE_REFERENCE * (long)ad_data) / VOLTAGE_RESOLUTION; input_volt = ad_volt * ((long)R1_OHM + (long)R2_OHM) / (long)R2_OHM; // 位で分割(小数点2桁を有効) work1 = input_volt / 10; work3 = work1 % 10; work1 /= 10; work2 = work1 % 10; work1 /= 10; // 送信データ構築 uart_tx_msg[0] = 0x04; // 少数点位置の設定 uart_tx_msg[1] = 0x0f & work1; uart_tx_msg[2] = (work2<< 4) | work3; uart_tx_cnt = 0; TXIE = 1; // 送信開始 __delay_ms( 1000 ); } return (EXIT_SUCCESS); }