模块图片,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. 求一个正实数X的开方

    问题:求一个正实数X的平方根,不能使用sqrt等库函数. 解析:本题要求求一个正实数的平方根,不能使用sqrt等已有的库函数,我们可以做一下考虑: 利用二分法,mid=X/2.0,若mid*mid&g ...

  2. Java-Shiro(三):Shiro与Spring MVC集成

    新建Java Daynamic Web项目 导入Spring.SpringMVC依赖包: 导入Spring & Spring MVC包(导入如下所有开发包): Spring AOP依赖扩展包: ...

  3. C++之new、delete 与malloc、free的异同

    在C/C++编程中常常会申请内存.而对内存的申请释放操作有两套方法: new.delete 与malloc.free.他们的使用最好是成对使用,不要去混搭---这可不是时尚界哦. 例如以下是这两组方法 ...

  4. POI中setDefaultColumnWidth方法不起作用的原因

    sheet.setDefaultRowHeight((short) (2 * 256)); //设置默认行高,表示2个字符的高度 sheet.setDefaultColumnWidth(17);   ...

  5. OSError: Could not find library geos_c or load any of its variants ['libgeos_c.so.1', 'libgeos_c.so

    OSError: Could not find library geos_c or load any of its variants ['libgeos_c.so.1', 'libgeos_c.so ...

  6. 使用CSS3生成的电子时钟特效

    在线演示 本地下载 突然觉得自己对带工作的态度亟需改正,虽然不喜欢现在的加班生活,但是自己要去接受自己不喜欢的,才能获得自己喜欢的. 这是自己好久之前丛过的一个时钟,网上应该有这个的教程,虽然实现的效 ...

  7. Android基础(五) Service全解析----看不见的Activity

    一.服务的介绍: 作为Android四大组件之中的一个,Service(服务)也常常运用于我们的日常使用中,它与Activity的差别在于:Service一直在后台执行.没实用户界面.所以绝不会到前台 ...

  8. Android 模仿QQ空间风格的 UI

    本文内容 环境 演示模仿QQ空间风格的UI 虽然这个 UI 跟现在的QQ空间有点差别,但是也能学到很多东西. 下载 Demo 环境 Windows 7 64 位 Eclipse ADT V22.6.2 ...

  9. Fragment的setUserVisibleHint方法实现懒加载,但setUserVisibleHint 不起作用?

    我们在做应用开发的时候,一个Activity里面可能会以viewpager(或其他容器)与多个Fragment来组合使用,而如果每个fragment都需要去加载数据,或从本地加载,或从网络加载,那么在 ...

  10. android中RecyclerView控件实现瀑布流布局

    本文是在之前文章的基础上做的修改:android中RecyclerView控件的使用 1.修改列表项news_item.xml: <?xml version="1.0" en ...