完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
/**************************************************************************//** * @file main.c * @version V3.00 * $Revision: 4 $ * $Date: 14/01/28 11:45a $ * @brief M051 Series timer Driver Sample Code * * @note * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved. ******************************************************************************/ #include #include "M051Series.h" #define PLLCON_SETTING CLK_PLLCON_50MHz_HXT #define PLL_CLOCK 50000000 unsigned int data; unsigned int count; unsigned int p_flag; /** * @brief Timer0 IRQ * * @param None * * @return None * * @Details The Timer0 default IRQ, declared in startup_M051Series.s. */ void TMR0_IRQHandler(void) { /* Clear Timer0 time-out interrupt flag */ TIMER_ClearIntFlag(TIMER0); if(count!=0) { if(data&0x01==0x01) P27=1; else P27=0; data=data>>1; count--; } } void UART_GPIO(unsigned char uart_data) { data=0; data=(uart_data<<1)|(1<<9); count=10; } void SYS_Init(void) { /*---------------------------------------------------------------------------------------------------------*/ /* Init System Clock */ /*---------------------------------------------------------------------------------------------------------*/ /* Enable IRC22M clock */ CLK->PWRCON |= CLK_PWRCON_IRC22M_EN_Msk; /* Waiting for IRC22M clock ready */ CLK_WaitClockReady(CLK_CLKSTATUS_IRC22M_STB_Msk); /* Switch HCLK clock source to HIRC */ CLK->CLKSEL0 = CLK_CLKSEL0_HCLK_S_HIRC; /* Set PLL to Power-down mode and PLL_STB bit in CLKSTATUS register will be cleared by hardware.*/ CLK->PLLCON |= CLK_PLLCON_PD_Msk; /* Enable external 12 MHz XTAL, IRC10K */ CLK->PWRCON |= CLK_PWRCON_XTL12M_EN_Msk | CLK_PWRCON_OSC10K_EN_Msk; /* Enable PLL and Set PLL frequency */ CLK->PLLCON = PLLCON_SETTING; /* Waiting for clock ready */ CLK_WaitClockReady(CLK_CLKSTATUS_PLL_STB_Msk | CLK_CLKSTATUS_XTL12M_STB_Msk | CLK_CLKSTATUS_IRC10K_STB_Msk); /* Switch HCLK clock source to PLL, STCLK to HCLK/2 */ CLK->CLKSEL0 = CLK_CLKSEL0_STCLK_S_HCLK_DIV2 | CLK_CLKSEL0_HCLK_S_PLL; /* Enable peripheral clock */ CLK->APBCLK = CLK_APBCLK_UART0_EN_Msk | CLK_APBCLK_TMR0_EN_Msk ; /* Peripheral clock source */ CLK->CLKSEL1 = CLK_CLKSEL1_UART_S_PLL |CLK_CLKSEL1_TMR0_S_HIRC; /* Update System Core Clock */ /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */ SystemCoreClockUpdate(); /*---------------------------------------------------------------------------------------------------------*/ /* Init I/O Multi-function */ /*---------------------------------------------------------------------------------------------------------*/ /* Set P3 multi-function pins for UART0 RXD, TXD */ SYS->P3_MFP = SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0; } void UART0_Init(void) { /*---------------------------------------------------------------------------------------------------------*/ /* Init UART */ /*---------------------------------------------------------------------------------------------------------*/ /* Reset IP */ SYS_ResetModule(UART0_RST); /* Configure UART0 and set UART0 Baudrate */ UART_Open(UART0, 115200); } /*---------------------------------------------------------------------------------------------------------*/ /* MAIN function */ /*---------------------------------------------------------------------------------------------------------*/ int main(void) { int i; volatile uint32_t u32InitCount; /* Unlock protected registers */ SYS_UnlockReg(); /* Init System, peripheral clock and multi-function I/O */ SYS_Init(); /* Lock protected registers */ SYS_LockReg(); /* Init UART0 for printf */ UART0_Init(); /* Configure P1.5 as Output mode*/ GPIO_SetMode(P2, BIT7, GPIO_PMD_OUTPUT); /* Open Timer0 frequency 38400 in periodic mode, and enable interrupt */ TIMER0->TCMPR = (22118400/38400); TIMER0->TCSR = TIMER_TCSR_IE_Msk | TIMER_PERIODIC_MODE; TIMER_SET_PRESCALE_VALUE(TIMER0, 0); /* Enable Timer0 NVIC */ NVIC_EnableIRQ(TMR0_IRQn); /* Start Timer0 counting */ TIMER_Start(TIMER0); while(1){ if(count==0) { CLK_SysTickDelay(1000); UART_GPIO(i); i++; } } } /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/ |
|
相关推荐
1个回答
|
|
ude "NuMicro.h"
#define RXBUFSIZE 1024 /*---------------------------------------------------------------------------------*/ /* Define functions prototype */ /*---------------------------------------------------------------------------------*/ void SYS_Init(void); void UART0_Init(void); void TIMER0_Init(void); void AdcContScanModeTest(void); /*---------------------------------------------------------------------------------*/ /* Define global variables and constants */ /*---------------------------------------------------------------------------------*/ volatile uint8_t comRbuf[RXBUFSIZE]; volatile uint16_t comRbytes = 0; /* Available receiving bytes */ volatile uint16_t comRhead = 0; volatile uint16_t comRtail = 0; volatile int32_t g_bWait = TRUE; volatile uint32_t g_u32AdcIntFlag; /*---------------------------------------------------------------------------------*/ /* MAIN function */ /*---------------------------------------------------------------------------------*/ int main(void) { uint8_t u8Item; /* Unlock protected registers */ SYS_UnlockReg(); /* Init system clock and multi-function I/O */ SYS_Init(); /* Init UART0 to 115200-8n1 for testing */ UART0_Init(); /* Init TIMER0 for ADC continuous scan mode test */ TIMER0_Init(); printf("n"); printf("+------------------------------------------------------------------------+n"); printf("| Timer Driver Sample Code |n"); printf("+------------------------------------------------------------------------+n"); printf(" This sample code will use TM0_CNT to trigger ADC conversion and print n"); printf(" the conversion result of channel 6(GPA.6) on console.n"); printf(" Example target : M051 Series MCUn"); printf(" * Please prepare a jumper wire on GPA.6 and AGND before running the *n"); printf(" * sample code. *n"); printf("+------------------------------------------------------------------------+nn"); while(1) { printf("+------------------------------------------------------------------------+n"); printf("| Timer Control Menu |n"); printf("+------------------------------------------------------------------------+n"); printf("| [1] Timer0 continuous counting and trigger ADC for channel 6(GPA.6) |n"); printf("| [2] Quit test |n"); printf("+------------------------------------------------------------------------+n"); u8Item = getchar(); switch(u8Item) { case '1': g_bWait = TRUE; NVIC_EnableIRQ(ADC_IRQn); AdcContScanModeTest(); break; default: break; } if(u8Item == '2') break; } /* Disable ADC IP clock */ CLK_DisableModuleClock(ADC_MODULE); printf("Exit Timer test.n"); /* Disable Timer0 IP clock */ CLK_DisableModuleClock(TMR0_MODULE); /* Disable UART0 IP clock */ CLK_DisableModuleClock(UART0_MODULE); /* Disable External Interrupt */ NVIC_DisableIRQ(EINT0_IRQn); /* Disable NVIC ADC interrupt */ NVIC_DisableIRQ(ADC_IRQn); /* Lock protected registers */ SYS_LockReg(); printf("Sample code end.n"); while(1); } /*---------------------------------------------------------------------------------*/ /* ADC continuous scan mode test */ /*---------------------------------------------------------------------------------*/ void AdcContScanModeTest(void) { uint8_t u8Option; int32_t i32ConversionData[8] = {0}; printf("Conversion result of channel 6(GPA.6) will be printed continuously.n"); printf("Exit with ESC key.nn"); /* Enable ADC IP clock */ CLK_EnableModuleClock(ADC_MODULE); /* ADC clock source is HIRC, set divider to 1, ADC clock is HIRC/1. */ CLK_SetModuleClock(ADC_MODULE, CLK_CLKSEL2_ADCSEL_HIRC, CLK_CLKDIV_ADC(1)); /* Set GPB multi-function pin for ADC channel 6 */ SYS->GPB_MFP &= ~SYS_GPB_MFP_PB1_Msk; SYS->GPB_MFP |= SYS_GPB_MFP_PB1_ADC_CH6; /* Power on ADC module */ ADC_POWER_ON(ADC); /* Set the channel 6 input mode as single-end, and select channel 6 */ ADC_Open(ADC, ADC_INPUT_MODE_SINGLE_END, ADC_OPERATION_MODE_CONTINUOUS, ADC_CH_6_MASK); /* Set timer0 frequency to 1HZ */ TIMER_SET_CMP_VALUE(TIMER0, 48000000 / 2); /* Enable timer counter and interrupt */ TIMER_Start(TIMER0); /* Clear conversion finished flag for ADC channel 6 */ ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT); while(1) { /* Wait conversion finished */ while(!ADC_GET_INT_FLAG(ADC, ADC_ADF_INT)); /* Clear conversion finished flag */ ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT); /* Start next conversion */ ADC_START_CONV(ADC); /* Get the conversion result of the channel 6 */ i32ConversionData[6] = ADC_GET_CONVERSION_DATA(ADC, 6); printf("Conversion result of channel 6: %dn", i32ConversionData[6]); if(!g_bWait) break; /* Check whether ESC key is pressed */ u8Option = getchar(); if(u8Option == 27) { printf("nnESC is pressed, exit continuous scan testn"); break; } } /* Disable timer counter and interrupt */ TIMER_Stop(TIMER0); /* Disable ADC IP clock */ CLK_DisableModuleClock(ADC_MODULE); } /*---------------------------------------------------------------------------------*/ /* Init UART0 */ /*---------------------------------------------------------------------------------*/ void UART0_Init(void) { /*---------------------------------------------------------------------------------------------------------*/ /* Init UART */ /*---------------------------------------------------------------------------------------------------------*/ /* Select UART function mode */ UART0->FUNCSEL = UART_FUNCSEL_UART; /* Set UART line configuration */ UART_Open(UART0, 115200); /* Enable UART Interrupt */ UART_EnableInt(UART0, UART_IER_RDA_IEN_Msk); NVIC_EnableIRQ(UART0_IRQn); } /*---------------------------------------------------------------------------------*/ /* UART Callback function */ /*---------------------------------------------------------------------------------*/ void UART0_IRQHandler(void) { uint8_t u8InChar=0xFF; int32_t i32Idx; /* Rx Ready or Time-out INT */ if(UART_GET_INT_FLAG(UART0, UART_ISR_RDA_INT_Msk) || UART_GET_INT_FLAG(UART0, UART_ISR_TOUT_INT_Msk)) { /* Get all the input characters */ while(UART_GET_INT_FLAG(UART0, UART_ISR_RDA_INT_Msk) || UART_GET_INT_FLAG(UART0, UART_ISR_TOUT_INT_Msk)) { /* Get the character from UART Buffer */ u8InChar = UART_READ(UART0); /* Check if buffer full */ if(comRbytes < RXBUFSIZE) { /* Enqueue the character */ comRbuf[comRtail++] = u8InChar; if(comRtail >= RXBUFSIZE) comRtail = 0; comRbytes++; } else { /* FIFO over run */ } } } } /*---------------------------------------------------------------------------------*/ /* Init TIMER0 */ /*---------------------------------------------------------------------------------*/ void TIMER0_Init(void) { /* Set TIMER0 to operate in the mode of continuous counting */ TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 1); /* Set TIMER0 timeout period to 0.5 seconds */ TIMER_SET_CMP_VALUE(TIMER0, SYS_CLK/2); /* Enable TIMER0 interrupt */ TIMER_EnableInt(TIMER0); NVIC_EnableIRQ(TMR0_IRQn); } /*---------------------------------------------------------------------------------*/ /* Timer0 IRQ Handler */ /*---------------------------------------------------------------------------------*/ void TMR0_IRQHandler(void) { /* Clear TIMER0 interrupt flag */ TIMER_ClearIntFlag(TIMER1); /* Start ADC conversion */ ADC_START_CONV(ADC); } void SYS_Init(void) { /*---------------------------------------------------------------------------------------------------------*/ /* Init System Clock */ /*---------------------------------------------------------------------------------------------------------*/ /* Enable external 12MHz XTAL */ CLK_EnableXtalRC(CLK_PWRCTL_HXT_EN_Msk); /* Waiting for 12MHz clock ready */ CLK_WaitClockReady(CLK_STATUS_HXT_STB_Msk); /* Switch HCLK clock source to HXT and set HCLK divider to 1 */ CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HXT, CLK_CLKDIV_HCLK(1)); /* Enable UART and ADC module clock */ CLK_EnableModuleClock(UART0_MODULE); CLK_EnableModuleClock(ADC_MODULE); /* Select UART module clock source from HXT (UART0 is pre-selected) and set UART module clock divider to 1 */ CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HXT, CLK_CLKDIV_UART(1)); /* Select ADC module clock source from HIRC (default) and set CLKDIV = 1 */ CLK_SetModuleClock(ADC_MODULE, CLK_CLKSEL2_ADCSEL_HIRC, CLK_CLKDIV_ADC(1)); /*---------------------------------------------------------------------------------------------------------*/ /* Init I/O Multi-function */ /*---------------------------------------------------------------------------------------------------------*/ /* Set GPB multi-function pins for UART0 RXD and TXD */ Uart0DefaultMPF(); /* Set GPB multi-function pin for ADC channel 6 */ SYS->GPB_MFP |= SYS_GPB_MFP_PB1_ADC_CH6; } |
|
|
|
只有小组成员才能发言,加入小组>>
698 浏览 0 评论
1109 浏览 1 评论
2476 浏览 5 评论
2810 浏览 9 评论
移植了freeRTOS到STMf103之后显示没有定义的原因?
2653 浏览 6 评论
使用eim外接fpga可是端口一点反应都没有有没有大哥指点一下啊
655浏览 9评论
651浏览 7评论
请教大神怎样去解决iMX6Q在linux3.0.35内核上做AP失败的问题呢
783浏览 6评论
631浏览 5评论
671浏览 5评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-23 15:24 , Processed in 1.018303 second(s), Total 78, Slave 59 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号