帮我写一个颜色传感器tcs3473识别红,蓝,绿的51程序,并在lcd12864上显示颜色汉字
时间: 2023-09-14 18:15:45 浏览: 132
好的,以下是一个简单的51程序,使用TCS3473颜色传感器,通过I2C协议与单片机通信,识别红、蓝、绿三种颜色,并在LCD12864上显示相应的颜色汉字。
```c
#include <reg52.h>
#include <intrins.h>
#include <stdio.h>
#define LCD_DATA P0 //LCD数据口定义
sbit LCD_WR = P2^6; //LCD写信号线定义
sbit LCD_RD = P2^5; //LCD读信号线定义
sbit LCD_RS = P2^4; //LCD寄存器选择信号线定义
sbit LCD_CS1 = P2^3; //LCD片选信号线1定义
sbit LCD_CS2 = P2^2; //LCD片选信号线2定义
#define SDA P1_7 //I2C总线SDA引脚定义
#define SCL P1_6 //I2C总线SCL引脚定义
sbit SDA_H = SDA;
sbit SDA_L = SDA;
sbit SCL_H = SCL;
sbit SCL_L = SCL;
sbit LED_RED = P1^0; //红色LED灯引脚定义
sbit LED_BLUE = P1^1; //蓝色LED灯引脚定义
sbit LED_GREEN = P1^2; //绿色LED灯引脚定义
#define TCS3473_ADDR 0x29 //TCS3473器件地址定义
void delay(int ms)
{
int i, j;
for (i = 0; i < ms; i++)
for (j = 0; j < 110; j++);
}
void I2C_start()
{
SDA_H = 1;
SCL_H = 1;
SDA_L = 0;
delay(1);
SCL_L = 0;
}
void I2C_stop()
{
SDA_L = 0;
SCL_H = 1;
SDA_H = 1;
delay(1);
}
void I2C_write_byte(unsigned char dat)
{
unsigned char i;
for (i = 0; i < 8; i++)
{
if (dat & 0x80)
SDA_H = 1;
else
SDA_L = 0;
SCL_H = 1;
dat <<= 1;
SCL_L = 0;
}
SDA_H = 1;
SCL_H = 1;
SCL_L = 0;
}
unsigned char I2C_read_byte()
{
unsigned char i, dat = 0;
SDA_H = 1;
for (i = 0; i < 8; i++)
{
dat <<= 1;
SCL_H = 1;
if (SDA)
dat |= 0x01;
SCL_L = 0;
}
return dat;
}
void TCS3473_init()
{
I2C_start();
I2C_write_byte(TCS3473_ADDR);
I2C_write_byte(0x80); //写控制寄存器地址
I2C_write_byte(0x03); //设置增益为16X,设置等待时间为2.4ms
I2C_stop();
}
unsigned int TCS3473_read(unsigned char addr)
{
unsigned int dat;
I2C_start();
I2C_write_byte(TCS3473_ADDR);
I2C_write_byte(addr | 0x80); //设置读地址
I2C_start();
I2C_write_byte(TCS3473_ADDR + 1);
dat = I2C_read_byte();
dat |= I2C_read_byte() << 8;
I2C_stop();
return dat;
}
void LCD_write_cmd(unsigned char cmd)
{
LCD_CS1 = 0;
LCD_CS2 = 0;
LCD_RS = 0;
LCD_DATA = cmd;
LCD_WR = 0;
_nop_();
LCD_WR = 1;
}
void LCD_write_dat(unsigned char dat)
{
LCD_CS1 = 0;
LCD_CS2 = 0;
LCD_RS = 1;
LCD_DATA = dat;
LCD_WR = 0;
_nop_();
LCD_WR = 1;
}
void LCD_clear()
{
int i, j;
for (i = 0; i < 8; i++)
{
LCD_write_cmd(0xb8 + i);
LCD_write_cmd(0x40);
for (j = 0; j < 128; j++)
LCD_write_dat(0x00);
}
}
void LCD_init()
{
LCD_write_cmd(0xe2); //软复位
delay(10);
LCD_write_cmd(0x2c); //开启内部时钟
LCD_write_cmd(0x2e); //开启电荷泵
LCD_write_cmd(0x2f); //开启LCD显示
LCD_write_cmd(0x23); //设置调节电位器范围
LCD_write_cmd(0x81); //设置电荷泵
LCD_write_cmd(0x28); //设置1/4偏压比
LCD_write_cmd(0x40); //设置行地址
LCD_write_cmd(0xb8); //设置页地址
LCD_clear();
}
void LCD_display_string(unsigned char page, unsigned char column, char *str)
{
int i;
LCD_write_cmd(0xb8 + page);
LCD_write_cmd(0x40 + column);
for (i = 0; str[i] != '\0'; i++)
LCD_write_dat(str[i]);
}
void main()
{
unsigned int red, green, blue;
char color[5];
TCS3473_init();
LCD_init();
while (1)
{
red = TCS3473_read(0x16); //读取红色通道值
green = TCS3473_read(0x18); //读取绿色通道值
blue = TCS3473_read(0x1a); //读取蓝色通道值
if (red > blue && red > green) //判断颜色
{
LED_RED = 1;
LED_BLUE = 0;
LED_GREEN = 0;
sprintf(color, "%s", "红色");
}
else if (blue > red && blue > green)
{
LED_RED = 0;
LED_BLUE = 1;
LED_GREEN = 0;
sprintf(color, "%s", "蓝色");
}
else if (green > red && green > blue)
{
LED_RED = 0;
LED_BLUE = 0;
LED_GREEN = 1;
sprintf(color, "%s", "绿色");
}
else
{
LED_RED = 0;
LED_BLUE = 0;
LED_GREEN = 0;
sprintf(color, "%s", "未知");
}
LCD_display_string(4, 0, color); //显示颜色汉字
delay(500);
}
}
```
需要注意的是,该代码中使用了I2C总线来与TCS3473颜色传感器通信,需要在程序中添加I2C相关的函数。此外,LCD12864的连接方式也需要根据具体的硬件来进行修改。
阅读全文