蓝桥杯模块综合——高质量讲解AT24C02,BS18B20,BS1302,AD/DA(PCF8591),超声波模块

本文主要是介绍蓝桥杯模块综合——高质量讲解AT24C02,BS18B20,BS1302,AD/DA(PCF8591),超声波模块,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

AT24C02——就是一个存储的东西,可以给他写东西,掉电不丢失。

void EEPROM_Write(unsigned char * EEPROM_String,unsigned char addr , unsigned char num)
{IIC_Start();IIC_SendByte(0xA0);IIC_WaitAck();IIC_SendByte(addr);IIC_WaitAck();while(num--){IIC_SendByte(*EEPROM_String++);IIC_WaitAck();IIC_Delay(200);}IIC_Stop();
}
void EEPROM_Read(unsigned char * EEPROM_String,unsigned char addr , unsigned char num)
{IIC_Start();IIC_SendByte(0xA0);IIC_WaitAck();IIC_SendByte(addr);IIC_WaitAck();IIC_Start();IIC_SendByte(0xA1);IIC_WaitAck();while(num--){*EEPROM_String++=IIC_RecByte();if(num) IIC_SendAck(0);//·¢ËÍÓ¦´ðelse IIC_SendAck(1);}IIC_Stop();
}

这个是这个代码,先开始IIC看他在不在(0xA0)等待应答,然后发送要写的页,和字节。然后发送然后等待 停止。

读——

一样的是先开始,然后找他,等待,然后写入页,然后IIC开始,进入读模式,然后等待。然后将读的东西给,数组。if num==0 就停止应答

下面是测试代码:

#include <STC15F2K60S2.H>
#include <iic.h>
void EEPROM_Write(unsigned char * EEPROM_String,unsigned char addr , unsigned char num);
void EEPROM_Read(unsigned char * EEPROM_String,unsigned char addr , unsigned char num);
unsigned char code Duan[22]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,
0xc6,0xc0,0x86,0x8e,0xbf,0x7f,0XC1,0X8C,0XC8,0xc3};
unsigned char dat[2]={30,60},keynum;
void Delay(unsigned int t)
{while(t--);}
void suocun(unsigned char n)
{switch(n){case 4:P2=(P2 & 0x1f)|0x80;break;case 5:P2=(P2 & 0x1f)|0xa0;break;case 6:P2=(P2 & 0x1f)|0xc0;break;case 7:P2=(P2 & 0x1f)|0xe0;break;}
}
void smg(unsigned char wei,unsigned char shu)
{suocun(6);P0=0x01<<wei;suocun(7);P0=Duan[shu];Delay(500);P0=0xff;
}
void shuma()
{smg(0,dat[0]/10);Delay(100);smg(1,dat[0]%10);Delay(100);smg(6,dat[1]/10);Delay(100);smg(7,dat[1]%10);Delay(100);
}
void jvzhen()
{P30=P31=P32=P33=1;P34=P35=P42=1;P44=0;if(P33==0){Delay(100);keynum=4;while(P33==0){shuma();}}if(P32==0){Delay(100);keynum=5;while(P32==0){shuma();}}if(P31==0){Delay(100);keynum=6;while(P31==0){shuma();}}if(P30==0){Delay(100);keynum=7;while(P30==0){shuma();}}
}
void zhixing()
{if(keynum==4){dat[0]+=10;keynum=0;}if(keynum==5){dat[0]-=10;keynum=0;}if(keynum==6){EEPROM_Write(dat,0,2);keynum=0;}}
void EEPROM_Write(unsigned char * EEPROM_String,unsigned char addr , unsigned char num)
{IIC_Start();IIC_SendByte(0xA0);IIC_WaitAck();IIC_SendByte(addr);IIC_WaitAck();while(num--){IIC_SendByte(*EEPROM_String++);IIC_WaitAck();IIC_Delay(200);}IIC_Stop();
}
void EEPROM_Read(unsigned char * EEPROM_String,unsigned char addr , unsigned char num)
{IIC_Start();IIC_SendByte(0xA0);IIC_WaitAck();IIC_SendByte(addr);IIC_WaitAck();IIC_Start();IIC_SendByte(0xA1);IIC_WaitAck();while(num--){*EEPROM_String++=IIC_RecByte();if(num) IIC_SendAck(0);//·¢ËÍÓ¦´ðelse IIC_SendAck(1);}IIC_Stop();
}void main()
{EEPROM_Read(dat,0,2);while(1){shuma();jvzhen();zhixing();}
}

 DS18B20——温度读取

float read_t()
{unsigned char low,high;init_ds18b20();Write_DS18B20(0xcc);Write_DS18B20(0x44);init_ds18b20();Write_DS18B20(0xcc);Write_DS18B20(0xbe);low = Read_DS18B20();high = Read_DS18B20();return ((high<<8)|low)/16.0;}

下面是测试代码,可能会有温度显示不正常,一直是85,就是DS18B20的Delay不正常,要在里面有一句 t*=12

#include <STC15F2K60S2.H>
#include <onewire.h>
#include <intrins.h>
sbit s0 = P3^0;
sbit s1 = P3^1;
sbit s2 = P3^2;
sbit s3 = P3^3;sbit r0 = P3^4;
sbit r1 = P3^5;
sbit r2 = P4^2;
sbit r3 = P4^4;
float t=31.5;
unsigned char keynum;
unsigned char code Duan[22]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,
0xc6,0xc0,0x86,0x8e,0xbf,0x7f,0XC1,0X8C,0XC8,0xc3};
void Delay750ms()		//@12.000MHz
{unsigned char i, j, k;_nop_();_nop_();i = 35;j = 51;k = 182;do{do{while (--k);} while (--j);} while (--i);
}void Delaysmg(unsigned int t)
{while(t--);
}
void Delayjan(unsigned char t)
{while(t--);
}
void suocun(unsigned char n)
{switch(n){case 4:P2 = (P2 & 0x1f) | 0x80; break;case 5:P2 = (P2 & 0x1f) | 0xa0; break;case 6:P2 = (P2 & 0x1f) | 0xc0; break;case 7:P2 = (P2 & 0x1f) | 0xe0; break;case 0:P2 = (P2 & 0x1f) | 0x00; break;}
}
void shuma(unsigned char wei,unsigned char shu)
{suocun(6);P0=0x01<<wei;suocun(0);suocun(7);P0=Duan[shu];Delaysmg(1000);P0=0xff;suocun(0);
}
void shumadong()
{t = read_t();shuma(5,(int)t/10%10);Delaysmg(500);shuma(6,(int)t%10);Delaysmg(500);shuma(7,(int)(t*10)%10);Delaysmg(500);}
void Delay_Key(unsigned char t)
{while(t--);
}
void jvzhen()
{s0=0;s1=s2=s3=1;r0=r1=r2=r3=1;if(r3==0){while(r3==0);keynum=0;shumadong();}
//=============================s0=0;s1=s2=s3=1;r0=r1=r2=r3=1;if(r2==0){while(r2==0);keynum=1;shumadong();}
//=============================s0=0;s1=s2=s3=1;r0=r1=r2=r3=1;if(r1==0){while(r1==0);keynum=2;shumadong();}
//=============================s0=0;s1=s2=s3=1;r0=r1=r2=r3=1;if(r2==0){while(r2==0);keynum=2;shumadong();}
//=========================================================s1=0;s0=s2=s3=1;r0=r1=r2=r3=1;if(r3==0){while(r3==0);keynum=3;shumadong();}
//=============s1=0;s0=s2=s3=1;r0=r1=r2=r3=1;if(r2==0){while(r2==0);keynum=4;shumadong();}
//=============s1=0;s0=s2=s3=1;r0=r1=r2=r3=1;if(r1==0){while(r1==0);keynum=5;shumadong();}
//====================================s2=0;s0=s1=s3=1;r0=r1=r2=r3=1;if(r3==0){while(r3==0);keynum=6;shumadong();}
//===================s2=0;s0=s1=s3=1;r0=r1=r2=r3=1;if(r2==0){while(r2==0);keynum=7;shumadong();}
//====================s2=0;s0=s1=s3=1;r0=r1=r2=r3=1;if(r1==0){while(r1==0);keynum=8;shumadong();}
//=================================s3=0;s0=s1=s2=1;r0=r1=r2=r3=1;if(r3==0){while(r3==0);keynum=9;shumadong();}
}
void main()
{while(1){shumadong();}
}

