蓝桥杯模块综合——高质量讲解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

相关文章

Node.js net模块的使用示例

《Node.jsnet模块的使用示例》本文主要介绍了Node.jsnet模块的使用示例,net模块支持TCP通信,处理TCP连接和数据传输,具有一定的参考价值,感兴趣的可以了解一下... 目录简介引入 net 模块核心概念TCP (传输控制协议)Socket服务器TCP 服务器创建基本服务器服务器配置选项服

Java集合中的List超详细讲解

《Java集合中的List超详细讲解》本文详细介绍了Java集合框架中的List接口,包括其在集合中的位置、继承体系、常用操作和代码示例,以及不同实现类(如ArrayList、LinkedList和V... 目录一,List的继承体系二,List的常用操作及代码示例1,创建List实例2,增加元素3,访问元

Python使用国内镜像加速pip安装的方法讲解

《Python使用国内镜像加速pip安装的方法讲解》在Python开发中,pip是一个非常重要的工具,用于安装和管理Python的第三方库,然而,在国内使用pip安装依赖时,往往会因为网络问题而导致速... 目录一、pip 工具简介1. 什么是 pip?2. 什么是 -i 参数?二、国内镜像源的选择三、如何

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详

Python利用自带模块实现屏幕像素高效操作

《Python利用自带模块实现屏幕像素高效操作》这篇文章主要为大家详细介绍了Python如何利用自带模块实现屏幕像素高效操作,文中的示例代码讲解详,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、获取屏幕放缩比例2、获取屏幕指定坐标处像素颜色3、一个简单的使用案例4、总结1、获取屏幕放缩比例from

nginx-rtmp-module模块实现视频点播的示例代码

《nginx-rtmp-module模块实现视频点播的示例代码》本文主要介绍了nginx-rtmp-module模块实现视频点播,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习... 目录预置条件Nginx点播基本配置点播远程文件指定多个播放位置参考预置条件配置点播服务器 192.

Redis的Zset类型及相关命令详细讲解

《Redis的Zset类型及相关命令详细讲解》:本文主要介绍Redis的Zset类型及相关命令的相关资料,有序集合Zset是一种Redis数据结构,它类似于集合Set,但每个元素都有一个关联的分数... 目录Zset简介ZADDZCARDZCOUNTZRANGEZREVRANGEZRANGEBYSCOREZ

Go中sync.Once源码的深度讲解

《Go中sync.Once源码的深度讲解》sync.Once是Go语言标准库中的一个同步原语,用于确保某个操作只执行一次,本文将从源码出发为大家详细介绍一下sync.Once的具体使用,x希望对大家有... 目录概念简单示例源码解读总结概念sync.Once是Go语言标准库中的一个同步原语,用于确保某个操

Window Server2016加入AD域的方法步骤

《WindowServer2016加入AD域的方法步骤》:本文主要介绍WindowServer2016加入AD域的方法步骤,包括配置DNS、检测ping通、更改计算机域、输入账号密码、重启服务... 目录一、 准备条件二、配置ServerB加入ServerA的AD域(test.ly)三、查看加入AD域后的变

Window Server2016 AD域的创建的方法步骤

《WindowServer2016AD域的创建的方法步骤》本文主要介绍了WindowServer2016AD域的创建的方法步骤,文中通过图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录一、准备条件二、在ServerA服务器中常见AD域管理器:三、创建AD域,域地址为“test.ly”