void Lcd_Init()
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); //使能GPIOB时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); //使能GPIOC时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);//使能SP1时钟(必须先使能时钟,否则配置无效)
GPIO_PinAFConfig(GPIOB,GPIO_PinSource3,GPIO_AF_0);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource5,GPIO_AF_0);
//PB3 时钟 PB5 spi 输出
// 配置SPI1的SCK, MOSI
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_3 |GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF; //复用功能
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
//GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
//GPIO_InitStructure.GPIO_PuPd =GPIO_PuPd_UP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
//PC10 片选
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10 ;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd =GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//给指定的端口写1
//GPIO_SetBits(GPIOB,GPIO_Pin_3|GPIO_Pin_5);
//给指定的端口写1
//GPIO_SetBits(GPIOC,GPIO_Pin_10);
SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx; //一线只发
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //设置SPI工作模式:设置为主SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //设置SPI的数据大小:SPI发送接收8位帧结构
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //选择了串行时钟的稳态:时钟悬空低
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //数据捕获于第二个时钟沿
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; //定义波特率预分频的值:波特率预分频值为256
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC值计算的多项式
//SPI_RxFIFOThresholdConfig(SPI1,SPI_RxFIFOThreshold_QF);//配置 SPI 的 FIFO 接收阈值(接收8bits就置 SPI_I2S_FLAG_RXNE)
//GPIO_PinAFConfig(GPIOB,GPIO_PinSource3|GPIO_PinSource5,GPIO_AF_0);//连接到外设
SPI_Init(SPI1, &SPI_InitStructure); //根据SPI_InitStruct中指定的参数初始化外设SPIx寄存器
SPI_Cmd(SPI1, ENABLE); //使能SPI外设
//设置LCD
GPIO_ResetBits(GPIOC,GPIO_Pin_10);
Delay(5);
LCD_Send(0x30, DC_CMD); ///*30---基本指令动作*/
Delay(1);
LCD_Send(0x0C, DC_CMD); ///*开显示,关游标*/
Delay(1);
LCD_Send(0x06, DC_CMD); ///*光标的移动方向*/
Delay(1);
LCD_Send(0x80, DC_CMD); //使用基本命令
Delay(1);
LCD_Send(0x01, DC_CMD); ///*清屏,地址指针指向00H*/
Delay(1);
//SPI1_ReadWriteByte(0xff); //启动传输
}
// 向LCD发送数据
void LCD_Send(unsigned char data, DCType dc)
{
Delay(1);
//这里一定要加延时
if (dc==DC_CMD) // 写指令
SPI_Send(SERIAL_WCMD); // 写入到IR
else
SPI_Send(SERIAL_WDATA );
SPI_Send(0xf0&data);
SPI_Send(data<<4);
}
//SPI发送1个数据
void SPI_Send(unsigned char data)
{
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_SendData8(SPI1, data);
}
/* x: 第几列 y: 第几行 */
void LCD_Set_XY(unsigned char x, unsigned char y)
{
unsigned char address;
switch(y){
case 0:
address = 0x80 + x;
break;
case 1:
address = 0x90 + x;
break;
case 2:
address = 0x88 + x;
break;
case 3:
address = 0x98 + x;
break;
default:
address = 0x80 + x;
break;
}
LCD_Send(address,DC_CMD);
return;
}
void LCD_PutString(unsigned char x, unsigned char y, char *str)
{
LCD_Set_XY(x,y);
while(*str)
{
LCD_Send(*str++,DC_DATA);
Delay(1);
}
return;
}
/*--------------清DDRAM------------------*/
void LCD_Clrram (void)
{
LCD_Send (0x30,DC_CMD);
Delay(1);
LCD_Send (0x01,DC_CMD);
Delay(1);
}
int main(void)
{
Init_Main();
Lcd_Init();
LCD_Clrram ();
Delay(50);
LCD_PutString(0,0, "工资涨涨");
LCD_PutString(0,1, "工资涨涨");
LCD_PutString(0,2, "工资涨涨");
Delay(5000);
LCD_Clrram ();
Delay(50);
LCD_Clrram ();
Delay(50);
}
现象:开机第一次不能显示,在随后可以显示文字,随后再一次乱码,这是那里出了问题,请大侠们赐教。
|