PIC24FJ256GB206 / 64pin package / UART1 TX example code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define USE_AND_OR /* To enable AND_OR mask setting */ | |
#include "include/gpio.h" | |
#include "include/board.h" | |
#include <PIC24F_periph_features.h> // from XC16 comfiler : LEEBS 2014_1209 | |
#include <PIC24F_plib.h> // from XC16 comfiler : LEEBS 2014_1209 | |
void Initial_UART_1(void) | |
{ | |
#if defined(PIC24FJ256GB206) | |
//********************** Assign UART 1 signals onto pins using PPS ************* | |
mPORTFOutputConfig(RF_5); | |
mPORTFInputConfig(RF_4); | |
ANSF = 0x0000; | |
iPPSInput(IN_FN_PPS_U1RX, IN_PIN_PPS_RP10); //Assing U1RX to pin RP10 | |
iPPSOutput(OUT_PIN_PPS_RP17, OUT_FN_PPS_U1TX); //Assing U1TX to pin RP17 | |
// Output for TX : SCL2/RP17/PMA8/CN18/RF5 | |
// Input for RX : SDA2/RP10/PMA9/CN17/RF4 | |
CloseUART1(); //dsiable UART if enabled previously | |
//********************** UART 1 Configuration ********************************** | |
// 9600 baudrate / Low baud rate / 8 bit transmission/reception | |
// No parity bit / 1 stop bit | |
/*Enable UART intrrupts*/ | |
//ConfigIntUART1(UART_RX_INT_EN |UART_RX_INT_PR6 | UART_TX_INT_EN |UART_TX_INT_PR6); | |
ConfigIntUART1(UART_RX_INT_EN |UART_RX_INT_PR1 | UART_TX_INT_EN |UART_TX_INT_PR2); | |
OpenUART1(UART_EN, UART_TX_ENABLE, 25); //configure UART and enable it | |
IFS0bits.U1TXIF = 1; // UART1 TX interrupt Enable. | |
#endif | |
} | |
void UART1PutChar(uint8_t data) | |
{ | |
while(BusyUART1()) // 1 is busy | |
; // wait for TX finish | |
U1TXREG = data;// WriteUART1(character); | |
while(!(U1STA && 0x0100)) // TRMT: Transmit Shift Register Empty bit (read-only) | |
; | |
EnableIntU1TX; | |
} | |
uint8_t UART_rx; | |
void __attribute__ ((interrupt, no_auto_psv)) _U1RXInterrupt(void) { | |
UART_rx = U1RXREG; | |
IFS0bits.U1RXIF = 0; | |
EnableIntU1RX; | |
} | |
void __attribute__ ((interrupt, no_auto_psv)) _U1TXInterrupt(void) { | |
IFS0bits.U1TXIF = 0; | |
} | |
void UART1PrintString( uint8_t *str ) | |
{ | |
uint8_t c; | |
while( (c = *str++) ) | |
UART1PutChar(c); | |
} | |
void main(void) | |
{ | |
Initial_UART_1(); | |
while(1) | |
{ | |
// Tx send same string for test only. | |
UART1PrintString("Hello\r\n"); | |
MY_delay_1ms(100); | |
} | |
} | |
// 8MHz clock ( 4 MIPS ) case... | |
void MY_delay_1ms(uint16_t delay) | |
{ | |
uint32_t temp, i; | |
temp = delay*312; | |
for(i=0; i<temp; i++) | |
; | |
} | |