使用TCS3200模块实现颜色识别的51单片机代码,并通过串口发送颜色信息,不用中断,不用定时器实现
时间: 2023-12-24 09:04:55 浏览: 26
以下是使用TCS3200模块实现颜色识别的51单片机代码,并通过串口发送颜色信息,不用中断,不用定时器实现。需要注意的是,本代码仅供参考,具体实现要根据自己的实际情况进行调整。
```
#include <reg52.h>
#include <stdio.h>
sbit S2 = P3^5;
sbit S3 = P3^6;
sbit Out = P3^7;
unsigned int red, green, blue;
void delay(unsigned int t)
{
unsigned int i, j;
for(i = t; i > 0; i--)
for(j = 110; j > 0; j--);
}
void TCS3200_Read(unsigned int *red_val, unsigned int *green_val, unsigned int *blue_val)
{
unsigned int i;
unsigned long count;
Out = 1;
S2 = 0;
S3 = 0;
delay(1);
Out = 0;
delay(1);
S2 = 1;
delay(1);
S2 = 0;
delay(1);
S3 = 1;
delay(1);
S3 = 0;
delay(1);
Out = 1;
count = 0;
while(!Out);
for(i = 0; i < 32; i++)
{
while(Out);
while(!Out);
count++;
}
*red_val = count;
Out = 0;
count = 0;
while(!Out);
for(i = 0; i < 32; i++)
{
while(Out);
while(!Out);
count++;
}
*green_val = count;
Out = 0;
S3 = 1;
count = 0;
while(!Out);
for(i = 0; i < 32; i++)
{
while(Out);
while(!Out);
count++;
}
*blue_val = count;
}
void UART_Init()
{
SCON = 0x50;
TMOD &= 0x0F;
TMOD |= 0x20;
TH1 = 0xFD;
TL1 = 0xFD;
TR1 = 1;
}
void UART_SendChar(unsigned char ch)
{
SBUF = ch;
while(!TI);
TI = 0;
}
void UART_SendString(char *str)
{
while(*str != '\0')
UART_SendChar(*str++);
}
void main()
{
unsigned char str[32];
UART_Init();
while(1)
{
TCS3200_Read(&red, &green, &blue);
sprintf(str, "R:%d G:%d B:%d\n", red, green, blue);
UART_SendString(str);
delay(100);
}
}
```