//----------------------------------------------------------------------------- // Note // PCから受信したデータをPCへ返す // PICの周波数を32Mhzに設定 //----------------------------------------------------------------------------- #include #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 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 = 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) unsigned char rx_data = 0; bool rx_flg = false; // シリアル通信の受信処理 // シリアルデータを受信する時に発生 void interrupt rx_uart0( void ) { if( RCIF ) { rx_data = RCREG; // レジスタからデータ格納 rx_flg = true; RCIF = 0; // 割込受信フラグをリセット } } int main(int argc, char** argv) { OSCCON = 0b01110000 ; // 内部クロック8MHz ANSELA = 0b00000000 ; // すべてデジタルI/Oに割当てる TRISA = 0b00101000 ; // RA3,5を入力とする PORTA = 0b00000000 ; // ピン状態初期化 RXDTSEL = 1; // RA5を受信とする TXCKSEL = 1; // RA4を送信とする TXSTA = 0x24; // 非同期/8ビット/パリティなし RCSTA = 0x90; // シリアルポート使用/連続受信あり BAUDCON = 0x08; // 16bitモード // 16Bitモードで設定 SPBRG = 0x40; // ボーレートを9600に設定(下位8bit) SPBRGH = 0x03; // ボーレートを9600に設定(上位8bit) RCIF = 0; // シリアル割込みの受信フラグを初期化 RCIE = 1; // シリアル割込みの受信を有効 PEIE = 1; // 周辺装置割込みを有効 GIE = 1; // 全割込み処理を許可 for(;;) { // データを受信したか? if( rx_flg ) { // 送信可能か? if( TXIF ) { TXREG = rx_data; // 送信レジスタへデータをセットする rx_flg = false; } } } return (EXIT_SUCCESS); }