1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
| /*********************************************************************
*
* PIC32MX UART Interrupt Example
*
*********************************************************************
* FileName: uart_interrupt.c
*
* Dependencies: plib.h
*
* Processor: PIC32
*
* Complier: MPLAB C32
* MPLAB IDE
* Company: Microchip Technology Inc.
*
* Software License Agreement
*
* The software supplied herewith by Microchip Technology Incorporated
* (the “Company�) for its PIC32 Microcontroller is intended
* and supplied to you, the Company’s customer, for use solely and
* exclusively on Microchip PIC32 Microcontroller products.
* The software is owned by the Company and/or its supplier, and is
* protected under applicable copyright laws. All rights are reserved.
* Any use in violation of the foregoing restrictions may subject the
* user to criminal sanctions under applicable laws, as well as to
* civil liability for the breach of the terms and conditions of this
* license.
*
* THIS SOFTWARE IS PROVIDED IN AN “AS IS� CONDITION. NO WARRANTIES,
* WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
* TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
* IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
* CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
**********************************************************************
* $Id: uart_interrupt.c 9390 2008-06-16 23:43:04Z rajbhartin $
**********************************************************************
* The purpose of this example code is to demonstrate the PIC32MX
* peripheral library macros and functions supporting the UART
* module and its various features.
*
* Platform: Explorer-16 with PIC32MX PIM
* RS-232 Cable
* A Terminal program for Windows - HyperTerminal
*
* Features demonstrated:
* - UART configuration and usage
* - Triggering UART interrupts
*
* Description:
* When the Explorer-16 is connected to a PC with an RS-232 cable,
* the device will echo what the user types into the terminal
* program and blink the left-most LED on the Explorer.
*
* Notes:
* - PIC32MX 2xx PIMS are unconnected to the Explorer-16 LEDs and
* DB9 connector. They must be soldered to the test points on top of
* the PIM for proper functionality. The README file contains a
* list of the connections that need to be made.
********************************************************************/
#include <plib.h>
#define __32MX575F256H__
#if defined (__32MX360F512L__) || (__32MX460F512L__) || (__32MX795F512L__) || (__32MX430F064L__) || (__32MX450F256L__) ||
(__32MX470F512L__) || (__32MX575F256H__)
// Configuration Bit settings
// SYSCLK = 80 MHz (8MHz Crystal / FPLLIDIV * FPLLMUL / FPLLODIV)
// PBCLK = 80 MHz (SYSCLK / FPBDIV)
// Primary Osc w/PLL (XT+,HS+,EC+PLL)
// WDT OFF
// Other options are don't care
#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
#pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_1
#define SYS_FREQ (80000000L)
#pragma config ICESEL = ICS_PGx2
#elif defined (__32MX220F032D__) || (__32MX250F128D__)
// Configuration Bit settings
// SYSCLK = 48 MHz (8MHz Crystal / FPLLIDIV * FPLLMUL / FPLLODIV)
// PBCLK = 48 MHz (SYSCLK / FPBDIV)
// Primary Osc w/PLL (XT+,HS+,EC+PLL)
// WDT OFF
// Other options are don't care
#pragma config FPLLMUL = MUL_24, FPLLIDIV = DIV_2, FPLLODIV = DIV_2, FWDTEN = OFF
#pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_1
#define SYS_FREQ (48000000L)
#endif
#define GetPeripheralClock() (SYS_FREQ/(1 << OSCCONbits.PBDIV))
#define GetInstructionClock() (SYS_FREQ)
#if defined (__32MX430F064L__) || (__32MX450F256L__) || (__32MX470F512L__) || (__32MX575F256H__)
#define UART_MODULE_ID UART1 // PIM is connected to Explorer through UART1 module
#else
#define UART_MODULE_ID UART2 // PIM is connected to Explorer through UART2 module
#endif
#define DESIRED_BAUDRATE (9600) //The desired BaudRate
void WriteString(const char *string);
int main(void)
{
#if defined (__32MX220F032D__) || defined (__32MX250F128D__)
PPSInput(2,U2RX,RPB5); // Assign RPB5 as input pin for U2RX
PPSOutput(4,RPB0,U2TX); // Set RPB0 pin as output for U2TX
#elif defined (__32MX430F064L__) || (__32MX450F256L__) || (__32MX470F512L__)
PPSInput(2,U1RX,RPF4); // Assign RPF4 as input pin for U1RX
PPSOutput(2,RPF5,U1TX); // Set RPF5 pin as output for U1TX
#endif
// Configure the device for maximum performance but do not change the PBDIV
// Given the options, this function will change the flash wait states, RAM
// wait state and enable prefetch cache but will not change the PBDIV.
// The PBDIV value is already set via the pragma FPBDIV option above.
SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
// Explorer-16 LEDs are on lower 8-bits of PORTA and to use all LEDs, JTAG port must be disabled.
mJTAGPortEnable(DEBUG_JTAGPORT_OFF);
mPORTGClearBits(BIT_7); // Turn off RA7 on startup.
mPORTGSetPinsDigitalOut(BIT_7); // Make RA7 as output.
// Configure UART module, set buad rate, turn on UART, etc.
UARTConfigure(UART_MODULE_ID, UART_ENABLE_PINS_TX_RX_ONLY);
UARTSetFifoMode(UART_MODULE_ID, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY);
UARTSetLineControl(UART_MODULE_ID, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
UARTSetDataRate(UART_MODULE_ID, GetPeripheralClock(), DESIRED_BAUDRATE);
UARTEnable(UART_MODULE_ID, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));
// Configure UART RX Interrupt
INTEnable(INT_SOURCE_UART_RX(UART_MODULE_ID), INT_ENABLED);
INTSetVectorPriority(INT_VECTOR_UART(UART_MODULE_ID), INT_PRIORITY_LEVEL_2);
INTSetVectorSubPriority(INT_VECTOR_UART(UART_MODULE_ID), INT_SUB_PRIORITY_LEVEL_0);
// Enable multi-vector interrupts
INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
INTEnableInterrupts();
WriteString("*** UART Interrupt-driven Application Example ***\r\n");
WriteString("*** Type some characters and observe echo and RA7 LED toggle ***\r\n");
// Let interrupt handler do the work
while (1);
}
// Helper functions
void WriteString(const char *string)
{
while(*string != '\0')
{
while(!UARTTransmitterIsReady(UART_MODULE_ID))
;
UARTSendDataByte(UART_MODULE_ID, *string);
string++;
while(!UARTTransmissionHasCompleted(UART_MODULE_ID))
;
}
}
void PutCharacter(const char character)
{
while(!UARTTransmitterIsReady(UART_MODULE_ID))
;
UARTSendDataByte(UART_MODULE_ID, character);
while(!UARTTransmissionHasCompleted(UART_MODULE_ID))
;
}
// UART 1 interrupt handler, set at priority level 2
#if defined (__32MX430F064L__) || (__32MX450F256L__) || (__32MX470F512L__) || (__32MX575F256H__)
void __ISR(_UART_1_VECTOR, ipl2) IntUart1Handler(void)
{
// Is this an RX interrupt?
if(INTGetFlag(INT_SOURCE_UART_RX(UART_MODULE_ID)))
{
// Echo what we just received.
PutCharacter(UARTGetDataByte(UART_MODULE_ID));
// Toggle LED to indicate UART activity
mPORTGToggleBits(BIT_7);
// Clear the RX interrupt Flag
INTClearFlag(INT_SOURCE_UART_RX(UART_MODULE_ID));
}
}
#else
// UART 2 interrupt handler, set at priority level 2
void __ISR(_UART2_VECTOR, ipl2) IntUart2Handler(void)
{
// Is this an RX interrupt?
if(INTGetFlag(INT_SOURCE_UART_RX(UART_MODULE_ID)))
{
// Echo what we just received.
PutCharacter(UARTGetDataByte(UART_MODULE_ID));
// Toggle LED to indicate UART activity
mPORTGToggleBits(BIT_7);
// Clear the RX interrupt Flag
INTClearFlag(INT_SOURCE_UART_RX(UART_MODULE_ID));
}
}
#endif |