STM32G070 DMA+SPI+LCD显示

SPI_HandleTypeDef hspi1;
DMA_HandleTypeDef hdma_spi1_tx;
/*
* 描述: LCD的SPI引脚初始化
* 参数: 无
* 返回: 无*/
void LCD_SPI_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};
	GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	GPIO_InitStruct.Alternate = GPIO_AF0_SPI1;
	HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
		
  hspi1.Instance= SPI1;
  hspi1.Init.Mode= SPI_MODE_MASTER;//设置成主机模式							
  hspi1.Init.Direction= SPI_DIRECTION_2LINES;//设置SPI单向或者双向的数据模式:
  hspi1.Init.DataSize= SPI_DATASIZE_8BIT;//设置SPI的数据大小:SPI发送接收8位帧结构
  hspi1.Init.CLKPolarity= SPI_POLARITY_HIGH;//串行同步时钟的空闲状态为高电平
  hspi1.Init.CLKPhase= SPI_PHASE_2EDGE;//串行同步时钟的第二个跳变沿(上升或下降)数据被采样
  hspi1.Init.NSS = SPI_NSS_SOFT;//内部NSS信号有SSI位控制
  hspi1.Init.BaudRatePrescaler= SPI_BAUDRATEPRESCALER_2; //波特率预分频值为2
  hspi1.Init.FirstBit= SPI_FIRSTBIT_MSB;//指数据传输从MSB位开始
  hspi1.Init.TIMode= SPI_TIMODE_DISABLE;//指定是否启用TI模式。
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;//指定是否启用CRC计算。
  hspi1.Init.CRCPolynomial= 7;//CRC值计算的多项式
	
	HAL_SPI_Init(&hspi1);//初始化
	__HAL_SPI_ENABLE(&hspi1);//使能SPI1
//	SPI_LCD_ReadByte();   //启动传输
}


/*
* 描述: LCD的DMA初始化
* 参数: 无
* 返回: 无*/
void LCD_DMA_Init(void)
{
	__HAL_RCC_DMA1_CLK_ENABLE();
	
	hdma_spi1_tx.Instance= DMA1_Channel1;        //通道选择 
	hdma_spi1_tx.Init.Request= DMA_REQUEST_SPI1_TX;
	hdma_spi1_tx.Init.Direction= DMA_MEMORY_TO_PERIPH; //存储器到外设
	hdma_spi1_tx.Init.PeriphInc= DMA_PINC_DISABLE;     //外设非增量模式
	hdma_spi1_tx.Init.MemInc= DMA_MINC_ENABLE;      //存储器增量模式
	hdma_spi1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;  //外设数据宽度
	hdma_spi1_tx.Init.MemDataAlignment= DMA_MDATAALIGN_BYTE;  //存储器数据宽度
	hdma_spi1_tx.Init.Mode = DMA_NORMAL;           //外设流控模式
	hdma_spi1_tx.Init.Priority = DMA_PRIORITY_HIGH;    //优先级
	
	if(HAL_DMA_Init(&hdma_spi1_tx) != HAL_OK)
	{
		Error_Handler();
	}   

	__HAL_LINKDMA(&hspi1,hdmatx,hdma_spi1_tx);
	
	HAL_NVIC_SetPriority(DMA1_Channel1_IRQn,1,1);
    HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
}

/*
* 描述: LCD芯片初始化
* 参数: 无
* 返回: 无*/
void LCD_GPIO_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};
	
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Pull = GPIO_PULLDOWN;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
	
	//背光,低电平为关,默认是关的
	GPIO_InitStruct.Pin = LCD_BG_GPIO_PIN;
	HAL_GPIO_Init(LCD_BG_GPIO, &GPIO_InitStruct);		
			
//	
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;	

	//复位引脚
	GPIO_InitStruct.Pin = LCD_RST_GPIO_PIN;
	HAL_GPIO_Init(LCD_RST_GPIO, &GPIO_InitStruct);	
	
	//LCD数据/命令选择引脚
	GPIO_InitStruct.Pin = LCD_DC_GPIO_PIN;
	HAL_GPIO_Init(LCD_DC_GPIO, &GPIO_InitStruct);
	
	//LCD显示片选引脚
	GPIO_InitStruct.Pin = LCD_CS_GPIO_PIN;
	HAL_GPIO_Init(LCD_CS_GPIO, &GPIO_InitStruct);

}

