PIC16F1827でシリアル通信(EUSART)をテストした。
とはいえ、MCCを使えば簡単。
まずはMCCで内蔵オシレーター32MHzで動くように設定。
次にMCCのEUSARTの設定。
確か、Enable EUSART InterruptsをONした以外はデフォルトのままだったと思う。
最後にMCCでRA1ピンをLED用に出力に割り当て。
ソースコード(書き換えた部分のみ抜粋)は次の通り。
Macで入力した文字をそのまま返すだけ。通信の間、LEDが光る。
GlobalとPeripheralの割り込みをEnableにするのを忘れない事!(動かなくてしばらく悩んだ…)
void main(void) { volatile uint8_t rxData; // initialize the device SYSTEM_Initialize(); // When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits // Use the following macros to: // Enable the Global Interrupts INTERRUPT_GlobalInterruptEnable(); // Enable the Peripheral Interrupts INTERRUPT_PeripheralInterruptEnable(); // Disable the Global Interrupts //INTERRUPT_GlobalInterruptDisable(); // Disable the Peripheral Interrupts //INTERRUPT_PeripheralInterruptDisable(); LATAbits.LATA1 = HIGH; while (1) { // Add your application code if (EUSART_is_rx_ready()) { LATAbits.LATA1 = LOW; rxData = EUSART_Read(); if (EUSART_is_tx_ready()) { EUSART_Write(rxData); } LATAbits.LATA1 = HIGH; } } }
回路図はコレ。
USBシリアルはFTDIのチップの載った古いモジュールで、Macにつないでターミナルからcuコマンドで接続。sudo cu -s 9600 -l /dev/cu.usbserial-A10042Fd