void delay (float t) {unsigned int j,i; j = t*100; while(j--) {for (i=1505;i;i--);} }这串代码是什么意思
时间: 2023-05-19 08:00:53 浏览: 164
这段代码是一个延时函数,输入参数为浮点数 t,表示延时的时间(单位为秒)。函数内部将 t 乘以 100 得到延时的次数 j,然后使用一个双重循环进行延时,每次循环内部使用一个计数器 i 进行计数,直到计数器 i 减为 0,完成一次延时。
相关问题
#include<reg51.h> #include<intrins.h> #define uchar unsigned char #define uint unsigned int sbit DQ=P3^7; sbit P2_0=P2^0; sbit k2=P2^2; sbit k4=P2^4; sbit k3=P2^3; uchar timp,F=0; float c; uchar a[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40}; uchar b[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef}; void delay5(uchar n) { do { _nop_(); _nop_(); _nop_(); n--; } while(n); } void init_DS18B20() { uchar x=0; DQ=0; delay5(120); DQ=1; delay5(16); delay5(80); } uchar readbyte() { uchar i=0; uchar date=0; for(i=8;i>0;i--) { DQ=0; delay5(1); DQ=1; date>>=1; if(DQ) date|=0x80; delay5(11); } return(date); } void writebyte(uchar dat) { uchar i=0; for(i=8;i>0;i--) { DQ=0; DQ=dat&0x01; delay5(12); DQ=1; dat>>=1; delay5(5); } } uchar retemp() { uchar a,b,tt; uint t; init_DS18B20(); writebyte(0xCC); writebyte(0x44); init_DS18B20(); writebyte(0xCC); writebyte(0xBE); a=readbyte(); b=readbyte(); t=b; t<<=8; t=t|a; if((t&0xf800)!=0xf800) { F=0; c=t*0.0625; tt=t*0.0625; timp=t*0.625-tt*10; } else { F=1; t=(~t)+1; c=t*0.0625; tt=t*0.0625; timp=t*0.625-tt*10; } return tt; } void main() { uchar i,temp; delay5(1000); while(1) { temp=retemp(); if(c>=25&&F==0) P2_0=0; else P2_0=1; for(i=0;i<15;i++) { k2=1;k3=1;k4=1; if(F==0) P0=a[temp/100]; else P0=a[10]; delay5(1000); // P2=0xfb;//11111011,0xfb k2=0;k3=1;k4=1; P0=a[temp%100/10]; delay5(1000); //P2=0xf7;//11110111,0xf7 k2=1;k3=0;k4=1; P0=b[temp%10]; delay5(1000); //P2=0xf3;//11110011,0xf3 k2=0;k3=0;k4=1; P0=a[timp]; delay5(1000); } if(c>=25&&F==0) P2_0=1; else P2_0=1; } }在这个代码的基础上利用串口把数据发送到电脑上的串口助手
好的,我帮您修改一下程序,添加串口通信代码,您可以将数据发送到电脑上的串口助手中:
```
#include<reg51.h>
#include<intrins.h>
#include<stdio.h>
#define uchar unsigned char
#define uint unsigned int
sbit DQ=P3^7;
sbit P2_0=P2^0;
sbit k2=P2^2;
sbit k4=P2^4;
sbit k3=P2^3;
uchar timp,F=0;
float c;
uchar a[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
uchar b[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};
void delay5(uchar n)
{
do
{
_nop_();
_nop_();
_nop_();
n--;
} while(n);
}
void init_DS18B20()
{
uchar x=0;
DQ=0;
delay5(120);
DQ=1;
delay5(16);
delay5(80);
}
uchar readbyte()
{
uchar i=0;
uchar date=0;
for(i=8;i>0;i--)
{
DQ=0;
delay5(1);
DQ=1;
date>>=1;
if(DQ)
date|=0x80;
delay5(11);
}
return(date);
}
void writebyte(uchar dat)
{
uchar i=0;
for(i=8;i>0;i--)
{
DQ=0;
DQ=dat&0x01;
delay5(12);
DQ=1;
dat>>=1;
delay5(5);
}
}
uchar retemp()
{
uchar a,b,tt;
uint t;
init_DS18B20();
writebyte(0xCC);
writebyte(0x44);
init_DS18B20();
writebyte(0xCC);
writebyte(0xBE);
a=readbyte();
b=readbyte();
t=b;
t<<=8;
t=t|a;
if((t&0xf800)!=0xf800)
{
F=0;
c=t*0.0625;
tt=t*0.0625;
timp=t*0.625-tt*10;
}
else
{
F=1;
t=(~t)+1;
c=t*0.0625;
tt=t*0.0625;
timp=t*0.625-tt*10;
}
return tt;
}
void init_uart()
{
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
SCON=0x50;
TR1=1;
}
void send_data(uchar dat)
{
SBUF=dat;
while(!TI);
TI=0;
}
void main()
{
uchar i,temp;
delay5(1000);
init_uart(); //初始化串口
while(1)
{
temp=retemp();
if(c>=25&&F==0)
P2_0=0;
else
P2_0=1;
for(i=0;i<15;i++)
{
k2=1;k3=1;k4=1;
if(F==0)
P0=a[temp/100];
else
P0=a[10];
delay5(1000);
k2=0;k3=1;k4=1;
P0=a[temp%100/10];
delay5(1000);
k2=1;k3=0;k4=1;
P0=b[temp%10];
delay5(1000);
k2=0;k3=0;k4=1;
P0=a[timp];
delay5(1000);
}
if(c>=25&&F==0)
P2_0=1;
else
P2_0=1;
//将温度值发送到串口助手
printf("Temperature is: %.2f\r\n", c);
}
}
```
注意:这里添加了头文件 `stdio.h`,需要在编译器中设置支持此头文件。另外,串口助手的波特率需要与代码中的波特率一致,这里设置为 9600。
#include<reg51.h> sbit MCP_SCK=P1^0; sbit MCP_MISO=P1^1; sbit MCP_CS=P1^2; sbit qianw= P2^0; sbit baiw = P2^1; sbit shiw = P2^2; sbit wan = P2^3; sbit qian = P2^4; sbit bai = P2^5; sbit shi = P2^6; sbit ge = P2^7; unsigned char date[8];//数码管位选 unsigned char code tb[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; char code dx516[3] _at_ 0x003b; void delay(unsigned int j) { unsigned char i=250; for(;j>0;j--) { while(--i); i=249; while(--i); i=250; } } unsigned int read_MCP(void) { unsigned int temp = 0; unsigned char i; MCP_CS=1; delay(1); MCP_CS=0; MCP_SCK=0; for(i=0;i<13;i++) { MCP_SCK=1; temp<<=1; if(MCP_MISO == 1) temp |=0x01; MCP_SCK = 0; } MCP_CS=1; temp &=0x03FF; return temp; } char code dx516[3] _at_ 0x003b; void main() { unsigned int vt; unsigned int R; unsigned char i; P2 |=0xf0; switch(i) //位选,选择点亮的数码管, { case(0): P0 = date[0];qianw= 0;break; case(1): P0 = date[1];baiw = 0;break; case(2): P0 = date[2];shiw = 0;break; case(3): P0 = date[3];wan = 0;break; case(4): P0 = date[4];qian = 0;break; case(5): P0 = date[5];bai = 0;break; case(6): P0 = date[6];shi = 0;break; case(7): P0 = date[7];ge = 0;break; } i++; if(i==8) { i=0; } R=read_MCP; vt = 2.5*R/1024; //测量电压值 date[0] =tb[vt/1000]; date[1] =tb[vt/1000]; date[2] =tb[vt/1000]; date[3] =tb[vt/1000]; date[4] =tb[vt/100%10]; date[5] =tb[vt/10%10]; date[6] =tb[vt%10]; date[7] =0x0a; }有问题吗
这段代码存在一些问题:
1. 在 switch 语句中使用了未初始化的变量 i,导致无法正常选择数码管进行显示。
2. 在调用 read_MCP 函数时没有加上函数的括号,导致无法正常读取 MCP3008 的数据。
3. 在计算电压值时,应该将 vt 的数据类型声明为 float 或者 double,否则 vt 只能保存整数部分,导致计算出来的电压值有误差。
4. 在将电压值转换为数码管显示的数码时,应该分别取出千位、百位、十位和个位上的数码,而不是把所有的位都设置为千位上的数码。
5. 在数码管的位选控制中,应该将对应的位控制信号置为低电平,而不是置为高电平。
6. 在程序结尾处应该加入一个死循环,以保证程序不会结束。
阅读全文
相关推荐
















