Sensorian  1.0
C API Reference Guide Library
main.c
Go to the documentation of this file.
1 /****************************************************************************
2  * Copyright (C) 2015 Sensorian
3  * *
4  * This file is part of Sensorian. *
5  * *
6  * Sensorian is free software: you can redistribute it and/or modify it *
7  * under the terms of the GNU Lesser General Public License as published *
8  * by the Free Software Foundation, either version 3 of the License, or *
9  * (at your option) any later version. *
10  * *
11  * Sensorian is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with Sensorian. *
18  * If not, see <http://www.gnu.org/licenses/>. *
19  ****************************************************************************/
20 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <stdbool.h>
33 #include "MCP79410.h"
34 #include "Utilities.h"
35 #include "i2c.h"
36 
37 int main(int argc, char **argv)
38 {
39  I2C_Initialize(MCP79410_ADDRESS); //Initialize I2C and setup chip address
40 
41  if(MCP79410_IsRunning()) //If clock is already running stop it
42  {
44  }
45  MCP79410_Initialize(); //Initialize RTCC with system time and date
46 
48 
49  RTCC_Struct *current_time = (RTCC_Struct *) malloc(sizeof(RTCC_Struct));
50  RTCC_Struct *alarm_time = (RTCC_Struct *) malloc(sizeof(RTCC_Struct));
51  RTCC_Struct user_alarm = {0, 28, 0, 0, 0, 0, 0}; //Change minutes value for match
52  alarm_time = &user_alarm;
53 
54  MCP79410_SetAlarmTime(alarm_time,ZERO); //Set alarm time
55  MCP79410_SetAlarmMatch(MINUTES_MATCH,ZERO); //Alarm ZERO will trigger on minutes match
56  MCP79410_SetAlarmMFPPolarity(LOWPOL,ZERO); //Configure Alarm pin polarity as HIGH
57  MCP79410_SetMFP_Functionality(ALARM_INTERRUPT); //Set alaram interrupt
58 
60  printf("MPL pin level is %d\r\n",(unsigned char)pval);
61 
62  while(1)
63  {
64  AlarmStatus_t s = MCP79410_GetAlarmStatus(ZERO); //Check alarm status
65  printf("Alarm interrupt status %d\r\n",(unsigned int)s);
66  if(s != 0)
67  {
68  //Read alarm pin
69  pval = ReadPinStatus(MPL_PIN); //This works only when the RTCC operates from battery input. Otherwise the MPF pin will read 0 despite the
70  printf("MPL pin level is %d\r\n",(unsigned char)pval); //fact that the interrupt will be active
71  delay_ms(1000);
72  }
73  current_time = MCP79410_GetTime();
74  printf("now: %d-%d-%d %d:%d:%d\n", current_time->year, current_time->month, current_time->date, current_time->hour, current_time->min, current_time->sec);
75  delay_ms(1000);
76  }
77 
78  I2C_Close(); //Return I2C pins to default status
79 
80  return 0;
81 }
82 
83 
84 
I2C driver.
#define MCP79410_ADDRESS
The I2C address of the MCP79410 real time clock and calendar.
Definition: MCP79410.h:45
void MCP79410_SetAlarmMatch(Match_t match, Alarm_t alarm)
Alarm Asserts on Match on seconds, minutes ,hours depending on match parameter.
Definition: MCP79410.c:364
void MCP79410_DisableOscillator(void)
Disables the clock oscillator.The RTCC does not operate once the oscillator is stopped.
Definition: MCP79410.c:73
unsigned char year
Month.
Definition: MCP79410.h:151
unsigned char MCP79410_IsRunning(void)
Checks if the chip clock is running.
Definition: MCP79410.c:84
void I2C_Close(void)
Closes the I2C peripheral.
Definition: i2c.c:174
unsigned char date
Weekday.
Definition: MCP79410.h:149
Definition: MCP79410.h:157
void MCP79410_SetAlarmMFPPolarity(Polarity_t MFP_pol, Alarm_t alarm)
This function sets the MFP polarity either high or low.
Definition: MCP79410.c:331
RTCC_Struct * MCP79410_GetTime(void)
The function returns a time structure with the current time from the RTCC.
Definition: MCP79410.c:100
unsigned char month
Date of the month.
Definition: MCP79410.h:150
unsigned char sec
Definition: MCP79410.h:145
PinLevel_t ReadPinStatus(PIN_t pin)
Configures the pins as input and returns the pin status.
Definition: Utilities.c:70
void MCP79410_ClearInterruptFlag(Alarm_t alarm)
Clears the interrupt alarm flag.
Definition: MCP79410.c:276
void I2C_Initialize(unsigned char address)
Initializes the I2C peripheral.
Definition: i2c.c:36
enum PinLev PinLevel_t
AlarmStatus_t MCP79410_GetAlarmStatus(Alarm_t alarm)
This function gets the status of the alarm interrupt flag.
Definition: MCP79410.c:258
Utilities driver header.
#define MPL_PIN
Definition: Utilities.h:42
void MCP79410_Initialize(void)
Initializes the RTCC with the system time.
Definition: MCP79410.c:43
int main(int argc, char **argv)
Definition: main.c:34
void MCP79410_SetAlarmTime(RTCC_Struct *time, Alarm_t alarm)
This function sets the alarm time for one of the two alarms.
Definition: MCP79410.c:297
void delay_ms(unsigned int ms)
Delay in ms.
Definition: Utilities.c:39
unsigned char min
Seconds.
Definition: MCP79410.h:146
unsigned char hour
Minutes.
Definition: MCP79410.h:147
enum AlarmStatus AlarmStatus_t
void MCP79410_SetMFP_Functionality(MFP_t mode)
This function sets the MFP pin mode.
Definition: MCP79410.c:414