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
| unsigned char greceiveddata[45];
unsigned int i = 0;
unsigned char atcmd1[] = "AT\r";
unsigned char atcmd2[] = "ATE0\r";
// UART 2 interrupt handler
// it is set at priority level 2
void __ISR(_UART2_VECTOR, ipl2) IntUart2Handler(void)
{
// Is this an RX interrupt?
if(U2RXIF)
{
if(OERR){
OERR = 0;
}
// Clear the RX interrupt Flag
U2RXIF = 0;
greceiveddata[i++] = U2RXREG;
greceiveddata[i] = '\0';
// Echo what we just received.
PutCharacter(greceiveddata[i-1]);
// Toggle LED to indicate UART activity
mPORTAToggleBits(BIT_7);
}
}
int main(void)
{
lcd_init();
lcd_cmd(0X1);//clear disp
delay(1000);
lcd_cmd(0X38);//8 bit 2 line mode
delay(1000);
lcd_cmd(0X0C);// cursor blink
delay(1000);
lcd_cmd(0X06);//move right
delay(1000);
lcd_cmd(0X80);//first line first position
delay(1000);
greceiveddata[0] = '\0';
#if defined (__32MX220F032D__) || defined (__32MX250F128D__)
U2RXRbits.U2RXR = 1; //SET RX to RPB5
RPB0Rbits.RPB0R = 2; //SET RPB0R to TX
#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(GetSystemClock(), SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
mPORTAClearBits(BIT_7); // Turn off RA7 on startup.
mPORTASetPinsDigitalOut(BIT_7); // Make RA7 as output.
// Explorer-16 uses UART2 to connect to the PC.
// This initialization assumes 36MHz Fpb clock. If it changes,
// you will have to modify baud rate initializer.
UARTConfigure(UART2, UART_ENABLE_PINS_TX_RX_ONLY);
UARTSetFifoMode(UART2, UART_INTERRUPT_ON_TX_NOT_FULL |
UART_INTERRUPT_ON_RX_NOT_EMPTY);
UARTSetLineControl(UART2, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE |
UART_STOP_BITS_1);
UARTSetDataRate(UART2, GetPeripheralClock(), DESIRED_BAUDRATE);
UARTEnable(UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));
// Configure UART2 RX Interrupt
INTEnable(INT_SOURCE_UART_RX(UART2), INT_ENABLED);
INTSetVectorPriority(INT_VECTOR_UART(UART2), INT_PRIORITY_LEVEL_2);
INTSetVectorSubPriority(INT_VECTOR_UART(UART2), INT_SUB_PRIORITY_LEVEL_0);
// configure for multi-vectored mode
INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
// enable interrupts
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);
{
SendData2UART(atcmd1);
while(greceiveddata[0] != '\0')
lcd_cmd(0X80);
lcd(greceiveddata);
greceiveddata[0] != '\0';
}
return 0;
}
// helper functions
void WriteString(const char greceiveddata[])
{
unsigned int j = 0;
while(greceiveddata[j++] != '\0')
{
while(!UARTTransmitterIsReady(UART2));
UARTSendDataByte(UART2, greceiveddata[j]);
while(!UARTTransmissionHasCompleted(UART2));
}
}
void PutCharacter(const char character)
{
while(!UARTTransmitterIsReady(UART2));
UARTSendDataByte(UART2, character);
while(!UARTTransmissionHasCompleted(UART2));
}
void SendData2UART(unsigned char data_[45]){
unsigned int j = 0;
while(data_[j++] != '\0')PutCharacter(data_[j]);
} |