标题: 有没有大佬帮忙看看这个单片机程序我写的是用tcs230然后用数码管显示 [打印本页]

作者: forky    时间: 2024-5-17 13:04
标题: 有没有大佬帮忙看看这个单片机程序我写的是用tcs230然后用数码管显示
我现在为了测试我上面的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;
        }
    }
}




作者: rundstedt    时间: 2024-5-17 14:30
white_balance_red这几个变量名字太长了吧,标准c语言支持31字符长度变量,这么写没问题。但是C51他不标准啊!查下最多支持几个字符?
作者: 人中狼    时间: 2024-5-17 15:33
编译没报错吗
作者: lkc8210    时间: 2024-5-17 17:57
你这代码只会显示0,绝不会显示1
看不到哪里有读取tcs230的代码
作者: forky    时间: 2024-5-17 19:08
发表于 2024-5-17 14:30
white_balance_red这几个变量名字太长了吧,标准c语言支持31字符长度变量,这么写没问题。但是C51他不标准 ...

他可以支持255个但是前32个有效,但是我在keil上可以编译但烧入到单片机上我的颜色传感器没反应

作者: forky    时间: 2024-5-17 19:29
发表于 2024-5-17 14:30
white_balance_red这几个变量名字太长了吧,标准c语言支持31字符长度变量,这么写没问题。但是C51他不标准 ...

我现在把变量名改短了但是我的数码管还是不会显示
作者: forky    时间: 2024-5-17 21:14
lkc8210 发表于 2024-5-17 17:57
你这代码只会显示0,绝不会显示1
看不到哪里有读取tcs230的代码

打错了是0,我的目的就是为了检验我的tcs230能不能用
作者: forky    时间: 2024-5-17 21:22
人中狼 发表于 2024-5-17 15:33
编译没报错吗

没有报错就是用不了
作者: forky    时间: 2024-5-17 21:57
lkc8210 发表于 2024-5-17 17:57
你这代码只会显示0,绝不会显示1
看不到哪里有读取tcs230的代码

怎么提取呀我不太会
作者: 人中狼    时间: 2024-5-17 22:15
recognize_color_with_white_balance();这个代码在哪里
作者: forky    时间: 2024-5-17 23:30
人中狼 发表于 2024-5-17 22:15
recognize_color_with_white_balance();这个代码在哪里

好像没有那是用recognize_color就好了嘛
作者: 人中狼    时间: 2024-5-18 19:03
forky 发表于 2024-5-17 23:30
好像没有那是用recognize_color就好了嘛

while (1) {   
        color = recognize_color_with_white_balance();
        switch (color) {
感觉你的问题很随意,答复的也很随意




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1