|
我要写一个蓝牙控制RGB变色和两个光电传感器计数的程序
单个的模块可以实现,但是我在往一起合的时候,就不能了
而且发现功能会互相影响,求大神帮我改一下代码,说一下
思路,怎么才能两个同时运行,,很急..- #include <reg52.h>
- #include <string.h>
- #include <intrins.h>
-
- #define LCD1602_DB P0
-
- unsigned char tmp;
- unsigned int c=0;
- unsigned char flag1s = 0;
- unsigned char flag2s = 0;
- unsigned char counter = 0;//jishu
- unsigned char str[] = "COUNT: ";
- sbit LCD1602_RS = P1^0;
- sbit LCD1602_RW = P1^1;
- sbit LCD1602_E = P1^5;
-
- sbit red = P2^0;
- sbit LED = P2^2;
- sbit red1 = P2^1;
-
- sbit RR = P3^5;
- sbit GG = P3^6;
- sbit BB = P3^7;
-
- int PWM_0 = 0;
- int PWM_1 = 0;
- int PWM_2 = 0;
-
- void InitLcd1602();
- void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str);
- void itoa (unsigned int n,unsigned char s[5]);
- void keepred();
-
- void init(); //串口初始化
- void send(unsigned char a); //单字节发送函数
- void ctrl(); //接收处理函数
- void color();
- void keepPWM();
-
- void delay (unsigned char t) //延时
- {
- unsigned char i;
- while(t--)
- {
- for(i=0; i<114; i++);
- }
- }
-
- void main()
- {
- unsigned int sec = 0, i=0, j = 0;
-
- unsigned char s[5] ;
- unsigned char x[6] = "none";
- init();
- red = 1;
- red1 = 1;
- TMOD = 0x21;
- TL0 = 0xC9; //设置定时初值
- TH0 = 0xFF;
- TR0 = 1;
- ET0 = 1;
- ET1 = 1;
- EA = 1;
- InitLcd1602();
-
- sec = 0;
- itoa(sec, s);
- LcdShowStr(8, 0, x);
- while(1)
- {
- keepPWM();
- if(RI==1) // 是否有数据到来
- {
- RI = 0;
- tmp = SBUF;
- ctrl();
- delay(5); // 暂存接收到的数据
- }
-
- else if(flag1s == 1 )
- {
- flag1s = 0;
- sec++;
- }
- else if(flag2s == 1 )
- {
- flag2s = 0;
- if(sec > 0)
- sec--;
- }
- else if(sec<=0)
- {
- sec = 0;
-
- LcdShowStr(8, 0, x);
- }
- else
- {
-
- itoa(sec, s);
- memset(s, '\0', 5);
- delay(50);
-
- }
- if(RI == 0)
- {
- PWM_0 = PWM_0;
- PWM_1 = PWM_1;
- PWM_2 = PWM_2;
- delay(5);
- }
- delay(5);
- }
- }
- void LcdWaitReady()
- {
- unsigned char sta;
-
- LCD1602_DB = 0xFF;
- LCD1602_RS = 0;
- LCD1602_RW = 1;
- do {
- LCD1602_E = 1;
- sta = LCD1602_DB;
- LCD1602_E = 0;
- }while(sta & 0x80);
- }
- void LcdWriteCmd(unsigned char cmd)
- {
- LcdWaitReady();
- LCD1602_RS = 0;
- LCD1602_RW = 0;
- LCD1602_DB = cmd;
- LCD1602_E = 1;
- LCD1602_E = 0;
- }
- void LcdWriteDat(unsigned char dat)
- {
- LcdWaitReady();
- LCD1602_RS = 1;
- LCD1602_RW = 0;
- LCD1602_DB = dat;
- LCD1602_E = 1;
- LCD1602_E = 0;
- }
- void LcdSetCursor(unsigned char x, unsigned char y)
- {
- unsigned char addr;
- if(y == 0)
- addr = 0x00 + x;
- else
- addr = 0x40 + x;
- LcdWriteCmd(addr | 0x80);
- }
- void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str)
- {
- LcdSetCursor(x, y);
- while(*str != '\0')
- {
- LcdWriteDat(*str++);
- }
- }
- void InitLcd1602()
- {
- LcdWriteCmd(0x38);
- LcdWriteCmd(0x0C);
- LcdWriteCmd(0x06);
- LcdWriteCmd(0x01);
- }
- void itoa (unsigned int n,unsigned char s[])
- {
- unsigned char temp;
- unsigned int i = 0;
- unsigned int len = 0;
- while(n != 0)
- {
- s[i] = (n % 10) + '0' ;//算出字符
- n = n / 10;
- i++;
- }
- s[i] = '\0';
- len = strlen(s);
- for(i = 0; i < len / 2; i++) //前后换顺序
- {
- temp = s[i];
- s[i] = s[len - 1 - i];
- s[len - 1 - i] = temp;
- }
- LcdShowStr(1, 0, str);
- LcdWriteCmd(0x01);
- LcdShowStr(1, 0, str);
- LcdShowStr(8, 0, s);
- s[0] = '\0';
- }
- void keepPWM()//保持输出
- {
- if(PWM_0 <=0)
- PWM_0 =0;
- if(PWM_1 <=0)
- PWM_1 =0;
- if(PWM_2 <=0)
- PWM_2 =0;
- if(PWM_0 >=255)
- PWM_0 =255;
- if(PWM_1 >=255)
- PWM_1 =255;
- if(PWM_2 >=255)
- PWM_2 =255;
- }
- void init() //串口初始化
- {
- ES=0; //关中断
- SCON = 0x50; // REN=1允许串行接受状态,串口工作模式1,
- //10位UART(1位起始位,8位数据位,1位停止位,无奇偶校验),波特率可变
-
- TMOD = 0x20; // 定时器1工作于方式2,8位自动重载模式, 用于产生波特率
- TH1=TL1=0xFD; // 波特率9600 (本次测试采用晶振为11.0592)
-
- PCON &= 0x7f; // 波特率不倍增
- TR1 = 1; //定时器1开始工作,产生波特率
- //发送标志位置0
- TI=0; //接收标志位置0
- RI=0;
- //EA=0;
- ES=1;
- }
-
- /*void send(unsigned char a) //单字节数据发送
- {
- //注意:若单片机TXD(P3.1)无上拉能力,必须在P3.1端接上拉电阻。本次测试需要接上拉电阻
- TI=0;
- SBUF=a;
- while(TI==0);
- TI=0;
- if(c%2) //发送指示灯标志,每接收一次,此灯亮灭交替
- led=1;
- else
- led=0;
- c++;
- }*/
-
- void ctrl() //接收处理函数
- {
- switch(tmp)
- {
- case '1': //白
- PWM_0 = 0;
- PWM_1 = 0;
- PWM_2 = 0;
- LED = ~LED;
- break;
- case '2':
- PWM_0 = 255-128; //紫
- PWM_1 = 255;
- PWM_2 = 255-128;
- LED = ~LED;
- break;
- case '3':
- PWM_0 = 255; //蓝色
- PWM_1 = 255;
- PWM_2 = 0;
- LED = ~LED;
- break;
- case '4':
- PWM_0 = 30; // 浅蓝
- PWM_1 = 100;
- PWM_2 = 255;
- LED = ~LED;
- break;
- case '5':
- PWM_0 = 255-33; // 灰
- PWM_1 = 255-33;
- PWM_2 = 255-33;
- LED = ~LED;
- break;
- case '6':
- PWM_0 = 254; // mie
- PWM_1 = 254;
- PWM_2 = 254;
- LED = ~LED;
- break;
- case 'a':
- PWM_0=PWM_0+5; // jiaqiang
- PWM_1=PWM_1+5;
- PWM_2=PWM_2+5;
- break;
- case 'b':
- PWM_0=PWM_0-5; // jianruo
- PWM_1=PWM_1-5;
- PWM_2=PWM_2-5;
- keepPWM();
- break;
- case 'c': //shanshuo
-
- color();
- LED = ~LED;
- break;
- default: //其他,灯全灭
- PWM_0 = 0; //hei
- PWM_1 = 0;
- PWM_2 = 0;
- // send(tmp);
- break;
- }delay(5);
- }
- void color()
- { //随机颜色
- while(RI==0)
- {
- PWM_0 = rand()%255;
- delay(2);
- PWM_1 = rand()%255;
- delay(2);
- PWM_2 = rand()%255;
- delay(2);
- }
- }
-
- void InterruptTimer0() interrupt 1
- {
- TH0 = 0xFC;
- TL0 = 0x67;
-
- if (red == 0&&red1==1) //两个光电判断人进出
- {
- delay(99);
- if(red1==0)
- flag1s = 1;
- }
- if (red1 == 0&&red == 1)
- {
- delay(99);
- if(red == 0)
- flag2s = 1;
- }
-
- counter++;
- if(counter >= 255) counter = 0; //三路pWM输出
- if(counter >= PWM_0) RR = 0; else RR = 1;
- if(counter >= PWM_1) GG = 0; else GG = 1;
- if(counter >= PWM_2) BB = 0; else BB = 1;
- }
复制代码
|
|