[PIC] pic32mx795f512l uart communication

Status
Not open for further replies.

bhoobalan

Member level 1
Joined
Dec 28, 2013
Messages
35
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Visit site
Activity points
280
Hi friends,
I am new to this pic32 microcontroller, I have two pic32 microcontrollers
one is pic32mx795f512l(pic32 usb starter kit 2) and second is pic32mx360f512l(pic32 starter kit).
I have connected 3 buttons in pic32mx795f512l microcontroller in the port pins of
SW1 in RD6, SW2 in RD7 and SW3 in RD13. And also i connected 3 led's in pic32mx360f512l microcontroller in the port pins of LED1 in RD0, LED2 in RD1 and LED3 in RD2.

I want to switch on and off the led's that connected to pic32mx360f512l using the buttons connected in pic32mx795f512 through uart communication.

I don't know how to code for this.
I want one basic sample code for pic32 uart communication ans code for the above operation.

Thanks in advance,
waiting for your reply............
 

hello u have to microchip directory already have example of UART . Below is the simple UART inturrput base example .
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

u have to add your pic to this line #if defined (__32MX360F512L__) || (__32MX460F512L__) || (__32MX795F512L__) || (__32MX430F064L__) || (__32MX450F256L__) || (__32MX470F512L__) || (__32MX575F256H__) .
 
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…