完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
#include
#include "M051Series.h" #define PLL_CLOCK 50000000 void SYS_Init(void) { /* Unlock protected registers */ SYS_UnlockReg(); /*---------------------------------------------------------------------------------------------------------*/ /* Init System Clock */ /*---------------------------------------------------------------------------------------------------------*/ /* Enable Internal RC 22.1184MHz clock */ CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk); /* Waiting for Internal RC clock ready */ CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk); /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */ CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1)); /* Enable external XTAL 12MHz clock */ CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk); /* Waiting for external XTAL clock ready */ CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk); /*选择外部时钟为HCLK时钟*/ // CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HXT, CLK_CLKDIV_HCLK(1)); /* Set core clock as PLL_CLOCK from PLL */ CLK_SetCoreClock(PLL_CLOCK); /* Enable UART module clock */ CLK_EnableModuleClock(UART0_MODULE); /* Select UART module clock source */ CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_PLL, CLK_CLKDIV_UART(1)); /*---------------------------------------------------------------------------------------------------------*/ /* Init I/O Multi-function */ /*---------------------------------------------------------------------------------------------------------*/ /* Set P3 multi-function pins for UART0 RXD and TXD */ SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk); SYS->P3_MFP |= (SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0); /* Lock protected registers */ SYS_LockReg(); } void UART0_Init(void) { /*---------------------------------------------------------------------------------------------------------*/ /* Init UART */ /*---------------------------------------------------------------------------------------------------------*/ /* Reset UART0 */ SYS_ResetModule(UART0_RST); UART_Open(UART0, 115200); } /*---------------------------------------------------------------------------------------------------------*/ /* MAIN function */ /*---------------------------------------------------------------------------------------------------------*/ int main(void) { //uint8_t a=1; //uint8_t *Buf=&a; /* Init System, peripheral clock and multi-function I/O */ unsigned char i,j; SYS_Init(); /* Init UART0 for printf */ UART0_Init(); // printf("\n\nCPU %d Hz\n", SystemCoreClock); // printf("+-------------------------------------------------+\n"); // printf("| P1.2(Output) and P4.1(Input) Sample Code |\n"); // printf("+-------------------------------------------------+\n\n"); /* Configure P1.2 as Output mode and P4.1 as Input mode */ GPIO_SetMode(P4, BIT2, GPIO_PMD_OUTPUT); while(1) { j=0; P42=1; //延时0.1s CLK_SysTickDelay(100000); P42=0; CLK_SysTickDelay(100000); for(i=0;i<5;i++) { UART_Write(UART0, &j, 1); j++; CLK_SysTickDelay(100000); } } } 先把UART时钟源选HIRC试试看,是不是外部晶振没起振?另外,用示波器量一下P3.1有没有波形输出? 先测试官方例子 /**************************************************************************** * @file main.c * @version V1.00 * $Revision: 4 $ * $Date: 14/01/28 11:45a $ * @brief M051 Series UART Interface Controller Driver Sample Code * * @note * Copyright (C) 2011 Nuvoton Technology Corp. All rights reserved. * ******************************************************************************/ #include #include "M051Series.h" #define PLL_CLOCK 50000000 #define RXBUFSIZE 1024 /*---------------------------------------------------------------------------------------------------------*/ /* Global variables */ /*---------------------------------------------------------------------------------------------------------*/ uint8_t g_u8RecData[RXBUFSIZE] = {0}; volatile uint32_t g_u32comRbytes = 0; volatile uint32_t g_u32comRhead = 0; volatile uint32_t g_u32comRtail = 0; volatile int32_t g_bWait = TRUE; /*---------------------------------------------------------------------------------------------------------*/ /* Define functions prototype */ /*---------------------------------------------------------------------------------------------------------*/ int32_t main(void); void UART_TEST_HANDLE(void); void UART_FunctionTest(void); void SYS_Init(void) { /*---------------------------------------------------------------------------------------------------------*/ /* Init System Clock */ /*---------------------------------------------------------------------------------------------------------*/ /* Enable Internal RC 22.1184MHz clock */ CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk); /* Waiting for Internal RC clock ready */ CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk); /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */ CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1)); /* Enable external XTAL 12MHz clock */ CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk); /* Waiting for external XTAL clock ready */ CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk); /* Set core clock as PLL_CLOCK from PLL */ CLK_SetCoreClock(PLL_CLOCK); /* Enable UART module clock */ CLK_EnableModuleClock(UART0_MODULE); /* Select UART module clock source */ CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1)); /*---------------------------------------------------------------------------------------------------------*/ /* Init I/O Multi-function */ /*---------------------------------------------------------------------------------------------------------*/ /* Set P3 multi-function pins for UART0 RXD and TXD */ SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk); SYS->P3_MFP |= (SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0); } void UART0_Init() { /*---------------------------------------------------------------------------------------------------------*/ /* Init UART */ /*---------------------------------------------------------------------------------------------------------*/ /* Reset IP */ SYS_ResetModule(UART0_RST); /* Configure UART0 and set UART0 Baudrate */ UART_Open(UART0, 115200); } /*---------------------------------------------------------------------------------------------------------*/ /* UART Test Sample */ /* Test Item */ /* It sends the received data to HyperTerminal. */ /*---------------------------------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------------------------------*/ /* MAIN function */ /*---------------------------------------------------------------------------------------------------------*/ int main(void) { /* Unlock protected registers */ SYS_UnlockReg(); /* Init System, IP clock and multi-function I/O */ SYS_Init(); /* Lock protected registers */ SYS_LockReg(); /* Init UART0 for printf and testing */ UART0_Init(); /*---------------------------------------------------------------------------------------------------------*/ /* SAMPLE CODE */ /*---------------------------------------------------------------------------------------------------------*/ printf("\n\nCPU @ %dHz\n", SystemCoreClock); printf("\n\nUART Sample Program\n"); /* UART sample function */ UART_FunctionTest(); } /*---------------------------------------------------------------------------------------------------------*/ /* ISR to handle UART Channel 0 interrupt event */ /*---------------------------------------------------------------------------------------------------------*/ void UART0_IRQHandler(void) { UART_TEST_HANDLE(); } /*---------------------------------------------------------------------------------------------------------*/ /* UART Callback function */ /*---------------------------------------------------------------------------------------------------------*/ void UART_TEST_HANDLE() { uint8_t u8InChar = 0xFF; uint32_t u32IntSts = UART0->ISR; if(u32IntSts & UART_ISR_RDA_INT_Msk) { printf("\nInput:"); /* Get all the input characters */ while(UART_IS_RX_READY(UART0)) { /* Get the character from UART Buffer */ u8InChar = UART_READ(UART0); printf("%c ", u8InChar); if(u8InChar == '0') { g_bWait = FALSE; } /* Check if buffer full */ if(g_u32comRbytes < RXBUFSIZE) { /* Enqueue the character */ g_u8RecData[g_u32comRtail] = u8InChar; g_u32comRtail = (g_u32comRtail == (RXBUFSIZE - 1)) ? 0 : (g_u32comRtail + 1); g_u32comRbytes++; } } printf("\nTransmission Test:"); } if(u32IntSts & UART_ISR_THRE_INT_Msk) { uint16_t tmp; tmp = g_u32comRtail; if(g_u32comRhead != tmp) { u8InChar = g_u8RecData[g_u32comRhead]; UART_WRITE(UART0, u8InChar); g_u32comRhead = (g_u32comRhead == (RXBUFSIZE - 1)) ? 0 : (g_u32comRhead + 1); g_u32comRbytes--; } } } /*---------------------------------------------------------------------------------------------------------*/ /* UART Function Test */ /*---------------------------------------------------------------------------------------------------------*/ void UART_FunctionTest() { printf("+-----------------------------------------------------------+\n"); printf("| UART Function Test |\n"); printf("+-----------------------------------------------------------+\n"); printf("| Description : |\n"); printf("| The sample code will print input char on terminal |\n"); printf("| Please enter any to start (Press '0' to exit) |\n"); printf("+-----------------------------------------------------------+\n"); /* Using a RS232 cable to connect UART0 and PC. UART0 is set to debug port. UART0 is enable RDA and RLS interrupt. When inputing char to terminal screen, RDA interrupt will happen and UART0 will print the received char on screen. */ /* Enable Interrupt and install the call back function */ UART_ENABLE_INT(UART0, (UART_IER_RDA_IEN_Msk | UART_IER_THRE_IEN_Msk | UART_IER_RTO_IEN_Msk)); NVIC_EnableIRQ(UART0_IRQn); while(g_bWait); /* Disable Interrupt */ UART_DISABLE_INT(UART0, (UART_IER_RDA_IEN_Msk | UART_IER_THRE_IEN_Msk | UART_IER_RTO_IEN_Msk)); NVIC_DisableIRQ(UART0_IRQn); g_bWait = TRUE; printf("\nUART Sample Demo End.\n"); } |
|
相关推荐 |
|
只有小组成员才能发言,加入小组>>
818 浏览 0 评论
1162 浏览 1 评论
2537 浏览 5 评论
2872 浏览 9 评论
移植了freeRTOS到STMf103之后显示没有定义的原因?
2720 浏览 6 评论
keil5中manage run-time environment怎么是灰色,不可以操作吗?
1115浏览 3评论
198浏览 2评论
465浏览 2评论
382浏览 2评论
M0518 PWM的电压输出只有2V左右,没有3.3V是怎么回事?
463浏览 1评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-29 13:30 , Processed in 0.989943 second(s), Total 77, Slave 58 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号