在数码管上显示“1234”的程序

本文介绍了如何在四位共阴极数码管上利用TM1650驱动芯片显示数字序列“1234”。程序在STM32F103微控制器上经过验证,有效减少了GPIO口的占用并简化了开发流程。
摘要由CSDN通过智能技术生成

在四位共阴极数码管上显示“1234”的主程序

使用TM1650驱动四位共阴极数码管可以节约芯片的GPIO口,同时还能够降低编程的工作量,简化开发。
本程序在stm32F103调试成功

#include "gpio.h"
#include "delay.h"
#include "systick.h"
#include "I2C.h"
#include "tm1650.h"

void disp()
{
   
		TM1650_Set(0x68,CODE[1]);  //最左边,千位
		TM1650_Set(0x6A,CODE[
下面是一个简单的51单片机动态显示DS18B20温度的代码示例,使用了4共阳数码管,并且使用了定时器中断来实现动态显示: ``` #include <REG51.H> #define uchar unsigned char #define uint unsigned int uchar code smgduan[17] = { // 数码管字形码表 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71, 0x00 }; uchar code smgwei[4] = { // 数码管选代码 0xfe, 0xfd, 0xfb, 0xf7 }; uchar temp_data[2]; // 存储读取到的温度数据 uchar display_buf[4]; // 存储要显示数码管数据 uchar display_pos = 0; // 当前显示数码管 uchar timer_cnt = 0; // 定时器中断计数器 bit display_flag = 0; // 是否刷新数码管标志 sbit DQ = P1^0; // 定义DS18B20的数据线 void Delay10us() { // 延时函数 uchar i, j; i = 2; j = 199; do { while (--j); } while (--i); } void ds18b20_init() { // DS18B20初始化函数 DQ = 1; // 把总线拉高 Delay10us(); DQ = 0; // 发送复脉冲 Delay10us(); DQ = 1; // 释放总线 Delay10us(); } void ds18b20_write_byte(uchar dat) { // 向DS18B20写入一个字节 uchar i, j; for (i = 0; i < 8; i++) { DQ = 0; // 发送起始信号 Delay10us(); DQ = dat & 0x01; // 发送数据 Delay10us(); DQ = 1; // 释放总线 dat >>= 1; } } uchar ds18b20_read_byte() { // 从DS18B20读取一个字节 uchar i, j, dat = 0; for (i = 0; i < 8; i++) { DQ = 0; // 发送起始信号 Delay10us(); DQ = 1; // 释放总线 Delay10us(); dat >>= 1; // 读取数据 if (DQ) dat |= 0x80; } return dat; } void display_data() { // 显示数码管数据 P2 = smgwei[display_pos]; // 先选定要显示数码管 P0 = smgduan[display_buf[display_pos]]; // 再输出数据 display_pos = (display_pos + 1) % 4; // 更新数码管置 } void timer0_isr() interrupt 1 { // 定时器中断服务函数 TH0 = (65536 - 50000) / 256; // 重新设置定时器初值 TL0 = (65536 - 50000) % 256; timer_cnt++; // 计数器加1 if (timer_cnt == 200) { // 200个中断周期后刷新一次显示 timer_cnt = 0; display_flag = 1; } } void main() { uchar i, temp; TMOD = 0x01; // 定时器0工作模式1 TH0 = (65536 - 50000) / 256; // 定时器初值 TL0 = (65536 - 50000) % 256; ET0 = 1; // 允许定时器0中断 EA = 1; // 开启总中断 TR0 = 1; // 启动定时器0 while (1) { ds18b20_init(); // 初始化DS18B20 ds18b20_write_byte(0xcc); // 跳过ROM操作 ds18b20_write_byte(0x44); // 启动温度转换 Delay10us(); ds18b20_init(); // 初始化DS18B20 ds18b20_write_byte(0xcc); // 跳过ROM操作 ds18b20_write_byte(0xbe); // 读取温度数据 temp_data[0] = ds18b20_read_byte(); // 读取温度数据的整数部分 temp_data[1] = ds18b20_read_byte(); // 读取温度数据的小数部分 temp = temp_data[0]; // 计算实际温度值 if (temp_data[1] & 0x80) temp += 5; display_buf[0] = temp / 10; // 将温度值拆分到数码管缓存数组中 display_buf[1] = temp % 10; display_buf[2] = 16; // 显示小数点 display_buf[3] = temp_data[1] & 0x7f; display_flag = 1; // 设置刷新标志 while (display_flag); // 等待定时器中断刷新显示 } } ``` 这个代码使用了定时器中断来实现动态显示,定时器中断周期为500us,每隔100ms刷新一次数码管。在主循环中读取温度数据并计算实际温度值,然后将温度值拆分到数码管缓存数组中,等待定时器中断来刷新数码管。在定时器中断服务函数中,每次刷新一个数码管的数据,然后更新数码管置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值