Pic32 uart transmit and receive full string problem ?

Status
Not open for further replies.
PORTG.7 used for LED?


Code C - [expand]
1
2
mPORTGClearBits(BIT_7);         // Turn off RA7 on startup.
mPORTGSetPinsDigitalOut(BIT_7); // Make RA7 as output.



Then change to


Code C - [expand]
1
2
mPORTAClearBits(BIT_7);         // Turn off RA7 on startup.
mPORTASetPinsDigitalOut(BIT_7); // Make RA7 as output.



UART1 used? then

Set RD3 and RD2 as input pins using TRISD.


Ok. I see there is no PORTA.
 

i am using PORTG for LED and yes I have to configure PORTD RD2 as a input and RD3 as a output . but still getting error on end f transmission bit.

and yes i am not using PORTA.
 

Then replace the below line in ISR with PORTG function.


Code C - [expand]
1
mPORTAToggleBits(BIT_7);




Add


Code C - [expand]
1
#define __32MX575F256H__



after


Code C - [expand]
1
#include <plib.h>

 
Last edited:

It has nothing to do with UART but I guess your LED was not toggling earlier. Also your LED toggles at 1200 times as your baudrate is 9600 bps. For every Serial interrupt (1 byte received) your LED toggles. You will not see the toggling. You will see either ON or OFF.

Try this code.


Code C - [expand]
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

 
Last edited:


but Milan Suppose i do not want toggling LED and after burn code in pic32 and check result i received same as above. u also try and test same result i have received i am sure also u have to received bro!!
 

Zip and post the MPLAB C32 project files. I don't have the PIC32 you are using to test.
 

i am already zipped code in my post #17 .

by the way second question is in my mind that if suppose My interrupt Priority is 1 and my application rotate three stepper motor which have also use timer interrupt which has a Priority is 2 , 3 and 4. so is that possible some time uart sending stop at that time we have run this three stepper motor.
 

If Interrupt Priority of UART is higher then it doesn't affect Serial reception. Do you have interrupt priorities starting at 0? If 0 is the highest then set UART IP to 0 and try.
 

ok thanks for your reply.

Now another question is Suppose My PC side i want to Send this Pattern Like {2Axxx} where xxx is the value of 000 to 255 .then how to get this command and reply back to this foramte like [2Axxx] where xxx is 000 to 255 .

for example suppose command send {2A100} then i have to receive and get back response like [2A100] . HOW TO HANDLE 000 TO 255 COMMAND IN ONE FUNCTION. give me some example bro!!!
 

You are sending the string "{2A000}" and want the PIC32 to send back "[2A000]", so, only the curly braces are replaced by square brackets. So, in ISR start filling the buffer after '{' is received and from bufferIndex = 1 and stop receiving when '{' is received or transmit the buffer back. buffer[0] should be set to '[' , buffer[6] should be set to ']' , buffer[7] to '\0' when buffer[8] is declared.
 

is that any condition required suppose unknown command i will get that time i didn't back any response ok . that's time any error occurred if receiving buffer is not empty. means suppose i have to send this command continuously .
{2S255} , {2A254} , {2D255} , {2A253}. That time no. 3 command is wrong one so any affect if we send 4th command. because at receiving side i have fill buffer and transmitting side i have clear buffer it is correct or not?
 

After '} is received which is the end of one command set buffer index to 1. buffer[0], buffer[6] and buffer[7] never change.

Condition one will be if buffer index is 1 then wait until '{' is received before filling buffer.

Condition two will be if '{' is received set buffer index to 1 (additionally you may clear buffer[1] to buffer[5].
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…