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

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

C 代码如下:

 #include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#define SCLK 12
#define RCLK 13
#define DIO 14
unsigned int code_char[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char code_segbit[]={0x01,0x02,0x04,0x08};
int pins[]={SCLK,RCLK,DIO};
int init(){
int i=;
for(i=;i<;i++){
pinMode(pins[i],OUTPUT);
digitalWrite(pins[i],LOW);
}
} int destroy(){
int i=;
for(i=;i<;i++){
digitalWrite(pins[i],LOW);
pinMode(pins[i],INPUT);
}
} void loop(){
time_t rawtime;
time(&rawtime);
struct tm *timeinfo;
timeinfo=localtime(&rawtime);
digitalWrite(RCLK,LOW);
if(timeinfo->tm_min>=)shiftOut(DIO,SCLK,,code_char[timeinfo->tm_min%]);//character
else shiftOut(DIO,SCLK,,code_char[timeinfo->tm_min]);
shiftOut(DIO,SCLK,,code_segbit[]);//bit
digitalWrite(RCLK,HIGH);
digitalWrite(RCLK,LOW);
if(timeinfo->tm_min>=)shiftOut(DIO,SCLK,,code_char[timeinfo->tm_min/]);//character
else shiftOut(DIO,SCLK,,code_char[]);
shiftOut(DIO,SCLK,,code_segbit[]);//bit
digitalWrite(RCLK,HIGH);
digitalWrite(RCLK,LOW);
if(timeinfo->tm_hour>=)shiftOut(DIO,SCLK,,code_char[timeinfo->tm_hour%]);//character
else shiftOut(DIO,SCLK,,code_char[timeinfo->tm_hour]);
shiftOut(DIO,SCLK,,code_segbit[]);//bit
digitalWrite(RCLK,HIGH);
digitalWrite(RCLK,LOW);
if(timeinfo->tm_hour>=)shiftOut(DIO,SCLK,,code_char[timeinfo->tm_hour/]);//character
else shiftOut(DIO,SCLK,,code_char[]);
shiftOut(DIO,SCLK,,code_segbit[]);//bit
digitalWrite(RCLK,HIGH);
//printf("%d %d\t%d %d %d %d\n",
// timeinfo->tm_hour,timeinfo->tm_min,
// timeinfo->tm_hour/10,timeinfo->tm_hour%10,
// timeinfo->tm_min/10,timeinfo->tm_min%10);
delayMicroseconds();
} int main(void){
if(wiringPiSetup()==-) //wiringPiSetupGpio==BCM
exit();
init();
while() {
loop();
}
destroy();
return ;
}

74HC595.h

 /*
* wiringShift.h:
* Emulate some of the Arduino wiring functionality.
*
* Copyright (c) 2009-2012 Gordon Henderson.
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/ #define LSBFIRST 0
#define MSBFIRST 1 #ifndef _STDINT_H
# include <stdint.h>
#endif #ifdef __cplusplus
extern "C" {
#endif extern uint8_t shiftIn (uint8_t dPin, uint8_t cPin, uint8_t order) ;
extern void shiftOut (uint8_t dPin, uint8_t cPin, uint8_t order, uint8_t val) ; #ifdef __cplusplus
}
#endif

74HC595.C

 /*
* wiringShift.c:
* Emulate some of the Arduino wiring functionality.
*
* Copyright (c) 2009-2012 Gordon Henderson.
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/ #include <stdint.h>
#include "wiringPi.h"
#include "wiringShift.h" /*
* shiftIn:
* Shift data in from a clocked source
*********************************************************************************
*/ uint8_t shiftIn (uint8_t dPin, uint8_t cPin, uint8_t order)
{
uint8_t value = ;
int8_t i ; if (order == MSBFIRST)
for (i = ; i >= ; --i)
{
digitalWrite (cPin, HIGH) ;
value |= digitalRead (dPin) << i ;
digitalWrite (cPin, LOW) ;
}
else
for (i = ; i < ; ++i)
{
digitalWrite (cPin, HIGH) ;
value |= digitalRead (dPin) << i ;
digitalWrite (cPin, LOW) ;
} return value;
} /*
* shiftOut:
* Shift data out to a clocked source
*********************************************************************************
*/
void shiftOut (uint8_t dPin, uint8_t cPin, uint8_t order, uint8_t val)
{
int8_t i;
if (order == MSBFIRST)
for (i = ; i >= ; --i)
{
digitalWrite (dPin, val & ( << i)) ;
digitalWrite (cPin, HIGH) ;
digitalWrite (cPin, LOW) ;
}
else
for (i = ; i < ; ++i)
{
digitalWrite (dPin, val & ( << i)) ;
digitalWrite (cPin, HIGH) ;
digitalWrite (cPin, LOW) ;
}
}

树莓派进阶之路 (016) - 通过595驱动4位LED显示系统时间的更多相关文章

  1. 树莓派进阶之路 (029) - 语音识别模块 LD3320(原创)

    近几天听朋友有说到LD3320 语音模块,刚好身边有块树莓派3,就在某宝上买了块自带mcu的LD3320 . 准备: 树莓派一个(配置了wiringPi开发环境的详情见本人博客:树莓派进阶之路 (00 ...

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

    写代码的时候敢接每次查wiringPi库函数挺麻烦的,自己wiringPi库封装了一下: #ifndef __PICUSTOM_H__ #define __PICUSTOM_H__ /******** ...

  3. 树莓派进阶之路 (023) - Windows下用串行连接控制树莓派(转)

    转载:http://shumeipai.nxez.com/2014/05/04/under-windows-serial-connection-control-raspberry-pi.html 在没 ...

  4. 树莓派进阶之路 (021) - 3.2inch RPi LCD (B)

    参考文档:http://www.waveshare.net/wiki/3.2inch_RPi_LCD_(B) 产品特点 320x240分辨率 电阻式触摸控制 兼容并可直接插入任何版本树莓派 提供Ras ...

  5. 树莓派进阶之路 (012) - 树莓派配置文档 config.txt 说明

    原文连接:http://elinux.org/RPi_config.txt 由于树莓派并没有传统意义上的BIOS, 所以现在各种系统配置参数通常被存在”config.txt”这个文本文件中. 树莓派的 ...

  6. 树莓派进阶之路 (010) - 树莓派raspi-config配置(转)

    经过前面两步我们的树莓派已经正常的工作起来了,但是在真正用它开发之前还需要进行一些列的配置以及软件的安装,这样开发起来才会得心应手,下面我们介绍一下常用的软件和服务 1.配置选项: 树莓派第一次使用的 ...

  7. 树莓派进阶之路 (006) - 树莓派安装wiringPi

    安装git-core sudo apt-get install git-core 下载winringPi库 git clone git://git.drogon.net/wiringPi 编译和安装库 ...

  8. 树莓派进阶之路 (038) - P2P 文件下载机

    硬件要求: 树莓派开发板 USB外接硬盘 一. Together 1. 更新安装程序 sudo apt-get update sudo apt-get upgrade sudo apt-get ins ...

  9. 树莓派进阶之路 (031) -字符问题(1) - GBK汉字编码表(转)

    转载:http://blog.sina.com.cn/s/blog_8184e033010109ug.html   基本简介 GB码,全称是GB2312-80<信息交换用汉字编码字符集基本集&g ...

随机推荐

  1. VC++深入详解-第四章学习心得

    这一章节主要讲解了 简单的绘图 主要是通过一些小的例子让我们学会了VC++的一些基本操作 void CDrawView::OnLButtonDown(UINT nFlags, CPoint point ...

  2. tensorflow项目构建流程

    https://blog.csdn.net/hjimce/article/details/51899683 一.构建路线 个人感觉对于任何一个深度学习库,如mxnet.tensorflow.thean ...

  3. Tensorflow Serving 模型部署和服务

    http://blog.csdn.net/wangjian1204/article/details/68928656 本文转载自:https://zhuanlan.zhihu.com/p/233614 ...

  4. SCIKIT-LEARN与GBDT使用案例

    http://blog.csdn.net/superzrx/article/details/47073847 安装 SCIKIT-LEARN是一个基于Python/numpy/scipy的机器学习库  ...

  5. 如何使用Android studio打开eclipse项目

    转: http://blog.csdn.net/zcw93219/article/details/50770445

  6. AngularJS中移动页面滚动穿透解决方案

    ()] + s[]) >= , preventDefault: false, click: IscrollAndroidBug.click() }); var _ele = document.g ...

  7. ArcGIS10.6了解一下

    因为计算机水平不断更新,ESRI不得不重新倾力打造下一代ArcMap,叫ArcGIS Pro,现在ArcGIS Pro功能有一定地突显,但还不够强大和稳定:而ArcGIS Desktop方面没有什么大 ...

  8. thinkphp3返回json或jsonp数据

    1.返回json数据 public function demo1() { $data = 'ok'; $this->ajaxReturn($data); } public function de ...

  9. NSMutableURLRequest Http 请求 同步 异步

    #pragma mark get country code//同步 -(void)getFKjsonCountryCode { dispatch_async(dispatch_get_global_q ...

  10. SQL字符串分割解析

    常用以下三种: [1]substring( expression ,start , length ): [2]CHARINDEX ( expression1 , expression2 [ , sta ...