|
我现在为了测试我上面的tcs传感器的程序简便使用数码管检验我的颜色传感器是否可以识别,我结果无论是什么颜色都显示1,但是我的数码管直接不亮,有大佬可以帮我看一下是单片机程序的问题还是我传感器的问题?
#include <reg52.h>
#include<math.h> //Keil library
#include<stdio.h> //Keil library
#include<INTRINS.H>
#define uchar unsigned char
sbit S2 = P0^0;
sbit S3 = P0^1;
sbit OUT = P0^2;
sbit S0 = P0^3;
sbit S1 = P0^4;
sbit P2_0 = P2^0;
sbit P2_1 = P2^1;
sbit P2_2 = P2^2;
sbit P2_3 = P2^3;
int white_balance_red,white_balance_green,white_balance_blue;
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,
0xf8,0x80,0x90};
void delay(unsigned int t) {
unsigned int i, j;
for (i = 0; i < t; i++)
for (j = 0; j < 125; j++);
}
unsigned int read_pulse() {
return 100;
}
void perform_white_balance() {
S2 = 0;
S3 = 0;
delay(100);
white_balance_red = read_pulse();
S2 = 1;
S3 = 1;
delay(100);
white_balance_green = read_pulse();
S2 = 0;
S3 = 1;
delay(100);
white_balance_blue = read_pulse();
}
unsigned char recognize_color() {
unsigned int red_pulse, green_pulse, blue_pulse;
red_pulse = read_pulse();
green_pulse = read_pulse();
blue_pulse = read_pulse();
red_pulse -= white_balance_red;
green_pulse -= white_balance_green;
blue_pulse -= white_balance_blue;
if (red_pulse > 200 && green_pulse > 200 && blue_pulse > 200) {
return 'W';
} else if (red_pulse > green_pulse && red_pulse > blue_pulse) {
return 'R';
} else if (green_pulse > red_pulse && green_pulse > blue_pulse) {
return 'G';
} else if (blue_pulse > red_pulse && blue_pulse > green_pulse) {
return 'B';
} else if (red_pulse > 150 && green_pulse > 100 && blue_pulse < 100) {
return 'Y';
} else if (red_pulse < 50 && green_pulse < 50 && blue_pulse < 50) {
return 'K';
} else {
return 'U';
}
}
void main() {
unsigned char color;
perform_white_balance();
while (1) {
color = recognize_color_with_white_balance();
switch (color) {
case 'W':
P0=table[0];
P2_0 = 0;
delay(5);
P2_0 = 1;
break;
case 'R': P0=table[0];
P2_0 = 0;
delay(5);
P2_0 = 1;
break;
case 'G': P0=table[0];
P2_0 = 0;
delay(5);
P2_0 = 1;
break;
case 'B': P0=table[0];
P2_0 = 0;
delay(5);
P2_0 = 1;
break;
case 'Y': P0=table[0];
P2_0 = 0;
delay(5);
P2_0 = 1;
OLED_Clear();
break;
case 'K': P0=table[0];
P2_0 = 0;
delay(5);
P2_0 = 1;
break;
}
}
}
|
|