PIC32MX450 串口4中断

// mplab  x IDE V1.7

/*********************************************************************

 *
 *                  UART Interrupt Example   
 *
 *********************************************************************
 * FileName:        uart_interrupt.c
 * Dependencies:
 * Processor:       PIC32
 *
 * Compiler:        MPLAB XC32
 *                  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 $
 * $Name: x.x $
 *
 **********************************************************************/


/***********************************************************************************
UART Interrupt Example README
 ***********************************************************************************
 * Objective: Become familiar with PIC32MX tool suite and understand
 *  basic UART Interrupt operations.
 *
 * Tools:
 * 1. MPLAB IDE with PIC32MX support
 * 2. C32 Compiler
 * 3. Explorer 16 Rev 4 or 5 board.
 * 4. RS-232 Cable
 * 5. A Terminal program for Windows - HyperTerminal
 *
 *
 ***********************************************************************************
 ***********************************************************************************/


#include <plib.h> // Peripheral Library
//#include <p32xxxx.h>


// *****************************************************************************
// *****************************************************************************
// Section: Configuration bits
// SYSCLK = 80 MHz (8MHz Crystal/ FPLLIDIV * FPLLMUL / FPLLODIV)
// PBCLK = 40 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




DEVCFG3
USERID = No Setting
//#pragma config FSRSSEL = PRIORITY_7     // Shadow Register Set Priority Select (SRS Priority 7)
//#pragma config PMDL1WAY = ON            // Peripheral Module Disable Configuration (Allow only one reconfiguration)
//#pragma config IOL1WAY = ON             // Peripheral Pin Select Configuration (Allow only one reconfiguration)
//#pragma config FUSBIDIO = ON            // USB USID Selection (Controlled by the USB Module)
//#pragma config FVBUSONIO = ON           // USB VBUS ON Selection (Controlled by USB Module)
//
DEVCFG2
//#pragma config FPLLIDIV = DIV_2         // PLL Input Divider (2x Divider)
//#pragma config FPLLMUL = MUL_20         // PLL Multiplier (20x Multiplier)
//#pragma config UPLLIDIV = DIV_12        // USB PLL Input Divider (12x Divider)
//#pragma config UPLLEN = OFF             // USB PLL Enable (Disabled and Bypassed)
//#pragma config FPLLODIV = DIV_1         // System PLL Output Clock Divider (PLL Divide by 1)
//
DEVCFG1
//#pragma config FNOSC = PRIPLL           // Oscillator Selection Bits (Primary Osc w/PLL (XT+,HS+,EC+PLL))
//#pragma config FSOSCEN = ON             // Secondary Oscillator Enable (Enabled)
//#pragma config IESO = ON                // Internal/External Switch Over (Enabled)
//#pragma config POSCMOD = HS             // Primary Oscillator Configuration (HS osc mode)
//#pragma config OSCIOFNC = OFF           // CLKO Output Signal Active on the OSCO Pin (Disabled)
//#pragma config FPBDIV = DIV_1           // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/1)
//#pragma config FCKSM = CSDCMD           // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled)
//#pragma config WDTPS = PS1048576        // Watchdog Timer Postscaler (1:1048576)
//#pragma config WINDIS = OFF             // Watchdog Timer Window Enable (Watchdog Timer is in Non-Window Mode)
//#pragma config FWDTEN = OFF             // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))
//#pragma config FWDTWINSZ = WISZ_25      // Watchdog Timer Window Size (Window Size is 25%)
//
DEVCFG0
//#pragma config DEBUG = OFF              // Background Debugger Enable (Debugger is Disabled)
//#pragma config JTAGEN = ON              // JTAG Enable (JTAG Port Enabled)
#pragma config ICESEL = ICS_PGx1        // ICE/ICD Comm Channel Select (Communicate on PGEC1/PGED1)
//#pragma config PWP = OFF                // Program Flash Write Protect (Disable)
//#pragma config BWP = OFF                // Boot Flash Write Protect bit (Protection Disabled)
//#pragma config CP = OFF                 // Code Protect (Protection Disabled)


