SEDO subject project

Dependencies:   ds3231 mbed-rtos mbed DHT

Committer:
ValenSalLop
Date:
Mon May 15 18:30:20 2017 +0000
Revision:
12:1d544cdab2cf
Parent:
10:f14f47225091
final;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ValenSalLop 10:f14f47225091 1 #include "mbed.h"
ValenSalLop 10:f14f47225091 2 #include "real_time_clock.h"
ValenSalLop 9:5e9c4277151d 3 Ds3231 rtc(D14, D15);
ValenSalLop 9:5e9c4277151d 4
ValenSalLop 9:5e9c4277151d 5 int setupRTC(void)
ValenSalLop 9:5e9c4277151d 6 {
ValenSalLop 9:5e9c4277151d 7 int error = 0;
ValenSalLop 9:5e9c4277151d 8 char temp[255];
ValenSalLop 9:5e9c4277151d 9 int token_count = 0;
ValenSalLop 10:f14f47225091 10 int time_data[7];
ValenSalLop 9:5e9c4277151d 11 ds3231_cntl_stat_t rtc_control_status = {0,0};
ValenSalLop 9:5e9c4277151d 12 ds3231_time_t rtc_time;
ValenSalLop 9:5e9c4277151d 13 ds3231_calendar_t rtc_calendar;
ValenSalLop 9:5e9c4277151d 14
ValenSalLop 9:5e9c4277151d 15 scanf("%d", &temp);
ValenSalLop 9:5e9c4277151d 16 char * pch;
ValenSalLop 10:f14f47225091 17 pch = strtok (temp,",");
ValenSalLop 9:5e9c4277151d 18 do {
ValenSalLop 9:5e9c4277151d 19
ValenSalLop 9:5e9c4277151d 20 printf ("Insert DOW,DOM,M,Y,H,m,S\n");
ValenSalLop 9:5e9c4277151d 21 while (pch != NULL) {
ValenSalLop 9:5e9c4277151d 22 if(token_count>=7) {
ValenSalLop 9:5e9c4277151d 23 error = 1;
ValenSalLop 9:5e9c4277151d 24 break;
ValenSalLop 9:5e9c4277151d 25 }
ValenSalLop 9:5e9c4277151d 26 time_data[token_count] = atoi(pch);
ValenSalLop 9:5e9c4277151d 27 token_count++;
ValenSalLop 9:5e9c4277151d 28 }
ValenSalLop 9:5e9c4277151d 29 } while(error==0);
ValenSalLop 9:5e9c4277151d 30
ValenSalLop 9:5e9c4277151d 31 rtc.set_cntl_stat_reg(rtc_control_status);
ValenSalLop 10:f14f47225091 32 rtc_calendar.day=time_data[0];
ValenSalLop 9:5e9c4277151d 33 rtc_calendar.date=time_data[1];
ValenSalLop 9:5e9c4277151d 34 rtc_calendar.month=time_data[2];
ValenSalLop 9:5e9c4277151d 35 rtc_calendar.year=time_data[3];
ValenSalLop 9:5e9c4277151d 36 rtc_time.mode=0;
ValenSalLop 9:5e9c4277151d 37 rtc_time.hours=time_data[4];
ValenSalLop 9:5e9c4277151d 38 rtc_time.minutes=time_data[5];
ValenSalLop 9:5e9c4277151d 39 rtc_time.seconds=time_data[6];
ValenSalLop 9:5e9c4277151d 40
ValenSalLop 9:5e9c4277151d 41 //Set the time, uses inverted logic for return value
ValenSalLop 9:5e9c4277151d 42 if(rtc.set_time(rtc_time)) {
ValenSalLop 9:5e9c4277151d 43 printf("\nrtc.set_time failed!!\n");
ValenSalLop 9:5e9c4277151d 44 error = 2;
ValenSalLop 9:5e9c4277151d 45 }
ValenSalLop 9:5e9c4277151d 46
ValenSalLop 9:5e9c4277151d 47 //Set the calendar, uses inverted logic for return value
ValenSalLop 9:5e9c4277151d 48 if(rtc.set_calendar(rtc_calendar)) {
ValenSalLop 9:5e9c4277151d 49 printf("\nrtc.set_calendar failed!!\n");
ValenSalLop 9:5e9c4277151d 50 error = 3;
ValenSalLop 9:5e9c4277151d 51 }
ValenSalLop 9:5e9c4277151d 52 return error;
ValenSalLop 9:5e9c4277151d 53 }