感谢电子产品世界和ST提供的本次NUCLEO-F412ZG的测评机会。
NUCLEO-F412ZG板卡具有3个用户LED和2个用户按钮和复位按钮。板连接器:SWDST Zio扩展连接器,包括ARDUINO® Uno V3ST morpho扩展连接器
灵活的供电选项:ST-LINK、USB VBUS或外部电源;具有USB重新枚举功能的板上ST-LINK调试器/编程器:大容量存储器、虚拟COM端口和调试端口
提供了全面的免费软件库和例程,可从STM32Cube MCU软件包获得支持多种集成开发环境(IDE),包括IAR™、Keil®、和STM32CubeIDE
开发板虽然支持符合IEEE-802.3-2002规范的以太网,但是可以看到板卡上的以太网接口和相关的变压器并没有焊接,所以本次测评暂时不进行以太网的测试。
一、使用STM32 CubeMX生成一个KEIL MDK例程
在Example Selector栏选择Board -> NUCLEO-F412ZG.
然后点击“Start Project in External IDE”:
自动生成的项目结构如下,并且MDK会自动打开该项目。
D:.
├─Drivers
│ ├─BSP
│ │ └─STM32F4xx_Nucleo_144
│ │ └─_htmresc
│ ├─CMSIS
│ │ ├─Device
│ │ │ └─ST
│ │ │ └─STM32F4xx
│ │ │ └─Include
│ │ └─Include
│ └─STM32F4xx_HAL_Driver
│ ├─Inc
│ │ └─Legacy
│ └─Src
├─EWARM
├─Inc
├─MDK-ARM
│ ├─DebugConfig
│ ├─RTE
│ │ └─_STM32412ZG_Nucleo
│ └─STM32412ZG_Nucleo
├─Src
└─STM32CubeIDE
└─Example
├─Startup
└─User
其中核心的主函数代码如下:
int main(void) { /* This sample code shows how to use GPIO HAL API to toggle LED1 and LED3 IOs in an infinite loop. */ /* STM32F4xx HAL library initialization: - Configure the Flash prefetch - Systick timer is configured by default as source of time base, but user can eventually implement his proper time base source (a general purpose timer for example or other time source), keeping in mind that Time base duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and handled in milliseconds basis. - Set NVIC Group Priority to 4 - Low Level Initialization */ HAL_Init(); /* Configure the system clock to 100 MHz */ SystemClock_Config(); /* -1- Enable GPIO Clock (to be able to program the configuration registers) */ LED1_GPIO_CLK_ENABLE(); LED2_GPIO_CLK_ENABLE(); LED3_GPIO_CLK_ENABLE(); /* -2- Configure IO in output push-pull mode to drive external LEDs */ GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Pin = LED1_PIN; HAL_GPIO_Init(LED1_GPIO_PORT, &GPIO_InitStruct); GPIO_InitStruct.Pin = LED2_PIN; HAL_GPIO_Init(LED2_GPIO_PORT, &GPIO_InitStruct); GPIO_InitStruct.Pin = LED3_PIN; HAL_GPIO_Init(LED3_GPIO_PORT, &GPIO_InitStruct); /* -3- Toggle IO in an infinite loop */ while (1) { HAL_GPIO_TogglePin(LED1_GPIO_PORT, LED1_PIN); /* Insert delay 100 ms */ HAL_Delay(100); HAL_GPIO_TogglePin(LED2_GPIO_PORT, LED2_PIN); /* Insert delay 100 ms */ HAL_Delay(100); HAL_GPIO_TogglePin(LED3_GPIO_PORT, LED3_PIN); /* Insert delay 100 ms */ HAL_Delay(100); } }
接着在KEIL里面检查一下烧录相关的设置,打开项目设置:
点击Download,即可烧录程序至F412.
实物展示:
二、增加TFT屏幕相关驱动
本着从易到难的方式,在前期实现GPIO驱动的基础上,通过更多的GPIO引脚模拟TFT的SPI引脚功能,来实现驱动TFT屏幕的目标。
硬件连接方面:
对应到开发板上的Arduino接口电路图如下:
PIN相关的操作如下:
#define SPI_MOSI_SET HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_SET) #define SPI_SCLK_SET HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET) #define LCD_CS_SET HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET)//片选 #define LCD_RS_SET HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_SET)//数据/命令 #define LCD_RST_SET HAL_GPIO_WritePin(GPIOF, GPIO_PIN_12, GPIO_PIN_SET)//复位 #define SPI_MOSI_CLR HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET) #define SPI_SCLK_CLR HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET #define LCD_CS_CLR HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET) //片选 #define LCD_RS_CLR HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_RESET)//数据/命令 #define LCD_RST_CLR HAL_GPIO_WritePin(GPIOF, GPIO_PIN_12, GPIO_PIN_RESET)//复位
TFT屏幕的接口不是Arduino兼容屏幕,为了避免繁琐的接线从而设计了一个简易转接板:
适配在一起:
代码实现上,首先是lcd初始化:
void LCD_Init(void) { LCD_GPIOInit();//LCD GPIO³õʼ»¯ LCD_RESET(); //LCD ¸´Î» //*************2.4inch ILI9341³õʼ»¯**********// LCD_WR_REG(0xCF); LCD_WR_DATA(0x00); LCD_WR_DATA(0xD9); //0xC1 LCD_WR_DATA(0X30); LCD_WR_REG(0xED); LCD_WR_DATA(0x64); LCD_WR_DATA(0x03); LCD_WR_DATA(0X12); LCD_WR_DATA(0X81); LCD_WR_REG(0xE8); LCD_WR_DATA(0x85); LCD_WR_DATA(0x10); LCD_WR_DATA(0x7A); LCD_WR_REG(0xCB); LCD_WR_DATA(0x39); LCD_WR_DATA(0x2C); LCD_WR_DATA(0x00); LCD_WR_DATA(0x34); LCD_WR_DATA(0x02); LCD_WR_REG(0xF7); LCD_WR_DATA(0x20); LCD_WR_REG(0xEA); LCD_WR_DATA(0x00); LCD_WR_DATA(0x00); LCD_WR_REG(0xC0); //Power control LCD_WR_DATA(0x1B); //VRH[5:0] LCD_WR_REG(0xC1); //Power control LCD_WR_DATA(0x12); //SAP[2:0];BT[3:0] 0x01 LCD_WR_REG(0xC5); //VCM control LCD_WR_DATA(0x08); //30 LCD_WR_DATA(0x26); //30 LCD_WR_REG(0xC7); //VCM control2 LCD_WR_DATA(0XB7); LCD_WR_REG(0x36); // Memory Access Control LCD_WR_DATA(0x08); LCD_WR_REG(0x3A); LCD_WR_DATA(0x55); LCD_WR_REG(0xB1); LCD_WR_DATA(0x00); LCD_WR_DATA(0x1A); LCD_WR_REG(0xB6); // Display Function Control LCD_WR_DATA(0x0A); LCD_WR_DATA(0xA2); LCD_WR_REG(0xF2); // 3Gamma Function Disable LCD_WR_DATA(0x00); LCD_WR_REG(0x26); //Gamma curve selected LCD_WR_DATA(0x01); LCD_WR_REG(0xE0); //Set Gamma LCD_WR_DATA(0x0F); LCD_WR_DATA(0x1D); LCD_WR_DATA(0x1A); LCD_WR_DATA(0x0A); LCD_WR_DATA(0x0D); LCD_WR_DATA(0x07); LCD_WR_DATA(0x49); LCD_WR_DATA(0X66); LCD_WR_DATA(0x3B); LCD_WR_DATA(0x07); LCD_WR_DATA(0x11); LCD_WR_DATA(0x01); LCD_WR_DATA(0x09); LCD_WR_DATA(0x05); LCD_WR_DATA(0x04); LCD_WR_REG(0XE1); //Set Gamma LCD_WR_DATA(0x00); LCD_WR_DATA(0x18); LCD_WR_DATA(0x1D); LCD_WR_DATA(0x02); LCD_WR_DATA(0x0F); LCD_WR_DATA(0x04); LCD_WR_DATA(0x36); LCD_WR_DATA(0x13); LCD_WR_DATA(0x4C); LCD_WR_DATA(0x07); LCD_WR_DATA(0x13); LCD_WR_DATA(0x0F); LCD_WR_DATA(0x2E); LCD_WR_DATA(0x2F); LCD_WR_DATA(0x05); LCD_WR_REG(0x2B); LCD_WR_DATA(0x00); LCD_WR_DATA(0x00); LCD_WR_DATA(0x01); LCD_WR_DATA(0x3f); LCD_WR_REG(0x2A); LCD_WR_DATA(0x00); LCD_WR_DATA(0x00); LCD_WR_DATA(0x00); LCD_WR_DATA(0xef); LCD_WR_REG(0x11); //Exit Sleep HAL_Delay(120); LCD_WR_REG(0x29); //display on LCD_direction(USE_HORIZONTAL);//设置LCD显示方向 LCD_Clear(WHITE);//清全屏白色 } void LCD_WR_DATA(uint8_t data) { LCD_CS_CLR; LCD_RS_SET; SPIv_WriteData(data); LCD_CS_SET; }
其次是写数据和写命令:
void LCD_WriteReg(uint8_t LCD_Reg, uint16_t LCD_RegValue) { LCD_WR_REG(LCD_Reg); LCD_WR_DATA(LCD_RegValue); } void LCD_WR_REG(uint8_t data) { LCD_CS_CLR; LCD_RS_CLR; SPIv_WriteData(data); LCD_CS_SET; } void LCD_WR_DATA(uint8_t data) { LCD_CS_CLR; LCD_RS_SET; SPIv_WriteData(data); LCD_CS_SET; }
三、测试
2024/8/8 触摸部分基本正常工作,如下所示:凯蒂猫 + EEPW手写。后续整理再分享给大家。
四、总结
通过本次的测评,体验了STM32的CubeMx强大的代码生成功能,用户可以根据使用习惯来生成适配KEIL或者IAR或者STM32CUBE IDE的工程。进而在此基础上,开发出更加有趣、功能更加丰富的功能。
同时借助本次测评机会,也验证了前段时间打板的一个TFT转接板的功能正常,这样后续在进行屏幕显示相关功能时,就无需通过杜邦线来回穿梭了,同时也避免了杜邦线失效带来的一些困扰。