也发个74HC595 8段数码管的DIY:显示系统时间
在TLL的帖子基础上写的,感谢他!http://forum.cubietech.com/forum ... =896&extra=page%3D1
发个图片先
然后是代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "lib/gpio_lib.h"
#define LSBFIRST 0
#define MSBFIRST 1
typedef unsigned char byte;
// Cubieboard port-D pin connected to ST_CP of 74HC595 (RCK)
unsigned int latchPin = 2;
// Cubieboard port-D pin connected to SH_CP of 74HC595 (SCK)
unsigned int clockPin = 3;
// Cubieboard port-D pin connected to DS of 74HC595 (DIN)
unsigned int dataPin = 4;
// Digits: 0,1,2,....,9,'dot','clear' for negative-shared 8-segment-LED
unsigned int Tab[] =
{ 0xc0, 0xcf, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0xff };
/**
* Set Cubieboard's GPIO port-D pin I/O mode: INPUT/OUTPUT
*/
void pinMode(unsigned int pin, unsigned int io_mode)
{
if (SETUP_OK != sunxi_gpio_set_cfgpin(SUNXI_GPD(pin), io_mode))
{
printf("Failed to config GPIO pin\n");
}
}
/**
* Set Cubieboard's GPIO port-D pin value(LOW/HIGH)
*/
void digitalWrite(int pin, int hl)
{
if (sunxi_gpio_output(SUNXI_GPD(pin), hl))
{
printf("Failed to set GPIO pin value\n");
}
}
/**
* Arduino shiftOut:
* https://github.com/arduino/Arduino/blob/master/hardware/arduino/cores/arduino/wiring_shift.c
*/
void shiftOut(unsigned int dataPin, unsigned int clockPin, int bitOrder, byte val)
{
byte i;
for (i = 0; i < 8; i++)
{
if (bitOrder == LSBFIRST)
digitalWrite(dataPin, ! !(val & (1 << i)));
else
digitalWrite(dataPin, ! !(val & (1 << (7 - i))));
digitalWrite(clockPin, HIGH);
digitalWrite(clockPin, LOW);
}
}
/**
* Initialize the GPIO & relative pins
*/
void init_gpio()
{
if (SETUP_OK != sunxi_gpio_init())
{
printf("Failed to initialize GPIO\n");
}
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
int main(int argc, char **argv)
{
time_t timep;
struct tm *p;
char buf;
// init GPIO & pins
init_gpio();
while (1)
{
// get localtime
time(&timep);
p = localtime(&timep);
sprintf(buf, "%02d%02d%02d", p->tm_hour, p->tm_min, p->tm_sec);
int i;
for (i = 0; i < sizeof(buf); i++)
{
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, MSBFIRST, 1 << i);
shiftOut(dataPin, clockPin, MSBFIRST, Tab - '0']);
digitalWrite(latchPin, 1);
usleep(1000);
}
}
return 0;
}
页:
[1]