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

Posted 极客先锋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了树莓派进阶之路 (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 }

 

以上是关于树莓派进阶之路 (016) - 通过595驱动4位LED显示系统时间的主要内容,如果未能解决你的问题,请参考以下文章

树莓派进阶之路 (024) - windows远程桌面连接树莓派通过xrdp服务(转)

树莓派使用SN74HC595控制数码管

树莓派进阶之路 (017) - 基于树莓派的专用摄像头实时监控

树莓派进阶之路 (030) -Picustom.h(原创)

树莓派进阶之路 (028) - 树莓派SQLite3的安装

树莓派进阶之路 (036) - 树莓派的音频部分