// *****************************************************************************
// *****************************************************************************
// Section: System Macros
// *****************************************************************************
// *****************************************************************************
#define GetSystemClock()              (80000000ul)
#define GetPeripheralClock()          (GetSystemClock()/(1 << OSCCONbits.PBDIV))
#define GetInstructionClock()         (GetSystemClock())


#define DESIRED_BAUDRATE              (9600)      //The desired BaudRate




void WriteString(const char *string);


int main(void)
{


  // 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);
  mJTAGPortEnable(DEBUG_JTAGPORT_OFF);


  INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
  INTEnableInterrupts();


    PPSOutput(3, RPD9, U4TX); // Set RPF5 pin as output for U2TX
    //PPSInput(4,U4RX,RPD5);
    PPSInput(4, U4RX, RPB2);
    PORTSetPinsDigitalIn(IOPORT_B, BIT_2);
  // Explorer-16 uses UART4 to connect to the PC.
  // This initialization assumes 36MHz Fpb clock. If it changes,
  // you will have to modify baud rate initializer.
  UARTConfigure(UART4, UART_ENABLE_PINS_TX_RX_ONLY);
  UARTSetFifoMode(UART4, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY);
  UARTSetLineControl(UART4, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
  //U4MODE = 1 << 3;
  UARTSetDataRate(UART4, GetPeripheralClock(), DESIRED_BAUDRATE);
  UARTEnable(UART4, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));


  // Configure UART4 RX Interrupt
//  INTEnable(INT_SOURCE_UART_RX(UART4) | INT_SOURCE_UART_TX(UART4), INT_ENABLED);
  INTSetVectorPriority(INT_VECTOR_UART(UART4), INT_PRIORITY_LEVEL_3);
  INTSetVectorSubPriority(INT_VECTOR_UART(UART4), INT_SUB_PRIORITY_LEVEL_0);
  INTEnable(INT_U4RX,INT_ENABLED);
//  INTEnable(INT_U4TX,INT_ENABLED);
  // configure for multi-vectored mode
  //WriteString("22");
  // Let interrupt handler do the work
  while (1)
  {
//     int data = 0;
//    if (UARTReceivedDataIsAvailable(UART4))
//    {
//      // Echo what we just received.
//      //PutCharacter(UARTGetDataByte(UART4));
//      // Clear the RX interrupt Flag
//      INTClearFlag(INT_SOURCE_UART_RX(UART4));
//    }
  }


  return 0;
}
// helper functions
void WriteString(const char *string)
{
  while (*string != '\0')
    {
      while (!UARTTransmitterIsReady(UART4))
        ;


      UARTSendDataByte(UART4, *string);


      string++;


      while (!UARTTransmissionHasCompleted(UART4))
        ;
    }
}


void PutCharacter(const char character)
{
  while (!UARTTransmitterIsReady(UART4))
    ;


  UARTSendDataByte(UART4, character);


  while (!UARTTransmissionHasCompleted(UART4))
    ;
}




char test_rx[5] = "111";
char test_tx[5] = "333";
void __ISR(_UART_4_VECTOR, ipl3) IntUart4Handler(void)
{
  // Is this an RX interrupt?
  if (INTGetFlag(INT_SOURCE_UART_RX(UART4)))
    {
      // Echo what we just received.
      // Clear the RX interrupt Flag
      //UARTSendDataByte(UART4, test_rx);
      //WriteString("222");
      INTClearFlag(INT_SOURCE_UART_RX(UART4));
    }
  // We don't care about TX interrupt
  if ( INTGetFlag(INT_SOURCE_UART_TX(UART4)) )
    {
      //UARTSendDataByte(UART4, test_tx);
      //WriteString("444");
      INTClearFlag(INT_SOURCE_UART_TX(UART4));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值