Hackster is hosting Hackster Holidays, Ep. 5: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 5 on Friday!
Sandeep DwivediSachin Dwivedi
Published

Energy Efficient Cooler For Home

This project will reduce the electricity cost for running home coolers. So save money on electricity bills and use that for new projects.

IntermediateProtip2 hours676
Energy Efficient Cooler For Home

Things used in this project

Hardware components

NXP Freedom boards (FRDM-K82F)
×1
Relay
×1

Story

Read more

Schematics

Flow Chart

How program works

Connection Diagram freedom board relay and motor

This Circuit Diagram will help to connect motor with Freedom Board through Relay

screen shots and connection diagram and code

screen shots and connection diagram and code and flowchart

Code

main.c

C/C++
this code will control the relay
/*
 * Sandeep Dwivedi 1 July 2016 Code for FlexIO freedom board
 *
 *
 */

#include <stdio.h>
#include "fsl_debug_console.h"
#include "fsl_common.h"
#include "fsl_rtc.h"
#include "board.h"

#include "pin_mux.h"
#include "clock_config.h"

#define Low  0
#define High  1
/*******************************************************************************
 * Definitions
 ******************************************************************************/


/*******************************************************************************
 * Prototypes
 ******************************************************************************/
void BOARD_SetRtcClockSource(void);
/*!
 * @brief Set the alarm which will be trigerred x secs later. The alarm trigger
 *        will print a notification on the console.
 *
 * @param offsetSec  Offset seconds is set for alarm.
 */

static void CommandSeconds(void);

/*!
 * @brief Get the current date time.
 *
 */



/*******************************************************************************
 * Variables
 ******************************************************************************/
static volatile uint8_t g_AlarmPending = 0U;
static volatile bool g_SecsFlag = false;



static char g_StrNewline[] = "\r\n";


/*******************************************************************************
 * Code
 ******************************************************************************/

void BOARD_SetRtcClockSource(void)
{
    /* Enable the RTC 32KHz oscillator */
    RTC->CR |= RTC_CR_OSCE_MASK;
}

uint32_t seconds_value = 0 ;
static void CommandSeconds(void)
{
    uint32_t count = 0U;
    rtc_datetime_t date;
    char sourceBuff[] = "\r10:10:00";

    g_SecsFlag = false;
    while (count < seconds_value)
    {
        /* If seconds interrupt ocurred, print new time */
        if (g_SecsFlag)
        {
            /* Build up the word */
            g_SecsFlag = false;
            count++;
            RTC_GetDatetime(RTC, &date);

            sourceBuff[1] = ((date.hour / 10) + 0x30);
            sourceBuff[2] = ((date.hour % 10) + 0x30);
            sourceBuff[3] = ':';
            sourceBuff[4] = ((date.minute / 10) + 0x30);
            sourceBuff[5] = ((date.minute % 10) + 0x30);
            sourceBuff[6] = ':';
            sourceBuff[7] = ((date.second / 10) + 0x30);
            sourceBuff[8] = ((date.second % 10) + 0x30);
            /* Print the time */
            PRINTF(sourceBuff);
        }
    }
    /* Disable Sec interrupt */
    RTC_DisableInterrupts(RTC, kRTC_SecondsInterruptEnable);
    PRINTF(g_StrNewline);
}



/*!
 * @brief Override the RTC IRQ handler.
 */
void RTC_IRQHandler(void)
{
    if (RTC_GetStatusFlags(RTC) & kRTC_AlarmFlag)
    {
        g_AlarmPending = 1U;

        /* Clear alarm flag */
        RTC_ClearStatusFlags(RTC, kRTC_AlarmInterruptEnable);
    }
}

/*!
 * @brief Override the RTC Second IRQ handler.
 */
void RTC_Seconds_IRQHandler(void)
{
    g_SecsFlag = true;
}

/*!
 * @brief Main function
 */
int main(void)
{

    rtc_config_t rtcConfig;
    rtc_datetime_t date;

    /* Board pin, clock, debug console init */
    BOARD_InitPins();
    BOARD_BootClockRUN();
    BOARD_InitDebugConsole();


    // Sandeep Code
    gpio_pin_config_t pin_config = {kGPIO_DigitalOutput,1,};
    /* pin_config struct hold pin configuration for Output pin 1 for output and 0 for input */
    GPIO_PinInit(GPIOC, 11U, &pin_config);



    // Sandeep Code

    /* Init RTC */
    /*
     * rtcConfig.wakeupSelect = false;
     * rtcConfig.updateMode = false;
     * rtcConfig.supervisorAccess = false;
     * rtcConfig.compensationInterval = 0;
     * rtcConfig.compensationTime = 0;
     */
    RTC_GetDefaultConfig(&rtcConfig);
    RTC_Init(RTC, &rtcConfig);
    /* Select RTC clock source */
    BOARD_SetRtcClockSource();

    /* Set a start date time and start RTC */
    date.year = 2015U;
    date.month = 11U;
    date.day = 11U;
    date.hour = 11U;
    date.minute = 11U;
    date.second = 11U;

    /* RTC time counter has to be stopped before setting the date & time in the TSR register */
    RTC_StopTimer(RTC);

    RTC_SetDatetime(RTC, &date);

    /* Enable at the NVIC */
    EnableIRQ(RTC_IRQn);
    EnableIRQ(RTC_Seconds_IRQn);

    /* Start the RTC time counter */
    RTC_StartTimer(RTC);


    while (1) 
    {

            	GPIO_WritePinOutput (GPIOC, 11U, High);
                RTC_EnableInterrupts(RTC, kRTC_SecondsInterruptEnable);
                seconds_value = 120 ;
                CommandSeconds(); // this function gives 2 min timer


                GPIO_WritePinOutput (GPIOC, 11U, Low);
                RTC_EnableInterrupts(RTC, kRTC_SecondsInterruptEnable);
                seconds_value = 120 ; // to set the value of timer
                CommandSeconds(); // this function gives 2 min timer

    }
}

pin_mux.c

C/C++
set pins input or output
/*
 * Sandeep Dwivedi 1 July 2016 Code for FlexIO freedom board
 */

#include "fsl_common.h"
#include "fsl_port.h"
#include "pin_mux.h"

/*******************************************************************************
 * Code
 ******************************************************************************/

void BOARD_InitPins(void)
{
    /* Initialize LPUART4 pins below */
    /* Ungate the port clock */
    CLOCK_EnableClock(kCLOCK_PortC);
    /* Affects PORTC_PCR14 register */
    PORT_SetPinMux(PORTC, 14U, kPORT_MuxAlt3);
    /* Affects PORTC_PCR15 register */
    PORT_SetPinMux(PORTC, 15U, kPORT_MuxAlt3);

    // Sandeep Code
    PORT_SetPinMux(PORTC, 11U, kPORT_MuxAsGpio);

    // Sets PTB16 ie Port C Pin 11 as GPIO pin
}

Credits

Sandeep Dwivedi

Sandeep Dwivedi

10 projects • 26 followers
just a learner curious about things
Sachin Dwivedi

Sachin Dwivedi

4 projects • 6 followers
Student of Final Year of B.Tech. in Computer Science

Comments