今天捣鼓了一下定时器点灯:
代码:
// *****************************************************************************
// *****************************************************************************
// 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
// DOM-IGNORE-BEGIN
/*******************************************************************************
* Copyright (C) 2018 Microchip Technology Inc. and its subsidiaries.
*
* Subject to your compliance with these terms, you may use Microchip software
* and any derivatives exclusively with Microchip products. It is your
* responsibility to comply with third party license terms applicable to your
* use of third party software (including open source software) that may
* accompany Microchip software.
*
* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
* PARTICULAR PURPOSE.
*
* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
*******************************************************************************/
// DOM-IGNORE-END
// *****************************************************************************
// *****************************************************************************
// Section: Main Entry Point
// *****************************************************************************
// *****************************************************************************
/* This function is called after period expires */
void TC0_Callback_InterruptHandler(TC_TIMER_STATUS status, uintptr_t context)
{
/* Toggle LED */
LED_Toggle();
}
// *****************************************************************************
// *****************************************************************************
// Section: Main Entry Point
// *****************************************************************************
// *****************************************************************************
int main ( void )
{
/* Initialize all modules */
SYS_Initialize ( NULL );
/* Register callback function for TC0 period interrupt */
TC0_TimerCallbackRegister(TC0_Callback_InterruptHandler, (uintptr_t)NULL);
/* Start the timer channel 0*/
TC0_TimerStart();
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 )
{
NVMCTRL_Initialize( );
PORT_Initialize();
CLOCK_Initialize();
EVSYS_Initialize();
TC0_TimerInitialize();
NVIC_Initialize();
}
/* Register callback function */
void TC0_TimerCallbackRegister( TC_TIMER_CALLBACK callback, uintptr_t context )
{
TC0_CallbackObject.callback = callback;
TC0_CallbackObject.context = context;
}
/* Enable the TC counter */
void TC0_TimerStart( void )
{
TC0_REGS->COUNT16.TC_CTRLA |= TC_CTRLA_ENABLE_Msk;
while((TC0_REGS->COUNT16.TC_SYNCBUSY & TC_SYNCBUSY_ENABLE_Msk) == TC_SYNCBUSY_ENABLE_Msk)
{
/* Wait for Write Synchronization */
}
}
效果图:
|