ATtiny402 2024 Free Christmas Gift

Free Gift to everyone for 2024 Christmas


Code compiled with Microchip Studio

/* * ATtiny402-2024-Christmas-gift-for-free.c * * Created: 9/18/2024 16:46:37 * Author : Sherman Stebbins * PA6 is fireplace * PA7 is Tree * PA1 is lamp */ 
#define F_CPU 10000000UL#include <avr/io.h>#include <util/delay.h>#include <avr/interrupt.h>#include <stdlib.h> //for rand()#define ModeMax 5#define Fireplace PIN6_bm#define Tree PIN7_bm#define Lamp PIN1_bm
volatile uint8_t mode = 0;volatile uint8_t randLoops = 0;volatile uint8_t sleepstate = 0;
void gotosleep(void);
int main(void){ CCP = 0xD8; CLKCTRL_MCLKCTRLB = 0b00000001; //pull up pin 2 and set it to trigger int on falling edge: PORTA.PIN2CTRL = PORT_ISC_RISING_gc; //this was my fix for changing current draw sei(); //Set enable interrupt //set sleep to power down //SLPCTRL.CTRLA = 0b00000101; //sherms way SLPCTRL.CTRLA = SLPCTRL_SMODE_PDOWN_gc | SLPCTRL_SEN_bm; //Microchip studio showed this gotosleep(); //sleep while (1){ //randLoops = (rand() % 100)+20;//3; //random 3-12 loops for display // mode=3; switch(mode){ case 0: PORTA_OUT =  Fireplace; _delay_ms(1500); PORTA_OUT =  Fireplace|Tree; _delay_ms(1500); PORTA_OUT = Lamp|Fireplace|Tree; _delay_ms(10000); PORTA_OUTCLR =  Fireplace|Lamp|Tree; break; case 1: PORTA_OUT =  Fireplace; _delay_ms(3000); PORTA_OUT = Lamp|Fireplace; _delay_ms(3000); PORTA_OUT = Tree|Fireplace|Lamp; _delay_ms(12000); PORTA_OUTCLR =  Fireplace|Lamp|Tree; break; case 2: PORTA_OUT = Lamp; _delay_ms(5000); PORTA_OUT = Tree|Lamp; _delay_ms(5000); PORTA_OUT = Fireplace|Lamp|Tree; _delay_ms(15000); PORTA_OUTCLR =  Fireplace|Lamp|Tree; break; case 3: PORTA_OUT =  Tree; _delay_ms(3000); PORTA_OUT = Lamp|Tree; _delay_ms(3000); PORTA_OUT = Fireplace|Lamp|Tree; _delay_ms(20000); PORTA_OUTCLR =  Fireplace|Lamp|Tree; break; case 4: PORTA_OUT = Fireplace; _delay_ms(4000); PORTA_OUT = Tree|Fireplace; _delay_ms(25000); PORTA_OUTCLR =  Fireplace|Lamp|Tree; break; case 5: PORTA_OUT =  PIN7_bm|PIN6_bm|PIN1_bm; _delay_ms(30000); PORTA_OUTCLR =  Fireplace|Lamp|Tree; break;
default: break; // mode=3; mode= (rand() % ModeMax); //Randomly select next mode/* if(mode++ > ModeMax-1){ mode=0; }*/ gotosleep(); }}
void gotosleep(void){ PORTA.DIRSET = 0b00000000;
sei(); SLPCTRL.CTRLA = 0b00000101; //set power down and sleep enable asm("sleep"); //sleep PORTA_DIRSET = PIN1_bm | PIN3_bm | PIN6_bm | PIN7_bm;

}
ISR(PORTA_PORT_vect){ if(sleepstate){ sleepstate=0; } //PORTA.INTFLAGS = 0x04; //clear int flag, from Ben Heck.. PORTA.INTFLAGS = PORT_INT2_bm; // got from iotn402.h}