2016年8月6日 星期六

DIY - PIC24:通用非同步收發器 UART 收發程式(三十五)

DIY - PIC24:通用非同步收發器 UART 收發程式(三十五): 

Microchip PIC24FJ64GA008 支持兩個通用非同步收發器 UART 串口,分別為 UART1 UART2,每個 UART 都包括 UxTXUxRXUxRTSRequest To Send)和 UxCTSClear To Send)接口,最簡單的通用非同步收發器 UART 收發程式是使用訪問/輪詢(Polling)方式接收 UART 數據資料,而發送 UART 數據資料是由 MCU 作主動處理。

PIC24FJ64GA008 開發板的 UART LCD1602 顯示
Microchip PIC24FJ64GA008 通用非同步收發器 UART Header 程式
#include "Microchip\system\uart.h"
#include "Microchip\system\uartcom.h"

Microchip PIC24FJ64GA008 通用非同步收發器 UART I/O 定義
// I/O definitions for UART1
#define CTS1     _RD14               // Cleart To Send, input, HW handshake
#define RTS1     _RD15               // Request To Send, output, HW handshake
#define TRTS1    TRISDbits.TRISD15    // Tris control for RTS pin

// Timing and Baud Rate
#define BRATE    416                      // 9600 baud (BREGH=1)
#define U_ENABLE 0x8008             // enable the UART peripheral (BREGH=1)
#define U_TX         0x0400              // enable transmission


Microchip PIC24FJ64GA008 通用非同步收發器 UART 收發程式
// 1. Init the UART1 Serial Port
initUART1();  //

char hello[]=("\n>>Hello Bugworkshop PICFJ64GA008 Development Board \n");
putstringUART1(hello);
putcharUART1( '\r');

SetDDRamAddr(0x40);
while(1){
unsigned char s;

if (U1STAbits.URXDA == 1){
s=getcharUART1();
WriteDataXLCD(s);
delayms(100);
}
}

筆者使用 Terminal 程式來收發 RS-232 的數據資料,當電腦 Terminal 程式啓動後,選擇 ComPort = Com1Baudrate = 9600Data Bit = 8Parity = noneHandshaking = none,連接 RS-232 串口線,重置 PIC24FJ64GA008 開發板,電腦 Terminal 程式便會接收到訊息,然後在 Transmit 空白位置,按打鍵盤,PIC24FJ64GA008 開發板便會接收到訊息。

電腦 Terminal 程式接收到訊息
PIC24FJ64GA008 開發板接收到訊息
2016年 8月 6日 天氣報告
氣溫:28.5@ 21:50
相對濕度:百分之 80%
天氣:天色良好

沒有留言:

張貼留言