航顺HK32F030MF4P6开发脱坑小记
航顺HK32F030MF4P6开发脱坑记1.SWCLK和SDWIO复用
1.1复用成IO,需要使用IOMUX寄存器
如将PB5(SDWIO)复用成输入IO口
GPIO_InitTypeDef m_gpio;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_IOMUX,ENABLE); //复用io口的时钟使能
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
m_gpio.GPIO_Mode = GPIO_Mode_IN;
m_gpio.GPIO_Pin = GPIO_Pin_5;
m_gpio.GPIO_PuPd = GPIO_PuPd_UP;
m_gpio.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_IOMUX_ChangePin(IOMUX_PIN11,IOMUX_PB5_SEL_PB5);
GPIO_Init(GPIOB, &m_gpio);
如果将PB5(SDWIO)复用成输出IO
GPIO_InitTypeDef m_gpio;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_IOMUX,ENABLE); //复用io口的时钟使能
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
m_gpio.GPIO_Mode = GPIO_Mode_OUT;
m_gpio.GPIO_OType = GPIO_OType_PP;
m_gpio.GPIO_Pin = GPIO_Pin_5;
m_gpio.GPIO_PuPd = GPIO_PuPd_UP;
m_gpio.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_IOMUX_ChangePin(IOMUX_PIN11,IOMUX_PB5_SEL_PB5);
GPIO_Init(GPIOB, &m_gpio); 注意1:不同pin数的芯片的其复用开关有差别,这里20脚的用IOMUX_PIN11,一定要查手册,另外宏定义一定要用选用的芯片,例程中的和选用的可能不一样。
注意2:作为输入IO,不要使用库里的uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)函数,而要使用uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin),两者有差别。英文含义也能判断。
复用成AD
如将SWDIO复用成AD0,要是用AF,作为多功能选择。不需要配置IOMUX寄存器, 代码如下
static void ADC0_PortInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOD,GPIO_PinSource5,GPIO_AF_7);
}
定时器
定时器选用TIM1和TIM2,使用基本定时器功能时,方法和stm32的类似。TIM_Period为计数周期(次数),TIM_Prescaler为时钟源的分频数。如果时钟源为32M,分频数为32000-1,那么给定时器提供的时钟为:32M/32000=1000Hz(1ms)。 如果计数100次,也就是100ms。 void Timer1_Init(void)
{
TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
uint16_t PrescalerValue = 0;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
/* Compute the prescaler value */
PrescalerValue = (uint16_t) ((SystemCoreClock ) / 32000000) - 1;//配置频率为32M
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 100-1; //对于1kHZ(32M/32000)的时钟源,计数100次,则为100毫秒中断一次
TIM_TimeBaseStructure.TIM_Prescaler = 32000-1; //100ms中断一次
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* TIM Interrupts enable */
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
/* TIM3 enable counter */
TIM_Cmd(TIM1, ENABLE);
/* Enable the TIM2 gloabal Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TRG_COM_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
另外要注意:中断服务函数是void TIM2_IRQHandler(void),不是随便小写的,这里直接拷贝别处的,导致没反应。
页:
[1]