//https://www.instructables.com/ATtiny85-Interrupt-Barebones-Example/
/*
* ATTiny85 simple interrupt handling example
*
* Pieced together from various places, but mostly from here:
* https://arduino.stackexchange.com/questions/3929/attiny85-interrupt-id-vs-pin-when-programming-with-arduino
*
* Contributors:
* GisMofx <https://arduino.stackexchange.com/users/11075/gismofx>
* Aaron S. Crandall <[email protected]> - 2019
*
* This code reacts to a Pin Change Interrupt on PB1 which switches an LED on and off on PB4.
* This is an "external interrupt" in some documents and other platforms
*
* To wire this example;
* Connect a switch that connects PB1 (pin 6) of the ATtiny85 to ground when pressed
* Connect an LED with:
* positive terminal (anode - the long lead) to PB4 (pin 3)
* negative terminal (cathode - the short lead) to a resistor (300 to 2k ohms all work)
* Connect resistor between LED and ground bus
* Connect ATtiny85 pin 8 (vcc) to +5 volts
* Connect ATtiny85 pin 4 (ground) to ground
*
* Shared under the Creative Commons Attribution 4.0 International license
* https://creativecommons.org/licenses/by/4.0/
*
*/
// Requires headers for AVR defines and ISR function
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
// Define pins for switch the LED, plus the chosen interrupt for reacting to
#define INTERRUPT_PIN PCINT2 // This is PB2 V_DET
#define BUZZ PB0 // Buzz
#define DATA_SW PB1 // Data Blue Led //PB1
#define V_DET PB2 // Opto couple slide sw ADC1
#define I_DET PB3 // port ACS712 I sensor ADC2
#define CHRG_SW PB4 // Chrg Red Led //SDA
#define RST PB5 // GRN SW RST
#define F_CPU 16000000UL
#define ADC_CH_2 A3
#define ADC_CH_3 A2
/*
* Alias for the ISR: "PCINT_VECTOR" (Note: There is only one PCINT ISR.
* PCINT0 in the name for the ISR was confusing to me at first,
* hence the Alias, but it's how the datasheet refers to it)
*/
#define PCINT_VECTOR PCINT0_vect
// FYI: Variables used within ISR must be declared Volatile.
// static volatile byte LEDState;
// ADC SUPPORT REG
uint8_t temp = 0;
uint16_t u=0, adcValue=0;
// The setup function runs only once when the ALU boots
void setup() {
pinMode(CHRG_SW, OUTPUT);
pinMode(BUZZ, OUTPUT); // Set our chosen CHRG RELAY SW
pinMode(DATA_SW, OUTPUT); // Set our chosen DATA RELAY SW
pinMode(I_DET,INPUT);
pinMode(V_DET,INPUT);
pinMode(V_DET, INPUT_PULLUP);
MCUCR &= (1 << PUD); //disabling Pull Up Disable i.e, enabling pullups, otherwise EXT pull up resistors cannot be enabled in pins
uint16_t ADC_VAl = 0;
char ADC_VAL_Char[5];
analogReference(INTERNAL2V56);
// IO configuration
DDRB &= ~(1 << ADC_CH_2) & ~(1 << ADC_CH_3); //configuring as input assign to ADC2 & 3
PORTB |= (1 << ADC_CH_2) | (1 << ADC_CH_3); // writing 1 to an input pin activates pullup-resistor
DIDR0 |= (1 << ADC_CH_2) | (1 << ADC_CH_3); // disable digital buffer in analog pins to save power
digitalWrite(BUZZ, HIGH); // Blink to show it's booting up and your LED is wired correctly
delay(500);
digitalWrite(BUZZ, LOW);
delay(500);
// Code here is the key piece of configuring and enabling the interrupt
cli(); // Disable interrupts during setup
PCMSK |= (1 << INTERRUPT_PIN); // Enable interrupt handler (ISR) for our chosen interrupt pin (PCINT1/PB1/pin 6)
//GIMSK |= (1 << PCIE); // Enable PCINT interrupt in the general interrupt mask
GIMSK |= (1 << PCIE);
MCUCR |= (0 << ISC01);
MCUCR |= (1 << ISC00);
pinMode(V_DET, INPUT_PULLUP); // Set our interrupt pin as input with a pullup to keep it stable
sei(); //last line of setup - enable interrupts after setup
}
//*************************************************
// // SOURCE : https://controllerstech.com/avr-tutorial-5-how-to-use-adc/
uint16_t ADC_Convert (void)
{
ADCSRA |= 1<<ADSC; // start conversion
while ((ADCSRA&(1<<ADSC)) == 1){} // wait for the conversion to finish
uint8_t adcl = ADCL; // read ADCL register
uint8_t adch = ADCH; // read ADCH Register
uint16_t val = ((adch<<8)|adcl)&0x3FF; // combine into single 10 bit value, 0x3FF-> 0b11 1111 1111
return val;
}
//*********************************************
void loop() {
//*********************************
/*/ SOURCE : https://controllerstech.com/avr-tutorial-5-how-to-use-adc/
uart_init();
ADC_init();
ADC_START_READ()
{
ADC_VAl = ADC_Convert();
sprintf(ADC_VAL_Char, "%u\n", ADC_VAl);
uart_str(ADC_VAL_Char);
_delay_ms(200);
}
//***************************************/
// Put any general processing code in the main loop
// Be aware - if the INT_PIN changes state, the loop will be suspended while the ISR runs to completion
}
// This is the interrupt handler called when there is any change on the INT_PIN
// ISR is defined in the headers - the ATtiny85 only has one handler
ISR(PCINT_VECTOR)
{
cli();
//IF LOW then CHRG PWR ON
if( digitalRead(V_DET) == LOW )
{
digitalWrite(BUZZ, LOW);
digitalWrite(DATA_SW,LOW);
digitalWrite(CHRG_SW,LOW);
delayMicroseconds(1000);
}
//TAB VCC HIGH means THEN CHRG DOWN OTG MODE
else
{
digitalWrite(BUZZ, HIGH);
digitalWrite(DATA_SW,HIGH);
digitalWrite(CHRG_SW,HIGH);
delayMicroseconds(1000);
}
sei();
} // end of isr
//ADC INTR VECTOR SERVICE
// SOURCE :
ISR(ADC_vect)
{
adcValue = ADCW;
//adcValue = (ADCH<<8)|ADCL;
if (adcValue==1023){
int i=1;
while(i++<20){
PORTB|= 1<<PB1;
_delay_ms(100);
PORTB&= ~(1<<PB1);
_delay_ms(100);
}
}else{
PORTB|= 1<<PB1;
} // END ADC VECT INTR
} //end isr adc vect