借到一块M263开发板,第一步先学点灯。折腾了2个小时终于搞清楚了,走起:
开发板的LED在PB10端口,开发板的Button1在PG4端口
接下来就是改写代码了
#include <stdio.h>
#include "NuMicro.h"
void SYS_Init(void)
{
/* Set PF multi-function pins for XT1_OUT(PF.2) and XT1_IN(PF.3) */
SYS->GPF_MFPL = (SYS->GPF_MFPL & (~SYS_GPF_MFPL_PF2MFP_Msk)) | SYS_GPF_MFPL_PF2MFP_XT1_OUT;
SYS->GPF_MFPL = (SYS->GPF_MFPL & (~SYS_GPF_MFPL_PF3MFP_Msk)) | SYS_GPF_MFPL_PF3MFP_XT1_IN;
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Enable HIRC clock */
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 clock divider as 1 */
CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));
/* Enable HXT clock */
CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);
/* Wait for HXT clock ready */
CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);
/* Enable PLL */
CLK->PLLCTL = CLK_PLLCTL_128MHz_HIRC;
/* Waiting for PLL stable */
CLK_WaitClockReady(CLK_STATUS_PLLSTB_Msk);
/* Select HCLK clock source as PLL and HCLK source divider as 2 */
CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_PLL, CLK_CLKDIV0_HCLK(2));
/* Enable UART module clock */
CLK_EnableModuleClock(UART0_MODULE);
/* Select UART module clock source as HIRC and UART module clock divider as 1 */
CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));
/*---------------------------------------------------------------------------------------------------------*/
/* Init I/O Multi-function */
/*---------------------------------------------------------------------------------------------------------*/
/* Set multi-function pins for UART0 RXD and TXD */
SYS->GPB_MFPH = (SYS->GPB_MFPH & (~(UART0_RXD_PB12_Msk | UART0_TXD_PB13_Msk))) | UART0_RXD_PB12 | UART0_TXD_PB13;
}
void UART0_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART */
/*---------------------------------------------------------------------------------------------------------*/
/* Reset UART0 */
SYS_ResetModule(UART0_RST);
/* Configure UART0 and set UART0 baud rate */
UART_Open(UART0, 115200);
}
void blink1(void)
{
}
void blink2(void)
{
CLK_SysTickDelay(500000);
GPIO_TOGGLE(PB10);
}
void blink3(void)
{
CLK_SysTickDelay(100000);
GPIO_TOGGLE(PB10);
}
/*---------------------------------------------------------------------------------------------------------*/
/* Main Function */
/*---------------------------------------------------------------------------------------------------------*/
int32_t main(void)
{
void (*blink)(void);
blink=blink1;
/* 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();
printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz\n", SystemCoreClock);
printf("+-------------------------------------------------+\n");
printf("| PB.10(Output) and PG.4(Input) Sample Code |\n");
printf("+-------------------------------------------------+\n\n");
/*-----------------------------------------------------------------------------------------------------*/
/* GPIO Basic Mode Test --- Use Pin Data Input/Output to control GPIO pin */
/*-----------------------------------------------------------------------------------------------------*/
printf(" >> LED at PB.10 ; Button1 at PG.4 \n");
/* Configure PB.10 as Output mode and PG.4 as Input mode then close it */
GPIO_SetMode(PB, BIT10, GPIO_MODE_OUTPUT);
GPIO_SetMode(PG, BIT4, GPIO_MODE_INPUT);
PB10 = 1;
printf("GPIO PB.10(output mode) and PD.7(input mode) ......");
while(1)
{
blink();
if(PG4==0)
{
CLK_SysTickDelay(50);
while(PG4==0);
blink=blink2;
}
}
}
开机后打印一些信息,然后就是进入一个空函数,这个时候LED是不亮的,因为初始化给关闭了。
然后按下Button1,松手后,修改主循环的函数指针到一个闪烁的子函数,接下来就是闪烁了。
同时学会了使用函数指针。
其实就是将函数名换成了(*p)
p为你的指针名字。比如我这里用的blink
然后调用这个指针指向的函数就是 p();
如果有参数就加上参数,如果有返回值,就加上返回值类型,没有就全部用void表示。
|