The mcu used is dsPIC33FJ128GP802/804
Code: Select all
program Uart_test
const
RP0 = 0
RP1 = 1
RP2 = 2
RP3 = 3
RP4 = 4
RP5 = 5
RP6 = 6
RP7 = 7
const Uart1TxPins as word[4] = (RP0,RP2,RP4,RP6)
const Uart1RxPins as word[4] = (RP1,RP3,RP5,RP7)
const Uart1Baud as word[4] = (9600,1200,19200,4800)
dim Uart1rxBuf as byte[256]
dim Uart1rxHead, Uart1rxTail as byte
dim hwUartChannel as word 'ch0=RP0,RP1 ch1=RP2,RP3 ch2=RP4,RP5 ch3=RP6,RP7
sub procedure Uart1rxInt org IVT_ADDR_U1RXINTERRUPT
Uart1rxBuf[Uart1rxHead] = Uart1_Read() 'put char in buf
Uart1rxHead = Uart1rxHead + 1 'increment head pointer
IFS0.U1RXIF = 0 'clear U1RXIF
end sub
sub procedure Uart1_Stop()
IEC0.U1RXIE = 0 'disable U1RX interrupt
U1MODE.UARTEN = 0 'disable uart1 module
end sub
main:
ADPCFG = $FFFF 'In order to use PORTB pins for digital I/O, the corresponding bits in
'the ADPCFG register must be set to ‘1’, even if A/D module is turned off.
'goal is to use uart1 module to support 4 uart channels
'by remapping uart1 module to different pins in a roundrobin scheme
while true
LATB = LATB OR $00FF 'set latches to uart idle level
TRISB = (TRISB AND $FF55) OR $0055 'make RP0,RP2,RP4,RP6 output, RP1,RP3,RP5,RP7 input
for hwUartChannel = 0 to 3
Unlock_IOLOCK()
PPS_Mapping(Uart1RxPins[hwUartChannel],_INPUT,_U1RX)
PPS_Mapping(Uart1TxPins[hwUartChannel],_OUTPUT,_U1TX)
Lock_IOLOCK()
Uart1rxHead = 0
Uart1rxTail = 0
Uart1_Init(Uart1Baud[hwUartChannel])
Uart_Set_Active(@UART1_Read, @UART1_Write, @UART1_Data_Ready, @UART1_Tx_Idle)
IPC2.U1RXIP_0 = 0 'U1RX interrupt priority 4
IPC2.U1RXIP_1 = 0
IPC2.U1RXIP_2 = 1
IEC0.U1RXIE = 1 'enable U1RX interrupt
'Send command
Uart1_Write(0x41 + hwUartChannel) 'send 'A', 'B', 'C' or 'D'
'Wait for response
while Uart1rxTail = Uart1rxHead
wend
'Get response and echo
Uart1_write(Uart1rxBuf[Uart1rxTail]) 'echo received bytes
Uart1rxTail = Uart1rxTail + 1
'Wait for transmission complete
while Uart1_Tx_Idle = 0
wend
Uart1_Stop() 'disable uart1 module
Delay_ms(2000) 'wait 2 seconds
next hwUartChannel
wend
end.
one myself.
Questions:
Do I take the right steps regarding unlock and lock functions?
Do I need to remap pins to _NULL before switching to next channel?
(I want a stopped channel to have its output pin at idle level)
Why is there no _NULL function for input pins?
(If I remap _U1RX it may be _U1RX is mapped to multiple pins)
regards peter