树莓派进阶之路 (016) - 通过595驱动4位LED显示系统时间

模块图片,4位共阳极数码管.

我们使用树莓派wiringPi的库来通过74HC595驱动4位数码管:

C 代码如下: 

 1 #include <wiringPi.h>
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <stdint.h>
 5 #include <time.h>
 6 #define SCLK 12
 7 #define RCLK 13
 8 #define DIO 14
 9 unsigned int code_char[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
10 unsigned char code_segbit[]={0x01,0x02,0x04,0x08};
11 int pins[3]={SCLK,RCLK,DIO};
12 int init(){
13     int i=0;
14     for(i=0;i<3;i++){
15     pinMode(pins[i],OUTPUT);
16     digitalWrite(pins[i],LOW);
17     }
18 }
19 
20 int destroy(){
21     int i=0;
22     for(i=0;i<3;i++){
23     digitalWrite(pins[i],LOW);
24     pinMode(pins[i],INPUT);
25     }
26 }
27 
28 void loop(){
29     time_t rawtime;
30     time(&rawtime);
31     struct tm *timeinfo;
32     timeinfo=localtime(&rawtime);
33     digitalWrite(RCLK,LOW);
34     if(timeinfo->tm_min>=10)shiftOut(DIO,SCLK,1,code_char[timeinfo->tm_min%10]);//character
35     else shiftOut(DIO,SCLK,1,code_char[timeinfo->tm_min]);
36     shiftOut(DIO,SCLK,1,code_segbit[0]);//bit
37     digitalWrite(RCLK,HIGH);
38     digitalWrite(RCLK,LOW);
39     if(timeinfo->tm_min>=10)shiftOut(DIO,SCLK,1,code_char[timeinfo->tm_min/10]);//character
40     else shiftOut(DIO,SCLK,1,code_char[0]);
41     shiftOut(DIO,SCLK,1,code_segbit[1]);//bit
42     digitalWrite(RCLK,HIGH);
43     digitalWrite(RCLK,LOW);
44     if(timeinfo->tm_hour>=10)shiftOut(DIO,SCLK,1,code_char[timeinfo->tm_hour%10]);//character
45     else shiftOut(DIO,SCLK,1,code_char[timeinfo->tm_hour]);
46     shiftOut(DIO,SCLK,1,code_segbit[2]);//bit
47     digitalWrite(RCLK,HIGH);
48     digitalWrite(RCLK,LOW);
49     if(timeinfo->tm_hour>=10)shiftOut(DIO,SCLK,1,code_char[timeinfo->tm_hour/10]);//character
50     else shiftOut(DIO,SCLK,1,code_char[0]);
51     shiftOut(DIO,SCLK,1,code_segbit[3]);//bit
52     digitalWrite(RCLK,HIGH);
53     //printf("%d %d\t%d %d %d %d\n",
54     // timeinfo->tm_hour,timeinfo->tm_min,
55     // timeinfo->tm_hour/10,timeinfo->tm_hour%10,
56     // timeinfo->tm_min/10,timeinfo->tm_min%10);
57     delayMicroseconds(10);
58 }
59 
60 int main(void){
61     if(wiringPiSetup()==-1) //wiringPiSetupGpio==BCM
62         exit(1);
63     init();
64     while(1) {
65         loop();
66     }
67     destroy();
68     return 0;
69 }

 

74HC595.h

 1 /*
 2  * wiringShift.h:
 3  *    Emulate some of the Arduino wiring functionality. 
 4  *
 5  * Copyright (c) 2009-2012 Gordon Henderson.
 6  ***********************************************************************
 7  * This file is part of wiringPi:
 8  *    https://projects.drogon.net/raspberry-pi/wiringpi/
 9  *
10  *    wiringPi is free software: you can redistribute it and/or modify
11  *    it under the terms of the GNU Lesser General Public License as published by
12  *    the Free Software Foundation, either version 3 of the License, or
13  *    (at your option) any later version.
14  *
15  *    wiringPi is distributed in the hope that it will be useful,
16  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *    GNU Lesser General Public License for more details.
19  *
20  *    You should have received a copy of the GNU Lesser General Public License
21  *    along with wiringPi.  If not, see <http://www.gnu.org/licenses/>.
22  ***********************************************************************
23  */
24 
25 #define    LSBFIRST    0
26 #define    MSBFIRST    1
27 
28 #ifndef    _STDINT_H
29 # include <stdint.h>
30 #endif
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 extern uint8_t shiftIn      (uint8_t dPin, uint8_t cPin, uint8_t order) ;
37 extern void    shiftOut     (uint8_t dPin, uint8_t cPin, uint8_t order, uint8_t val) ;
38 
39 #ifdef __cplusplus
40 }
41 #endif

 

