回答

收藏

[评测分享] 【灵动Motor-DK电机控制板】-2、搭建开发环境点灯测试

#板卡评测 #板卡评测 1584 人阅读 | 0 人回复 | 2023-07-27

  本次主要是基于灵动微电子Motor-DK电机控制板搭建开发环境并点灯测试。
以下测试使用keil软件,首先要安装keil的MCU pack包。然后参照例程初始化LED和按键KEY的gpio,顺便初始化串口,方便数据调试输出。
创建模板工程如下:


根据板子原理图得知LED是PB9,KEY1是PB8端口。


下面初始化PB8和PB9端口。
  1. void PLATFORM_InitGPIO(void)
  2. {
  3.     GPIO_InitTypeDef GPIO_InitStruct;

  4.     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  5.     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
  6.    
  7.     //LED = PB9
  8.     GPIO_StructInit(&GPIO_InitStruct);
  9.     GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_9;
  10.     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High;
  11.     GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_Out_PP;
  12.     GPIO_Init(GPIOB, &GPIO_InitStruct);
  13.     //KEY1 = PB8
  14.     GPIO_StructInit(&GPIO_InitStruct);
  15.     GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_8;
  16.     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High;
  17.     GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_IPU;
  18.     GPIO_Init(GPIOB, &GPIO_InitStruct);
  19. }
复制代码
初始化串口,串口是用PA11和PA12端口:
  1. void PLATFORM_InitConsole(uint32_t Baudrate)
  2. {
  3.     GPIO_InitTypeDef  GPIO_InitStruct;
  4.     USART_InitTypeDef USART_InitStruct;
  5.    
  6.     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

  7.     GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_5);
  8.     GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_5);

  9.     GPIO_StructInit(&GPIO_InitStruct);
  10.     GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_11|GPIO_Pin_12;
  11.     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High;
  12.     GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_AF_PP;
  13.     GPIO_Init(GPIOA, &GPIO_InitStruct);
  14.    
  15.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART1, ENABLE);

  16.     USART_StructInit(&USART_InitStruct);
  17.     USART_InitStruct.USART_BaudRate   = Baudrate;
  18.     USART_InitStruct.USART_WordLength = USART_WordLength_8b;
  19.     USART_InitStruct.USART_StopBits   = USART_StopBits_1;
  20.     USART_InitStruct.USART_Parity     = USART_Parity_No;
  21.     USART_InitStruct.USART_Mode       = USART_Mode_Tx|USART_Mode_Rx;
  22.     USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  23.     USART_Init(USART1, &USART_InitStruct);

  24.     USART_Cmd(USART1, ENABLE);
  25. }
复制代码
下面映射printf输出:
  1. int fputc(int ch, FILE *f)
  2. {
  3.     USART_SendData(USART1, (uint8_t)ch);
  4.     while (RESET == USART_GetFlagStatus(USART1, USART_FLAG_TC))
  5.     {
  6.     }
  7.     return (ch);
  8. }
复制代码
main测试点灯和按键:
  1. int main(void)
  2. {
  3.     PLATFORM_Init();
  4.     printf("Demo Test %s", __FUNCTION__);
  5.     while (1)
  6.     {
  7.         PLATFORM_DelayMS(10);
  8.         if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_8) == 0)
  9.         {
  10.             GPIO_ResetBits(GPIOB, GPIO_Pin_9);
  11.             printf("KeyPress.\r\n");
  12.         }else
  13.         {
  14.             GPIO_SetBits(GPIOB, GPIO_Pin_9);
  15.             PLATFORM_DelayMS(100);
  16.             GPIO_ResetBits(GPIOB, GPIO_Pin_9);
  17.             PLATFORM_DelayMS(100);
  18.         }
  19.     }
  20. }
复制代码
编译下载代码,我们就可以看到led闪烁了,注意led焊反了要处理下。
按下按键led常亮。



代码模板如下:
MM32-GPIO.zip (559.33 KB, 下载次数: 0)






分享到:
回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条