RTC Lib & Output

Dependencies:   LM75B RTC8563 mbed

main.cpp

Committer:
EmilMcDuck
Date:
2015-04-08
Revision:
0:b2c29eef3ac4
Child:
1:9d545a2abf32

File content as of revision 0:b2c29eef3ac4:

#include "mbed.h"
#include "LM75B.h"

#define RTC8563_ADR 0xA2

#define CONTROL1 0x00
#define CONTROL2 0x01
#define SECONDS 0x02
#define MINUTES 0x03
#define HOURS 0x04
#define DAYS 0x05
#define WEEKDAYS 0x06
#define MONTHS 0x07
#define YEARS 0x08
#define MINUTE_ALARM 0x09
#define HOUR_ALARM 0x0A
#define DAY_ALARM 0x0B
#define WEEKDAY_ALARM 0x0C
#define CLOCKOUT_FREQ 0x0D
#define TIMER_CINTROL 0x0E
#define TIMER 0x0F
#define _READ 0x01

char year, month, day, week;
char hour, minute, sec;

char week_chr[7][4] = {"MON","TUE","WED","THU","FRI","SAT","SUN"};

BusOut myled(P1_13,P1_12,P1_7,P1_6,P1_4,P1_3,P1_1,P1_0,LED4,LED3,LED2,LED1);
AnalogIn LDR (p16);

InterruptIn sw1(P0_10);
InterruptIn sw2(P0_15);
InterruptIn sw3(P0_23);
InterruptIn sw4(P1_16);

Serial pc(USBTX, USBRX);
LM75B sensor(p28, p27);
I2C i2c(p28, p27);

typedef enum state_t {menue, date, ldr, temp} state_t;
state_t state = menue;

void getNextState();
void printTime();
void flashLDR();
void rtc_init();
char rtc_read(char address);
void rtc_write(char address, char value);

int main()
{
    sensor.open();
    rtc_init();

    sw1.rise(&getNextState);
    sw2.rise(&getNextState);
    sw3.rise(&getNextState);
    sw4.rise(&getNextState);

    while(1) {
        myled = 0x0000;
        switch(state) {
            case menue:
                pc.printf("########### M E N U ###########");
                pc.printf("1.Taste => Menue anzeigen\n\r");
                pc.printf("2.Taste => Uhrzeit und Datum\n\r");
                pc.printf("3.Taste => LDR-Wert und LED-Array\n\r");
                pc.printf("4.Taste => Temperatur anzeigen\n\r");
                break;
            case date:
                printTime();
                break;
            case ldr:
                pc.printf("LDR: %f\n", LDR.read());
                flashLDR();
                break;
            case temp:
                pc.printf("Temperatur: %.1f\n\r", (float) sensor);
                break;
        }
        pc.printf("##############################\n\r");
        wait(0.2);
    }
}

void flashLDR()
{
    for(float w = 0; w < 0.6; w+=0.05)
        if(LDR>=w)
            myled = (myled << 1) + 1;
}

void getNextState()
{
    if(sw1) state = menue;
    else if(sw2) state = date;
    else if(sw3) state = ldr;
    else if (sw4) state = temp;
}

void printTime()
{
    year = rtc_read(YEARS);
    month = rtc_read(MONTHS);
    day = rtc_read(DAYS);
    week = rtc_read(WEEKDAYS);
    hour = rtc_read(HOURS);
    minute = rtc_read(MINUTES);
    sec = rtc_read(SECONDS);

    pc.printf("20%d/%02d/%02d %s\n", year, month, day, week_chr[week]); // Date
    pc.printf("%02d:%02d:%02d\n", hour, minute, sec); // Time
}

void rtc_init()
{
    pc.printf("RTC-Setup...");

    rtc_write(CONTROL1, 0x20); //stop
    rtc_write(CONTROL2, 0x00);
    rtc_write(YEARS, 15);
    rtc_write(MONTHS,3);
    rtc_write(DAYS, 13);
    rtc_write(HOURS, 21);
    rtc_write(MINUTES, 7);
    rtc_write(SECONDS, 0);
    rtc_write(WEEKDAYS, 5);
    rtc_write(CLOCKOUT_FREQ, 0x00); // 0x83 = TE on & 1Hz
    rtc_write(TIMER_CINTROL, 0x00);
    rtc_write(CONTROL1, 0x00); //start
}

char rtc_read(char address)
{
    char value;
    i2c.start();
    i2c.write(RTC8563_ADR);
    i2c.write(address);
    i2c.start();
    i2c.write(RTC8563_ADR | _READ);
    value = i2c.read(0);
    i2c.stop();

    return value;
}
void rtc_write(char address, char value)
{
    i2c.start();
    i2c.write(RTC8563_ADR);
    i2c.write(address);
    i2c.write(value);
    i2c.stop();
}