用SPI总线通讯
1、模拟spi:
static void LCD_GPIO_Init(void)
{
nrf_gpio_cfg_output(LCD_CS_PIN);
nrf_gpio_pin_set(LCD_CS_PIN);
nrf_gpio_cfg_output(LCD_RESET_PIN);
nrf_gpio_cfg_output(LCD_DC_PIN);
nrf_gpio_cfg_output(LCD_SCLK_PIN);
nrf_gpio_cfg_output(LCD_SDI_PIN);
nrf_gpio_cfg_output(LCD_VCC_PIN);
nrf_gpio_pin_clear(LCD_VCC_PIN);
}
static void enable_cs_pin(void)
{
nrf_gpio_pin_clear(LCD_CS_PIN);
}
static void disable_cs_pin(void)
{
nrf_gpio_pin_set(LCD_CS_PIN);
}
void lcd_240x240_reset(void)
{
/*
//---------------------Reset sequence----------//
LCD_RESET=1; //
nrf_delay_ms(1); //Delay 1ms
LCD_RESET=0;
nrf_delay_ms(10); //Delay 10ms
LCD_RESET=1;
nrf_delay_ms(120); //Delay 120ms
//--------------------------------------------------------------------//
*/
nrf_gpio_pin_set(LCD_VCC_PIN);
nrf_delay_ms(10);
nrf_gpio_pin_set(LCD_RESET_PIN);
nrf_delay_ms(1);
nrf_gpio_pin_clear(LCD_RESET_PIN);
nrf_delay_ms(10);
nrf_gpio_pin_set(LCD_RESET_PIN);
nrf_delay_ms(120);
}
void LCD240x240_WriteCMD( unsigned char data)
{
/*四线SPI接口屏的操作*/
unsigned char i,MB=0;
nrf_gpio_pin_clear(LCD_DC_PIN);/*低,0:代表CMD,芯片规格书规定*/
enable_cs_pin(); //CS low
MB=data;
for(i=0; i<8; i++) /*模拟SPI协义*/
{
//nrf_gpio_pin_set(LCD_SCLK_PIN);//注意芯片规格书时序图,先准备数据,再拉时钟线
if(MB&0x80)
nrf_gpio_pin_set(LCD_SDI_PIN);
else
nrf_gpio_pin_clear(LCD_SDI_PIN);
nrf_gpio_pin_set(LCD_SCLK_PIN);
nrf_gpio_pin_clear(LCD_SCLK_PIN);
MB<<=1;
}
disable_cs_pin(); //CS high
}
void LCD240x240_WriteDAT(unsigned char data)
{
/*四线SPI接口屏的操作*/
unsigned char i,MB=0;
nrf_gpio_pin_set(LCD_DC_PIN);/*高,1,代表数据,芯片规格书规定*/
enable_cs_pin(); //CS low
MB=data;
for(i=0; i<8; i++)
{
//nrf_gpio_pin_set(LCD_SCLK_PIN);//注意芯片规格书时序图,先准备数据,再拉时钟线
if(MB&0x80)
nrf_gpio_pin_set(LCD_SDI_PIN);
else
nrf_gpio_pin_clear(LCD_SDI_PIN);
nrf_gpio_pin_set(LCD_SCLK_PIN);
nrf_gpio_pin_clear(LCD_SCLK_PIN);
MB<<=1;
}
disable_cs_pin(); //CS high
}
void lcd_240x240_init_test(void)
{
LCD_GPIO_Init();
lcd_240x240_reset();
LCD240x240_WriteCMD(0x11); //退出睡眠
nrf_delay_ms(120);
LCD240x240_WriteCMD(0x36); // Memory Data Access Control
LCD240x240_WriteDAT(0x00);
LCD240x240_WriteCMD(0x3A); // Interface Pixel Format
LCD240x240_WriteDAT(0x55);
LCD240x240_WriteCMD(0xB2); //Porch Setting
LCD240x240_WriteDAT(0x0C);
LCD240x240_WriteDAT(0x0C);
LCD240x240_WriteDAT(0x00);
LCD240x240_WriteDAT(0x33);
LCD240x240_WriteDAT(0x33);
// LCD240x240_WriteCMD(0x13);//Normal Display Mode On
LCD240x240_WriteCMD(0xB7); // Gate Control
LCD240x240_WriteDAT(0x75);
LCD240x240_WriteCMD(0xC2); //VDV and VRH Command Enable
LCD240x240_WriteDAT(0x01);
LCD240x240_WriteCMD(0xC3); //VRH Set
LCD240x240_WriteDAT(0x19);
LCD240x240_WriteCMD(0xC4);//VDV Set
LCD240x240_WriteDAT(0x20);
LCD240x240_WriteCMD(0xC6); //Frame Rate Control in Normal Mode
LCD240x240_WriteDAT(0x0F);
LCD240x240_WriteCMD(0xD0); // Power Control 1
LCD240x240_WriteDAT(0xA4);
LCD240x240_WriteDAT(0xA1);
LCD240x24