if((temp&0xf8)==0x00)\n\t{\n\t\ttemp>>=4;\n\t\ttemp=temp*10;\n\t\ttemp=temp+(low &0x0f)*0.0625;\n\t}\n\treturn temp;\n
时间: 2024-04-11 16:25:29 浏览: 16
这段代码是一个函数,根据输入的参数进行一些计算,并返回结果。代码的逻辑如下:
1. 首先,判断`(temp & 0xf8) == 0x00`,即判断`temp`的高5位是否为0。如果满足条件,则执行以下操作:
2. 将`temp`右移4位,相当于将`temp`的低4位舍弃。
3. 将`temp`乘以10。
4. 将`low`的低4位与`0x0f`进行按位与操作,得到`low`的低4位的值。
5. 将上一步得到的值乘以0.0625。
6. 将上一步得到的结果与`temp`相加,得到最终的结果。
7. 返回最终的结果。
这段代码的作用是将输入的参数进行一些处理,并返回处理后的结果。
相关问题
#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。
解释下这串代码int32_t temp; sys_nvic_priority_group_config(group); /* 设置分组 */ temp = pprio << (4 - group); temp |= sprio & (0x0f >> group); temp &= 0xf; /* 取低四位 */ NVIC->ISER[ch / 32] |= 1 << (ch % 32); /* 使能中断位(要清除的话,设置ICER对应位为1即可) */ NVIC->IP[ch] |= temp << 4;
这段代码是在设置中断优先级的过程中使用的。让我逐行解释一下:
1. `int32_t temp;`:声明一个变量 `temp`,类型为 `int32_t`,用于存储中间值。
2. `sys_nvic_priority_group_config(group);`:调用函数 `sys_nvic_priority_group_config`,将参数 `group` 传递给它,用于设置中断优先级分组。
3. `temp = pprio << (4 - group);`:将变量 `pprio` 左移 `(4 - group)` 位,并将结果赋值给 `temp`。这里使用了位操作符 `<<`,表示左移操作。
4. `temp |= sprio & (0x0f >> group);`:对变量 `temp` 进行按位或操作,将其与变量 `sprio` 与上 `(0x0f >> group)` 的结果进行按位与操作后的值进行按位或。这里使用了位操作符 `|` 和 `&`。
5. `temp &= 0xf;`:将变量 `temp` 与 `0xf` 进行按位与操作,只保留低四位的值。这里使用了位操作符 `&`。
6. `NVIC->ISER[ch / 32] |= 1 << (ch % 32);`:使能中断位。首先通过除以32来计算索引,然后使用位操作符 `|=` 将对应位置为1,表示使能该中断位。这里使用了位操作符 `|` 和 `<<`。
7. `NVIC->IP[ch] |= temp << 4;`:设置中断优先级。将变量 `temp` 左移4位,并通过位操作符 `|=` 将结果与 `NVIC->IP[ch]` 进行按位或操作,以设置中断的优先级。
以上是对这段代码的逐行解释。请注意,具体的代码含义可能需要结合上下文来确定,这里只是给出了一般的解释。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)