I'm trying controll the power of attity13a and turn off/turn on the led.
I'm going to goals:
- After to click on the button0 led will blicking 5 second nad then the attiny13a going to sleep.
- After to click on the button1 then wake attiny13a up and then led will shane.
The button0 to sleep and blink the led.
the button1 to wake up and turn on the led.
Could you explain what is wrong in this sketch, please?
thank you for your answers.
code:
#define F_CPU 1200000UL
#define LED PB2 // PB2
#define BUTTON1 PB1 // PCINT1
#define BUTTON0 PB0 // PCINT0
#define HIGH 1
#define LOW 0
#define INPUT 0
#define OUTPUT 1
#define INPUT_PULLUP 2
#define FALSE 0
#define TRUE 1
#define _BV(bit) (1 << (bit))
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <util/delay.h>
void pinMode(uint8_t pin, uint8_t mode)
{
if(mode == OUTPUT) // Pin as output
DDRB |= _BV(pin);
else // Pin as input or input pullup
{
DDRB &= ~_BV(pin); // Set pin as input
if(mode == INPUT_PULLUP)
PORTB |= _BV(pin); // Enable pullup resistors
}
}
void digitalWrite(uint8_t pin, uint8_t val)
{
if(val)
PORTB |= _BV(pin); // Set pin high
else
PORTB &= ~_BV(pin); // Set pin low
}
uint8_t digitalRead(uint8_t pin)
{
return !!(PINB & _BV(pin));
}
// go to sleep
ISR(PCINT0_vect)
{
if(digitalRead(BUTTON0)){
int i=0;
while(i<10){
blink();i=i+1;
}
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_cpu();
}
}
//wake up
ISR(PCINT1_vect){
if(digitalRead(BUTTON1)){
digitalWrite(LED, HIGH);
}
}
void blink(){
digitalWrite(LED, LOW);
_delay_ms(500);
digitalWrite(LED, HIGH);
_delay_ms(500);
}
int main(void)
{
pinMode(BUTTON0, INPUT_PULLUP);
pinMode(BUTTON1, INPUT_PULLUP);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
// Sets the Pin change interrupt mask PB0, PB1
PCMSK |= _BV(PCINT0)|_BV(PCINT1);
//Enables Interrupts globally after setup
//sei();
SREG |= _BV(SREG_I);
while(TRUE){}
}
blink()
lacks a delay in theHIGH
state, and the tiny13 has noPCINT1_vect
. – Edgar Bonet Aug 10 '19 at 11:35