我一开始装的MPLAB V5.30,找不到Harmony的下载地方。
后来下载了一个MPLABX V5.40,谁知还是不行。我记得说明书上用的就是V5.40
没办法,又下载了一个V5.50,废了老大的劲,终于下好了Harmony,建立了hello工程。
照着说明书做,因为版本不同,有个别地方还不太一样。
上代码:
// *****************************************************************************
// *****************************************************************************
// Section: Included Files
// *****************************************************************************
// *****************************************************************************
#include <stddef.h> // Defines NULL
#include <stdbool.h> // Defines true
#include <stdlib.h> // Defines EXIT_FAILURE
#include "definitions.h" // SYS function prototypes
char menu[]="How Are you?";
// *****************************************************************************
// *****************************************************************************
// Section: Main Entry Point
// *****************************************************************************
// *****************************************************************************
int main ( void )
{
/* Initialize all modules */
SYS_Initialize ( NULL );
printf("\r\n--------------I am ATSAMD51--------\r\n");
SERCOM2_USART_Write(menu,sizeof(menu));
while ( true )
{
/* Maintain state machines of all polled MPLAB Harmony modules. */
SYS_Tasks ( );
}
/* Execution should not come here during normal operation */
return ( EXIT_FAILURE );
}
/*******************************************************************************
End of File
*/
初始化代码:
void SYS_Initialize ( void* data )
{
/* MISRAC 2012 deviation block start */
/* MISRA C-2012 Rule 2.2 deviated in this file. Deviation record ID - H3_MISRAC_2012_R_2_2_DR_1 */
NVMCTRL_Initialize( );
STDIO_BufferModeSet();
PORT_Initialize();
CLOCK_Initialize();
SERCOM2_USART_Initialize();
EVSYS_Initialize();
NVIC_Initialize();
/* MISRAC 2012 deviation block end */
}
void SERCOM2_USART_Initialize( void )
{
/*
* Configures USART Clock Mode
* Configures TXPO and RXPO
* Configures Data Order
* Configures Standby Mode
* Configures Sampling rate
* Configures IBON
*/
SERCOM2_REGS->USART_INT.SERCOM_CTRLA = SERCOM_USART_INT_CTRLA_MODE_USART_INT_CLK | SERCOM_USART_INT_CTRLA_RXPO(0x0UL) | SERCOM_USART_INT_CTRLA_TXPO(0x0UL) | SERCOM_USART_INT_CTRLA_DORD_Msk | SERCOM_USART_INT_CTRLA_IBON_Msk | SERCOM_USART_INT_CTRLA_FORM(0x0UL) | SERCOM_USART_INT_CTRLA_SAMPR(0UL) ;
/* Configure Baud Rate */
SERCOM2_REGS->USART_INT.SERCOM_BAUD = (uint16_t)SERCOM_USART_INT_BAUD_BAUD(SERCOM2_USART_INT_BAUD_VALUE);
/*
* Configures RXEN
* Configures TXEN
* Configures CHSIZE
* Configures Parity
* Configures Stop bits
*/
SERCOM2_REGS->USART_INT.SERCOM_CTRLB = SERCOM_USART_INT_CTRLB_CHSIZE_8_BIT | SERCOM_USART_INT_CTRLB_SBMODE_1_BIT | SERCOM_USART_INT_CTRLB_RXEN_Msk | SERCOM_USART_INT_CTRLB_TXEN_Msk;
/* Wait for sync */
while((SERCOM2_REGS->USART_INT.SERCOM_SYNCBUSY) != 0U)
{
/* Do nothing */
}
/* Enable the UART after the configurations */
SERCOM2_REGS->USART_INT.SERCOM_CTRLA |= SERCOM_USART_INT_CTRLA_ENABLE_Msk;
/* Wait for sync */
while((SERCOM2_REGS->USART_INT.SERCOM_SYNCBUSY) != 0U)
{
/* Do nothing */
}
}
效果图:
|