74HC595.C

 1 /*
 2  * wiringShift.c:
 3  *    Emulate some of the Arduino wiring functionality. 
 4  *
 5  * Copyright (c) 2009-2012 Gordon Henderson.
 6  ***********************************************************************
 7  * This file is part of wiringPi:
 8  *    https://projects.drogon.net/raspberry-pi/wiringpi/
 9  *
10  *    wiringPi is free software: you can redistribute it and/or modify
11  *    it under the terms of the GNU Lesser General Public License as published by
12  *    the Free Software Foundation, either version 3 of the License, or
13  *    (at your option) any later version.
14  *
15  *    wiringPi is distributed in the hope that it will be useful,
16  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *    GNU Lesser General Public License for more details.
19  *
20  *    You should have received a copy of the GNU Lesser General Public License
21  *    along with wiringPi.  If not, see <http://www.gnu.org/licenses/>.
22  ***********************************************************************
23  */
24 
25 #include <stdint.h>
26 #include "wiringPi.h"
27 #include "wiringShift.h"
28 
29 /*
30  * shiftIn:
31  *    Shift data in from a clocked source
32  *********************************************************************************
33  */
34 
35 uint8_t shiftIn (uint8_t dPin, uint8_t cPin, uint8_t order)
36 {
37   uint8_t value = 0 ;
38   int8_t  i ;
39  
40   if (order == MSBFIRST)
41     for (i = 7 ; i >= 0 ; --i)
42     {
43       digitalWrite (cPin, HIGH) ;
44       value |= digitalRead (dPin) << i ;
45       digitalWrite (cPin, LOW) ;
46     }
47   else
48     for (i = 0 ; i < 8 ; ++i)
49     {
50       digitalWrite (cPin, HIGH) ;
51       value |= digitalRead (dPin) << i ;
52       digitalWrite (cPin, LOW) ;
53     }
54 
55   return value;
56 }
57 
58 /*
59  * shiftOut:
60  *    Shift data out to a clocked source
61  *********************************************************************************
62  */
63 void shiftOut (uint8_t dPin, uint8_t cPin, uint8_t order, uint8_t val)
64 {
65   int8_t i;
66   if (order == MSBFIRST)
67     for (i = 7 ; i >= 0 ; --i)
68     {
69       digitalWrite (dPin, val & (1 << i)) ;
70       digitalWrite (cPin, HIGH) ;
71       digitalWrite (cPin, LOW) ;
72     }
73   else
74     for (i = 0 ; i < 8 ; ++i)
75     {
76       digitalWrite (dPin, val & (1 << i)) ;
77       digitalWrite (cPin, HIGH) ;
78       digitalWrite (cPin, LOW) ;
79     }
80 }

 

转载于:https://www.cnblogs.com/jikexianfeng/p/7095875.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/269051.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

福禄克OFP光纤测试仪5个强大的功能

众所周知&#xff0c;福禄克网络的铜缆测试仪非常出名&#xff0c;专业、零误差、测试快是福禄克DSX级别铜缆测试仪的代名词&#xff0c;而今天由福禄克指定经销商明辰智航的工程师给大家科普&#xff0c;福禄克网络专业级别的不止铜缆测试仪还有光纤测试仪OFP&#xff0c;那么…

[独家]网易遭遇****** 留下“装B”两字

计算机安全咨询中心&#xff08;[url]www.kingzoo.com[/url]&#xff09;反病毒斗士报:今天,动物家园计算机安全咨询中心接到报料,说网易&#xff08;163.com&#xff09;的被人***&#xff0c;***的路径是&#xff1a;[url]http://co.163.com/idiot.jsp[/url] ***在页面留下&a…

rust电器元件需要什么材料_云南自考大专报考需要什么材料?

今天有同学问到“云南自考大专报考需要什么材料?”别着急&#xff0c;接下来云南自考网特意为大家带来详细讲解&#xff0c;希望能帮助大家顺利备考哦。预祝大家考试成功哦。凡中国公民&#xff0c;不受性别、年龄、民族、种族、学历、身体健康状况、居住地等限制&#xff0c;…

网络传输性能分析仪

网络传输性能分析仪的主要是采用RFC2544测试方法&#xff0c;但是很少人知道&#xff0c;只有RFC2544测试方法是行不通的&#xff0c;还要有Y.1564测试方法。这是一款网络传输性能分析仪必备的测试功能。 Y.1564与RFC2544都是进行以太网性能测试的标准&#xff0c;这两个标准有…

校验功能算eo还是ilf_如何区分ILF和EIF?

ILF和EIF是数据功能的两种类型&#xff0c;它表示提供给用户的满足内部或外部数据存储需求的功能。识别ILF和EIF之前&#xff0c;首先判定是否是数据功能;判定为数据功能后再来区分是ILF还是EIF。为了识别数据功能&#xff0c;需要执行以下活动&#xff1a;(1)识别计数范围内所…

春游植物园

