All pastes #2128040 Raw Edit

Something

public c v1 · immutable
#2128040 ·published 2012-03-14 08:18 UTC
rendered paste body
//Main/***************************************************************** Author: kh@ipimp.at****************************************************************** Minimum setup of GPIO****************************************************************/#include "gpio.h"   // Generel Purpose Input/Output#include "rcc.h"		// Reset and clock Control#include "usart.h"#include "I2C.h"#include <stdio.h>#include <stdint.h>#define EPOCH_YEAR 1970	// Start year#define GMT_TIME_ZONE 1	// +1 GMT (Denmark)#define DAYLIGHT_SAVING 1 // Winter = 1#define DEBUG_INFO 1 // Enable debug = 1/* *	Time structure */struct time {    int sec;    int min;    int hour;    int day;    int month;    int year;};/* * Check if year is leap year */int leap_year(int year) {    if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {        return 1; // Is leap year    }    else {        return 0; // Is not leap year    }}/* * Get year lenght in days */int year_len(int year) {    if(leap_year(year)) {        return 366; // 366 days in leap year    }    else {        return 365; // 365 days in normal year    }}/* * Convert seconds to a day * * @param int s * @return float */int time_to_day(int s) {    int d;    d = s/86400;    return d;}/* * Get month lenght in days * * @param int month * @param int year * @return int */int month_len(int month, int year) {    if(month == 2) {        // February is 28/29 days        return (leap_year(year) ? 29 : 28);    }    else {        // Normal is 30/31 days        return ((month - 1) % 7 % 2 ? 30 : 31);    }}/* * Convert time to seconds *  * @param struct time pointer * @return int */int time_to_seconds(struct time *t) {    int i, seconds, days_second;	if(DEBUG_INFO)		printf("\n====== time_to_seconds() debug ======\r\n\n");	// Year    if(DEBUG_INFO)        printf("Calculating seconds using epoch year %i...\r\n", EPOCH_YEAR);    for(i = EPOCH_YEAR; i<t->year; i++) {        int days;        days = year_len(i);        if(DEBUG_INFO)            printf("Calculating seconds in year %i... ", i);        // Convert year to seconds        seconds += 60*60*24*days;        if(DEBUG_INFO)            printf("(days: %i)\r\n", days);    }	// Month    for(i = 1; i<t->month; i++) {        int days;        days = month_len(i, t->year);        if(DEBUG_INFO)            printf("Calculating seconds in %i-%02i... ", t->year, i);        seconds += 60*60*24*days;		if(DEBUG_INFO)        	printf("(days: %i)\r\n", days);    }	// Days    for(i = 1; i<t->day; i++) {        if(DEBUG_INFO)            printf("Calculating seconds in %i-%02i-%02i... ", t->year,  t->month, i);        days_second = 60*60*24;				if(DEBUG_INFO)        	printf("(%i seconds)\r\n", days_second);		seconds += days_second;    }	// Last day    if(DEBUG_INFO)        printf("Calculating seconds in %i-%02i-%02i... ", t->year,  t->month, t->day);    days_second = (60*60*t->hour) + (60*t->min) + (t->sec);	if(DEBUG_INFO)        	printf("(%i seconds)\r\n", days_second);    seconds += days_second;	// Correct daylight saving    if(DAYLIGHT_SAVING) {        if(DEBUG_INFO)            printf("Adjusting to daylight savings time\r\n");        seconds -= 3600;    }	// Correct Time Zone    if(DEBUG_INFO)        printf("Adjusting timezone (GMT %i)\r\n", GMT_TIME_ZONE);    seconds -= 3600 * GMT_TIME_ZONE;    if(DEBUG_INFO)        printf("Converted %i-%02i-%02i %02i:%02i:%02i to %i secs\r\n", t->year, t->month, t->day, t->hour, t->min, t->sec, seconds);	if(DEBUG_INFO)		printf("\n====== time_to_seconds() debug ======\r\n\n");    return seconds;}/* * Convert seconds to time * * param int seconds * param struct time pointer */void seconds_to_time(int seconds, struct time *t) {    int year = EPOCH_YEAR, month = 1, day = 1, hour = 0, min = 0, sec = 0, seconds_debug;	seconds_debug = seconds;	if(DEBUG_INFO)		printf("\n====== second_to_time() debug ======\r\n\n");    // Year	if(DEBUG_INFO)		printf("Calculating date using epoch year %i... ", year);    while(seconds > 60*60*24*365) {	// Run as long as we have enough seconds for a year        if(leap_year(year)) { // Is leap year?            if(seconds > 60*60*24*366) { // and do we have enough seconds left for a full leap year?                // Remove a leap year in seconds                seconds -= 60*60*24*366;                // Increase year by one                year++;            } else { // Year is not over yet, because it's one day longer than a normal year.                break;            }        } else { // Normal year            // Remove a year in seconds            seconds -= 60*60*24*365;            // Increase year by one            year++;        }    }	if(DEBUG_INFO)		printf("(Year is: %i)\r\n", year);	// Months	if(DEBUG_INFO)		printf("Calculating date using epoch month %02i... ", month);	while(seconds > 60*60*24*28){		int month_len_secs, next_month;		next_month = month + 1;		// Get the lenght of the month		month_len_secs = 60*60*24*month_len(next_month, year);		if(seconds > month_len_secs){			seconds -= month_len_secs;			month++;		} else {			break;		}	}	if(DEBUG_INFO)		printf("(Month is: %02i)\r\n", month);	// Day	if(DEBUG_INFO)		printf("Calculating date using epoch day %02i... ", day);	while(seconds > 60*60*24){		day++;		seconds	-= 60*60*24;	}	if(DEBUG_INFO)		printf("(Day is: %02i)\r\n", day);		// Hour	if(DEBUG_INFO)		printf("Calculating clock... ");	while(seconds >= 60*60){		hour++;		seconds	-= 60*60;	}		// Min	while(seconds >= 60){		min++;		seconds	-= 60;	}	// Sec	sec = seconds;	seconds -= seconds;	if(DEBUG_INFO)		printf("(Time is: %02i:%02i:%02i)\r\n", hour, min, sec);		// Correct daylight saving    if(DAYLIGHT_SAVING) {		if(DEBUG_INFO)	            printf("Adjusting to daylight savings time\r\n");		hour++;	}	// Correct Time Zone	if(DEBUG_INFO)        printf("Adjusting timezone (GMT %i)\r\n", GMT_TIME_ZONE);	hour += GMT_TIME_ZONE;	// Set time struct    t->year = year;    t->month = month;    t->day = day;    t->hour = hour;    t->min = min;    t->sec = sec;	if(DEBUG_INFO)		printf("Converted %i secs to %i-%02i-%02i %02i:%02i:%02i\r\n", seconds_debug, t->year, t->month, t->day, t->hour, t->min, t->sec);	if(DEBUG_INFO)		printf("\n====== /second_to_time() debug ======\r\n\n");}int main (void) {    int seconds;    struct time t;	// Start Clock on GPIO Port E (Enable port E)    rcc_apb2enr(RCC_APB2ENR_IOPEEN,ENABLE);    rcc_clockmode5();    usart_enable();	//unsigned int volatile * const RSSE2 = (unsigned int *) (RCC_BASE + 0x18);	//*RSSE2 |= 1 << 6;	//Setting bit 8 to 15 of PORT E to Generel Purpose output push-pull max 50Mhz    GPIOE->CRH=0x33333333;    putstr("\x1b[2J\x1b[H");    putstr("I2C Initialise\n\r");    I2C_Init();    printf("Setting time\r\n");	// Set time    t.sec  = 44;    t.min  = 11;    t.hour = 22;    t.day = 5;    t.month  = 7;    t.year = 2012;    seconds = time_to_seconds(&t);    seconds_to_time(seconds, &t);	//Do stuff    printf("Starting main-loop\r\n");    printf("\n==================\r\n\n");    while (1) {        // Running    }}