Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Pic32 uart transmit and receive full string problem ?

Status
Not open for further replies.

nick703

Advanced Member level 1
Advanced Member level 1
Joined
Oct 17, 2011
Messages
422
Helped
21
Reputation
44
Reaction score
22
Trophy points
1,298
Location
surat
Visit site
Activity points
3,987
i have to configure UART1 on my pic32mx575f256h . so please help me to my problem ?

how to configure interrupt based transmit and receive char . and also how to receive full string and transmit full string via uart .

suppose i have to send command through PC to PIC like
{START} and how to get response like [START] this method .

if any code available then please guide me .

thanks
 

Pic32 uart receive full string problem ?

i have complete simulate code in MPLAB SIM using pic32mx575f256h device and c32 compiler ok

below is my code

and now my problem is every thing is fine in mplab simulator but in real hardware i saw only two line of code but when i press {TEST} command that time i didn't get back responce what is the problem please give me some idea .
 

Attachments

  • Pic32 UART USB All command 1.rar
    61.4 KB · Views: 172

Re: Pic32 uart receive full string problem ?


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
#include <plib.h>
 
// Configuration Bit settings
// SYSCLK = 80 MHz (8MHz Crystal/ FPLLIDIV * FPLLMUL / FPLLODIV)
// PBCLK = 80 MHz
// 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)
#define FALSE            0
#define TRUE             1
 
#define DESIRED_BAUDRATE        (9600)      // The desired BaudRate
 
 char ComOn[] = {"[START]"};
 
unsigned char i = 0,j = 0;
unsigned char C[15],consider,CommandFrame[10],CommandLenth,Command_error,Tempc;
unsigned char FirstChar = 2,SndChar = 0,a = 0;
unsigned char CaptureCompare1,CaptureCompare2,PulseWModulation;
         char MessageFrameLong[10];
         char MessageFrame[10];
unsigned char StrokeLenth = 1,StrokeSpeed = 10,TopSpeed = 20,TemStrokeLenth_1 = 1;
 
 
struct UserBit{
    unsigned B0:1;
    unsigned B1:1;
    unsigned B2:1;
    unsigned B3:1;
    unsigned B4:1;
    unsigned B5:1;
    unsigned B6:1;
    unsigned B7:1;
    unsigned B8:1;
    unsigned B9:1;
    unsigned B10:1;
    unsigned B11:1;
    unsigned B12:1;
    unsigned B13:1;
    unsigned B14:1;
    unsigned B15:1;
}Flage0, Flage1;
 
 
 
#define bPassword       Flage0.B4
 
 
 
void WriteString(char *string)
{
    a = 0;
    i = 0;
    j = 0;
    while(*string)
    {
        while(!UARTTransmitterIsReady(UART1));
 
        UARTSendDataByte(UART1, *string++);
        
        C[a++] = 0;
        C[a++] = 0;
        
        while(!UARTTransmissionHasCompleted(UART1));
        
    
        
    }
}
 
 
void PutCharacter(char character)
{
        while(!UARTTransmitterIsReady(UART1))
            ;
 
        UARTSendDataByte(UART1, character);
 
 
        while(!UARTTransmissionHasCompleted(UART1))
            ;
}
 
 
 
int main(void)
{
   int    pbClk;
   unsigned char data;
 
   pbClk=SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
 
 
   
   OpenUART1(UART_EN, UART_RX_ENABLE | UART_TX_ENABLE, pbClk/16/DESIRED_BAUDRATE-1);    // calculate actual BAUD generate value.
   INTEnable(INT_SOURCE_UART_RX(UART1), INT_ENABLED);
   INTSetVectorPriority(INT_VECTOR_UART(UART1), INT_PRIORITY_LEVEL_2);
   INTSetVectorSubPriority(INT_VECTOR_UART(UART1), INT_SUB_PRIORITY_LEVEL_0);
   INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
   INTEnableInterrupts();
 
 
   putsUART1("*** UART Simple Application Example ***\r\n");
   putsUART1("*** Type some characters and observe echo and RA7 LED toggle  ***\r\n");
 
   consider = FALSE;
   bPassword = FALSE;
  
  
   
 
 while(1)
 {
    if(!strncmp(C, "{TEST}", sizeof("{TEST}")))
    {
        bPassword = TRUE;
        WriteString(ComOn); 
    }
    else if(!strncmp(C, "{TRUE}", sizeof("{TRUE}")))
    {
        WriteString("false");
    }
 };
 
   return 0;
}
 
