新唐M031学习笔记(一)定时器基础计数应用
生活随笔
收集整理的這篇文章主要介紹了
新唐M031学习笔记(一)定时器基础计数应用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
先上代碼
void Hw_Timer0_Init(void) {//20:100ms 200:10ms 2000:1ms 20000:100us 200000:10us TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 200000);/* Update prescale to set proper resolution. */TIMER_SET_PRESCALE_VALUE(TIMER0, 1); /* Enable Timer0 interrupt */TIMER_EnableInt(TIMER0);NVIC_EnableIRQ(TMR0_IRQn); TIMER_Start(TIMER0); }可以參考注釋修改TIMER_Open的參數,想設置為2 000 000 定時1us沒有成功,進hardfalt了,沒有去細細研究,后續可能會補上。有需要的可以調用官方參考代碼中的TIMER_Delay。
整個工程參考代碼,這里修改了串口0的IO口為P14 P15
#include <stdio.h> #include "NuMicro.h" #include "hw_uart01.h" #include "hw_timer0.h"void SYS_Init(void) { /* Unlock protected registers */SYS_UnlockReg();/* Enable HIRC clock (Internal RC 48MHz) */CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);/* Wait for HIRC clock ready */CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);/* Select HCLK clock source as HIRC and HCLK source divider as 1 */CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));/* Enable UART0 clock */CLK_EnableModuleClock(UART0_MODULE);/* Switch UART0 clock source to HIRC */CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));/* Enable IP clock */CLK_EnableModuleClock(TMR0_MODULE);/* Select IP clock source */CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_HIRC, 0);/* Update System Core Clock */SystemCoreClockUpdate();/* UART0 RXD=PA.14 and TXD=PA.15 */SYS->GPA_MFPH = (SYS->GPA_MFPH & ~(SYS_GPA_MFPH_PA14MFP_Msk | SYS_GPA_MFPH_PA15MFP_Msk))|(SYS_GPA_MFPH_PA14MFP_UART0_TXD | SYS_GPA_MFPH_PA15MFP_UART0_RXD );/* Enable USCI UART interrupt */UUART_ENABLE_TRANS_INT(UUART0, UUART_INTEN_RXENDIEN_Msk);NVIC_EnableIRQ(USCI_IRQn);/* Lock protected registers */SYS_LockReg(); }int main() {SYS_Init();/* Init UART0 to 115200-8n1 for print message */Hw_Uart0_Init();Hw_Timer0_Init();while(1){printf("Hello World\r\n");App_Func_Delay(100000);//1s} } #include "hw_timer0.h" #include "NuMicro.h" #include "stdio.h"//這里一定要加volatile //大家可以試試不加會有什么BUG,我這里是100%復現 volatile unsigned int g_delay_time = 0; volatile unsigned int g_delay_flag = 0;void Hw_Timer0_Init(void) {//20:100ms 200:10ms 2000:1ms 20000:100us 200000:10us TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 200000);/* Update prescale to set proper resolution. */TIMER_SET_PRESCALE_VALUE(TIMER0, 1);/* Enable Timer0 interrupt */TIMER_EnableInt(TIMER0);NVIC_EnableIRQ(TMR0_IRQn); TIMER_Start(TIMER0); }void App_Func_Delay(unsigned int ms) {g_delay_time = ms;while(0 == g_delay_flag);g_delay_flag = 0; }void TMR0_IRQHandler(void) {if(g_delay_time){g_delay_time--;if(0 == g_delay_time){g_delay_flag = 1;}}/* Clear interrupt flag */TIMER_ClearIntFlag(TIMER0); }總結
以上是生活随笔為你收集整理的新唐M031学习笔记(一)定时器基础计数应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《楚乔传》宇文玥母亲真实身份起底 是极乐
- 下一篇: 颐和园老年人免门票吗