这是参考网上猪的博客改的,原地址请百度。
//CDM4 LCD 6位显示 ZC99696P
//;************硬件连接************
//;310-99696串行移位数据输出
//;DISEL脚要接地MCU才会输出6位数据
#include <intrins.h>
#include <reg51.h>
sbit DLEN = P3^3;
sbit SCK = P3^4;
sbit DAT = P3^5;
sbit LCD_RS = P2^0;
sbit LCD_RW = P2^1;
sbit LCD_E = P2^2;
#define LCD_DATA P0
//********************************
typedef union
{
char byte;
struct
{
char DOT_A: 1; //bit 0
char DOT_B: 1;
char DOT_C: 1;
char DOT_D: 1;
char DOT_E: 1;
char DOT_F: 1;
char DOT_G: 1;
char DOT_H: 1; //bit7,dot
}BIT;
}SEG8_STRUCT;
typedef union
{
char byte;
struct
{
char A_B: 1; //bit 0
char ERR: 1;
char PLAY: 1;
char PAUSE: 1;
char REP: 1;
char REP_ALL: 1;
char S_TIME: 1;
char T_TIME: 1; //bit7
}BIT;
}STATUS_1;
typedef union
{
char byte;
struct
{
char R_TIME: 1; //bit0
char CD: 1;
char DOT: 1;
char N_6: 1;
char N_7: 1;
char N_8: 1;
char N_9: 1;
char DISP_2_ROW: 1; //bit7
}BIT;
}STATUS_2;
typedef struct
{
SEG8_STRUCT buffer[6];
STATUS_1 ctrl_status1;
STATUS_2 ctrl_status2;
}DISP_STRUCT;
bdata DISP_STRUCT disp; //at 0x20
char buffer[10]; //at 0x30
char State1,State2;
const char load_str[] = "LOADING";
const char no_disk_str[] = "NO DISK";
const char open_str[] = " OPEN ";
const char pause_str[] = " PAUSE ";
const char ab_str[] = " A-B ";
const char play_str[] = " PLAY ";
const char stop_str[] = " STOP ";
const char nop_str[] = " ";
void LCD_init(void);
void disp_stat(const char *ptr);
void LED2LCD(char src,char idata *dest_ptr);
void disp_time(void);
void LCD_write_cmd(char dat);
void LCD_write_cmd_N(char dat);
void LCD_write_data(char dat);
void Delay_10ms(void);
void WAIT_LCD_IDLE(void);
//;*************************
void Init(void)
{
disp.buffer[0].byte = 0;
disp.buffer[1].byte = 0;
disp.buffer[2].byte = 0;
disp.buffer[3].byte = 0;
disp.buffer[4].byte = 0;
disp.buffer[5].byte = 0;
disp.ctrl_status1.byte = 0;
disp.ctrl_status2.byte = 0;
State1 = 0;
State2 = 0;
LCD_init();//调用显示初始化
SCK = 1;//打开端口
DAT = 1;
DLEN = 1;
}
void Skip_1_clock(void)
{
while(!SCK); //jnb SCK,$
while(SCK); //jb SCK,$
}
void main(void)
{
char temp;
Init();
while(1)
{
while(DLEN); //jb DLEN,$
while(!DLEN); //jnb DLEN,$
while(!SCK); //jnb SCK,$
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[0].BIT.DOT_H= 0;
if(DAT)
disp.buffer[0].BIT.DOT_H= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[0].BIT.DOT_D= 0;
if(DAT)
disp.buffer[0].BIT.DOT_D= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[0].BIT.DOT_C= 0;
if(DAT)
disp.buffer[0].BIT.DOT_C= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[0].BIT.DOT_B= 0;
if(DAT)
disp.buffer[0].BIT.DOT_B= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[1].BIT.DOT_A= 0;
if(DAT)
disp.buffer[1].BIT.DOT_A= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[1].BIT.DOT_D= 0;
if(DAT)
disp.buffer[1].BIT.DOT_D= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[1].BIT.DOT_C= 0;
if(DAT)
disp.buffer[1].BIT.DOT_C= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[1].BIT.DOT_B= 0;
if(DAT)
disp.buffer[1].BIT.DOT_B= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[2].BIT.DOT_A= 0;
if(DAT)
disp.buffer[2].BIT.DOT_A= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[2].BIT.DOT_D= 0;
if(DAT)
disp.buffer[2].BIT.DOT_D= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[2].BIT.DOT_C= 0;
if(DAT)
disp.buffer[2].BIT.DOT_C= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[2].BIT.DOT_B= 0;
if(DAT)
disp.buffer[2].BIT.DOT_B= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[3].BIT.DOT_A= 0;
if(DAT)
disp.buffer[3].BIT.DOT_A= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[3].BIT.DOT_D= 0;
if(DAT)
disp.buffer[3].BIT.DOT_D= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[3].BIT.DOT_C= 0;
if(DAT)
disp.buffer[3].BIT.DOT_C= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[3].BIT.DOT_B= 0;
if(DAT)
disp.buffer[3].BIT.DOT_B= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[4].BIT.DOT_A= 0;
if(DAT)
disp.buffer[4].BIT.DOT_A= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[4].BIT.DOT_D= 0;
if(DAT)
disp.buffer[4].BIT.DOT_D= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[4].BIT.DOT_C= 0;
if(DAT)
disp.buffer[4].BIT.DOT_C= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[4].BIT.DOT_B= 0;
if(DAT)
disp.buffer[4].BIT.DOT_B= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[5].BIT.DOT_A= 0;
if(DAT)
disp.buffer[5].BIT.DOT_A= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[5].BIT.DOT_D= 0;
if(DAT)
disp.buffer[5].BIT.DOT_D= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[5].BIT.DOT_C= 0;
if(DAT)
disp.buffer[5].BIT.DOT_C= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[5].BIT.DOT_B= 0;
if(DAT)
disp.buffer[5].BIT.DOT_B= 1;
while(SCK); //jb SCK,$
//*************************************
while(!SCK); //jnb SCK,$
disp.ctrl_status1.BIT.S_TIME= 0;
if(DAT)
disp.ctrl_status1.BIT.S_TIME= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.ctrl_status2.BIT.R_TIME= 0;
if(DAT)
disp.ctrl_status2.BIT.R_TIME= 1;
while(SCK); //jb SCK,$
Skip_1_clock();
while(!SCK); //jnb SCK,$
disp.ctrl_status1.BIT.REP= 0;
if(DAT)
disp.ctrl_status1.BIT.REP= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.ctrl_status2.BIT.CD= 0;
if(DAT)
disp.ctrl_status2.BIT.CD= 1;
while(SCK); //jb SCK,$
Skip_1_clock();
while(!SCK); //jnb SCK,$
disp.ctrl_status1.BIT.PLAY= 0;
if(DAT)
disp.ctrl_status1.BIT.PLAY= 1;
while(SCK); //jb SCK,$
Skip_1_clock();
while(!SCK); //jnb SCK,$
if(!DAT)
continue;
while(DLEN); //jb DLEN,$
while(!DLEN); //jnb DLEN,$
Skip_1_clock();
Skip_1_clock();
while(!SCK); //jnb SCK,$
disp.buffer[0].BIT.DOT_E= 0;
if(DAT)
disp.buffer[0].BIT.DOT_E= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[0].BIT.DOT_G= 0;
if(DAT)
disp.buffer[0].BIT.DOT_G= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[0].BIT.DOT_F= 0;
if(DAT)
disp.buffer[0].BIT.DOT_F= 1;
while(SCK); //jb SCK,$
Skip_1_clock();
while(!SCK); //jnb SCK,$
disp.buffer[1].BIT.DOT_E= 0;
if(DAT)
disp.buffer[1].BIT.DOT_E= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[1].BIT.DOT_G= 0;
if(DAT)
disp.buffer[1].BIT.DOT_G= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[1].BIT.DOT_F= 0;
if(DAT)
disp.buffer[1].BIT.DOT_F= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.ctrl_status1.BIT.A_B= 0;
if(DAT)
disp.ctrl_status1.BIT.A_B= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[2].BIT.DOT_E= 0;
if(DAT)
disp.buffer[2].BIT.DOT_E= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[2].BIT.DOT_G= 0;
if(DAT)
disp.buffer[2].BIT.DOT_G= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[2].BIT.DOT_F= 0;
if(DAT)
disp.buffer[2].BIT.DOT_F= 1;
while(SCK); //jb SCK,$
Skip_1_clock();
while(!SCK); //jnb SCK,$
disp.buffer[3].BIT.DOT_E= 0;
if(DAT)
disp.buffer[3].BIT.DOT_E= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[3].BIT.DOT_G= 0;
if(DAT)
disp.buffer[3].BIT.DOT_G= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[3].BIT.DOT_F= 0;
if(DAT)
disp.buffer[3].BIT.DOT_F= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.ctrl_status2.BIT.DOT= 0;
if(DAT)
disp.ctrl_status2.BIT.DOT= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[4].BIT.DOT_E= 0;
if(DAT)
disp.buffer[4].BIT.DOT_E= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[4].BIT.DOT_G= 0;
if(DAT)
disp.buffer[4].BIT.DOT_G= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[4].BIT.DOT_F= 0;
if(DAT)
disp.buffer[4].BIT.DOT_F= 1;
while(SCK); //jb SCK,$
Skip_1_clock();
while(!SCK); //jnb SCK,$
disp.buffer[5].BIT.DOT_E= 0;
if(DAT)
disp.buffer[5].BIT.DOT_E= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[5].BIT.DOT_G= 0;
if(DAT)
disp.buffer[5].BIT.DOT_G= 1;
while(SCK); //jb SCK,$
while(!SCK); //jnb SCK,$
disp.buffer[5].BIT.DOT_F= 0;
if(DAT)
disp.buffer[5].BIT.DOT_F= 1;
while(SCK); //jb SCK,$
Skip_1_clock();
while(!SCK); //jnb SCK,$
disp.ctrl_status1.BIT.T_TIME= 0;
if(DAT)
disp.ctrl_status1.BIT.T_TIME= 1;
while(SCK); //jb SCK,$
Skip_1_clock();
while(!SCK); //jnb SCK,$
disp.ctrl_status1.BIT.REP_ALL= 0;
if(DAT)
disp.ctrl_status1.BIT.REP_ALL= 1;
while(SCK); //jb SCK,$
Skip_1_clock();
Skip_1_clock();
while(!SCK); //jnb SCK,$
disp.ctrl_status1.BIT.PAUSE= 0;
if(DAT)
disp.ctrl_status1.BIT.PAUSE= 1;
while(SCK); //jb SCK,$
Skip_1_clock();
disp.ctrl_status2.BIT.DISP_2_ROW = 1;
if(disp.buffer[0].byte == 0x40)
{
State1 = 0x03;
disp_stat(&load_str);
disp.ctrl_status2.BIT.DISP_2_ROW = 0;
}
else
{
if(State1 == 0)
{
if(disp.buffer[2].byte == 0x5E)
{
disp_stat(&no_disk_str);
disp.ctrl_status2.BIT.DISP_2_ROW = 0;
goto disp2;
}
else
if(disp.buffer[3].byte == 0x73)
{
disp_stat(&open_str);
disp.ctrl_status2.BIT.DISP_2_ROW = 0;
goto disp2;
}
else
if(disp.ctrl_status1.BIT.PAUSE)
{
disp_stat(&pause_str);
goto disp2;
}
else
if(disp.ctrl_status1.BIT.A_B)
{
State2 = 0x03;
disp_stat(&ab_str);
goto disp2;
}
else
if(State2 == 0x00)
{
if(disp.ctrl_status1.BIT.PLAY)
{
disp_stat(&play_str);
goto disp2;
}
else
if(disp.buffer[1].byte != 0)
{
disp_stat(&stop_str);
goto disp2;
}
else
{
disp.ctrl_status2.BIT.DISP_2_ROW = 0;
disp_stat(&nop_str);
goto disp2;
}
}
else
{
State2--;
}
}
else
{
State1--;
disp.ctrl_status2.BIT.DISP_2_ROW = 0;
}
disp_stat(&nop_str);
}
disp2:
buffer[0] = ' ';
buffer[1] = ' ';
buffer[2] = ' ';
buffer[3] = ' ';
buffer[4] = ' ';
buffer[5] = ' ';
buffer[6] = ' ';
buffer[7] = ' ';
buffer[8] = ' ';
buffer[9] = ' ';
if(disp.ctrl_status2.BIT.DISP_2_ROW)
{
if(!disp.ctrl_status1.BIT.S_TIME)
{
if(!disp.ctrl_status1.BIT.T_TIME)
{
if(!disp.ctrl_status2.BIT.R_TIME)
{
if(disp.ctrl_status2.BIT.DOT)
{
buffer[7]= 0x3A;
}
}
else
{
buffer[4] = 0x2D;
}
}
else
{
buffer[3] = 0x54;
}
}
if(disp.ctrl_status2.BIT.DOT)
{
buffer[7] = 0x3A;
}
LED2LCD(disp.buffer[0].byte,&buffer[0]);
LED2LCD(disp.buffer[1].byte,&buffer[1]);
LED2LCD(disp.buffer[2].byte,&buffer[5]);
LED2LCD(disp.buffer[3].byte,&buffer[6]);
LED2LCD(disp.buffer[4].byte,&buffer[8]);
temp = buffer[5]^0x20;
if(temp == 0)
{
temp = buffer[6] ^ 0x20;
if(temp != 0)
{
buffer[5] = 0x30;
}
}
LED2LCD(disp.buffer[5].byte,&buffer[9]);
disp_time();
}
}
}
void LED2LCD(char src,char idata *dest_ptr)
{
char r6,i;
const char LED_num[]={
0x3f,0x06,0x5b,0x4f, //db 00111111B,00000110B,01011011B,01001111B
0x66,0x6d,0x7d,0x07, //db 01100110B,01101101B,01111101B,00000111B
0x7f,0x6f,0x00,0x40 //db 01111111B,01101111B,00000000B,01000000B
};
i=0;
for(r6 = 12;r6;r6--)
{
if(src == LED_num)
{
*dest_ptr = LED_num;
break;
}
i++;
}
}
void disp_stat(const char *ptr)
{
char r6,r7;
LCD_write_cmd(0x80|0x04);
r6=0;
r7=7;
do
{
LCD_write_data(ptr[r6]);
r6++;
}while(--r7);
}
void disp_time(void)
{
char *ptr;
char r7;
LCD_write_cmd(0xC0|0x03);
ptr=buffer;
r7=10;
do
{
LCD_write_data(*ptr++);
}while(--r7);
}
//;*****************************
//;LCD初始化
//;*************************************************
void LCD_init(void)
{
Delay_10ms();
Delay_10ms();//延时15MS
Delay_10ms();
LCD_write_cmd_N(0x38);//显示模式设置(不检测忙信号);共三次
Delay_10ms();
LCD_write_cmd_N(0x38);
Delay_10ms();
LCD_write_cmd(0x38);//显示模式设置(以后均检测忙信号)
LCD_write_cmd(0x08);//显示关闭
LCD_write_cmd(0x01);//显示清屏
LCD_write_cmd(0x0C);//显示光标移动设置
LCD_write_cmd(0x06);//显示开及光标设置
}
//;*************************************************
//;LCD读写
//;*************************************************
void LCD_write_cmd(char dat)
{
WAIT_LCD_IDLE(); //送控制字子程序(检测忙信号)
LCD_write_cmd_N(dat); //送控制字子程序(不检测忙信号)
}
void LCD_write_cmd_N(char dat)
{
LCD_RS = 0; //RS=0 RW=0 E=高脉冲
LCD_RW = 0;
LCD_DATA = dat;
LCD_E = 1;
_nop_();
LCD_E = 0;
}
void LCD_write_data(char dat)//写字符子程序
{
WAIT_LCD_IDLE();
LCD_RS = 1; //RS=1 RW=0 E=高脉冲
LCD_RW = 0;
LCD_DATA = dat;
LCD_E = 1;
_nop_();
LCD_E = 0;
}
void WAIT_LCD_IDLE(void)
{
LCD_DATA = 0xff; //正常读写操作之前必须检测LCD控制器状态
LCD_RS = 0; //RS=0 RW=1 E=高电平
LCD_RW = 1;
LCD_E = 1;
do
{
_nop_();
}while(LCD_DATA & 0x80);//DB7:0 LCD控制器空闲;1 LCD控制器忙
LCD_E = 0;
}
//;********************************************
void delay_10ms(void)
{
char r7,r6;
for(r7=100;r7;r7--)
{
for(r6=100;r6;r6--);
_nop_();
}
}
|
|