#include "C8051F040.h"
#define uchar unsigned char
#define uint unsigned int
uchar xdata virt_port;
bit SPI_BUSY;
unsigned char xdata tab1[16]={0x20,0x20,0x42,0x54,0x20,0x46,0x30,0x34,0x30,0x20,0x56,0x31,0x2e,0x30
,0x20,0x20};
unsigned char xdata Netdata[16]={0x77,0x77,0x77,0x2e,0x78,0x68,0x6c,0x2e,0x63,0x6f,
0x6d,0x2e,0x63,0x6e,0x20,0x20};
sbit LCDCs=P2^5;
void os_init();
void port_init();
void spi_init();
void writecode(uchar ins);
void writEDAta(uchar d);
void lcdchar (void);
void lcdinit();
void writelcd(uchar spi_data);
void main()
{
WDTCN = 0xDE; // 关闭看门狗
WDTCN = 0xAD;
os_init();
port_init();
spi_init();
SFRPAGE = CONFIG_PAGE;
EIE1 = 0x01;
EA=1;
while(1)
{
lcdinit();
lcdchar();
}
}
void os_init()
{
int i = 0;
SFRPAGE = CONFIG_PAGE;
OSCXCN = 0x67; //外部22.1184M晶振
for (i = 0; i < 3000; i++); //等待外部晶振稳定
while ((OSCXCN & 0x80) == 0);
CLKSEL = 0x01; //系统时钟不分频
}
void port_init()
{
SFRPAGE = CONFIG_PAGE;
P0MDOUT=0xff;
XBR0 = 0x06;
XBR2 = 0x40;
}
void spi_init()
{
SFRPAGE = SPI0_PAGE;
SPI0CFG = 0x40;
SPI0CN = 0x01;
SPI0CKR = 0x6D;
}
void delay1us(void)
{
uchar xdata i;
for(i=10;i>0;i--);
}
void writelcd(uchar spi_data) //向LV595写数据以SPI方式
{
uint xdata i,j,k;
for(i=0;i<9;i++);
LcdCs=0;
delay1us();
SPI_BUSY=1;
SFRPAGE = SPI0_PAGE;
SPI0DAT=spi_data;
for(j=0;j<500;j++)
{
if(!SPI_BUSY)break;
}
delay1us();
LcdCs=1;
delay1us();
delay1us();
delay1us();
delay1us();
delay1us();
delay1us();
for(k=0;k<5000;k++);
/* **************************************************************************
// 以下这段代码,是用IO口模拟SPI传输的程序
LcdCs=0;
for(i=0;i<8;i++)
{ SPICLK=0;
if(spi_data&0x80)
spi_mosi=1;
else
spi_mosi=0;
spi_data<<=1;
SPICLK=1;
for(j=0;j<10;j++)
{_nop_();}
SPICLK=0;
}
LcdCs=1;
/***************************************************************************/
}
void writecode(uchar ins) //Write command to LCD control module
{
uchar xdata t;
uint xdata j;
for(j=0;j<500;j++);
virt_port|=ins&0xf0; //Get high byte and write data to 1602
writelcd(virt_port); //Write high 4 bits first and then low 4 bits. This is when selecting four data lines
virt_port|=0x08;
writelcd(virt_port); //Enable E pin as high level
virt_port&=~0x08;
writelcd(virt_port); //Enable E pin as low level
t=ins<<4;
virt_port|=t&0xf0; //Get low byte and write data to 1602
writelcd(virt_port);
virt_port|=0x08; //Enable E pin as high level
writelcd(virt_port);
virt_port&=~0x08; //Enable E pin as low level
writelcd(virt_port);
virt_port=0;
writelcd(virt_port);
}
void writedata(uchar d) //Write data to 1602
{
uchar xdata i,t;
uint xdata j;
for(j=0;j<500;j++);
virt_port|=0x02; //Cursor return instruction
writelcd(virt_port);
virt_port|=d&0xf0; //Get high byte data to data register
writelcd(virt_port);
virt_port|=0x08; //Enable E pin as high level
writelcd(virt_port);
virt_port&=0xf7; //Enable E pin as low level
writelcd(virt_port);
virt_port&=0x07; //Set cursor to right, text also moves right
writelcd(virt_port);
t=d&0x0f;
virt_port|=t<<4; //Get low byte data to data register
writelcd(virt_port);
virt_port|=0x08; //Enable E pin as high level
writelcd(virt_port);
for(i=3;i>0;i--);
virt_port&=0xf7; //Enable E pin as low level
writelcd(virt_port);
virt_port=0; //Data clear
writelcd(virt_port);
}
void lcdchar (void)
{
uint xdata i,j,k;
for(j=0;j<5000;j++); //data read and write operations, the screen does not move
for(k=0;k<5000;k++);
for(i=0;i<16;i++)
{
writedata(tab1[i]);
for(j=0;j<1000;j++);
for(k=0;k<1000;k++);
for(k=0;k<5000;k++);
for(k=0;k<1000;k++);
for(k=0;k<5000;k++);
for(k=0;k<5000;k++);
for(k=0;k<5000;k++);
}
writecode(0xb0); // 换行
for(j=0;j<5000;j++);
for(k=0;k<5000;k++);
for(i=0;i<16;i++)
{
writedata(Netdata[i]);
for(j=0;j<5000;j++) delay1us();
for(k=0;k<5000;k++);
for(k=0;k<5000;k++);
for(k=0;k<5000;k++);
for(k=0;k<5000;k++);
};
}
void lcdinit()
{
uint xdata i;
virt_port=0x00;
writelcd(virt_port); //RS脚为零写指令寄存器 RW为低电平
virt_port|=0x08; //使能脚E=1
writelcd(virt_port);
virt_port&=0xf7; //使E=0使能脚发生跳变高--低
writelcd(virt_port);
for(i=1000;i>0;i--); //3次延时,重要!
for(i=8000;i>0;i--); //用延时代替查询
virt_port=0; //写入20码,设置为4位总线方式,非常重要!!!!!
writelcd(virt_port);
virt_port|=0x20; //只写高字节
writelcd(virt_port);
virt_port|=0x08; //E=1
writelcd(virt_port);
for(i=20;i>0;i--); //使命令执行
virt_port&=0xf7; //E=0
writelcd(virt_port);
for(i=10;i>0;i--);
virt_port=0;
writelcd(virt_port);
writecode(0x38); //设两行显示,4线方式5*10字符显示
for(i=50;i>0;i--);
writecode(0x06); //光标右移
for(i=50;i>0;i--);
writecode(0x0c); //开显示指令
for(i=50;i>0;i--);
writecode(0x02); //光标返回指令
for(i=50;i>0;i--);
writecode(0x01); //清屏指令
for(i=50;i>0;i--);
}
void spi_srs () interrupt 6
{
if(SPIF)
{
SFRPAGE = SPI0_PAGE;
SPIF=0;
SPI_BUSY=0;
}
if(WCOL)
{
SFRPAGE = SPI0_PAGE;
WCOL=0;
}
}
Previous article:89C2051 driving stepper motor circuit and source code
Next article:C8051F005 MCU PWM test program
Recommended ReadingLatest update time:2024-11-23 15:32
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Popular Science: Talking about Carrier Aggregation
- Which software is good for EDA design PCB now?
- RP2040 Stamp Micro Handheld Terminal
- EEWORLD University ---- MOS Transistor (Columbia University, English subtitles)
- SPMC75F2413A Application Solution
- Domestic M0 chip transplantation FreeRTOS notes 1
- You decide the forum functions! Make a habit of voting by replying to posts!
- pic10F322 demo board 02 Features
- Please answer my question: What is the role of capacitor in this synchronous buck circuit?
- RT-Thread's new book is here, full of useful information