程序是按照例程写的,查了说有可能是LCD1602忙检测的问题,然后把忙检测删了,改成延时,还是不亮。。
#include<at89x52.h>
#include<stdio.h>
#include<intrins.h>
#define TLC549_CS P1_1 //片选信号
#define TLC549_DATAOUT P1_0 //数据输入口
#define TLC549_CLK P1_2 //时钟信号
#define LCM_RW P2_1 //定义引脚
#define LCM_RS P2_2
#define LCM_E P2_0
#define LCM_Data P0
//#define Busy 0x80 //用于检测LCM 状态字中的Busy 标识
/*------------------------------------------
子函数声明
------------------------------------------*/
void Write_Data_LCM(unsigned char WDLCM);
void Write_Command_LCM(unsigned char WCLCM,BuysC);
void Read_Status_LCM(void);
void LCM_Init(void);
void Set_xy_LCM(unsigned char x, unsigned char y);
void Display_List_Char(unsigned char x, unsigned char y, unsigned char *s);
void delay_nms(unsigned char n)
{
while(n)
{
_nop_();
n--;
}
}
/*========================= 函数名:Read_Status_LCM() 功 能:忙检测函数
void Read_Status_LCM(void)
{
unsigned char read=0;
LCM_RW = 1;
LCM_RS = 0;
LCM_E = 1;
LCM_Data = 0xff;
do read = LCM_Data;
while(read & Busy);
LCM_E = 0;
}
/*-------------------------------------------
函数名:Write_Data_LCM ( )
功 能:对LCD 1602 写数据
--------------------------------------------*/
void Write_Data_LCM(unsigned char WDLCM)
{
// Read_Status_LCM(); //检测忙
LCM_RS = 1;
LCM_RW = 0;
LCM_Data &= 0x0f;
LCM_Data |= WDLCM&0xf0;
LCM_E = 1; //若晶振速度太高可以在这后加小的延时
LCM_E = 1; //延时
LCM_E = 0;
WDLCM = WDLCM<<4;
LCM_Data &= 0x0f;
LCM_Data |= WDLCM&0xf0;
LCM_E = 1;
LCM_E = 1; //延时
LCM_E = 0;
}
/*------------------------------------------------
函数名:Write_Command_ LCM ( )
功 能:对LCD 1602 写指令
--------------------------------------------------*/
void Write_Command_LCM(unsigned char WCLCM) //BuysC 为0 时忽略忙检测
{
// if (BuysC)
// Read_Status_LCM(); //根据需要检测忙
LCM_RS = 0;
LCM_RW = 0;
LCM_Data &= 0x0f;
LCM_Data |= WCLCM&0xf0;//传输高四位
LCM_E = 1;
LCM_E = 1;
LCM_E = 0;
WCLCM = WCLCM<<4; //传输低四位
LCM_Data &= 0x0f;
LCM_Data |= WCLCM&0xf0;
LCM_E = 1;
LCM_E = 1;
LCM_E = 0;
}
/*---------------------------------------------
函数名:LCM_Init() 功 能:对LCD 1602 初始化
----------------------------------------------*/
void LCM_Init(void) //LCM 初始化
{
LCM_Data = 0;
Write_Command_LCM(0x28); //三次显示模式设置,不检测忙信号
delay_nms(15);
Write_Command_LCM(0x28);
delay_nms(15);
Write_Command_LCM(0x28);
delay_nms(15);
// Write_Command_LCM(0x28,1); //显示模式设置,开始要求每次检测忙信号
Write_Command_LCM(0x08,1); //关闭显示
delay_nms(15);
Write_Command_LCM(0x01,1); //显示清屏
delay_nms(15);
Write_Command_LCM(0x06,1); //显示光标移动设置
delay_nms(15);
Write_Command_LCM(0x0C,1); //显示开及光标设置
delay_nms(15);
}
/*-------------------------------------------
函数名:Set_xy_LCM ()
功 能:设定显示坐标位置
--------------------------------------------*/
void Set_xy_LCM(unsigned char x, unsigned char y)
{
unsigned char address;
if( x == 0 )
address = 0x80+y;
else
address = 0xc0+y;
Write_Command_LCM(address,1);
}
/*-------------------------------------------
函数名:Display_List_Char()
功 能:按指定位置显示一串字符
--------------------------------------------*/
void Display_List_Char(unsigned char x, unsigned char y, unsigned char *s)
{
Set_xy_LCM(x,y);
while(*s)
{
LCM_Data = *s;
Write_Data_LCM(*s);
s++;
}
}
void delay_us(unsigned char n)
{
while(n)
{
_nop_();
n--;
}
}
unsigned char ad_conv_tlc549() //TLC549A/D转换子函数
{
unsigned char i;
unsigned char tmp_data=0;
TLC549_CS=1; //此时DATA OUT接口处于高阻态,I/O CLK不起作用
_nop_();
TLC549_CLK=0;
_nop_();
TLC549_CS=0; //等待两个内部时钟上升沿和一个下降沿,单片机确认这一变化,然后自动将前一次转换结果的最高位(D7)位输出到DATAOUT
_nop_();
_nop_();
for(i=0;i<8;i++)
{
TLC549_CLK=1;
tmp_data=(tmp_data<<1)|TLC549_DATAOUT;
TLC549_CLK=0;
}
TLC549_CS=1;
delay_us(17);
return tmp_data;
}
void init_uart() //串口初始化子函数
{
TMOD=0x20; //use timer1,mode2
TH1=0xfd; //19200
TL1=0xfd; //19200
PCON=0x80;
SCON=0x52;
TCON=0x69;
EA=1; //start uart
}
void main() //主函数
{
unsigned char str[32];
unsigned char longstr[16];
float v;
float L;
float u;
init_uart();
LCM_Init();
while(1)
{
v=(int)ad_conv_tlc549()*1.0/256.0*5.0;
u=v*1000; //转换成毫伏显示
L=v*10.0*100.0; //标定后0~50000lux对应输出0~5V DC,则Y=10X
sprintf(str,"U=%.2fmV",u); //显示电压值
sprintf(longstr,"L=%.2fLux",L); //显示光照强度值,单位为lux
Display_List_Char(0,0,&str);
Display_List_Char(1,0,&longstr);
delay_nms(200);
printf("U=%.2fmV\r\n",u); //输出U=?V的字符串,带小数点,两位小数
}
} |