STM32F407中的RT-thread系统中移植LCD的程序-FSMC控制

环境

  • 开发板:正点原子探索者
  • 芯片:STM32F407ZGT6
  • 系统:RT-Thread
  • 屏幕型号:TFTLCD 55510 4.3寸
  • 开发软件:Keil,CubeMX, Env

目的

现在将裸板上的LCD程序,移植到RT-thread上,裸板程序有lcd.c 和lcd.h,front.h

过程

使用F4芯片的FSMC来控制LCD,类似与将LCD当初存储设备SRAM,利用 CubeMX来进行配置
在 FSMC的bank1下的NE4下配置LCD。这样的话芯片与LCD对应的GPIO接口都可以被配置
STM32F407中的RT-thread系统中移植LCD的程序-FSMC控制_第1张图片

配置完成后再打开env,生成mdk5工程
STM32F407中的RT-thread系统中移植LCD的程序-FSMC控制_第2张图片

把lcd的相关代码文件,放到新工程中,这里与main放在一起
STM32F407中的RT-thread系统中移植LCD的程序-FSMC控制_第3张图片
lcd.c需要修改一下,LCD_init原本有时钟使能,GPIO配置,引脚复用的配置,都清除,CubeMX图形配置会做这些事。有些裸板上的头文件也可以去掉,换成rt-thread常用的头文件。FSMC的配置放到main里
STM32F407中的RT-thread系统中移植LCD的程序-FSMC控制_第4张图片
另外在把对应的延时函数,换成rt-thread上的对应函数,gpio的使用接口也要替换,例如点亮LCD背光的部分

	LCD_Display_Dir(0);		//默认为竖屏
	rt_pin_mode(LCD_PIN, PIN_MODE_OUTPUT);
	rt_pin_write(LCD_PIN, PIN_HIGH);				//点亮背光
	LCD_Clear(WHITE);

这里需要添加下面两个库文件,main函数中对FSMC初始化要用到
STM32F407中的RT-thread系统中移植LCD的程序-FSMC控制_第5张图片

在main函数中添加MX_FSMC_Init,进行FSMC的初始话,lcd_display在LCD上显示

SRAM_HandleTypeDef hsram1;
static void MX_FSMC_Init(void)
{

  /* USER CODE BEGIN FSMC_Init 0 */

  /* USER CODE END FSMC_Init 0 */

  FSMC_NORSRAM_TimingTypeDef Timing = {0};
  FSMC_NORSRAM_TimingTypeDef ExtTiming = {0};

  /* USER CODE BEGIN FSMC_Init 1 */

  /* USER CODE END FSMC_Init 1 */

  /** Perform the SRAM1 memory initialization sequence
  */
  hsram1.Instance = FSMC_NORSRAM_DEVICE;
  hsram1.Extended = FSMC_NORSRAM_EXTENDED_DEVICE;
  /* hsram1.Init */
  hsram1.Init.NSBank = FSMC_NORSRAM_BANK4;
  hsram1.Init.DataAddressMux = FSMC_DATA_ADDRESS_MUX_DISABLE;
  hsram1.Init.MemoryType = FSMC_MEMORY_TYPE_SRAM;
  hsram1.Init.MemoryDataWidth = FSMC_NORSRAM_MEM_BUS_WIDTH_16;
  hsram1.Init.BurstAccessMode = FSMC_BURST_ACCESS_MODE_DISABLE;
  hsram1.Init.WaitSignalPolarity = FSMC_WAIT_SIGNAL_POLARITY_LOW;
  hsram1.Init.WrapMode = FSMC_WRAP_MODE_DISABLE;
  hsram1.Init.WaitSignalActive = FSMC_WAIT_TIMING_BEFORE_WS;
  hsram1.Init.WriteOperation = FSMC_WRITE_OPERATION_ENABLE;
  hsram1.Init.WaitSignal = FSMC_WAIT_SIGNAL_DISABLE;
  hsram1.Init.ExtendedMode = FSMC_EXTENDED_MODE_ENABLE;
  hsram1.Init.AsynchronousWait = FSMC_ASYNCHRONOUS_WAIT_DISABLE;
  hsram1.Init.WriteBurst = FSMC_WRITE_BURST_DISABLE;
  /* Timing */
  Timing.AddressSetupTime = 15;
  Timing.AddressHoldTime = 0;
  Timing.DataSetupTime = 24;
  Timing.BusTurnAroundDuration = 0;
  Timing.CLKDivision = 0;
  Timing.DataLatency = 0;
  Timing.AccessMode = FSMC_ACCESS_MODE_A;
  /* ExtTiming */
  ExtTiming.AddressSetupTime = 8;
  ExtTiming.AddressHoldTime = 15;
  ExtTiming.DataSetupTime = 8;
  ExtTiming.BusTurnAroundDuration = 0;
  ExtTiming.CLKDivision = 0;
  ExtTiming.DataLatency = 0;
  ExtTiming.AccessMode = FSMC_ACCESS_MODE_A;

  if (HAL_SRAM_Init(&hsram1, &Timing, &ExtTiming) != HAL_OK)
  {
    Error_Handler( );
  }

  /** Disconnect NADV
  */

//  __HAL_AFIO_FSMCNADV_DISCONNECTED();

  /* USER CODE BEGIN FSMC_Init 2 */

  /* USER CODE END FSMC_Init 2 */
}

static int lcd_display(void)
{ 
 	u8 x=0;
	u8 lcd_id[12];				//存放LCD ID字符串
 	LCD_Init();           //初始化LCD FSMC接口
	POINT_COLOR=RED;      //画笔颜色:红色
	sprintf((char*)lcd_id,"LCD ID:%04X",lcddev.id);//将LCD ID打印到lcd_id数组。				 	
  	while(1) 
	{		 
		switch(x)
		{
			case 0:LCD_Clear(WHITE);break;
			case 1:LCD_Clear(BLACK);break;
			case 2:LCD_Clear(BLUE);break;
			case 3:LCD_Clear(RED);break;
			case 4:LCD_Clear(MAGENTA);break;
			case 5:LCD_Clear(GREEN);break;
			case 6:LCD_Clear(CYAN);break; 
			case 7:LCD_Clear(YELLOW);break;
			case 8:LCD_Clear(BRRED);break;
			case 9:LCD_Clear(GRAY);break;
			case 10:LCD_Clear(LGRAY);break;
			case 11:LCD_Clear(BROWN);break;
		}
		POINT_COLOR=RED;	  
		LCD_ShowString(30,40,210,24,24,"Explorer STM32F4");	
		LCD_ShowString(30,70,200,16,16,"TFTLCD TEST");
		LCD_ShowString(30,90,200,16,16,"RT-thread");
 		LCD_ShowString(30,110,200,16,16,lcd_id);		//显示LCD ID	      					 
		LCD_ShowString(30,130,200,12,12,"2014/5/4");	      					 
		x++;
		if(x==12)x=0;
        rt_pin_write(LED0_PIN, PIN_HIGH);
        rt_thread_mdelay(500);
        rt_pin_write(LED0_PIN, PIN_LOW);
        rt_thread_mdelay(500);
	} 
}
MSH_CMD_EXPORT(lcd_display, lcd_display samole);

上电启动并在uart串口上显示的输入命令lcd_display,即可正常显示

你可能感兴趣的:(RT-thread,STM32,rt-thread)