给位大神,小弟刚刚接触ST的芯片,因为项目需要SPI的接口,今天小试了一下,想做SPI控制LCD12864.测试了一天也没能把数据写到液晶里面,下面是我的代码:
#include "stm8s.h"
#include "stm8s_clk.h"
#include "stm8s_gpio.h"
#include "stm8s_spi.h"
#define SPI_SCK_H ((GPIOC->ODR) |= GPIO_PIN_5)
#define SPI_SCK_L ((GPIOC->ODR) &= ~GPIO_PIN_5)
#define SPI_CS_H ((GPIOG->ODR) |= GPIO_PIN_0)
#define SPI_CS_L ((GPIOG->ODR) &= ~GPIO_PIN_0)
#define SPI_BMOD_O SPI->CR2 |= 0x40
#define SPI_BMOD_I SPI->CR2 &= ~0x40 //控制SPI输出方向 0只输入,1输出
u8 tab1[]={"人生若只是初见,"
"何事秋风悲画扇。"
"等闲变却故人心,"
"却道故人心易变。"};
void Delay(u16 time)
{
while(time !=0)
{
time--;
}
}
/*时钟选择初始化,CLK_PRESCALER_HSIDIV2定义为内部高速时钟,最大16M/128分频*/
void Init_CLK(void)
{
CLK_HSIPrescalerConfig(CLK_PRESCALER_CPUDIV128);
}
/*SPI接口初始化*/
void Init_SPI(void)
{
SPI_DeInit(); //初始化SPI用到的所有寄存器,寄存器恢复到初始值
SPI->CR1 |= 0xbc; //先发送高位、关闭SPI使能、波特率为时钟256分频、主设备第一个脉冲上升沿
SPI->CR2 |= 0xC0; //单线双向,输出使能
// SPI->CRC |= 0X07; //CRC初始化
SPI_Cmd(ENABLE);
}
void LCD_Write(u8 Dat)
{
// SPI_CS_H; //选择设备
SPI_SendData(Dat);
Delay(20);
// SPI_CS_L;
}
void LCD_Cmd(u8 Dat)
{
u8 cmd,temp1,temp2;
cmd = 0xf8;
temp1 = Dat & 0xf0;
temp2 = (Dat << 4) & 0xf0;
LCD_Write(cmd); //发送命令到LCD
Delay(5); //适当延时,等待LCD释放
LCD_Write(temp1);
Delay(1);
LCD_Write(temp2);
Delay(1);
}
void LCD_DatWrite(u8 Dat)
{
u8 startdat,temp1,temp2;
startdat = 0xfa;
temp1 = Dat & 0xf0;
temp2 = (Dat << 4) & 0xf0;
LCD_Write(startdat); //发送命令到LCD
Delay(5); //适当延时,等待LCD释放
LCD_Write(temp1);
Delay(1);
LCD_Write(temp2);
Delay(1);
}
void LCD_Init()
{
// SPI_CS_H; //选择设备
Delay(2);
LCD_Cmd(0x30);
LCD_Cmd(0x0c);
LCD_Cmd(0x01);
}
void LCD_Display(u8* str)
{
u8 i,j;
LCD_Cmd(0x30);
LCD_Cmd(0x80);
for(j=0;j<4;j++)
{
for(i=0;i<16;i++)
LCD_DatWrite(str[j*16+i]);
}
}
void LCD_ClearRam()
{
LCD_Cmd(0x30);
LCD_Cmd(0x01);
Delay(180);
}
void Init_GPIO()
{
GPIOC->DDR |= 0x60;
GPIOG->DDR |= 0x01;
GPIOC->CR1 |= 0x60;
GPIOC->CR2 &= ~0x60;
GPIOG->CR1 |= 0x01;
GPIOG->CR2 &= ~0x01;
}
void main(void)
{
u8 i;
Init_SPI();
Delay(10);
Init_GPIO();
LCD_Init();
SPI_CS_L;
while(1)
{
LCD_ClearRam();
LCD_Display(tab1);
for(i=0;i<5;i++)
Delay(1000);
}
}
忘各位大神不吝赐教,小弟不胜感激
|