C8051F340单片机SPI串行驱动JCM12232F液晶屏遇到问题
时间:10-02
整理:3721RD
点击:
程序代码如下,求大神指点
#include "c8051F340.h"
#include "def.h"
//#include "USB_API.h"
#include "intrins.h"
//#endif
// ********************************
// *** 局部常数
// ****** MCU
#define MCU_SYS_CLK (12000000) // clock frequency in MHz
#define MCU_SPI_CLK (1000000) // MCU与RC522之间的通信速率最大为1MHz
#define MCU_UART_RATE (9600) // Baud rate of UART in bps
sbit LCD_CS = P0^7; // LCD片选信号
// ********************************
// *** 全局变量
dbyte KEY_XDATA gRecevBuffer[30] = {0x00};
uint1 gCount = 0;
dbool gCompl = 0;
dvoid McuDelayUs(uint1 aUs)
{
while(aUs--)
{
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
}
}
dvoid McuDelayMs(uint1 aMs)
{
while(aMs--)
{
McuDelayUs(1000);
}
}
void SpiInit()
{
SPI0CFG = 0x60; // 允许主方式
SPI0CN = 0x0D; // 4线单主方式,使能SPI0
SPI0CKR = MCU_SYS_CLK / (2 * MCU_SPI_CLK) - 1; // 注意选择SPI时钟
NSSMD0 = 1; // NSS的输出值由NSSMD0(SPI0CN.2)控制
}
dbyte SpiWrite(dbyte aByte)
{
SPIF = 0;
SPI0DAT = aByte;
while(!SPIF);
SPIF = 0;
return SPI0DAT;
}
dvoid Spi0SelectLcd()
{
NSSMD0 = 0;
}
dvoid Spi0DisableAllDevices()
{
NSSMD0 = 1;
}
// ********************************
// *** 全局函数
dvoid McuInit()
{
PCA0MD &= ~0x40;
EA = 1;
OSCICN |= 0x83; // 内部振荡器使能,SYSCLK为内部振荡器不分频
RSTSRC |= 0x04; // 使能时钟丢失检测器
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
// P0.0 - SCK (SPI0), Push-Pull, Digital
// P0.1 - MISO (SPI0), Open-Drain, Digital
// P0.2 - MOSI (SPI0), Push-Pull, Digital
// P0.3 - NSS (SPI0), Push-Pull, Digital
// P0.4 - TX0 (UART0), Push-Pull, Digital
// P0.5 - RX0 (UART0), Open-Drain, Digital
// p2.4 - RST (SPI0), Push-Pull, Digital, skipped
// P0.7 - LCD_CS(SPI0),Push-Pull, Digital, skipped
P0MDOUT = 0xDD;
P0SKIP = 0xC0;
P2MDOUT |= 0x10;
P2SKIP |= 0x10;
P4MDOUT |= 0x80;
// P4 &= 0x7f;// 接收
P4 |= 0x80;// 发送
XBR0 = 0x03; // SPI I/O连到端口引脚
XBR1 = 0x40; // 交叉开关使能
//-----------------------------------------------------------------------------
SpiInit();
}
void Lcd_Write(uint1 flag, dbyte command_data)
{
uint1 i_data,temp_data1,temp_data2;
LCD_CS = 1;
if(1 == flag) //flag是1,写入指令
{
Spi0SelectLcd();
SpiWrite(0xf8);
Spi0DisableAllDevices();
}
else //flag非1,写入数据
{
Spi0SelectLcd();
SpiWrite(0xfa);
Spi0DisableAllDevices();
}
i_data = command_data;
i_data &= 0xf0;
Spi0SelectLcd();
SpiWrite(i_data);
Spi0DisableAllDevices();
i_data = command_data;
temp_data1 = i_data & 0xf0;
temp_data2 = i_data & 0x0f;
temp_data1 >>= 4;
temp_data2 <<= 4;
i_data = temp_data1 | temp_data2;
i_data &= 0xf0;
Spi0SelectLcd();
SpiWrite(i_data);
Spi0DisableAllDevices();
LCD_CS = 0;
McuDelayMs(10);
}
void Lcd_init()
{
uint1 command_data;
McuDelayMs(100);
command_data = 0x30;
Lcd_Write(1,command_data); /*功能设置:一次送8位数据,基本指令集*/
command_data = 0x04;
Lcd_Write(1,command_data); /*点设定:显示字符/光标从左到右移位,DDRAM地址加1*/
command_data = 0x0f;
Lcd_Write(1,command_data); /*显示设定:开显示,显示光标,当前显示位反白闪动*/
command_data = 0x01;
Lcd_Write(1,command_data); /*清DDRAM*/
command_data = 0x02;
Lcd_Write(1,command_data); /*DDRAM地址归位*/
command_data = 0x80;
Lcd_Write(1,command_data); /*把显示地址设为0X80,即为第一行的首位*/
}
void Lcd_Display()
{
uint1 command_data;
command_data = 0x01;
Lcd_Write(1,command_data); /*清DDRAM*/
command_data = 0x80;
Lcd_Write(1,command_data); /*把显示地址设为0X80,即为第一行的首位*/
Lcd_Write(2,'a');
McuDelayMs(3000);
}
int main()
{
McuInit();
Lcd_init();
Lcd_Display();
while(1);
return 0;
}
#include "c8051F340.h"
#include "def.h"
//#include "USB_API.h"
#include "intrins.h"
//#endif
// ********************************
// *** 局部常数
// ****** MCU
#define MCU_SYS_CLK (12000000) // clock frequency in MHz
#define MCU_SPI_CLK (1000000) // MCU与RC522之间的通信速率最大为1MHz
#define MCU_UART_RATE (9600) // Baud rate of UART in bps
sbit LCD_CS = P0^7; // LCD片选信号
// ********************************
// *** 全局变量
dbyte KEY_XDATA gRecevBuffer[30] = {0x00};
uint1 gCount = 0;
dbool gCompl = 0;
dvoid McuDelayUs(uint1 aUs)
{
while(aUs--)
{
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
}
}
dvoid McuDelayMs(uint1 aMs)
{
while(aMs--)
{
McuDelayUs(1000);
}
}
void SpiInit()
{
SPI0CFG = 0x60; // 允许主方式
SPI0CN = 0x0D; // 4线单主方式,使能SPI0
SPI0CKR = MCU_SYS_CLK / (2 * MCU_SPI_CLK) - 1; // 注意选择SPI时钟
NSSMD0 = 1; // NSS的输出值由NSSMD0(SPI0CN.2)控制
}
dbyte SpiWrite(dbyte aByte)
{
SPIF = 0;
SPI0DAT = aByte;
while(!SPIF);
SPIF = 0;
return SPI0DAT;
}
dvoid Spi0SelectLcd()
{
NSSMD0 = 0;
}
dvoid Spi0DisableAllDevices()
{
NSSMD0 = 1;
}
// ********************************
// *** 全局函数
dvoid McuInit()
{
PCA0MD &= ~0x40;
EA = 1;
OSCICN |= 0x83; // 内部振荡器使能,SYSCLK为内部振荡器不分频
RSTSRC |= 0x04; // 使能时钟丢失检测器
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
// P0.0 - SCK (SPI0), Push-Pull, Digital
// P0.1 - MISO (SPI0), Open-Drain, Digital
// P0.2 - MOSI (SPI0), Push-Pull, Digital
// P0.3 - NSS (SPI0), Push-Pull, Digital
// P0.4 - TX0 (UART0), Push-Pull, Digital
// P0.5 - RX0 (UART0), Open-Drain, Digital
// p2.4 - RST (SPI0), Push-Pull, Digital, skipped
// P0.7 - LCD_CS(SPI0),Push-Pull, Digital, skipped
P0MDOUT = 0xDD;
P0SKIP = 0xC0;
P2MDOUT |= 0x10;
P2SKIP |= 0x10;
P4MDOUT |= 0x80;
// P4 &= 0x7f;// 接收
P4 |= 0x80;// 发送
XBR0 = 0x03; // SPI I/O连到端口引脚
XBR1 = 0x40; // 交叉开关使能
//-----------------------------------------------------------------------------
SpiInit();
}
void Lcd_Write(uint1 flag, dbyte command_data)
{
uint1 i_data,temp_data1,temp_data2;
LCD_CS = 1;
if(1 == flag) //flag是1,写入指令
{
Spi0SelectLcd();
SpiWrite(0xf8);
Spi0DisableAllDevices();
}
else //flag非1,写入数据
{
Spi0SelectLcd();
SpiWrite(0xfa);
Spi0DisableAllDevices();
}
i_data = command_data;
i_data &= 0xf0;
Spi0SelectLcd();
SpiWrite(i_data);
Spi0DisableAllDevices();
i_data = command_data;
temp_data1 = i_data & 0xf0;
temp_data2 = i_data & 0x0f;
temp_data1 >>= 4;
temp_data2 <<= 4;
i_data = temp_data1 | temp_data2;
i_data &= 0xf0;
Spi0SelectLcd();
SpiWrite(i_data);
Spi0DisableAllDevices();
LCD_CS = 0;
McuDelayMs(10);
}
void Lcd_init()
{
uint1 command_data;
McuDelayMs(100);
command_data = 0x30;
Lcd_Write(1,command_data); /*功能设置:一次送8位数据,基本指令集*/
command_data = 0x04;
Lcd_Write(1,command_data); /*点设定:显示字符/光标从左到右移位,DDRAM地址加1*/
command_data = 0x0f;
Lcd_Write(1,command_data); /*显示设定:开显示,显示光标,当前显示位反白闪动*/
command_data = 0x01;
Lcd_Write(1,command_data); /*清DDRAM*/
command_data = 0x02;
Lcd_Write(1,command_data); /*DDRAM地址归位*/
command_data = 0x80;
Lcd_Write(1,command_data); /*把显示地址设为0X80,即为第一行的首位*/
}
void Lcd_Display()
{
uint1 command_data;
command_data = 0x01;
Lcd_Write(1,command_data); /*清DDRAM*/
command_data = 0x80;
Lcd_Write(1,command_data); /*把显示地址设为0X80,即为第一行的首位*/
Lcd_Write(2,'a');
McuDelayMs(3000);
}
int main()
{
McuInit();
Lcd_init();
Lcd_Display();
while(1);
return 0;
}
真心需要啊啊啊啊