树莓派进阶之路 (016) - 通过595驱动4位LED显示系统时间

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

树莓派进阶之路 (016) - 通过595驱动4位LED显示系统时间树莓派进阶之路 (016) - 通过595驱动4位LED显示系统时间

我们使用树莓派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) ;
}
}
上一篇:BZOJ1565——[NOI2009]植物大战僵尸


下一篇:【刷题】BZOJ 1565 [NOI2009]植物大战僵尸