新手入门
- 积分
- 9
- 金钱
- 9
- 注册时间
- 2019-8-13
- 在线时间
- 2 小时
|
楼主 |
发表于 2020-3-31 09:25:21
|
显示全部楼层
- #include"reg52.h"
- sfr T2MOD=0XC9;
- unsigned char command =0x00;
- sbit S0=P1^0;
- sbit S1=P1^1;
- sbit S2=P1^2;
- sbit S3=P1^3;
- sbit K4=P3^3;
- unsigned int Gyz,Ryz,Byz;
- unsigned char Rzhi,Bzhi,Gzhi;
- unsigned char code number[]={0,1,2,3,4,5,6,7,8,9};
- unsigned char num=0;
- unsigned char color=0;
- void baipingheng();
- void ceyanse();
- void T0_Init();
- void T1_Init();
- void T2_Init();
- void System_Init()
- {
- S0=1;
- S1=1;
- S2=0;
- S3=0;
- T0_Init();
- T1_Init();
- T2_Init();
- }
- void delay600ms(void)
- {
- unsigned char a,b,c;
- for(c=89;c>0;c--)
- for(b=230;b>0;b--)
- for(a=12;a>0;a--);
- }
- void Delay(unsigned char t)
- {
- while(t--);
- }
- /***********************
- 串口
- *****************************/
- void T2_Init()
- {
- T2MOD|=0X02;
- T2CON=0X34;
- TH2=RCAP2H=0XFF;
- TL2=RCAP2L=0XD9;
- SCON=0X50;
- TR2=1;
- ES=1;
- EA=1;
- }
- void Send_Byte(unsigned char dat)
- {
- SBUF=dat;
- while(TI==0);
- TI=0;
- }
- //void Receive()interrupt 4
- //{
- // if(RI==1)
- // {
- // RI=0;
- // command=SBUF;
- // Send_Byte(command+1);
- // }
- //
- // }
- /*******************************
- 定时器设置
- **************************************/
- void T0_Init()
- {
- TMOD|=0x01;
- TH0=(65536-10000)/256;
- TL0=(65536-10000)%256;
- TR0=0;
- ET0=1;
- EA=1;
- }
- void T1_Init()
- {
- TMOD|=0x50;
- TH1=0x00;
- TL1=0x00;
- TR1=0;
- EX1=1;//外部
- IT1=1;
- EA=1;
- }
- void Service_T0()interrupt 1
- {
- TR1=0;//关闭寄存器
- TR0=0;
- TH0=(65536-10000)/256;//t0初值填入
- TL0=(65536-10000)%256;
- if(color==0) //选择功能
- {
- baipingheng();
- }
- else
- ceyanse();
- }
- void baipingheng()
- {
- if(num==0)//白红
- {
- Ryz=TH1*256+TL1;
- num=1;
- S2=0;
- S3=1;
- TH1=0X00;
- TL1=0X00;
- TR0=1;
- TR1=1;
- }
- if(num==1)//白蓝
- {
- Byz=TH1*256+TL1;
- num=2;
- S2=1;
- S3=1;
- TH1=0X00;
- TL1=0X00;
- TR0=1;
- TR1=1; //继续下一次测量
- }
- if(num==2)//白绿
- {
- Gyz=TH1*256+TL1;
- num=0;
- S2=0;
- S3=0;
- color++;
- TH1=0X00;
- TL1=0X00;
- TR0=0;
- TR1=0; //关闭定时器
- }
- }
- void ceyanse()
- {
- if(num==0)//测红
- {
- Rzhi=(unsigned long)(TH1*256+TL1)*255/Ryz;
- num=1;
- S2=0;
- S3=1;
- TH1=0X00;
- TL1=0X00;
- TR0=1;
- TR1=1;
- }
- if(num==1)//测蓝
- {
- Bzhi=(unsigned long)(TH1*256+TL1)*255/Byz;;
- num=2;
- S2=1;
- S3=1;
- TH1=0X00;
- TL1=0X00;
- TR0=1;
- TR1=1; //继续下一次测量
- }
- if(num==2)//测绿
- {
- Gzhi=(unsigned long)(TH1*256+TL1)*255/Gyz;;
- num=1;
- S2=0;
- S3=0;
- TH1=0X00;
- TL1=0X00;
- TR0=0;
- TR1=0; //关闭定时器
- }
- }
- void KeyScan()
- {
- while(K4==0);
- if(K4==0)
- {
- Delay(20);
- if(K4==0)
- {
-
- TR1=1;
- TR0=1;
- }
- }
- }
- void main()
- {
- System_Init();
- TR0=1;
- TR1=1;
- while(1)
- {
- KeyScan();
- delay600ms();
- Send_Byte(Rzhi);
- Send_Byte(Bzhi);
- Send_Byte(Gzhi);
- }
- }
复制代码 |
|