/*
* 描述: LCD初始化
* 参数: 无
* 返回: 无*/
void LCD_Init(void)
{
	LCD_GPIO_Init();
	LCD_SPI_Init();
	LCD_DMA_Init();
	LCD_Reset(); //
	
	LCD_WR_REG(0x11);    //Exit Sleep 
	HAL_Delay(120); 
	
	//ST7735R Frame Rate
	LCD_WR_REG(0xB1); 
	LCD_WR_DATA(0x01); 
	LCD_WR_DATA(0x2C); 
	LCD_WR_DATA(0x2D); 

	LCD_WR_REG(0xB2); 
	LCD_WR_DATA(0x01); 
	LCD_WR_DATA(0x2C); 
	LCD_WR_DATA(0x2D); 

	LCD_WR_REG(0xB3); 
	LCD_WR_DATA(0x01); 
	LCD_WR_DATA(0x2C); 
	LCD_WR_DATA(0x2D); 
	LCD_WR_DATA(0x01); 
	LCD_WR_DATA(0x2C); 
	LCD_WR_DATA(0x2D); 
	
	LCD_WR_REG(0xB4); //Column inversion 
	LCD_WR_DATA(0x07); 
	
	//ST7735R Power Sequence
	LCD_WR_REG(0xC0); 
	LCD_WR_DATA(0xA2); 
	LCD_WR_DATA(0x02); 
	LCD_WR_DATA(0x84); 
	LCD_WR_REG(0xC1); 
	LCD_WR_DATA(0xC5); 

	LCD_WR_REG(0xC2); 
	LCD_WR_DATA(0x0A); 
	LCD_WR_DATA(0x00); 

	LCD_WR_REG(0xC3); 
	LCD_WR_DATA(0x8A); 
	LCD_WR_DATA(0x2A); 
	LCD_WR_REG(0xC4); 
	LCD_WR_DATA(0x8A); 
	LCD_WR_DATA(0xEE); 
	
	LCD_WR_REG(0xC5); //VCOM 
	LCD_WR_DATA(0x0E); 
	
	LCD_WR_REG(0x36); //MX, MY, RGB mode 
	LCD_WR_DATA(0xC8); 
	
	//ST7735R Gamma Sequence
	LCD_WR_REG(0xe0); 
	LCD_WR_DATA(0x0f); 
	LCD_WR_DATA(0x1a); 
	LCD_WR_DATA(0x0f); 
	LCD_WR_DATA(0x18); 
	LCD_WR_DATA(0x2f); 
	LCD_WR_DATA(0x28); 
	LCD_WR_DATA(0x20); 
	LCD_WR_DATA(0x22); 
	LCD_WR_DATA(0x1f); 
	LCD_WR_DATA(0x1b); 
	LCD_WR_DATA(0x23); 
	LCD_WR_DATA(0x37); 
	LCD_WR_DATA(0x00); 	
	LCD_WR_DATA(0x07); 
	LCD_WR_DATA(0x02); 
	LCD_WR_DATA(0x10); 

	LCD_WR_REG(0xe1); 
	LCD_WR_DATA(0x0f); 
	LCD_WR_DATA(0x1b); 
	LCD_WR_DATA(0x0f); 
	LCD_WR_DATA(0x17); 
	LCD_WR_DATA(0x33); 
	LCD_WR_DATA(0x2c); 
	LCD_WR_DATA(0x29); 
	LCD_WR_DATA(0x2e); 
	LCD_WR_DATA(0x30); 
	LCD_WR_DATA(0x30); 
	LCD_WR_DATA(0x39); 
	LCD_WR_DATA(0x3f); 
	LCD_WR_DATA(0x00); 
	LCD_WR_DATA(0x07); 
	LCD_WR_DATA(0x03); 
	LCD_WR_DATA(0x10);  
	
	LCD_WR_REG(0x2a);
	LCD_WR_DATA(0x00);
	LCD_WR_DATA(0x00);
	LCD_WR_DATA(0x00);
	LCD_WR_DATA(0x7f);

	LCD_WR_REG(0x2b);
	LCD_WR_DATA(0x00);
	LCD_WR_DATA(0x00);
	LCD_WR_DATA(0x00);
	LCD_WR_DATA(0x9f);
	
	LCD_WR_REG(0xF0); //Enable test command  
	LCD_WR_DATA(0x01); 
	LCD_WR_REG(0xF6); //Disable ram power save mode 
	LCD_WR_DATA(0x00); 
	
	LCD_WR_REG(0x3A); //65k mode 
	LCD_WR_DATA(0x05); 
	
	LCD_WR_REG(0x29);//Display on		
		
	LCD_SetParam();//
	LCD_BG_ON();	
	
}


/*
* 描述: 显示图片
* 参数: 无
* 返回: 无*/
void LCD_Show_Imiage(u8 x,u8 y,u8 image_x,u8 image_y,const u8 *p)
{
	u8 lcd_width_tembuf[image_x*2];
	u16 i,j = 0;

	LCD_Set_Windows(x,y,x+image_x-1,y+image_y-1);
		
	SetLCD_CS_Low
	SetLCD_DC_High
	
	for(i=0;i<image_y+1;)
	{
		if(spi_dma_lcd_flag == 1)
		{
			spi_dma_lcd_flag = 0;
			for(j=0;j<image_x;j++)
			{
				lcd_width_tembuf[j*2] = *(p + 1);
				lcd_width_tembuf[j*2+1] = *p;
				p += 2;
			}
			i++;
			HAL_SPI_Transmit_DMA(&hspi1,lcd_width_tembuf,image_x*2);	
		}
	}
	SetLCD_CS_High
}


/*
* 描述: LCD显示控制
* 参数: 无
* 返回: 无*/
void LCD_Control(void)
{
	LCD_Show_Imiage(0,0,128,128,gImage_3);
}

//注意gImage_3为图片数组,需要自行设置




//------------------->stm32g0xx_it.h<-------------------//



/*
* 描述: 注册DMA中断
* 参数: 无
* 返回: 无*/
void DMA1_Channel1_IRQHandler(void)
{
  HAL_DMA_IRQHandler(&hdma_spi1_tx);
}




/*
* 描述: 编写DMA发送完成中断回调函数
* 参数: 无
* 返回: 无*/
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi)
{
	UNUSED(hspi);
	if(hspi == &hspi1)
	{
		if(0 == spi_dma_lcd_flag )
		{
			spi_dma_lcd_flag = 1;
		}
	}
}










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值