早上还担心有点小雨&#xff0c;不过报纸说天气会转晴&#xff0c;约了三五好友&#xff0c;一同去逛植物园&#xff0c;果然是美好的春季&#xff0c;还有我的小黑&#xff08;去年刚买的尼康P5100&#xff0c;效果真好&#xff0c;买对了。&#xff09;&#xff0c;拍了很多美…

使用简单工厂模式demo

传统方式&#xff1a; 披萨抽象类 奶酪披萨类 希腊披萨类 订单披萨类 披萨客户端类 传统方式的优缺点 简单工厂模式介绍 简单工厂模式

facenet训练自己的数据_基于SSD与Facenet的人脸识别

小方哥-AI人工智能模式识别算法系统介绍随着深度学习的日益发展&#xff0c;人脸识别的技术越来越趋于成熟。基于人脸识别的也伴随着众多的应用&#xff0c;比如说基于人脸识别的工厂打卡或者签到系统&#xff1b;基于人脸识别的五官定位以及美颜相机&#xff0c;智慧医疗方面的…

以太网性能测试仪应该具备什么功能?

吐吞量、延迟、丢包、背靠背具备这些测试功能&#xff0c;作为网络管理人员及维护人员你是否觉得这些在处理网络问题时&#xff0c;已经够用了&#xff1f;不&#xff0c;远远不止这么简单&#xff0c;日常工作需要的测试方法也远远不止这些。那么一款合格的以太网测试仪应该都…

luogu P1774 最接近神的人_NOI导刊2010提高(02)

题目描述 破解了符文之语&#xff0c;小FF开启了通往地下的道路。当他走到最底层时&#xff0c;发现正前方有一扇巨石门&#xff0c;门上雕刻着一幅古代人进行某种活动的图案。而石门上方用古代文写着“神的殿堂”。小FF猜想里面应该就有王室的遗产了。但现在的问题是如何打开这…

万兆以太网测试仪应该具备的测试功能

自1982年以太网协议被IEEE采纳成为标准后&#xff0c;到目前为止&#xff0c;已经经历了40年的风风雨雨。在这40年中&#xff0c;以太网技术作为局域网链路层标准战胜令牌、令牌总线等技术。以太网技术在当前局域网市场范围占有使用率90%以上。数据中心、IDC机房、网络运营商的…

ic启动器我的世界_我的世界启动器手机版下载-我的世界启动器安卓版(永久免费)v1.5.1-Minecraft中文分享站...

我的世界启动器是一款非常好用的游戏助手&#xff0c;专门为广大游戏玩家打造的&#xff0c;具有超多强大功能&#xff0c;有了它就可以快速登录游戏&#xff0c;不仅仅是这样&#xff0c;内含超多精彩内容&#xff0c;服务器、mod、皮肤、材质包、地图等等应有尽有&#xff0c…

技术与英语

我就一直很纳闷&#xff0c;一些学技术的人总是对英语存在怪异的看法&#xff0c;对英语考试更是不屑一顾&#xff0c;想想我就觉得恶心&#xff0c;且不说英语会成为学习技术的瓶颈&#xff0c;作为一门语言&#xff0c;英语可以深化到艺术层面&#xff0c;也可以定义为一种学…

给linux用户加入sudo权限

加入sudo权限方法&#xff1a;须要在/etc/sudoers文件里加入想要条目以实现权限。其加入有两种方法&#xff1a;&#xff08;在root用户环境下执行的&#xff09;1&#xff1a;直接使用visudo(在root状态下)&#xff0c;vim /etc/sudoers&#xff08;首先要用root用户改动该文件…

虚拟化运维平台云安对政府机构的解决方案

目前针对政府部门的部分应用像&#xff1a;社保网、政府采购网、各省市地方政府工作平台等应用都隐患的存在着虚拟化网络复杂&#xff0c;一旦一个区域出现故障会波及整个网络&#xff0c;运维人员因无法精准定位到故障症结&#xff0c;而大大降低运行效率。 明辰智航云安网络…

VS2008系列培训教程之四:What's new in C# 3.0 Visual Basic 9.0

这篇文章是参考微软内部文档重新编写而成&#xff0c;感谢相关内容的各位作者的辛勤劳动&#xff01;其内容介绍如下&#xff1a;During this brief session, you will learn about some of the key new language enhancements that are provided as part of the C# 3.0 and Vi…

hive建立内部表映射hbase_快手 HBase 在千亿级用户特征数据分析中的应用与实践...

分享嘉宾&#xff1a;陈杨 快手编辑整理&#xff1a;Hoh Xil内容来源&#xff1a;BigData NoSQL 12th Meetup出品社区&#xff1a;DataFun注&#xff1a;欢迎转载&#xff0c;转载请注明出处。快手建设 HBase 差不多有2年时间&#xff0c;在公司里面有比较丰富的应用场景&#…