博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
树莓派进阶之路 (016) - 通过595驱动4位LED显示系统时间
阅读量:4310 次
发布时间:2019-06-06

本文共 6240 字,大约阅读时间需要 20 分钟。

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

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

C 代码如下: 

1 #include 
2 #include
3 #include
4 #include
5 #include
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]);//character35 else shiftOut(DIO,SCLK,1,code_char[timeinfo->tm_min]);36 shiftOut(DIO,SCLK,1,code_segbit[0]);//bit37 digitalWrite(RCLK,HIGH);38 digitalWrite(RCLK,LOW);39 if(timeinfo->tm_min>=10)shiftOut(DIO,SCLK,1,code_char[timeinfo->tm_min/10]);//character40 else shiftOut(DIO,SCLK,1,code_char[0]);41 shiftOut(DIO,SCLK,1,code_segbit[1]);//bit42 digitalWrite(RCLK,HIGH);43 digitalWrite(RCLK,LOW);44 if(timeinfo->tm_hour>=10)shiftOut(DIO,SCLK,1,code_char[timeinfo->tm_hour%10]);//character45 else shiftOut(DIO,SCLK,1,code_char[timeinfo->tm_hour]);46 shiftOut(DIO,SCLK,1,code_segbit[2]);//bit47 digitalWrite(RCLK,HIGH);48 digitalWrite(RCLK,LOW);49 if(timeinfo->tm_hour>=10)shiftOut(DIO,SCLK,1,code_char[timeinfo->tm_hour/10]);//character50 else shiftOut(DIO,SCLK,1,code_char[0]);51 shiftOut(DIO,SCLK,1,code_segbit[3]);//bit52 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==BCM62 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 modify11  *    it under the terms of the GNU Lesser General Public License as published by12  *    the Free Software Foundation, either version 3 of the License, or13  *    (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 of17  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the18  *    GNU Lesser General Public License for more details.19  *20  *    You should have received a copy of the GNU Lesser General Public License21  *    along with wiringPi.  If not, see 
.22 ***********************************************************************23 */24 25 #define LSBFIRST 026 #define MSBFIRST 127 28 #ifndef _STDINT_H29 # include
30 #endif31 32 #ifdef __cplusplus33 extern "C" {34 #endif35 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 __cplusplus40 }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 modify11  *    it under the terms of the GNU Lesser General Public License as published by12  *    the Free Software Foundation, either version 3 of the License, or13  *    (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 of17  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the18  *    GNU Lesser General Public License for more details.19  *20  *    You should have received a copy of the GNU Lesser General Public License21  *    along with wiringPi.  If not, see 
.22 ***********************************************************************23 */24 25 #include
26 #include "wiringPi.h"27 #include "wiringShift.h"28 29 /*30 * shiftIn:31 * Shift data in from a clocked source32 *********************************************************************************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 else48 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 source61 *********************************************************************************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 else74 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

你可能感兴趣的文章
Git(四) - 分支管理
查看>>
PHP Curl发送数据
查看>>
HTTP协议
查看>>
HTTPS
查看>>
git add . git add -u git add -A区别
查看>>
apache下虚拟域名配置
查看>>
session和cookie区别与联系
查看>>
PHP 实现笛卡尔积
查看>>
Laravel中的$loop
查看>>
CentOS7 重置root密码
查看>>
Centos安装Python3
查看>>
PHP批量插入
查看>>
laravel连接sql server 2008
查看>>
Laravel 操作redis的各种数据类型
查看>>
Laravel框架学习笔记之任务调度(定时任务)
查看>>
laravel 定时任务秒级执行
查看>>
浅析 Laravel 官方文档推荐的 Nginx 配置
查看>>
Swagger在Laravel项目中的使用
查看>>
Laravel 的生命周期
查看>>
CentOS Docker 安装
查看>>