用C语言编写AT89C52与ADC0809、LED数码管、ptc热敏电阻、DS18b20、继电器、24c02的代码
时间: 2023-07-10 13:03:45 浏览: 108
AT89C52+74HC164+数码管组成的数据显示电路包含原理图和源代码和Proteus仿真电路
5星 · 资源好评率100%
以下是基于AT89C52单片机的ADC0809、LED数码管、PTC热敏电阻、DS18B20、继电器、24c02的C语言代码。
这里只提供了代码框架和基本函数,具体实现需要根据硬件电路进行调整和修改。
```
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
// ADC0809定义
sbit ADC0809_CS = P1^0;
sbit ADC0809_RD = P1^1;
sbit ADC0809_WR = P1^2;
sbit ADC0809_INTR = P1^3;
// LED数码管定义
sbit LED_DIO = P0^0;
sbit LED_CLK = P0^1;
sbit LED_LAT = P0^2;
// PTC热敏电阻定义
sbit PTC_DO = P3^3;
sbit PTC_DIN = P3^4;
sbit PTC_CLK = P3^5;
// DS18B20定义
sbit DS18B20_DQ = P3^2;
// 继电器定义
sbit Relay = P2^0;
// 24C02定义
#define EEPROM_ADDR 0xA0
sbit EEPROM_SCL = P2^1;
sbit EEPROM_SDA = P2^2;
// ADC0809读取函数
uchar ADC0809_Read(uchar ch) {
uchar result = 0;
ADC0809_CS = 0;
ADC0809_WR = 1;
ADC0809_RD = 1;
ADC0809_WR = 0;
P0 = ch;
ADC0809_WR = 1;
while(!ADC0809_INTR);
ADC0809_RD = 0;
result = P0;
ADC0809_RD = 1;
ADC0809_CS = 1;
return result;
}
// LED数码管显示函数
void LED_Display(uchar *buf, uchar len) {
uchar i, j;
for(i = 0; i < len; i++) {
for(j = 0; j < 8; j++) {
LED_CLK = 0;
LED_DIO = buf[i] & 0x01;
buf[i] >>= 1;
LED_CLK = 1;
}
}
LED_LAT = 0;
LED_LAT = 1;
}
// PTC热敏电阻读取函数
uint PTC_Read() {
uchar i;
uint result = 0;
PTC_DIN = 1;
PTC_CLK = 1;
PTC_CLK = 0;
PTC_DIN = 0;
PTC_CLK = 1;
PTC_CLK = 0;
for(i = 0; i < 16; i++) {
PTC_CLK = 1;
result <<= 1;
result |= PTC_DO;
PTC_CLK = 0;
}
return result;
}
// DS18B20复位函数
uchar DS18B20_Reset() {
uchar result = 0;
DS18B20_DQ = 1;
DS18B20_DQ = 0;
DS18B20_DQ = 1;
while(DS18B20_DQ);
result = DS18B20_DQ;
while(!DS18B20_DQ);
return result;
}
// DS18B20读取温度函数
int DS18B20_ReadTemp() {
uchar i;
int result = 0;
DS18B20_Reset();
DS18B20_DQ = 1;
DS18B20_DQ = 0;
DS18B20_DQ = 1;
while(DS18B20_DQ);
DS18B20_DQ = 1;
for(i = 0; i < 8; i++) {
DS18B20_DQ = 0;
DS18B20_DQ = 1;
result >>= 1;
if(DS18B20_DQ) {
result |= 0x8000;
}
}
return result;
}
// 继电器开关函数
void Relay_Control(uchar state) {
Relay = state;
}
// 24C02写入函数
void EEPROM_Write(uchar addr, uchar data) {
uchar i;
EEPROM_SCL = 1;
EEPROM_SDA = 1;
EEPROM_SCL = 0;
EEPROM_SDA = 0;
for(i = 0; i < 8; i++) {
EEPROM_SCL = 0;
if(addr & 0x80) {
EEPROM_SDA = 1;
} else {
EEPROM_SDA = 0;
}
EEPROM_SCL = 1;
addr <<= 1;
}
EEPROM_SDA = 1;
EEPROM_SCL = 0;
EEPROM_SCL = 1;
EEPROM_SDA = 0;
for(i = 0; i < 8; i++) {
EEPROM_SCL = 0;
if(data & 0x80) {
EEPROM_SDA = 1;
} else {
EEPROM_SDA = 0;
}
EEPROM_SCL = 1;
data <<= 1;
}
EEPROM_SCL = 0;
EEPROM_SDA = 1;
}
// 24C02读取函数
uchar EEPROM_Read(uchar addr) {
uchar i, result = 0;
EEPROM_SCL = 1;
EEPROM_SDA = 1;
EEPROM_SCL = 0;
for(i = 0; i < 8; i++) {
EEPROM_SCL = 0;
if(addr & 0x80) {
EEPROM_SDA = 1;
} else {
EEPROM_SDA = 0;
}
EEPROM_SCL = 1;
addr <<= 1;
}
EEPROM_SDA = 1;
EEPROM_SCL = 0;
EEPROM_SCL = 1;
for(i = 0; i < 8; i++) {
EEPROM_SCL = 0;
result <<= 1;
if(EEPROM_SDA) {
result |= 0x01;
}
EEPROM_SCL = 1;
}
EEPROM_SCL = 0;
EEPROM_SDA = 1;
return result;
}
void main() {
uchar adc_result, led_buf[4], ptc_buf[4], eeprom_data;
uint ptc_result;
int ds18b20_temp;
// 初始化LED数码管
LED_CLK = 0;
LED_LAT = 1;
LED_DIO = 0;
while(1) {
// 读取ADC0809
adc_result = ADC0809_Read(0);
// 将ADC值转化为LED数码管显示数据
// ...
// 显示LED数码管
LED_Display(led_buf, 4);
// 读取PTC热敏电阻
ptc_result = PTC_Read();
// 将PTC值转化为LED数码管显示数据
// ...
// 显示LED数码管
LED_Display(ptc_buf, 4);
// 读取DS18B20温度
ds18b20_temp = DS18B20_ReadTemp();
// 判断温度是否超过阈值,控制继电器开关
// ...
// 读取24C02数据
eeprom_data = EEPROM_Read(0x00);
// 写入24C02数据
EEPROM_Write(0x00, eeprom_data + 1);
// 延时
// ...
}
}
```
阅读全文