void __ISR(_UART1_VECTOR, ipl2) IntUart1Handler(void)
{
 
    if(INTGetFlag(INT_SOURCE_UART_RX(UART1)))
    {
        // Clear the RX interrupt Flag
        INTClearFlag(INT_SOURCE_UART_RX(UART1));
        
        C[i++] = ReadUART1();
        
        if(i > 15)
        {i = 0;j = 0;}
 
    }
 
    
    if ( INTGetFlag(INT_SOURCE_UART_TX(UART1)) )
    {
        INTClearFlag(INT_SOURCE_UART_TX(UART1));
    }
}

 

Re: Pic32 uart receive full string problem ?

hello milan i have to display in my serial port this two line only

Code:
  *** UART Simple Application Example ***
*** Type some characters and observe echo and RA7 LED toggle  ***
but when i send command like {START} and {TRUE}. that time i didn't get any response what is the metter but MPLAB SIM when i fired {START} then i have receive [START]. SO waht is the problem i didn't understand bro !!!
 

Re: Pic32 uart receive full string problem ?

If any valid command like [START] is received then you have to reset the UART buffer counter i.

You are using strncmp() and passing C[] as argument but it has to be null terminated to make it a string.


Code C - [expand]
1
2
C[i++] = ReadUART1();
C[i] = '\0';

 

Re: Pic32 uart receive full string problem ?

but i cant get any valid command means in real hardware there is not trigger in receive interrupt section that is my problem .
 

Re: Pic32 uart receive full string problem ?

HELLO milan my first post attachment is the complete file have u seen or not?
 

some time my UART is freez . what's going on onj my uart sproblem i didn't know how.

in my device PIC32MX575F256H has a UART1A ,UART2A ,UART3A .
in some librarary i see the function openconfigU1() function . so therefor my second question is suppose i have to use UART2A then which library i can use i don't know.

please any one have a configure uart in register mode then please tell me .
 

Your MCU will have (Rx1, Tx1), (Rx2, Tx2), (Rx3, Tx3) pins. Which pin pairs are you using?

**broken link removed**

openconfigU1(), openconfigU2()... are C32 UART library related functions. If you use library functions then you don't have to set registers manually. It also has functions to read and write to UART like putsUSART(), writeUSART(), readUSART()...
 
Last edited:

ok milan suppose i want to configure UART2A then what function i have to use .
 

thanks milan your great information .

now i will testing your code for my device and test.

suppose i have a some time freez application is that problem in configuration . in my application windows form based application send command like {SATRT} , {STOP} and i have to reply with this formate like [start] , [stop].

this type of 20 above command. is that possible in run time system will be hang as the issue of some uart confiuration bit is not set. ? please reply
 

ok now i am trying this below code and this is my result (My Device 32mx575f256h and c32 compiler ok)
Code:
/*********************************************************************
 *
 *      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>				

#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)))
	{
            // Clear the RX interrupt Flag
	    INTClearFlag(INT_SOURCE_UART_RX(UART_MODULE_ID));

            // Echo what we just received.
            PutCharacter(UARTGetDataByte(UART_MODULE_ID));

            // Toggle LED to indicate UART activity
            mPORTAToggleBits(BIT_7);
	}

	// We don't care about TX interrupt
	if (INTGetFlag(INT_SOURCE_UART_TX(UART_MODULE_ID)))
	{
            INTClearFlag(INT_SOURCE_UART_TX(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)))
	{
            // Clear the RX interrupt Flag
	    INTClearFlag(INT_SOURCE_UART_RX(UART_MODULE_ID));

            // Echo what we just received.
            PutCharacter(UARTGetDataByte(UART_MODULE_ID));

            // Toggle LED to indicate UART activity
            mPORTAToggleBits(BIT_7);
	}

	// We don't care about TX interrupt
	if ( INTGetFlag(INT_SOURCE_UART_TX(UART_MODULE_ID)) )
	{
            INTClearFlag(INT_SOURCE_UART_TX(UART_MODULE_ID));
	}
}
#endif

and below is my image what i receive ok

serial receive.JPG

in this snap shot u see blue command is that i have to sent and red command is i have receive. u see when i send {START} i recieve also {START} BUT another one bit also . and first line i also get some misbehave of string . is that any solution required i am uaing FT232R for usb to serial uart.
 

You need 100 ms delay after UART initialization (UART stabilization time). Zip and post MPLAB C18 project files for code you posted in post #15.
 

in this example there is no buffer but simply echo back data .

in this example used PutCharacter function means when data is received in interrupt routine that time data send using putcharacter function.
 

See attached image. Might be somethinf related to your serial terminal software settings? Try Termite or RealTerm serial terminal software. I didn't find any problem in the code.
 

Attachments

  • pic32uart.png
    pic32uart.png
    143.6 KB · Views: 171

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top