//----------------------------------------------------------------------------- // 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 SONIC_TRIG RA5 #define SONIC_ECHO RA4 #define IO_OFF 0 #define IO_ON 1 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) { long echo_time; long dist; short work1,work2,work3; OSCCON = 0b01110000 ; // 内部クロック8MHz ANSELA = 0b00000000 ; // デジタルI/Oに割当てる TRISA = 0b00011000 ; // RA3,4を入力にし、他は出力に割当てる PORTA = 0b00000000 ; // 出力ピンの初期化(全てLOWにする) T1CON = 0b00110000 ; // FOSC/4を選択 分周比1/8 T1GCON = 0b11010000 ; // ゲート有効、Hiでカウント、単一パルス、T1Gピン TXSTA = 0x24; // 送信情報設定:非同期モード 8ビット・ノンパリティ RCSTA = 0x90; // 受信情報設定 BAUDCON = 0x08; // 16bit SPBRG = 0x40; // ボーレートを9600(高速モード)に設定 SPBRGH = 0x03; RXDTSEL = 0; TXCKSEL = 0; TMR1ON = 1; // タイマー1開始 TXIE = 0; TXIF = 0; PEIE = 1; GIE = 1; for(;;) { // タイマ1のカウンタ準備 TMR1 = 0; // カウンタの初期化 T1GGO = 1; // 単一パルスを使用する // トリガ送信 SONIC_TRIG = IO_ON; __delay_us( 10 ); SONIC_TRIG = IO_OFF; // エコー信号のON待ち while( SONIC_ECHO == IO_OFF ) { // 処理なし } // エコー信号のOFF待ち while( SONIC_ECHO == IO_ON ) { // 処理なし } // 超音波の往復時間を取得 echo_time = TMR1; // 往復時間から片道の時間にする echo_time /= 2; // パルス時間から距離(cm)に変更 dist = echo_time * 34 / 1000; if( dist < 400 ) { // 位で分割 work1 = dist % 10; dist /= 10; work2 = dist % 10; dist /= 10; work3 = dist; // 送信データ構築 uart_tx_msg[0] = 0x00; // 少数点位置の設定 uart_tx_msg[1] = 0x0f & work3; uart_tx_msg[2] = (work2 << 4) | work1; } else { // 送信データ構築 uart_tx_msg[0] = 0x00; // 少数点位置の設定 uart_tx_msg[1] = 0x04; uart_tx_msg[2] = 0x00; } uart_tx_cnt = 0; TXIE = 1; // 送信開始 __delay_ms( 1000 ); } return (EXIT_SUCCESS); }