打印
[PIC32/SAM]

PIC32MX450 串口4中断

[复制链接]
659|8
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
wiba|  楼主 | 2019-7-25 12:08 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
// 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


使用特权

评论回复
沙发
wiba|  楼主 | 2019-7-25 12:08 | 只看该作者
//// 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);

使用特权

评论回复
板凳
wiba|  楼主 | 2019-7-25 12:08 | 只看该作者
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;
}

使用特权

评论回复
地板
wiba|  楼主 | 2019-7-25 12:09 | 只看该作者
// helper functions
void WriteString(const char *string)
{
  while (*string != '\0')
    {
      while (!UARTTransmitterIsReady(UART4))
        ;


      UARTSendDataByte(UART4, *string);


      string++;


      while (!UARTTransmissionHasCompleted(UART4))
        ;
    }
}

使用特权

评论回复
5
wiba|  楼主 | 2019-7-25 12:09 | 只看该作者
void PutCharacter(const char character)
{
  while (!UARTTransmitterIsReady(UART4))
    ;


  UARTSendDataByte(UART4, character);


  while (!UARTTransmissionHasCompleted(UART4))
    ;
}

使用特权

评论回复
6
wiba|  楼主 | 2019-7-25 12:09 | 只看该作者
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));
    }
}

使用特权

评论回复
7
598330983| | 2019-7-31 23:54 | 只看该作者
分享的例子啊?看不懂啊,没个解释。

使用特权

评论回复
8
643757107| | 2019-7-31 23:58 | 只看该作者
性能真好,可以跑到好几十个M

使用特权

评论回复
9
wiba|  楼主 | 2019-9-9 16:06 | 只看该作者
感谢您的支持

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

77

主题

3312

帖子

3

粉丝