???о???AT89S52 ???? STC89C52 ??AT89S51 STC89C51
????12MHZ
????????Keil
?????zhengzhongxign39*/
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
//==============LCD1602??????????=====================
/*-----------------------------------------------------
|DB0-----P2.0 | DB4-----P2.4 | RW-------P0.6 |
|DB1-----P2.1 | DB5-----P2.5 | RS-------P0.7 |
|DB2-----P2.2 | DB6-----P2.6 | E--------P0.5 |
|DB3-----P2.3 | DB7-----P2.7 |
---------------------------------------------------*/
//================================================*/
#define LCM_Data P0 //LCD1602??????
#define Busy 0x80 //??????LCM?????е?Busy???
sbit LCM_RW = P3^6; //??д??????????LCD1602??????
sbit LCM_RS = P3^5; //??????????????LCD1602??????
sbit LCM_E = P3^4; //???????????,LCD1602???6??
//=================??????????????=====================
/*-----------------------------------------------------
|EO-----GND
|S0-----VCC | S2-----P1.0 | OUT-------P3.5
|S1-----VCC | S3-----P1.1 |
---------------------------------------------------*/
sbit tcs230_s2=P1^0;//TCS230 S2??????P1.0
sbit tcs230_s3=P1^1;//TCS230 S3??????P1.1
sbit tcs230_en=P3^0; //TCS230 EN(E0)??GND
//**************????????***************************************
void WriteDataLCM (uchar WDLCM);//LCD???д????
void WriteCommandLCM (uchar WCLCM,BuysC); //LCD???д???
uchar ReadStatusLCM(void);//??LCD???????
void DisplayOneChar(uchar X,uchar Y,uchar ASCII);//???X+1?е??Y+1λ???????????
void LCMInit(void);//LCD???
void DelayMs(uint Ms);//1MS??????????
void baipingheng();//??????????
void celiang();//??????????
uint ryz,gyz,byz;//??????????? ??????? ???????
uint rb,gb,bb;//RGB?
uchar tab1[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
//***********************??????******************************
main()
{
TMOD=0x51;//?趨T0????????1???10????
LCMInit();//LCD???
baipingheng();//?????????????
while(1)
{
celiang();//???????
DisplayOneChar(0, 0,'R');//??????????RGB?к??????
DisplayOneChar(0, 1, rb/100+0x30); //?????λ????
DisplayOneChar(0, 2, rb/10%10+0x30);//????λ????
DisplayOneChar(0, 3, rb%10+0x30);//?????λ????
DisplayOneChar(0, 5,'G');//??????????RGB?????????
DisplayOneChar(0, 6, gb/100+0x30); //?????λ????
DisplayOneChar(0, 7, gb/10%10+0x30);
DisplayOneChar(0, 8, gb%10+0x30);
DisplayOneChar(0, 10,'B');//??????????RGB?????????
DisplayOneChar(0, 11, bb/100+0x30);
DisplayOneChar(0, 12, bb/10%10+0x30);
DisplayOneChar(0, 13, bb%10+0x30);
//*****??LCD1602????????16???????RGB*******************
DisplayOneChar(1, 1, tab1[rb/16]);
DisplayOneChar(1, 2, tab1[rb%16]);
DisplayOneChar(1, 3, 'H');
DisplayOneChar(1, 6, tab1[gb/16]);
DisplayOneChar(1, 7, tab1[rb%16]);
DisplayOneChar(1, 8, 'H');
DisplayOneChar(1, 11,tab1[bb/16]);
DisplayOneChar(1, 12,tab1[bb%16]);
DisplayOneChar(1, 13,'H');
DelayMs(250);//???0.25???????????
}
}
//******************************************************
//??????????
void celiang()
{
//*********??R?************************************
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
TH1=0;
TL1=0;
tcs230_s2=0;
tcs230_s3=0;//??????????
tcs230_en=0;
TR0=1;//10????????
TR1=1;//???????
while(TF0==0);//???????????
TF0=0;//????????0??????
TR0=0;//?????0
TR1=0;
rb=(unsigned long)(TH1*256+TL1)*255/ryz;
if(rb>255)rb=255;//?ж?RGB??????
//***********??B?**************************************
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
TH1=0;
TL1=0;
tcs230_s2=0;
tcs230_s3=1;//???????????
TR0=1;//10????????
TR1=1;//???????
while(TF0==0);//???????????
TF0=0;//????????0??????
TR0=0;//?????0
TR1=0;
bb=(unsigned long)(TH1*256+TL1)*255/byz;
if(bb>255)bb=255;//?ж?RGB??????
//***********??G?**************************************
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
TH1=0;
TL1=0;
tcs230_s2=1;
tcs230_s3=1;//???????????
TR0=1;//10????????
TR1=1;//???????
while(TF0==0);//???????????
TF0=0;//????????0??????
TR0=0;//?????0
TR1=0;
tcs230_en=1;
gb=(unsigned long)(TH1*256+TL1)*255/gyz;
if(gb>255)gb=255;//?ж?RGB??????
}
//******************************************************
//??????????
void baipingheng()
{
//**************??????????***********************
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
TH1=0;
TL1=0;
tcs230_s2=0;
tcs230_s3=0;//??????????
tcs230_en=0;
TR0=1;//10????????
TR1=1;//???????
while(TF0==0);//???????????
TF0=0;//????????0??????
TR0=0;//?????0
TR1=0;
ryz=TH1*256+TL1;//???????????????????255/(TH1*256+TL1)
//**************??????????***********************
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
TH1=0;
TL1=0;
tcs230_s2=0;
tcs230_s3=1;//???????????
TR0=1;//10????????
TR1=1;//???????
while(TF0==0);//???????????
TF0=0;//????????0??????
TR0=0;//?????0
TR1=0;
byz=TH1*256+TL1;//???????????????????255/(TH1*256+TL1)
//**************??????????***********************
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
TH1=0;
TL1=0;
tcs230_s2=1;
tcs230_s3=1;//???????????
TR0=1;//10????????
TR1=1;//???????
while(TF0==0);//???????????
TF0=0;//????????0??????
TR0=0;//?????0
TR1=0;
tcs230_en=1;
gyz=TH1*256+TL1;//???????????????????255/(TH1*256+TL1)
}
/*======================================================================
LCM?????
======================================================================*/
void LCMInit(void)
{
LCM_Data = 0;
WriteCommandLCM(0x38,0); //???????????????????????
DelayMs(5);
WriteCommandLCM(0x38,0);
DelayMs(5);
WriteCommandLCM(0x38,0);
DelayMs(5);
WriteCommandLCM(0x38,1); //?????????,????????μ??????
WriteCommandLCM(0x08,1); //??????
WriteCommandLCM(0x01,1); //???????
WriteCommandLCM(0x06,1); // ?????????????
WriteCommandLCM(0x0C,1); // ??????????????
DelayMs(100);
}
//==============================LCD1602????????================================================
// д???????: E =?????? RS=1 RW=0
//======================================================================*/
void WriteDataLCM(uchar WDLCM)
{
ReadStatusLCM(); //????
LCM_Data = WDLCM;
LCM_RS = 1;
LCM_RW = 0;
LCM_E = 0; //????????????????????С?????
LCM_E = 0; //???
LCM_E = 1;
}
/*====================================================================
д?????: E=?????? RS=0 RW=0
======================================================================*/
void WriteCommandLCM(uchar WCLCM,BuysC) //BuysC?0?????????
{
if (BuysC) ReadStatusLCM(); //???????????
LCM_Data = WCLCM;
LCM_RS = 0;
LCM_RW = 0;
LCM_E = 0;
LCM_E = 0;
LCM_E = 1;
}
/*====================================================================
??????д????????????LCD????????:E=1 RS=0 RW=1;
DB7: 0 LCD?????????У?1 LCD?????????
????
======================================================================*/
uchar ReadStatusLCM(void)
{
LCM_Data = 0xFF;
LCM_RS = 0;
LCM_RW = 1;
LCM_E = 0;
LCM_E = 0;
LCM_E = 1;
while (LCM_Data & Busy); //???????
return(LCM_Data);
}
/*======================================================================
?? ??: ??1602 ???λ???????????:?????λ??0~15,?????16~31
? ??: ?? X ??,?? y ?? ???:????????????16?????
======================================================================*/
void DisplayOneChar( uchar X, uchar Y, uchar ASCII)
{
X &= 0x1;
Y &= 0xF; //????Y???????15??X???????1
if (X) Y |= 0x40; //?????????????????+0x40;
Y |= 0x80; // ????????
WriteCommandLCM(Y, 0); //????????????????????
WriteDataLCM(ASCII);
}
/*====================================================================
?趨??????:x*1ms
====================================================================*/
void DelayMs(uint Ms)
{
uint i,TempCyc;
for(i=0;i<Ms;i++)
{
TempCyc = 250;
while(TempCyc--);
}
}
|