void smg_process() { smg_buf[0] = smg_code[0]; smg_buf[1] = smg_code[数字]; smg_buf[2] = 0x40; //表示 - smg_buf[3] = 0x40; //如果想加入百位,可以输入smg_code[TIME/10/10%10],此处的/10则表示“加一位” smg_buf[4] = smg_code[TIME/10%10];//表示个位数,此处两行为数码管后两位的10s倒计时显示, smg_buf[5] = smg_code[TIME%10]; //表示十位数 }解释一下
时间: 2023-11-24 08:06:34 浏览: 33
这段代码涉及到数码管的显示,其中smg_buf是一个数组,用于存储要显示的数码管的数值。smg_code也是一个数组,用于存储0~9以及一些特殊符号(如“-”)在数码管上的对应显示值。
在这段代码中,smg_buf[0]表示数码管的第一个位置,即固定显示为0;smg_buf[1]表示数码管的第二个位置,数字为变量number所代表的值;smg_buf[2]和smg_buf[3]表示数码管的第三个和第四个位置,固定显示为“-”符号;smg_buf[4]表示数码管的第五个位置,数字为变量TIME的十位数;smg_buf[5]表示数码管的第六个位置,数字为变量TIME的个位数。
如果想要加入百位数的显示,可以在smg_buf[3]的位置上加入smg_code[TIME/10/10%10],其中/10表示“加一位”,TIME/10/10%10表示TIME的百位数。
相关问题
save_buf[0]=save_value/100; save_buf[1]=save_value%100/10; save_buf[2]=save_value%100%10; smg_display(save_buf,6);这段代码显示数字改为528
你可以将这段代码修改为以下内容,实现在数码管上显示数字528:
```c
save_value = 528; // 将变量 save_value 的值设置为528
save_buf[0] = save_value / 100; // 528 / 100 = 5,将商赋值给 save_buf[0]
save_buf[1] = save_value % 100 / 10; // 528 % 100 = 28,28 / 10 = 2,将商赋值给 save_buf[1]
save_buf[2] = save_value % 100 % 10; // 528 % 100 = 28,28 % 10 = 8,将余数赋值给 save_buf[2]
smg_display(save_buf, 6); // 在数码管上以静态方式显示数字528
```
这段代码将变量 `save_value` 的值设置为528,并根据题目要求将其拆分成三个数字,保存到数组 `save_buf` 中,然后调用 `smg_display()` 函数在数码管上以静态方式显示这三个数字。
#include "public.h" #include "smg.h" #include "ds18b20.h" #include "reg52.h" #include <stdio.h> void init_uart() { SCON = 0x50; TMOD = 0x20; TH1 = 0xfd; TL1 = 0xfd; TR1 = 1; EA = 1; ES = 1; } void send_byte(unsigned char dat) { SBUF = dat; while(!TI); TI = 0; } void send_string(char *str) { while(*str != '\0') { send_byte(*str++); } } void main() { u8 i=0; int temp_value; u8 temp_buf[5]; char str[10]; init_uart(); // ds18b20_init(); // while(1) { i++; if(i%50==0) // temp_value=ds18b20_read_temperture()*10; if(temp_value<0) { temp_value=-temp_value; temp_buf[0]=0x40; } else temp_buf[0]=0x00; temp_buf[0]=gsmg_code[temp_value/1000]; temp_buf[1]=gsmg_code[temp_value%1000/100]; temp_buf[2]=gsmg_code[temp_value%1000%100/10]|0x80; temp_buf[3]=gsmg_code[temp_value%1000%100%10]; smg_display(temp_buf,1); sprintf(str, "%d.%d\r\n", temp_value/100, temp_value%10); send_string(str); } }请逐行解释代码
好的,我来逐行解释这段代码:
```c
#include "public.h"
#include "smg.h"
#include "ds18b20.h"
#include "reg52.h"
#include <stdio.h>
```
这里是包含了程序所需要的头文件。
```c
void init_uart() {
SCON = 0x50;
TMOD = 0x20;
TH1 = 0xfd;
TL1 = 0xfd;
TR1 = 1;
EA = 1;
ES = 1;
}
```
这个函数是初始化串口的函数,其中 SCON、TMOD、TH1、TL1、TR1、EA 和 ES 都是 8051 的寄存器。SCON 设置串口工作方式,TMOD 设置计时器工作方式,TH1 和 TL1 设置波特率,TR1 开启计时器,EA 和 ES 开启中断。
```c
void send_byte(unsigned char dat) {
SBUF = dat;
while(!TI);
TI = 0;
}
```
这个函数是发送一个字节的函数,其中 SBUF 是串口数据寄存器,TI 是发送中断标志位。这个函数的作用是将 dat 发送出去,并等待发送完成。
```c
void send_string(char *str) {
while(*str != '\0')
{
send_byte(*str++);
}
}
```
这个函数是发送字符串的函数,其中 *str 是指向字符串的指针,'\0' 表示字符串结束符。这个函数的作用是将整个字符串发送出去,调用了上面的 send_byte 函数。
```c
void main() {
u8 i=0;
int temp_value;
u8 temp_buf[5];
char str[10];
init_uart(); // 初始化串口
while(1)
{
i++;
if(i%50==0) // 读取温度
temp_value=ds18b20_read_temperture()*10;
if(temp_value<0)
{
temp_value=-temp_value;
temp_buf[0]=0x40;
}
else
temp_buf[0]=0x00;
temp_buf[0]=gsmg_code[temp_value/1000];
temp_buf[1]=gsmg_code[temp_value%1000/100];
temp_buf[2]=gsmg_code[temp_value%1000%100/10]|0x80;
temp_buf[3]=gsmg_code[temp_value%1000%100%10];
smg_display(temp_buf,1); // 数码管显示温度
sprintf(str, "%d.%d\r\n", temp_value/100, temp_value%10);
send_string(str); // 通过串口发送温度值
}
}
```
这个函数是主函数,其中 i、temp_value、temp_buf 和 str 都是变量。首先调用了 init_uart 函数初始化串口,然后进入一个无限循环。在循环中,每次 i 自增 1,当 i 能被 50 整除时,调用 ds18b20_read_temperture 函数读取温度值,并将其乘以 10。接着判断温度值是否小于 0,如果是,则将其取反,并将 temp_buf[0] 的值设置为 0x40,否则为 0x00。然后将温度值的千位、百位、十位和个位分别存放到 temp_buf 数组中,并调用 smg_display 函数将温度值显示在数码管上。最后,使用 sprintf 函数将温度值转换为字符串格式,再通过 send_string 函数将温度值发送到串口。
希望这些解释能够帮助您理解这段代码。
相关推荐
![](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)