Difference between revisions of "PIC32MX: RS232"
Andrew Long (talk | contribs) |
Andrew Long (talk | contribs) |
||
Line 118: | Line 118: | ||
return 0; |
return 0; |
||
} |
} |
||
Compile and Program this Hex file to the PIC. |
|||
Open Hyperterminal with the following information: |
|||
*Bits per second: 9600 |
|||
*Data bits: 8 |
|||
*Parity: None |
|||
*Stop bits: 1 |
|||
*Flow Control: Hardware |
|||
If you press reset on the PIC32 board, you should see the start up message. |
Revision as of 15:27, 26 August 2009
RS232 is a method of real time communication between two devices such as PIC to PIC or PIC to PC.
More information about RS232 can be found here.
The PIC32 has 2 sets of pins UART1 and UART2. The pins are:
- UxCTS - clear to send (input to PIC)
- UxRTS - ready to send (output from PIC)
- UxRX - PIC receive
- UxTX - PIC transmit
PIC to PC
To communicate PIC to PC, you need to have a COMM port or a USB to RS232 converter. You can read about the TTL-232R cable (USB to RS232 converter) here.
The TTL-232R cable has six wires, color coded and in this sequence on the SIP connector:
BLACK GROUND Connect to PIC ground. BROWN CTS# Unused. "Clear to send" (flow control). RED Vcc +5V from PC. !! DO NOT CONNECT TO PIC +5 !! ORANGE Tx "PC Transmit." Data from PC, to PIC Rx (pin 26). YELLOW Rx "PC Receive." Data from PIC Tx (pin 25), to PC. GREEN RTS# Unused. "Request to send" (flow control).
Note that the TTL-232R cable gives you access to the PC's +5 supply (Red wire), which is limited in current (probably to 500mA) by the USB port which is designed to protect from short circuits. If you wish, you can probably power your PIC from the PC, using USB as a power source. If you do that, get rid of any other +5 source. Don't connect two +5 sources at once. In contrast, be sure that you do connect the PIC's ground to the PC's ground (Black wire).
Make the following connections for PIC to PC communication (not using CTS or RTS):
- PC Transmit -- PIC Receive
- PC Receive -- PIC Transmit
Basic Sample Code
This code details how to echo typing in a terminal such as Hyper Terminal. The PIC reads the pressed key from the PC and then transmits the key back to the PC via the TTL-232R cable. The key will be displayed on the terminal. If the key 'c' is pressed, LED1 will turn on (turns off LED2 if on). If a different key is pressed, LED2 will turn on (turns off LED1 if on).
Include the standard header files
#include "Compiler.h" #include <plib.h> #include "HardwareProfile.h"
Define the system frequency and the desired baudrate.
#define SYS_FREQ (80000000L) #define DESIRED_BAUDRATE (9600) // The desired BaudRate
Start Main function
int main(void) {
Define local variables for the peripheral bus clock and data.
int pbClk; unsigned char data;
Configure the system - this function returns the peripheral bus clock
pbClk = SYSTEMConfigPerformance(SYS_FREQ);
Initialize LEDs
mInitAllLEDs();
Setup Configuration 1 for OpenUARTx (most of these are default and not needed). Other constants can be found in uart.h in the peripheral include of C32.
- Module Enable
- Work in IDLE mode
- Communication through usual pins
- Disable wake-up
- Loop back disabled
- Input to Capture module from ICx pin
- No parity 8 bit
- 1 stop bit
- IRDA encoder and decoder disabled
- CTS and RTS pins are disabled
- UxRX idle state is '1'
- 16x baud clock - normal speed
#define config1 UART_EN | UART_IDLE_CON | UART_RX_TX | UART_DIS_WAKE | UART_DIS_LOOPBACK | UART_DIS_ABAUD | UART_NO_PAR_8BIT | UART_1STOPBIT | UART_IRDA_DIS | UART_DIS_BCLK_CTS_RTS| UART_NORMAL_RX | UART_BRGH_SIXTEEN
Setup Configuration 2 for OpenUARTx
- IrDA encoded UxTX idle state is '0'
- Enable UxRX pin
- Enable UxTX pin
- Interrupt on transfer of every character to TSR
- Interrupt on every char received
- Disable 9-bit address detect
- Rx Buffer Over run status bit clear
#define config2 UART_TX_PIN_LOW | UART_RX_ENABLE | UART_TX_ENABLE | UART_INT_TX | UART_INT_RX_CHAR | UART_ADR_DETECT_DIS | UART_RX_OVERRUN_CLEAR
Open UART2 with config1 and config2. The third input is the value to get the generated BAUD value (not exact to the desired).
OpenUART2( config1, config2, pbClk/16/DESIRED_BAUDRATE-1); // calculate actual BAUD generate value.
Display start up information. The function putsUARTx is used to display strings.
putsUART2("*** UART Simple Application Example ***\r\n"); putsUART2("*** Type some characters and observe echo and LED1 toggle ***\r\n");
Begin while loop
while(1) {
Wait for data from the PC
while(!DataRdyUART2()); /* Wait for data in the UARTRx. */
When data is available read the data
data = (char)ReadUART2(); /* Read data from Rx. */
Wait until transmitter is free
while(BusyUART2()); /* Wait till the UART transmitter is free. */
Transmit (write) data to PC. The function putcUARTx writes 1 character to the terminal.
putcUART2(data); /* Write data into Tx. */
Test to see if data read / wrote is the letter c
if(data == 'c') { mLED_1_On(); mLED_2_Off(); } else { mLED_2_On(); mLED_1_Off(); }
End while loop and main function
}; return 0; }
Compile and Program this Hex file to the PIC. Open Hyperterminal with the following information:
- Bits per second: 9600
- Data bits: 8
- Parity: None
- Stop bits: 1
- Flow Control: Hardware
If you press reset on the PIC32 board, you should see the start up message.