程序
#include<REG52.H>
#include<math.h>
#include<stdio.h>
#include<INTRINS.H>
#define uchar unsigned char
#define uint unsigned int
#define DataPort P0
sbit LCM_RS=P1^2;
sbit LCM_RW=P1^1;
sbit LCM_EN=P1^0;
sbit s2=P2^1;
sbit s3=P2^0;
sbit test_pin=P2^2;
uchar ge,shi,bai ;
uchar rp=3,gp=3,bp=6;
uchar count;
uchar disp_R[3];
uchar disp_G[3];
uchar disp_B[3];
void delay(unsigned int k);
void InitLcd();
void WriteDataLCM(uchar dataW);
void WriteCommandLCM(uchar CMD,uchar Attribc);
void DisplayOneChar(uchar X,uchar Y,uchar DData);
void InitLcd()
{
WriteCommandLCM(0x38,1);
WriteCommandLCM(0x08,1);
WriteCommandLCM(0x01,1);
WriteCommandLCM(0x06,1);
WriteCommandLCM(0x0c,1);
}
void WaitForEnable(void)
{
DataPort=0xff;
LCM_RS=0;LCM_RW=1;_nop_();
LCM_EN=1;_nop_();_nop_();
while(DataPort&0x80);
LCM_EN=0;
}
void WriteCommandLCM(uchar CMD,uchar Attribc)
{
if(Attribc)WaitForEnable();
LCM_RS=0;LCM_RW=0;_nop_();
DataPort=CMD;_nop_();
LCM_EN=1;_nop_();_nop_();LCM_EN=0;
}
void WriteDataLCM(uchar dataW)
{
WaitForEnable();
LCM_RS=1;LCM_RW=0;_nop_();
DataPort=dataW;_nop_();
LCM_EN=1;_nop_();_nop_();LCM_EN=0;
}
void DisplayOneChar(uchar X,uchar Y,uchar DData)
{
Y&=1;
X&=15;
if(Y)X|=0x40;
X|=0x80;
WriteCommandLCM(X,0);
WriteDataLCM(DData);
}
void delay(unsigned int k)
{
unsigned int i,j;
for(i=0;i<k;i++)
{
for(j=0;j<121;j++)
{
;}
}
}
/*******************************************
* 函数名称: t0_init()
* 函数功能: 定时器0初始化
* 入口参数: 无
* 出口参数: 无
/********************************************/
void t0_init()
{
TMOD=0x51;
TH1=0x00;
TL1=0x00;
TH0=0xE0;
TL0=0x00;
EA=1;
ET0=1;
TR0=1;
TR1=1;
}
void conversion(uint temp_data)
{
bai=temp_data/100+0x30 ;
temp_data=temp_data%100;
shi=temp_data/10+0x30 ;
ge=temp_data%10+0x30;
}
/*******************************************
* 函数名称: main()
/********************************************/
void main()
{
delay(10);
InitLcd();
s2=0;
s3=0;
t0_init();
while(1)
{
DisplayOneChar(0, 0, 'T');
DisplayOneChar(1, 0, 'C');
DisplayOneChar(2, 0, 'S');
DisplayOneChar(3, 0, '2');
DisplayOneChar(4, 0, '3');
DisplayOneChar(5, 0, '0');
DisplayOneChar(10, 0, 'R');
DisplayOneChar(11, 0, '[');
DisplayOneChar(12, 0, disp_R[0]);
DisplayOneChar(13, 0, disp_R[1]);
DisplayOneChar(14, 0, disp_R[2]);
DisplayOneChar(15, 0, ']');
DisplayOneChar(0, 1, 'G');
DisplayOneChar(1, 1, '[');
DisplayOneChar(2, 1, disp_G[0]);
DisplayOneChar(3, 1, disp_G[1]);
DisplayOneChar(4, 1, disp_G[2]);
DisplayOneChar(5, 1, ']');
DisplayOneChar(10, 1, 'B');
DisplayOneChar(11, 1, '[');
DisplayOneChar(12, 1, disp_B[0]);
DisplayOneChar(13, 1, disp_B[1]);
DisplayOneChar(14, 1, disp_B[2]);
DisplayOneChar(15, 1, ']');
delay(100) ;
}
}
void c10ms_out(void) interrupt 1
{
uint temp;
test_pin=!test_pin;
TR0=0;
TR1=0;
if(count==0)
{
count++;
s2=1;s3=1;
temp=(8<<TH1)+TL1;
temp/=rp;
conversion(temp);
disp_R[2]=ge;
disp_R[1]=shi;
disp_R[0]=bai;
}
else if(count==1)
{
count++;
s2=1;s3=0;
temp=(8<<TH1)+TL1;
temp/=gp;
conversion(temp);
disp_G[2]=ge;
disp_G[1]=shi;
disp_G[0]=bai;
}
else if(count==2)
{
count=0;
s2=0;s3=0;
temp=(8<<TH1)+TL1;
temp/=bp;
conversion(temp);
disp_B[2]=ge;
disp_B[1]=shi;
disp_B[0]=bai;
}
TH0=0xE0;
TL0=0x00;
TL1=0x00;
TH1=0x00;
TR0=1;
TR1=1;
}
TCS230模块原理及程序视频讲解:
TCS230模块