|

楼主 |
发表于 2014-1-3 19:54:16
|
显示全部楼层
#include "stm32f4xx.h"
#include "delay.h"
#include "stdio.h"
#define RA8875_CS_0() GPIO_ResetBits(GPIOA, GPIO_Pin_4)
#define RA8875_CS_1() GPIO_SetBits(GPIOA, GPIO_Pin_4)
#define RA8875_RST_0() GPIO_ResetBits(GPIOA, GPIO_Pin_3)
#define RA8875_RST_1() GPIO_SetBits(GPIOA, GPIO_Pin_3)
void RA8875_InitSPI(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
/* 开启GPIO时钟 */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
/* 开启 SPI1 外设时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource5,GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_SPI1);
/* 配置 PA5、PA7 为复用推挽输出,用于 SCK, and MOSI */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 |GPIO_Pin_7;
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(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_3;
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_UP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
/* 配置 SPI1工作模式 */
SPI_Cmd(SPI1,DISABLE);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; /* 软件控制片选 */
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1,&SPI_InitStructure);
/* 使能 SPI1 */
SPI_Cmd(SPI1,ENABLE);
}
uint8_t SPI_ShiftByte(uint8_t _ucByte)
{
uint8_t ucRxByte;
/* 等待发送缓冲区空 */
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
/* 发送一个字节 */
SPI_I2S_SendData(SPI1, _ucByte);
/* 等待数据接收完毕 */
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
/* 读取接收到的数据 */
ucRxByte = SPI_I2S_ReceiveData(SPI1);
/* 返回读到的数据 */
return ucRxByte;
}
void RA8875_WriteCmd(uint8_t _ucRegAddr)
{
RA8875_CS_0();
__nop();__nop();
SPI_ShiftByte(0x80);
SPI_ShiftByte(_ucRegAddr);
__nop();__nop();
}
void RA8875_WriteData(uint8_t _ucRegValue)
{
RA8875_CS_0();
__nop();__nop();
SPI_ShiftByte(0x00);
SPI_ShiftByte(_ucRegValue);
__nop();__nop();
}
void RA8875_WriteReg(uint8_t _ucRegAddr, uint8_t _ucRegValue)
{
RA8875_WriteCmd(_ucRegAddr);
RA8875_WriteData(_ucRegValue);
}
uint8_t RA8875_ReadReg(uint8_t _ucRegAddr)
{
uint8_t value;
RA8875_WriteCmd(_ucRegAddr);
delay_us(1);
RA8875_CS_0();
__nop();__nop();
SPI_ShiftByte(0x40);
value = SPI_ShiftByte(0x00);
__nop();__nop();
return value;
}
void RA8875_pll_init(void)
{
RA8875_WriteReg(0x88,0x07);
delay_ms(1);
RA8875_WriteReg(0x89,0x03);
delay_ms(10);
}
void lcd_init(void)
{
RA8875_pll_init();
delay_ms(20);
RA8875_WriteReg(0x10,0x0c);
RA8875_WriteReg(0x04,0x80);
delay_ms(10);
RA8875_WriteReg(0x14,0x3b);
RA8875_WriteReg(0x15,0x00);
RA8875_WriteReg(0x16,0x01);
RA8875_WriteReg(0x17,0x00);
RA8875_WriteReg(0x18,0x05);
RA8875_WriteReg(0x19,0x0f);
RA8875_WriteReg(0x1a,0x01);
RA8875_WriteReg(0x1b,0x02);
RA8875_WriteReg(0x1c,0x00);
RA8875_WriteReg(0x1d,0x07);
RA8875_WriteReg(0x1e,0x00);
RA8875_WriteReg(0x1f,0x09);
RA8875_WriteReg(0x30,0x00);
RA8875_WriteReg(0x31,0x00);
RA8875_WriteReg(0x34,0xdf);
RA8875_WriteReg(0x35,0x01);
RA8875_WriteReg(0x32,0x00);
RA8875_WriteReg(0x33,0x00);
RA8875_WriteReg(0x36,0x0f);
RA8875_WriteReg(0x37,0x01);
RA8875_WriteReg(0xc7,0x01);
RA8875_WriteReg(0x01,0x80);
}
int main(void)
{
uint8_t temp1,temp2;
delay_init(168);
RA8875_InitSPI();
delay_ms(1);
RA8875_RST_0();
delay_ms(1);
RA8875_RST_1();
delay_ms(10);
lcd_init();
RA8875_WriteReg(0x8A, 1 << 6); //pwm为最亮
while (1)
{
}
}
版主,按你那样用硬件SPI来做,SPI时序和数据用示波器看了都是对的,但感觉就是没有把值写到RA8875寄存器里面去,屏幕一直没有点亮。板子上两个电阻位置也是按SPI那样焊接的,麻烦帮我看看是不是程序哪里出了问题,还是少配置了寄存器才没有点亮的 |
|