模块图片,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. MSVC and MinGW DLLs

    Posted February 26th, 2009 by earnie dll faq msvc TODO: Reformat to new wiki syntax. !!! [Minimalist ...

  2. IE下推断IE版本号的语句

    样例: 1. <!--[if !IE]> 除IE外都可识别 <![endif]--> 2. <!--[if IE]> 全部的IE可识别 <![endif]-- ...

  3. Window.sessionStorage

      The sessionStorage property allows you to access a session Storage object for the current origin. ...

  4. 设置Linux中的Mysql不区分表名大小写

    1. MySQL数据库的表名在Linux系统下是严格区分大小写的,在Windows系统下开发的程序移植到Linux系统下,如果程序中SQL语句没有严格按照大小写访问数据库表,就可能会出现找不到表的错误 ...

  5. Android改动包名称规范方法

    第一步.在项目上右键,选择android tools->rename application package,输入须要改为的名称,然后选择须要替换的文件里的包名.这里仅改动了project中包括 ...

  6. 【Python】使用torrentParser1.03对单文件torrent的分析结果

    C:\Users\horn1\Desktop\python\42-torrentParser>python torrentParser.py 文件名=./5.torrent 文件结构: anno ...

  7. WIN7系统开题提示loli.vbs 操作超时怎么办

    这个是魔兽争霸的一个病毒,但是该病毒没有任何危害性,只是作为检测进入房间的地图是否含有作弊脚本,主动提供了清除工具   搜索loli,删除所有bat和exe,vbs文件   如果魔兽争霸3安装目录存在 ...

  8. Apache Kafka学习 (一)

    前言:最近公司开始要研究大数据的消息记录,于是开始研究kafka. 市面上kafka的书很少,有的也版本比较落后,于是仗着自己英文还不错,上官网直接学习. ^_^ 1. 开始 - 基本概念 学习一样东 ...

  9. 自定义控件之万能Repeater源码

    using System.ComponentModel; using System.Web.UI; [assembly: TagPrefix("Jinlong.Control", ...

  10. Extjs的各版本下载

    Extjs的版本繁多,本文收集了Extjs各个版本的下载链接,包括官网和非官网的,以及各种汉化版api,欢迎大家下载分享. Extjs最新版下载链接:http://www.sencha.com/pro ...