본문 바로가기

[Microchip]/PIC32MZ

[PIC32MZ EVM] UART 테스트

[PIC32MZ EVM] UART 테스트




PIC32MZ에서 UART를 사용하려면 MPLAB Harmony Configurator 를 실행해서 UART를 활성화 하고 설정해 주면 쉽게 사용가능하다.






[PIC32MZ EVM] 보드의 UART는 RD14->TXD, RD15-RXD에 할당되어 있다.

핀 다이어그램 에서 원하는 핀을 클릭해서 설정 할 수 있다.



그러면 아래와 같은 코드들을 자동으로 생성해 준다.

void SYS_Initialize ( void* data )

{

    /* Core Processor Initialization */

    SYS_CLK_Initialize( NULL );

    sysObj.sysDevcon = SYS_DEVCON_Initialize(SYS_DEVCON_INDEX_0, (SYS_MODULE_INIT*)&sysDevconInit);

    SYS_DEVCON_PerformanceConfig(SYS_CLK_SystemFrequencyGet());

    SYS_PORTS_Initialize();


    /* Initialize Drivers */

    sysObj.drvUsart0 = DRV_USART_Initialize(DRV_USART_INDEX_0, (SYS_MODULE_INIT *)&drvUsart0InitData);

    SYS_INT_VectorPrioritySet(INT_VECTOR_UART1_TX, INT_DISABLE_INTERRUPT);

    SYS_INT_VectorSubprioritySet(INT_VECTOR_UART1_TX, INT_SUBPRIORITY_LEVEL0);

    SYS_INT_VectorPrioritySet(INT_VECTOR_UART1_RX, INT_PRIORITY_LEVEL1);

    SYS_INT_VectorSubprioritySet(INT_VECTOR_UART1_RX, INT_SUBPRIORITY_LEVEL0);

    SYS_INT_VectorPrioritySet(INT_VECTOR_UART1_FAULT, INT_PRIORITY_LEVEL1);

    SYS_INT_VectorSubprioritySet(INT_VECTOR_UART1_FAULT, INT_SUBPRIORITY_LEVEL0);


    /* Initialize System Services */


    /*** Interrupt Service Initialization Code ***/

    SYS_INT_Initialize();

  

    /* Initialize Middleware */


    /* Enable Global Interrupts */

    SYS_INT_Enable();


    /* Initialize the Application */

    APP_Initialize();

}




void SYS_PORTS_Initialize(void)

{

    /* PORT D Initialization */

    PLIB_PORTS_OpenDrainEnable(PORTS_ID_0, PORT_CHANNEL_D, SYS_PORT_D_ODC);

    PLIB_PORTS_Write( PORTS_ID_0, PORT_CHANNEL_D,  SYS_PORT_D_LAT);

    PLIB_PORTS_DirectionOutputSet( PORTS_ID_0, PORT_CHANNEL_D,  SYS_PORT_D_TRIS ^ 0xFFFF);

    PLIB_PORTS_ChangeNoticePerPortTurnOn(PORTS_ID_0, PORT_CHANNEL_D);

    PLIB_PORTS_ChannelModeSelect(PORTS_ID_0, PORT_CHANNEL_D, SYS_PORT_D_ANSEL ^ 0xFFFF, PORTS_PIN_MODE_DIGITAL);

    PLIB_PORTS_ChannelChangeNoticeEnable(PORTS_ID_0, PORT_CHANNEL_D, SYS_PORT_D_CNEN);

    PLIB_PORTS_ChannelChangeNoticePullUpEnable(PORTS_ID_0, PORT_CHANNEL_D, SYS_PORT_D_CNPU);

    PLIB_PORTS_ChannelChangeNoticePullDownEnable(PORTS_ID_0, PORT_CHANNEL_D, SYS_PORT_D_CNPD);

    

    /* PORT H Initialization */

    PLIB_PORTS_OpenDrainEnable(PORTS_ID_0, PORT_CHANNEL_H, SYS_PORT_H_ODC);

    PLIB_PORTS_Write( PORTS_ID_0, PORT_CHANNEL_H,  SYS_PORT_H_LAT);

    PLIB_PORTS_DirectionOutputSet( PORTS_ID_0, PORT_CHANNEL_H,  SYS_PORT_H_TRIS ^ 0xFFFF);

    PLIB_PORTS_ChangeNoticePerPortTurnOn(PORTS_ID_0, PORT_CHANNEL_H);

    PLIB_PORTS_ChannelModeSelect(PORTS_ID_0, PORT_CHANNEL_H, SYS_PORT_H_ANSEL ^ 0xFFFF, PORTS_PIN_MODE_DIGITAL);

    PLIB_PORTS_ChannelChangeNoticeEnable(PORTS_ID_0, PORT_CHANNEL_H, SYS_PORT_H_CNEN);

    PLIB_PORTS_ChannelChangeNoticePullUpEnable(PORTS_ID_0, PORT_CHANNEL_H, SYS_PORT_H_CNPU);

    PLIB_PORTS_ChannelChangeNoticePullDownEnable(PORTS_ID_0, PORT_CHANNEL_H, SYS_PORT_H_CNPD);



    /* PPS Input Remapping */

    PLIB_PORTS_RemapInput(PORTS_ID_0, INPUT_FUNC_U1RX, INPUT_PIN_RPD14 );


    /* PPS Output Remapping */

    PLIB_PORTS_RemapOutput(PORTS_ID_0, OUTPUT_FUNC_U1TX, OUTPUT_PIN_RPD15 );

}






PIC32MZ UART 송수신 함수


void U0_PutByte (char chr)

{

   PLIB_USART_TransmitterByteSend (DRV_USART_INDEX_0, chr); 

   //U4TXREG = chr;


   while (! PLIB_USART_TransmitterIsEmpty (DRV_USART_INDEX_0));

  //while (! U4STAbits.TRMT); 

}



unsigned char U0_GetByte (char chr)

{

    while(!PLIB_USART_ReceiverDataIsAvailable(DRV_USART_INDEX_0));

    //while(U1STAbits.URXDA == 0);

    

    return PLIB_USART_ReceiverByteReceive(DRV_USART_INDEX_0);

}







XC32 컴파일러에서 printf 함수를 사용하고 싶다면 _mon_putc() 함수를 재정의 해 주면 사용할 수 있다.


void _mon_putc(char c)

{

     U0_PutByte(c);






반응형