All pastes #2127767 Raw Edit

Untitled

public c v1 · immutable
#2127767 ·published 2012-03-13 11:50 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>/* *	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	}}/* * Get month lenght in days  */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);	}}int time_to_seconds(struct time *t){	int i, seconds;	// Year	printf("Calculating seconds since 1970\r\n");	for(i = 1970; i<t->year; i++){		int days;		days = year_len(i);		printf("Calculating number of seconds in year %i (days: %i)\r\n", i, days);		// Convert year to seconds		seconds += 60*60*24*days;	}	// Month	for(i = 1; i<t->month; i++){		int days;		days = month_len(i, t->year);					printf("Calculating number of seconds in month %i in %i (days: %i)\r\n", i, t->year, days);		seconds += 60*60*24*days;	}	// Days	printf("Calculating seconds in days in month %i in year %i\r\n", t->month, t->year);	for(i = 1; i<t->day; i++){		printf("Calculating number of seconds in day %i in month %i\r\n", i, t->month);		seconds += 60*60*24;	}	// Hours	printf("Calculating seconds in hours in day %i in month %i\r\n", t->day, t->month);	seconds += 60*60*t->hour;	// Minuts	printf("Calculating seconds in minuts in hour %i in day %i in month %i\r\n", t->hour, t->day, t->month);	seconds += 60*t->min;		// Seconds	printf("Adding remaining seconds\r\n");	seconds += t->sec;	// Return	return seconds;}int seconds_to_time(int seconds){	return 0;}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");	// Convert datetime struct to secconds	t.sec  = 44;    t.min  = 11;    t.hour = 22;    t.day = 5;    t.month  = 7;    t.year = 2012;	seconds = time_to_seconds(&t);	printf("Converted %i-%i-%i %02i:%02i:%02i to %i secs\r\n", t.year, t.month, t.day, t.hour, t.min, t.sec, seconds);	//Do stuff	printf("Starting main-loop\r\n");	printf("==================\r\n\n");	while (1) {		// Running	}}