51单片机数码管显示

【Arduino实验10 数码管显示】 任务描述:1位数码显示;4位数码显示;自动计时显示 阅读详情

前言

专栏内容会持续补充更新,目前正在进行优惠活动!

目录

一、数码管显示原理介绍

二、简单例程引例

1. 数码管循环显示 0~9

2. 循环点亮数码管

三、 数码管实现时钟显示


一、数码管显示电路图

图1 单片机部分

图 2 数码管连接部分 


二、简单例程引例

1. 数码管循环显示 0~9

#include"reg52.h"
typedef unsigned char u8;
typedef unsigned int u16;
sbit a=P2^2;
sbit b=P2^3;
sbit c=P2^4;

u8 code sumagd[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,
0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

void delay(u16 i)
{
    while(i--);
}

void main()
{
    u8 i;
    a=0;
    b=0;
    c=0;

    while(1)
    {
        for(i=0;i<10;i++)
        {
            P0=sumagd[i];
            delay(50000);
        }
    }
}

2. 循环点亮数码管

// 循环点亮数码管
#include"reg52.h"
#include"instrins.h"
typedef unsigned char u8;
typedef unsigned int u16;
sbit a=P2^2;
sbit b=P2^3;
sbit c=P2^4;
u8 code sumagd[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,
0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

void delay(u16 i)
{
	while(i--);
}
void xianshi()
{
	u16 i;
	for(i=0;i<8;i++)
	{
		switch(i)
		{
			case 0: a=0;b=0;c=0;break;
			case 1: a=1;b=0;c=0;break;
			case 2: a=0;b=1;c=0;break;
			case 3: a=1;b=1;c=0;break;
			case 4: a=0;b=0;c=1;break;
			case 5: a=1;b=0;c=1;break;
			case 6: a=0;b=1;c=1;break;
			case 7: a=1;b=1;c=1;break;
		}
		
		P0=sumagd[i];//数目管赋值
		delay(100000);//延时显示数据
		P0=0x00;//清空P0空数据
	}
}

void main()
{
	while(1)
	{
		xianshi();
	} 
}

三、 数码管实现时钟显示

本程序利用 STC89C52 单片机的定时器中断和 4 个八段共阴极 LED 数码管,显示电路采用 LED 动态显示的方法,共阴极端由 P3 口控制,分为段选线和位选线,通过编程设计实现时钟的功能。

四个数码管的 dp 段通过 8421 码显示小时数;第一二个数码管显示分钟数,第三四个数码管显示秒数。通电并下载程序后,初试数码管显示 0000,dp 段均不亮,开始由秒计时,每过 1 秒,秒位加 1,满 60 秒,分位加 1,满 60 分,小时位加 1(最大值位为 15 时 59 分 59 秒),从而实现时钟的功能。

#include<reg52.h>
//定义使用的IO口
#define GPIO_DIG   P0 //段选

sbit D1=P3^6;
sbit D2=P3^7;

unsigned char code DIG_PLACE[8] = {
0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; 
unsigned char code seg[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};

unsigned char xianshi[8];//保存时钟的数据
char second,minit,hours;

//段位选
void DigDisplay()
{
    unsigned char i;
    unsigned int j;

    for(i=0; i<4; i++)
    {
        GPIO_DIG = DIG_PLACE[i];  //发送位选
        D2=1;
        D2=0;

        GPIO_DIG = xianshi[i];     //发送段码
        D1=1;
        D1=0;
        j = 100;       
        while(j--); 
        GPIO_DIG = 0x00;//消隐
        D1=1;
        D1=0;
     }
}

//数码管显示
void Display ()
{      
    if(hours/8%2==1)
    xianshi[0]=seg[minit/10]|0x80;
    else
    xianshi[0]=seg[minit/10];

    if(hours/4%2==1)
    xianshi[1]=seg[minit%10]|0x80;
    else
    xianshi[1]=seg[minit%10];

    if(hours/2%2==1)
    xianshi[2]=seg[second/10]|0x80;
    else
    xianshi[2]=seg[second/10];

    if(hours%2==1)
    xianshi[3]=seg[second%10]|0x80;
    else
    xianshi[3]=seg[second%10];
   DigDisplay();
}

//主函数
void main()
{
    second=0;
    minit=0;
    hours=0;
    TMOD=0X01;
    TH0 = 0xFC;
    TL0 = 0x17;  //10ms
    TR0=1;
    ET0=1;
    EA=1;//打开总中断 
    while(1)
    {
       Display (); 
    } 
}

//定时器中断
void time0() interrupt 1
{
    static unsigned int j;
    TH0 = 0xDC;
    TL0 = 0x00;  //10ms
    j++;
    if(j==100) //1s
    {
  	   j=0;
 	   second++;
  	   if(second==60)        //秒满60清零
 	   {
  	     second=0;
  	     minit++;
  	     if(minit==60)        //分满60清零
         {
   	 	   minit=0;
   		   hours++;
   	 	   if(hours>15)hours=0; //时满16清零
   	     }
       }
     }
}
51单片机教程(九)- 数码管的动态显示 数码管动态显示,数码管显示0-9,数码管显示0-99 阅读详情

相关推荐

51单片机学习笔记(二) 单个数码管显示1到9

转自我的博客:http://sincos.in/?p=20 数码管,其实就是8个发光二极管按顺序排列而成的,所以让数码管显示特定的数字,只需要将该亮的二极管点亮,不该亮的灭了就好了。为了达到这个目的,就得参考电路图了,51HEI开发板的电路图在我的相册里有,但是整张贴出来就

lu188887的专栏 1万+

51单片机 数码管的静态显示和动态显示

51单片机 数码管的静态显示和动态显示 附有代码示例

<云飞扬>的专栏 3万+

51单片机60秒倒计时 数码管显示

51单片机60秒倒计时 数码管显示 本资源适用于初学者,使用单片机型号为普中51单片机: 下面展示 代码。 #include"reg52.h" sbit LSA=P2^2; sbit LSB=P2^3; sbit LSC=P2^4;//38译码器控制位选 char code smgduan[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; char a1,a2,s=60;//s为要定

yeyeyeyyeye的博客 1万+

C51 单片机静态数码管显示原理与应用

在 C51 编程中,首先需要包含相关的头文件,如,然后定义与数码管连接的单片机引脚。// 定义数码管段选引脚//...依次定义其他段选引脚C51 单片机控制静态数码管是嵌入式系统开发中的一项基础且重要的技术。通过理解静态数码管的工作原理,合理设计硬件连接电路,并编写相应的软件程序,可以实现稳定、可靠的数字显示功能。在实际应用中,可以根据具体需求进一步扩展功能,如多数码管显示、动态显示切换等,以满足各种复杂的人机交互要求。

wandouzys的博客 1968

51单片机之数码管显示

<br />51单片机之数码管显示<br />一、数码管的结构和原理<br />数码管是由发光二极管显示字段的显示器件。外型结构与原理图如下所示:<br /><br /><br />数码管通常分为共阴与共阳两种结构,图如上面所示:<br />共阴数码管的发光二级管阴极共连,当接低电平时,某发光二极管阳极接高电平时,则此发光二极管被点亮;共阴数码管的发光二极管阳极共连,当接高电平时,某发光二极管接低电平时,则此发光二极管被点亮。只要将相应的发光二极管点亮,则可显示不同的字形。如要点亮“2”字形时,只要将对应的

gdut_lisa的专栏 4617

51单片机数码管滚动显示学号_静、动态数码管动态显示

1. 数码管静态、动态显示原理 静态显示的特点是每个数码管的段选必须接一个8位数据线来保持显示的字形码。当送入一次字形码后,显示字形可一直保持,直到送入新字形码为止。这种方法的优点是占用CPU时间少,显示便于监测和控制。缺点是硬件电路比较复杂,成本较高 动态显示的特点是将所有数码管的段选线并联在一起,由位选线控制是哪一位数码管有效。选亮数码管采用动态扫描显示。所谓...

weixin_39931923的博客 6857

51单片机数码管滚动显示

#include <reg51.h> unsigned char code smgduan[18]={0x00, 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; unsigned char code smgwei[8] = {0x00, 0x04,0x08, 0x0c...

dada12138的博客 6658

【51单片机】LED数码管显示

共阴极1亮0灭,共阳极0亮1灭。

weixin_51387065的博客 3893

ISP Tuning实战指南:从基础到高级的色彩与亮度优化

本文详细介绍了ISP Tuning的基础概念与高级调优技术,包括黑电平校正(BLC)、自动白平衡(AWB)、色彩校正矩阵(CCM)和Gamma曲线调整等核心算法。通过实战案例和调试技巧,帮助工程师提升摄像头成像质量,解决高光过曝、暗部噪点等常见问题,适用于智能门铃、车载摄像头等多种场景。

weixin_30879169的博客 108

CNN卷积神经网络手写数字识别实例及代码详解

本文的代码来自githup的Deep Learning的toolbox,是用Matlab实现的。感谢该toolbox的作者付出和分享。 我在应该该代码进行训练时,出现一些报错,如expand函数应用不对,flipall函数未定义等问题,对这些问题进行了修正,完成网络训练和验证。 本文mnist_uint8.mat的获取可以参照我的另一篇博客:MNIST数据库处理--matlab生成mnist_

VivienFu的专栏 6万+

手把手搭建你的第一个OCT仿真模型:用Python和光学仿真库重现A-SCAN信号

本文详细介绍了如何使用Python和光学仿真库搭建OCT仿真模型,重现A-SCAN信号。从环境配置、干涉仪建模到信号处理全流程,手把手教你实现光学相干层析成像的核心技术。通过代码示例和性能优化技巧,帮助开发者深入理解OCT原理并应用于医疗诊断等领域。

weixin_42562079的博客 240

LabVIEW与MySQL实时数据存储:传感器采集与数据库同步实践

本文详细介绍了如何将LabVIEW与MySQL结合,实现传感器数据的实时采集与高效存储。通过设计合理的数据库表结构、配置LabVIEW数据库连接工具包,并编写核心的采集与存储VI,构建了稳定可靠的数据链路。文章重点分享了批量插入、连接管理、时间同步等关键优化技巧与避坑指南,帮助工程师应对工业监控、环境监测等场景下的实时数据同步挑战。

weixin_29281915的博客 261

k8s学习笔记(3)--- kubernetes核心技术概念

kubernetes核心技术概念 1.API对象         API对象是K8s集群中的管理操作单元。K8s集群系统每支持一项新功能,引入一项新技术,一定会新引入对应的API对象,支持对该功能的管理操作。例如副本集Replica Set对应的API对象是RS。 2.集群(Cluster)         集群是指由...

梦谜的博客 1291

Aurix TC3xx系列MCU ADC模块简介(一)

简单介绍英飞凌Aurix TC3xx系列单片机ADC模块的特点及内部结构。

m0_38045338的博客 6361

openCv +3.4.0 车牌定位(C++版本)

#include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc.hpp> #include "opencv2/shape.hpp" using namespace ...

Bamboo265925的博客 2337

Android 13.0 app进程保活白名单功能实现

在13.0的系统rom产品开发中,在某些重要的app即使进入后台,产品需求要求也不想被系统杀掉进程,需要app长时间保活,就是app进程保活白名单功能的实现, 所以需要在系统杀进程的时候不杀掉白名单的进程,接下来就看怎么样来实现这些功能

安卓兼职framework和app工程师的博客 3142

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain&#39;s apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath(&#39;DeepLearnToolbox&#39;));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand(&#39;state&#39;,0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W&#39;); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand(&#39;state&#39;,0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = &#39;sigm&#39;;%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, &#39;Too big error&#39;);```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand(&#39;state&#39;,0)sae = saesetup([784 100]);sae.ae{1}.activation_function = &#39;sigm&#39;;sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)&#39;)% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;;nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, &#39;Too big error&#39;);```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x&#39;,28,28,60000))/255;test_x = double(reshape(test_x&#39;,28,28,10000))/255;train_y = double(train_y&#39;);test_y = double(test_y&#39;);%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you&#39;ll get around 1.2% errorrand(&#39;state&#39;,0)cnn.layers = { struct(&#39;type&#39;, &#39;i&#39;) %input layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 6, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %sub sampling layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 12, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, &#39;Too big error&#39;);```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, &#39;Too big error&#39;);%% ex2 neural net with L2 weight decayrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex3 neural net with dropoutrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex4 neural net with sigmoid activation functionrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex5 plotting functionalityrand(&#39;state&#39;,0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = &#39;softmax&#39;; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand(&#39;state&#39;,0)nn = nnsetup([784 20 10]); nn.output = &#39;softmax&#39;; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

上一篇: 51单片机实现蜂鸣器报警
下一篇: 51单片机循环点亮数码管
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个写代码的修车工

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值