树莓派进阶之路 (016) - 通过595驱动4位LED显示系统时间
模块图片,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显示系统时间的更多相关文章
- 树莓派进阶之路 (029) - 语音识别模块 LD3320(原创)
近几天听朋友有说到LD3320 语音模块,刚好身边有块树莓派3,就在某宝上买了块自带mcu的LD3320 . 准备: 树莓派一个(配置了wiringPi开发环境的详情见本人博客:树莓派进阶之路 (00 ...
- 树莓派进阶之路 (030) -Picustom.h(原创)
写代码的时候敢接每次查wiringPi库函数挺麻烦的,自己wiringPi库封装了一下: #ifndef __PICUSTOM_H__ #define __PICUSTOM_H__ /******** ...
- 树莓派进阶之路 (023) - Windows下用串行连接控制树莓派(转)
转载:http://shumeipai.nxez.com/2014/05/04/under-windows-serial-connection-control-raspberry-pi.html 在没 ...
- 树莓派进阶之路 (021) - 3.2inch RPi LCD (B)
参考文档:http://www.waveshare.net/wiki/3.2inch_RPi_LCD_(B) 产品特点 320x240分辨率 电阻式触摸控制 兼容并可直接插入任何版本树莓派 提供Ras ...
- 树莓派进阶之路 (012) - 树莓派配置文档 config.txt 说明
原文连接:http://elinux.org/RPi_config.txt 由于树莓派并没有传统意义上的BIOS, 所以现在各种系统配置参数通常被存在”config.txt”这个文本文件中. 树莓派的 ...
- 树莓派进阶之路 (010) - 树莓派raspi-config配置(转)
经过前面两步我们的树莓派已经正常的工作起来了,但是在真正用它开发之前还需要进行一些列的配置以及软件的安装,这样开发起来才会得心应手,下面我们介绍一下常用的软件和服务 1.配置选项: 树莓派第一次使用的 ...
- 树莓派进阶之路 (006) - 树莓派安装wiringPi
安装git-core sudo apt-get install git-core 下载winringPi库 git clone git://git.drogon.net/wiringPi 编译和安装库 ...
- 树莓派进阶之路 (038) - P2P 文件下载机
硬件要求: 树莓派开发板 USB外接硬盘 一. Together 1. 更新安装程序 sudo apt-get update sudo apt-get upgrade sudo apt-get ins ...
- 树莓派进阶之路 (031) -字符问题(1) - GBK汉字编码表(转)
转载:http://blog.sina.com.cn/s/blog_8184e033010109ug.html 基本简介 GB码,全称是GB2312-80<信息交换用汉字编码字符集基本集&g ...
随机推荐
- 【Java】阿里巴巴Java开发手册(纪念版)
下载地址:(最新纪念版 2017-11-30 V1.3.1) <阿里巴巴Java开发手册>是阿里巴巴集团技术团队的集体智慧结晶和经验总结,经历了多次大规模一线实战的检验及不断的完善,系统化 ...
- Web安全学习规划
一名合格的Web安全工程师是要具备很多的知识点,不但要对网站架构熟悉,通讯协议,测试流程与测试工具使用,漏洞利用脚本编写,还有需要经验的积累等. 互联网进入下半场,竞争越发的激烈,能与人工智能比肩的热 ...
- 转: gcc 指定运行时动态库路径
gcc 指定运行时动态库路径 Leave a reply 由于种种原因,Linux 下写 c 代码时要用到一些外部库(不属于标准C的库),可是由于没有权限,无法将这写库安装到系统目录,只好安装用户目录 ...
- mybaits动态SQL中的DECIMAL
数据库:mysql数据库字段类型:decimal(11,2)java程序类型:java.math.BigDecimal 使用mybatis的动态语句 <if test ="money! ...
- [Docker] Getting Started with Container Networks
It is possible to group containers into a network and we can create multi networks so that container ...
- 使maven2在下载依赖包的同时下载其源代码包。
使maven2在下载依赖包的同时下载其源代码包的方法: 1. 使用maven命令:mvn dependency:sources 下载依赖包的源代码. 2. 使用参数: -DdownloadSource ...
- ReSharper修改命名风格
默认情况下,ReSharper会建议你全局变量命名使用下划线开头,且第一个字母小写.否则,会给你标记出来,如下: 但我个人不喜欢这种风格,一般使用首字母大写且不带下划线,可以通过配置来调整:ReSha ...
- 解决Android Studio无法下载sdk的问题
因为google被墙了,android sdk无法下载.然后各种百度,都是说让设置代理,给的代理地址一般都是用的下面这个代理服务器: 大连东软信息学院镜像服务器地址: mirrors.neusoft. ...
- js开发思路
$.ui = $.ui || {}; var version = $.ui.version = "1.12.1"; // 是否为ie浏览器 var ie = $.ui.ie = ! ...
- 转:VS2013快捷键大全
Ctrl+E,D ----格式化全部代码 Ctrl+E,F ----格式化选中的代码 CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CTRL + O 打开文件 CTRL ...