蓝桥杯单片机——备赛(DS18B20)数码管亮度不同问题,温度一直是85或者95的问题。初步看赛点资源包!!!

&:

运算规则:如果两个二进制数的同一个位数上的数都为1,则当前位为1,否则为0.

|  :

运算规则:如果两个二进制数的同一个位数上的数有一个不为0则当前位为1,否则为0.

I/O扩展和寄存器扩展

跟着小蜜蜂,点亮LED灯和数码管。有两个模式 一个IO一个寄存器:

先是寄存器:

#include <STC15F2K60S2.H>
#include <absacc.h>
void Delay(unsigned int t)
{
	while(t--);
	while(t--);
}
void LED()
{
	XBYTE[0x8000]=0xf0;
	Delay(65535);
	Delay(65535);
	XBYTE[0x8000]=0x0f;
	Delay(65535);
	Delay(65535);
	XBYTE[0x8000]=0xff;
}
void shuma()
{
	unsigned char i;
	for (i=0;i<8;i++)
	{
		XBYTE[0xc000]=0x01<<i;
		XBYTE[0xe000]=0x00;
		Delay(65535);
	Delay(65535);
	}
	XBYTE[0xed000]=0xff;
	Delay(65535);
	Delay(65535);
}
void main()
{
	while(1)
	{
	LED();
	shuma();
	}
}

其中注意的是:其实就是HC754——寄存器

0x80——是4,

0xa0——是5,

0xc0——是6,

0xe0——是7,



DS18B20:(解决最后一位数码管亮)

最后一位亮的本质是,数码管显示不好

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);
}

给他关了,在下一次打开就好了!!!



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;
	
}

 期初就是一直是95.也就是读不到。

经过检查

void Delay_OneWire(unsigned int t)  
{
	t*=12;
	while(t--);
}

这里的时间改成这样。为什么呢?

看给的资料包

在读和写的时候,会有时间的限制。

这就是资源包。!!! 

全部代码:

main

#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();
	}
}

有矩阵和数码管。

ds18b20:

#include "onewire.h"
#include <STC15F2K60S2.H>
sbit DQ = P1^4;  
//µ¥×ÜÏßÄÚ²¿ÑÓʱº¯Êý
void Delay_OneWire(unsigned int t)  
{
	t*=12;
	while(t--);
}

//µ¥×ÜÏßд²Ù×÷
void Write_DS18B20(unsigned char dat)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
		DQ = 0;
		DQ = dat&0x01;
		Delay_OneWire(5);
		DQ = 1;
		dat >>= 1;
	}
	Delay_OneWire(5);
}

//µ¥×ÜÏ߶Á²Ù×÷
unsigned char Read_DS18B20(void)
{
	unsigned char i;
	unsigned char dat;
  
	for(i=0;i<8;i++)
	{
		DQ = 0;
		dat >>= 1;
		DQ = 1;
		if(DQ)
		{
			dat |= 0x80;
		}	    
		Delay_OneWire(5);
	}
	return dat;
}

//DS18B20³õʼ»¯
bit init_ds18b20(void)
{
  	bit initflag = 0;
  	
  	DQ = 1;
  	Delay_OneWire(12);
  	DQ = 0;
  	Delay_OneWire(80);
  	DQ = 1;
  	Delay_OneWire(10); 
    initflag = DQ;     
  	Delay_OneWire(5);
  
  	return initflag;
}

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;
	
}

就是最后一个。/16也可以写成*0.625

为什么要这么写 ,还是看赛点资源包!!!

4个不同精度的。温度。 

最近更新

  1. TCP协议是安全的吗?

    2024-03-17 05:44:05       17 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-17 05:44:05       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-17 05:44:05       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-17 05:44:05       18 阅读

热门阅读

  1. Linux-centos系统中如何去除配置文件中的注释部分

    2024-03-17 05:44:05       16 阅读
  2. LLMOps:机器学习运营的下一个前沿

    2024-03-17 05:44:05       21 阅读
  3. 数据结构 第4章 串(一轮习题总结)

    2024-03-17 05:44:05       18 阅读
  4. npm install报错

    2024-03-17 05:44:05       19 阅读
  5. 数据结构-哈希表(一)

    2024-03-17 05:44:05       20 阅读
  6. List、Set、数据结构、Collections-笔记

    2024-03-17 05:44:05       21 阅读
  7. 供应RTC77601E芯片现货

    2024-03-17 05:44:05       21 阅读