DS1302——时间的计时

unsigned char wirter[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
unsigned char read[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
//20Äê18ÈÕ  ÖÜÁù 23.59.24
unsigned char Timer[7]={0x24,0x59,0x23,0x18,0x04,0x06,0x20};
unsigned char code Duan[22]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,
0xc6,0xc0,0x86,0x8e,0xbf,0x7f,0XC1,0X8C,0XC8,0xc3};
void Delaysmg(unsigned int t)
{while(t--);
}
void Ds1302()
{char i;Write_Ds1302_Byte(0x8e,0x00);for (i=0;i<7;i++){Write_Ds1302_Byte(wirter[i],Timer[i]);}Write_Ds1302_Byte(0x8e,0x80);
}
void readds()
{char i;for(i=0;i<7;i++){Timer[i]=Read_Ds1302_Byte(read[i]);}
}

 写入有七个地址,读也是七个,然后存入数据 ,,上面我是 2020年周六4月18日,23分59分24秒

刚刚好七位,就是分别写入七个地址。

下面是测试代码:

#include <STC15F2K60S2.H>
#include <ds1302.h>
unsigned char wirter[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
unsigned char read[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
//20Äê18ÈÕ  ÖÜÁù 23.59.24
unsigned char Timer[7]={0x24,0x59,0x23,0x18,0x04,0x06,0x20};
unsigned char code Duan[22]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,
0xc6,0xc0,0x86,0x8e,0xbf,0x7f,0XC1,0X8C,0XC8,0xc3};
void Delaysmg(unsigned int t)
{while(t--);
}
void Ds1302()
{char i;Write_Ds1302_Byte(0x8e,0x00);for (i=0;i<7;i++){Write_Ds1302_Byte(wirter[i],Timer[i]);}Write_Ds1302_Byte(0x8e,0x80);
}
void readds()
{char i;for(i=0;i<7;i++){Timer[i]=Read_Ds1302_Byte(read[i]);}
}
void suocun(unsigned char n)
{switch(n){case 4:P2=(P2 & 0x1f)|0x80;break;case 5:P2=(P2 & 0x1f)|0xa0;break;case 6:P2=(P2 & 0x1f)|0xc0;break;case 7:P2=(P2 & 0x1f)|0xe0;break;case 0:P2=(P2 & 0x1f)|0x00;break;}
}
void smg(unsigned char wei,unsigned char shu)
{suocun(6);P0=0x01<<wei;suocun(7);P0=Duan[shu];Delaysmg(500);P0=0xff;
}
void xianshi()
{smg(0,Timer[2]/16);Delaysmg(100);smg(1,Timer[2]%16);Delaysmg(100);smg(3,Timer[1]/16);Delaysmg(100);smg(4,Timer[1]%16);Delaysmg(100);smg(6,Timer[0]/16);Delaysmg(100);smg(7,Timer[0]%16);Delaysmg(100);}
void main()
{Ds1302();while(1){readds();xianshi();}
}

PCF8591——数模转换 AD/DA

unsigned char Ad_read(unsigned char addr)
{unsigned char temp;IIC_Start();IIC_SendByte(0x90);//дÈëIIC_WaitAck();IIC_SendByte(addr);//Òª¶ÁµÄÊÇIIC_WaitAck();IIC_Start();IIC_SendByte(0x91);//¶ÁIIC_WaitAck();temp = IIC_RecByte();IIC_SendAck(1);IIC_Stop();return temp;
}void Da_write(unsigned char dat)
{IIC_Start();IIC_SendByte(0x90);//дÈëIIC_WaitAck();IIC_SendByte(0x41);//ʹÄÜDACIIC_WaitAck();IIC_SendByte(dat);//дÈëIIC_WaitAck();IIC_Stop();
}

经典的IIC。

读——

开始IIC,发送找到模块(0x90),等待,然后从哪里读数据,等待,然后IIC开始读,停止响应,然后IIC停止。

写——

开始 写数据,写入到0x41(0x43)等待响应,写入,然后写入。

测试代码:

#include <STC15F2K60S2.H>
#include <iic.h>
unsigned char code Duan[22]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,
0xc6,0xc0,0x86,0x8e,0xbf,0x7f,0XC1,0X8C,0XC8,0xc3};
unsigned char dat;
void Delay(unsigned int t)
{while(t--);
}
void suocun(unsigned char n)
{switch(n){case 4:P2=(P2 & 0x1f)|0x80;break;case 5:P2=(P2 & 0x1f)|0xa0;break;case 6:P2=(P2 & 0x1f)|0xc0;break;case 7:P2=(P2 & 0x1f)|0xe0;break;}	
}
void shuma(unsigned char wei,unsigned char shu)
{suocun(6);P0=0x01<<wei;suocun(7);P0=Duan[shu];Delay(500);P0=0xff;
}
void xianshi()
{shuma(1,1);Delay(100);shuma(2,2);Delay(100);
}
void chuli()
{dat=Ad_read(0x43);shuma(0,dat/100%10);Delay(100);shuma(1,dat/10%10);Delay(100);shuma(2,dat%10);Delay(100);}
void main()
{while(1){chuli();}
}

超声波——

左边发松所以左边为TX ,右边为RX

void Ut_Wave_init()
{unsigned char i;for(i=0;i<8;i++){TX=1;Delay12us();TX=0;Delay12us();}
}
unsigned char Ut_Wave_Data()
{unsigned int time;TMOD &= 0x0f;TH1=TL1=0;Ut_Wave_init();TR1=1;while((RX==1)&&(TF1 == 0));TR1=0;if(TF1==0){time = TH1<<8|TL1;return (time*0.017);}else{TF1=0;return 0;}
}

发送一个方波信号,

然后就是用定时器1去完成,波的计算。发送完成之后开启定时器,然后等待,如果RX==0 或者计数溢出(TF1=1)就会退出,停止计数。得到TH1和TL1。然后输出距离.

实例代码:

#include <STC15F2K60S2.H>
#include <intrins.h>
sbit TX = P1^0;
sbit RX = P1^1;
floatjvli;
unsigned char code Duan[22]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,
0xc6,0xc0,0x86,0x8e,0xbf,0x7f,0XC1,0X8C,0XC8,0xc3};void Delay12us()		//@12.000MHz
{unsigned char i;_nop_();_nop_();i = 33;while (--i);
}
void Delay(unsigned int t)
{while(t--);
}
void suocun(unsigned char n)
{switch(n){case 4:P2= (P2 & 0x1f)|0x80;break;case 5:P2= (P2 & 0x1f)|0xa0;break;case 6:P2= (P2 & 0x1f)|0xc0;break;case 7:P2= (P2 & 0x1f)|0xe0;break;}
}
void smg(unsigned char wei,unsigned char shu)
{suocun(6);P0=0x01<<wei;suocun(7);P0=Duan[shu];Delay(500);P0=0xff;
}
void Ut_Wave_init()
{unsigned char i;for(i=0;i<8;i++){TX=1;Delay12us();TX=0;Delay12us();}
}
unsigned char Ut_Wave_Data()
{unsigned int time;TMOD &= 0x0f;TH1=TL1=0;Ut_Wave_init();TR1=1;while((RX==1)&&(TF1 == 0));TR1=0;if(TF1==0){time = TH1<<8|TL1;return (time*0.017);}else{TF1=0;return 0;}
}
void xianshi()
{smg(1,(int)jvli/100%10);Delay(100);smg(2,(int)jvli/10%10);Delay(100);smg(3,(int)jvli%10);Delay(100);}
void main()
{while(1){jvli=Ut_Wave_Data();xianshi();}
}

