TCS34725颜色识别用OLED显示基于stc8h8k64u主函数例程
时间: 2024-03-17 08:47:11 浏览: 93
以下是基于STC8H8K64U的主函数例程,用OLED显示TCS34725颜色识别结果:
```
#include <reg52.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <intrins.h>
#include "tcs34725.h"
#include "oled.h"
#define uchar unsigned char
#define uint unsigned int
sbit SCL = P1^7;
sbit SDA = P1^6;
void Delay_10us(uint t)
{
while(t--) _nop_();
}
void Delay_ms(uint t)
{
uint i;
while(t--)
{
i = 110;
while(i--);
}
}
void IIC_Start()
{
SDA = 1;
SCL = 1;
Delay_10us(1);
SDA = 0;
Delay_10us(1);
SCL = 0;
}
void IIC_Stop()
{
SDA = 0;
SCL = 1;
Delay_10us(1);
SDA = 1;
Delay_10us(1);
}
void IIC_Ack()
{
SDA = 0;
Delay_10us(1);
SCL = 1;
Delay_10us(1);
SCL = 0;
Delay_10us(1);
SDA = 1;
}
void IIC_NAck()
{
SDA = 1;
Delay_10us(1);
SCL = 1;
Delay_10us(1);
SCL = 0;
Delay_10us(1);
}
uchar IIC_SendByte(uchar dat)
{
uchar i;
for(i = 0; i < 8; i++)
{
SDA = (dat & 0x80) >> 7;
dat <<= 1;
Delay_10us(1);
SCL = 1;
Delay_10us(1);
SCL = 0;
Delay_10us(1);
}
SDA = 1;
Delay_10us(1);
SCL = 1;
Delay_10us(1);
i = SDA;
SCL = 0;
Delay_10us(1);
return i;
}
uchar IIC_ReadByte()
{
uchar i, dat = 0;
SDA = 1;
for(i = 0; i < 8; i++)
{
Delay_10us(1);
SCL = 1;
Delay_10us(1);
dat <<= 1;
dat |= SDA;
SCL = 0;
Delay_10us(1);
}
return dat;
}
void TCS34725_WriteReg(uchar reg, uchar dat)
{
IIC_Start();
IIC_SendByte(TCS34725_ADDRESS << 1);
IIC_SendByte(reg | TCS34725_COMMAND_BIT);
IIC_SendByte(dat);
IIC_Stop();
}
uchar TCS34725_ReadReg(uchar reg)
{
uchar dat;
IIC_Start();
IIC_SendByte(TCS34725_ADDRESS << 1);
IIC_SendByte(reg | TCS34725_COMMAND_BIT);
IIC_Start();
IIC_SendByte((TCS34725_ADDRESS << 1) | 1);
dat = IIC_ReadByte();
IIC_NAck();
IIC_Stop();
return dat;
}
void TCS34725_Init()
{
TCS34725_WriteReg(TCS34725_ENABLE, TCS34725_ENABLE_PON);
Delay_ms(3);
TCS34725_WriteReg(TCS34725_ENABLE, TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN);
Delay_ms(3);
}
void TCS34725_GetRGB(uchar *r, uchar *g, uchar *b)
{
*r = TCS34725_ReadReg(TCS34725_RDATAL);
*g = TCS34725_ReadReg(TCS34725_GDATAL);
*b = TCS34725_ReadReg(TCS34725_BDATAL);
*r = (*r) * 255 / 65535;
*g = (*g) * 255 / 65535;
*b = (*b) * 255 / 65535;
}
void OLED_ShowRGB(uchar r, uchar g, uchar b)
{
char str[10];
OLED_Clear();
OLED_ShowString(0, 0, "R:");
sprintf(str, "%d", r);
OLED_ShowString(16, 0, str);
OLED_ShowString(48, 0, "G:");
sprintf(str, "%d", g);
OLED_ShowString(64, 0, str);
OLED_ShowString(96, 0, "B:");
sprintf(str, "%d", b);
OLED_ShowString(112, 0, str);
}
void main()
{
uchar r, g, b;
TCS34725_Init();
OLED_Init();
OLED_ShowString(0, 0, "TCS34725 Test");
while(1)
{
TCS34725_GetRGB(&r, &g, &b);
OLED_ShowRGB(r, g, b);
Delay_ms(200);
}
}
```
其中,`tcs34725.h`和`oled.h`是TCS34725和OLED的头文件,需要自行编写。在主函数中,通过调用`TCS34725_GetRGB()`获取TCS34725的RGB值,然后通过`OLED_ShowRGB()`在OLED上显示。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)