Re: Retour vers le Futur
Posté : mar. 27 janv. 2015 14:02
Ah, bien vu, merci.
Veloce
Veloce
Les forums français du Raspberry Pi
http://forums.framboise314.fr/
Code : Tout sélectionner
sudo nano /etc/modules
Code : Tout sélectionner
i2c-bcm2708
i2c-dev
rtc-ds1307
Code : Tout sélectionner
sudo apt-get install python-smbus
sudo apt-get install i2c-tools
Code : Tout sélectionner
sudo nano /etc/rc.local
Code : Tout sélectionner
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
sudo hwclock -s
Code : Tout sélectionner
date --set "27 JAN 2015 19:20:00"
Code : Tout sélectionner
static const uint charTable[] =
{
0x3F, //0
0x06, //1
0x5B, //2
0x4F, //3
0x66, //4
0x6D, //5
0x7D, //6
0x07, //7
0x7F, //8
0x6F, //9
0x77, //A
0x7C, //b
0x39, //C
0x5E, //d
0x79, //E
0x71, //F
0x3d, //G
0x76, //H
0x04, //i
0x0E, //j
0x74, //k
0x38, //L
0x37, //M
0x54, //n
0x5c, //o
0x73, //P
0x67, //q
0x50, //r
0x6d, //S
0x78, //t
0x1c, //u
0x72, //V
0x3e, //W
0x52, //X
0x6E, //y
0x49, //Z
0x00 //SPACE
};
Code : Tout sélectionner
sudo i2cdetect -y 1
Code : Tout sélectionner
// initialise GPIOs
wiringPiSetup();
pinMode (0, OUTPUT); // line address A0
pinMode (1, OUTPUT); // line address A1
pinMode (2, OUTPUT); // AM led line 0
pinMode (3, OUTPUT); // PM led line 0
pinMode (4, OUTPUT); // AM led line 1
pinMode (5, OUTPUT); // PM led line 1
pinMode (6, OUTPUT); // AM led line 2
pinMode (7, OUTPUT); // PM led line 2
Code : Tout sélectionner
sudo apt-get install git-core
Code : Tout sélectionner
git clone git://git.drogon.net/wiringPi
Code : Tout sélectionner
cd wiringPi
./build
Code : Tout sélectionner
gpio -p write <broche> 1
gpio -p write <broche> 0
gpio -p read <broche>
etc.
Code : Tout sélectionner
sudo ./run
Code : Tout sélectionner
#include <stdio.h>
#include <stdlib.h>
#include <time.h> //required for localtime, struct tm, etc
#include <wiringPi.h> // library for GPIOs
#include <wiringPiI2C.h> // library for I2C management
/*********************************************************
* Doc Brown's TIME MACHINE
* For Raspberry Pi
* Using 9 Adafruit's LED Backpacks
* and a 74HC138 DMUX for addressing
* written by Laurent Sineux
* Inspired by Adafruit and movie "Back to the Future"
*
* *******************************************************/
/*************************************************************
* setUp
*
* Starts internal oscillator of HT16K33
* (not advised with address DMUX, can stall chip)
*
* input variables:
* int fd handle to I2C device
*
* returns 0 if OK
*************************************************************/
int setUp (int fd)
{
int result;
result = wiringPiI2CWrite (fd, 0x21);
return result;
}
/*************************************************************
* setBrightness
*
* set by PWM in the HT16K33
*
* input variables:
* int fd handle to the I2C device
* int bness brightness
* correct values 0-15
* PWM ratio = (bness+1)/16
*
* returns 0 if OK
*************************************************************/
int setBrightness (int fd, int bness)
{
int result;
result = wiringPiI2CWrite (fd, 0xE0 | bness);
return result;
}
/*************************************************************
* setBlink
*
* defines blink rate
* & turns display on/off
*
* input variables:
* int fd handle for the I2C device
* int br blink rate
* correct values:
* 0 no blinking
* 2 2Hz
* 4 1Hz
* 6 0.5 Hz
*
* returns 0 if OK
*************************************************************/
// first we define the commnds for the I2C led drivers
#define HT16K33_BLINK_CMD 0x80
#define HT16K33_BLINK_DISPLAYON 0x01
#define HT16K33_BLINK_OFF 0x00
#define HT16K33_BLINK_2HZ 0x02
#define HT16K33_BLINK_1HZ 0x04
#define HT16K33_BLINK_0HZ5 0x06
int setBlink (int fd, int br)
{
int result;
result = wiringPiI2CWrite (fd, HT16K33_BLINK_CMD \
| HT16K33_BLINK_DISPLAYON | br);
return result;
}
/*************************************************************
* writeBuffer
*
* Writes a character to the display
* input variables:
* int fd handle to the I2C device
* int pos position of the digit
* correct values:
* 0 digit n°1 (leftmost)
* 2 digit n°2
* 4 colon (:)
* 6 digit n°3
* 8 digit n°4 (rightmost)
* int value
* correct values:
* 0-9 digits
* 10-35 characters
* 36 space (clear digit)
*
* note: send value || 0xF0 to light the decimal point
*
* returns 0 if OK, error code otherwise
*************************************************************/
// mapping of digits and characters to corresponding 7 segments
static const uint charTable[] =
{
0x3F, //0
0x06, //1
0x5B, //2
0x4F, //3
0x66, //4
0x6D, //5
0x7D, //6
0x07, //7
0x7F, //8
0x6F, //9
0x77, //A
0x7C, //b
0x39, //C
0x5E, //d
0x79, //E
0x71, //F
0x3d, //G
0x76, //H
0x04, //i
0x0E, //j
0x74, //k
0x38, //L
0x37, //M
0x54, //n
0x5c, //o
0x73, //P
0x67, //q
0x50, //r
0x6d, //S
0x78, //t
0x1c, //u
0x72, //V
0x3e, //W
0x52, //X
0x6E, //y
0x49, //Z
0x00 //SPACE
};
# define DECIMAL_POINT 0x80
int writeBuffer (int fd, int pos, int value)
{
int result;
result = wiringPiI2CWriteReg8 (fd, pos, 0xFF & value);
return result;
}
/*************************************************************
* selectLine
*
* sets address to the 74HC138 for selected line
*
* input variables:
* int line (0, 1 or 2)
*
* ***********************************************************/
int selectLine(int line)
{
if ((line < 0) || (line > 2)) return -1;
digitalWrite (0, line & 1);
digitalWrite (1, (line/2)&1 );
return 0;
}
/*************************************************************
* displayDate
*
* Displays a date on a given line
*
* input variables:
* struct tm *ptrDate pointer to the date to print
* int *dhArray pointer to the device handle array
* int line line number concerned
*
*
* ***********************************************************/
int displayDate (struct tm *ptrDate, int dhArray[], int line)
{
int month, day, year, hour, min, ampm, pin;
// printf("month.day year ampm hour:minute\n");
// printf("%d.%d %d %d %d:%d\n", ptrDate->tm_mon+1, \
ptrDate->tm_mday, ptrDate->tm_year+1900, ptrDate->tm_isdst, \
ptrDate->tm_hour,ptrDate->tm_min);
selectLine(line); //select line address
// write month
month = ptrDate->tm_mon+1;
// device n°0 (left display)
// digits n°0 and 2
// don't forget to OR the decimal point
// write 10's of the month
writeBuffer(dhArray[0], 0, charTable[month / 10 % 10]);
// write units of the month (+OR decimal point)
writeBuffer(dhArray[0], 2, charTable[month % 10]|DECIMAL_POINT);
// write day
day = ptrDate->tm_mday;
// device n°0 (left display)
// digits n°6 and 8 (4 being the colon)
// write 10's of the day
writeBuffer(dhArray[0], 6, charTable[day / 10 % 10]);
// write units of the day
writeBuffer(dhArray[0], 8, charTable[day %10]);
//write year
// device n°1 (middle display)
// digits 0, 2, 6, 8 (4 being the colon)
// years are relative from 1900
year = ptrDate->tm_year+1900;
// write 1000's of the year
writeBuffer(dhArray[1], 0, charTable[year / 1000 % 10]);
// write 100's of the year
writeBuffer(dhArray[1], 2, charTable[year / 100 % 10]);
// write 10's of the year
writeBuffer(dhArray[1], 6, charTable[year / 10 % 10]);
// write units of the year
writeBuffer(dhArray[1], 8, charTable[year % 10]);
//write hour
// device n°2 (right display)
hour = ptrDate->tm_hour;
// set ampm (morning/afternoon) flag
if (hour >= 12)
{
ampm = 1;
}
else
{
ampm = 0;
}
// convert 24h to 12h
if (hour > 12) hour -= 12;
// write 10's of the hour
writeBuffer(dhArray[2], 0, charTable[hour / 10 % 10]);
// write units of the year
writeBuffer(dhArray[2], 2, charTable[hour % 10]);
//write min
// device n°2 (right display)
min = ptrDate->tm_min;
// write 10's of the min
writeBuffer(dhArray[2], 6, charTable[min / 10 % 10]);
// write units of the min
writeBuffer(dhArray[2], 8, charTable[min % 10]);
//set AM/PM LEDS
// line Pins: AM PM
// 0 2 3
// 1 4 5
// 2 6 7
pin = (line+1)*2; // which goes 2, 4, 6
// AM led is on if ampm = 0 (ie is false)
digitalWrite (pin, !ampm);
// PM led is on if ampm = 1 (ie is true)
pin++; // which goes 3, 5, 7
digitalWrite (pin, ampm);
// printf("%d.%d %d %d %d:%d\n", month, day, year,\
ampm, hour, min);
return 0;
}
/**************************************************************
* blinkColon
*
* switches colon between hours and minutes
* input variables
* int *dhArray pointer to the device handle array
* int value (1 = on, 0 = off)
*
*************************************************************/
int blinkColon (int dhArray[], int value)
{
int line;
for (line = 0; line < 3; line ++)
{
selectLine(line); // set line address in mux
writeBuffer(dhArray[2], 4, value);
/* printf("line = %d device = %d value = %d\n",line,dhArray[2]\
,value);
*/
}
return 0;
}
/*************************************************************
* main
*
*
*************************************************************/
// table of addresses for the I2C display devices
static const int device[] =
{
0x70,
0x71,
0x72
};
int main (void)
{
int dh[3]; // I2C device handles
int result; // return from functions
int i,j; // counters
int row, line;
int colon=0;
struct tm toTime, *ptrToTime;
struct tm nowTime, *ptrNowTime;
struct tm fromTime, *ptrFromTime;
// initialise GPIOs
wiringPiSetup();
pinMode (0, OUTPUT); // line address A0
pinMode (1, OUTPUT); // line address A1
pinMode (2, OUTPUT); // AM led line 0
pinMode (3, OUTPUT); // PM led line 0
pinMode (4, OUTPUT); // AM led line 1
pinMode (5, OUTPUT); // PM led line 1
pinMode (6, OUTPUT); // AM led line 2
pinMode (7, OUTPUT); // PM led line 2
// create device handles for the 3 rows
selectLine(0); // select first line
for (row=0; row<3; row++)
{
if((dh[row]=wiringPiI2CSetup(device[row]))<0)
{
printf("No device at address %X\n",device[row]);
exit (-1);
}
else
{
// printf("device #%i at address %X\n",dh[row],device[row]);
}
}
// initialise all display devices
// line by line
for (line = 0; line < 3; line ++)
{
selectLine(line); // set line address in mux
// row by row
for (row = 0; row < 3; row ++)
{
result = setUp(dh[row]); // start oscillator
result = setBrightness(dh[row], 8); // average brightness
result = setBlink(dh[row], HT16K33_BLINK_OFF); // no blink
// digit by digit
for (i=0;i<5;i++)
{
result = writeBuffer(dh[row], i*2, 0x00); // clear digit
}
}
}
while (1)
{
time_t t = time (NULL);
// printf("month.day year ampm hour:minute\n");
toTime.tm_mon = 11-1;
toTime.tm_mday = 5;
toTime.tm_year = 1955-1900;
toTime.tm_hour = 06;
toTime.tm_min = 00;
toTime.tm_sec = 0;
toTime.tm_isdst = 0;
result=displayDate(&toTime, dh, 0);
ptrNowTime = localtime (&t);
nowTime = *ptrNowTime;
result=displayDate(&nowTime, dh, 1);
fromTime.tm_mon = 10-1;
fromTime.tm_mday = 26;
fromTime.tm_year = 1985-1900;
fromTime.tm_hour = 01;
fromTime.tm_min = 20;
fromTime.tm_isdst = 0;
result=displayDate(&fromTime, dh, 2);
blinkColon(dh, colon);
if(colon == 0)
{
colon = 0xFF;
}
else
{
colon = 0x00;
}
delay(500);
}
return 0;
}