这篇关于蓝桥杯模块综合——高质量讲解AT24C02,BS18B20,BS1302,AD/DA(PCF8591),超声波模块的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/829539

相关文章

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

Python Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

Python正则表达式语法及re模块中的常用函数详解

《Python正则表达式语法及re模块中的常用函数详解》这篇文章主要给大家介绍了关于Python正则表达式语法及re模块中常用函数的相关资料,正则表达式是一种强大的字符串处理工具,可以用于匹配、切分、... 目录概念、作用和步骤语法re模块中的常用函数总结 概念、作用和步骤概念: 本身也是一个字符串,其中

Python中的getopt模块用法小结

《Python中的getopt模块用法小结》getopt.getopt()函数是Python中用于解析命令行参数的标准库函数,该函数可以从命令行中提取选项和参数,并对它们进行处理,本文详细介绍了Pyt... 目录getopt模块介绍getopt.getopt函数的介绍getopt模块的常用用法getopt模

python logging模块详解及其日志定时清理方式

《pythonlogging模块详解及其日志定时清理方式》:本文主要介绍pythonlogging模块详解及其日志定时清理方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录python logging模块及日志定时清理1.创建logger对象2.logging.basicCo

Qt spdlog日志模块的使用详解

《Qtspdlog日志模块的使用详解》在Qt应用程序开发中,良好的日志系统至关重要,本文将介绍如何使用spdlog1.5.0创建满足以下要求的日志系统,感兴趣的朋友一起看看吧... 目录版本摘要例子logmanager.cpp文件main.cpp文件版本spdlog版本:1.5.0采用1.5.0版本主要

C++ vector的常见用法超详细讲解

《C++vector的常见用法超详细讲解》:本文主要介绍C++vector的常见用法,包括C++中vector容器的定义、初始化方法、访问元素、常用函数及其时间复杂度,通过代码介绍的非常详细,... 目录1、vector的定义2、vector常用初始化方法1、使编程用花括号直接赋值2、使用圆括号赋值3、ve

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

Python基础文件操作方法超详细讲解(详解版)

《Python基础文件操作方法超详细讲解(详解版)》文件就是操作系统为用户或应用程序提供的一个读写硬盘的虚拟单位,文件的核心操作就是读和写,:本文主要介绍Python基础文件操作方法超详细讲解的相... 目录一、文件操作1. 文件打开与关闭1.1 打开文件1.2 关闭文件2. 访问模式及说明二、文件读写1.

C# WinForms存储过程操作数据库的实例讲解

《C#WinForms存储过程操作数据库的实例讲解》:本文主要介绍C#WinForms存储过程操作数据库的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、存储过程基础二、C# 调用流程1. 数据库连接配置2. 执行存储过程(增删改)3. 查询数据三、事务处