Smart House

A3BU code:

#include <asf.h>
#include <board.h>
#include “pwm.h”
#include “gpio.h”
#include “ioport.h”
#include “port_driver.h”
#include “avr\interrupt.h”
#include “gfx_mono.h”
#include <st7565r.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int dutyCycle = 5;
struct pwm_config motorPwm;
#define lightLevel IOPORT_CREATE_PIN(PORTB,0)
#define alarmSense IOPORT_CREATE_PIN(PORTA,0)
int alarmOn = 0;

//Button motor toggle
ISR(PORTF_INT0_vect)
{
if (dutyCycle == 5)
{
dutyCycle = 10;
pwm_start(&motorPwm, dutyCycle);
}
else
{
dutyCycle = 5;
pwm_start(&motorPwm, dutyCycle);
}
}

//Alarm Flag
ISR(PORTA_INT0_vect)
{
int val = ioport_get_pin_level(alarmSense);

if(val == 1 && alarmOn == 0)
{
alarmOn = 1;
gpio_set_pin_low(LED0);
gpio_set_pin_low(LED1);
gfx_mono_draw_string(“\n\nALARM DETECTED!!!!”,0,0,&sysfont);
}
if (val == 0  && alarmOn == 1)
{
alarmOn = 0;
gpio_set_pin_high(LED0);
gpio_set_pin_high(LED1);
gfx_mono_draw_string(“\n\nNo alarm detected.”,0,0,&sysfont);
}
}

//Light flag
ISR(PORTB_INT0_vect)
{
if (ioport_get_pin_level(lightLevel) == 1)
{
gfx_mono_draw_string(“\nLights are on. “,0,0,&sysfont);
}
else if (ioport_get_pin_level(lightLevel) == 0)
{
gfx_mono_draw_string(“\nLights are off.”,0,0,&sysfont);
}
}

int main (void)
{
// Insert system clock initialization code here (sysclk_init()).
sysclk_init();
board_init();
gfx_mono_init();
st7565r_init();

gpio_set_pin_high(LCD_BACKLIGHT_ENABLE_PIN);
gfx_mono_draw_string(“MONITOR ONLINE:”,0,0,&sysfont);
gfx_mono_draw_string(“\nLights are off.”,0,0,&sysfont);
gfx_mono_draw_string(“\n\nNo alarm detected.”,0,0,&sysfont);

pwm_init(&motorPwm, PWM_TCC0, PWM_CH_A, 50);//this is SDA on J1 on the A3BU Xplained
pwm_start(&motorPwm, dutyCycle);

/* Configure PF1 as input, triggered on rising edge. */
PORT_ConfigurePins( &PORTF, 0x02, false, false, PORT_OPC_TOTEM_gc, PORT_ISC_RISING_gc );
PORT_SetPinsAsInput( &PORTF, 0x02 );
/* Configure Interrupt0 to have medium interrupt level, triggered by pin 1. */
PORT_ConfigureInterrupt0( &PORTF, PORT_INT0LVL_MED_gc, 0x02);

//Alarm Flag PA0 J3
PORT_ConfigurePins( &PORTA, 0x01, false, false, PORT_OPC_TOTEM_gc, PORT_ISC_BOTHEDGES_gc );
PORT_SetPinsAsInput( &PORTA, 0x01 );
/* Configure Interrupt0 to have medium interrupt level, triggered by pin 1. */
PORT_ConfigureInterrupt0( &PORTA, PORT_INT0LVL_MED_gc, 0x01);

/* Enable medium level interrupts in the PMIC. */
PMIC.CTRL |= PMIC_MEDLVLEN_bm;
/* Enable the global interrupt flag. */
sei();
while (true)
{
}
}