0

Given that I accomplished shrinking size of my project and with huge help here and on arduino forums, basically rewrited to atmel studio, my "fire cat" displays all(but 1 led, this was problem before idk, sometimes works sometimes not, not that big of a problem) leds, problem now is I have a button connected on Attiny13a PB3, in theory and simulator everything works, but then IRL it doesnt, like it cant even read the input pin

#include <stdbool.h>
#include <string.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/atomic.h>

#define bit_is_set(sfr, bit) (_SFR_BYTE(sfr) & _BV(bit))
#define bit_is_clear(sfr, bit) (!(_SFR_BYTE(sfr) & _BV(bit)))

#define INPUT 0x0f
#define LOW   0x00
#define HIGH  0x01

// Configure Timer 0 to count milliseconds, assuming F_CPU = 8 MHz.
static void init_timer() {
    OCR0A  = 125 - 1;     // period = 125*64 = 8000 CPU cycles
    TCCR0A = _BV(WGM01);  // CTC, TOP = OCR0A
    TCCR0B = _BV(CS00)    // clock @ F_CPU/64
    | _BV(CS01);   // ditto
    TIMSK0 = _BV(TOIE0);  // enable timer overflow interrupt
}

volatile uint16_t millis_counter;

// Count a millisecond on each timer overflow.
ISR(TIM0_OVF_vect) {
    ++millis_counter;
}

// Read the milliseconds counter avoiding race conditions.
static uint16_t millis() {
    uint16_t millis_counter_copy;
    ATOMIC_BLOCK(ATOMIC_FORCEON) {
        millis_counter_copy = millis_counter;
    }
    return millis_counter_copy;
}

static void setup() {

    PORTB |=  _BV(PB3);
    DDRB  &= ~_BV(PB3);
    PORTB |=  _BV(PB3);  // pullup on PB3
    init_timer();
    sei();
}

// Set a pin as either INPUT, LOW or HIGH.
static void set_pin(uint8_t pin_mask, uint8_t state) {
    if (state == INPUT) {
        DDRB   &= ~pin_mask;     // INPUT
        PORTB &= ~pin_mask;       // no pullup
        } else {
        if (state)
            PORTB |= pin_mask;       // HIGH
        else
            PORTB &= ~pin_mask;  // LOW
        DDRB |= pin_mask;            // OUTPUT
    }
}

static void charlie(uint8_t a[])
{
    set_pin(_BV(PB0), a[0]);
    set_pin(_BV(PB1), a[1]);
    set_pin(_BV(PB2), a[2]);
    set_pin(_BV(PB4), a[3]);
}

static uint16_t oldTime = 0;
static bool holdButt = false;
static uint16_t holdTime = 0;
static uint16_t shortPressATime = 0;
static bool kozichOn = true;

void loop() {

    static uint8_t koz1[4] = {INPUT,LOW,HIGH,INPUT};

    static uint8_t koz2[4] = {INPUT,HIGH,LOW,INPUT};/*
    memcpy(koz2, koz1, sizeof(koz1[0])*4);
    koz2[1] = HIGH;
    koz2[2] = LOW;
*/
    static uint8_t koz3[4] = {LOW,HIGH,INPUT,INPUT};
    static uint8_t koz4[4] = {HIGH,LOW,INPUT,INPUT};;/*
    memcpy(koz4, koz3, sizeof(koz3[0])*4);
    koz4[0] = HIGH;
    koz4[1] = LOW;
*/
    static uint8_t koz5[4] = {LOW, INPUT,INPUT,HIGH};
    static uint8_t koz6[4] = {HIGH, INPUT,INPUT,LOW};/*
    memcpy(koz6, koz5, sizeof(koz5[0])*4);
    koz4[0] = HIGH;
    koz4[3] = LOW;
*/
    static uint8_t cap[4] = {INPUT, HIGH,INPUT,LOW};

    static uint8_t oko1[4] = {LOW, INPUT,HIGH,INPUT};
    static uint8_t oko2[4] = {HIGH, INPUT,LOW,INPUT};/*
    memcpy(oko2, oko1, sizeof(oko1[0])*4);
    oko2[0] = HIGH;
    oko2[2] = LOW;
*/



    uint16_t Ctime = millis();
    uint16_t diffT = Ctime - oldTime;
    oldTime = Ctime;



    if (bit_is_clear(PINB, PB3))
    {
        holdButt = true;
        holdTime = holdTime + diffT;
    }
    else if (holdButt && bit_is_set(PINB, PB3)){
        holdButt = false;
        if(holdTime > 1000){
            kozichOn = !kozichOn;
        }
        else
        {
            shortPressATime = 5000;
        }
        holdTime = 0;
    }

    if(shortPressATime > 0)
    {
        shortPressATime = shortPressATime - diffT;
        if(shortPressATime & 0x100){
            charlie(oko1);
            charlie(oko2);
        }
    }
    else{       
        charlie(oko1);
        charlie(oko2);
    }
    if(kozichOn){
        charlie(koz1);
        charlie(koz2);
        charlie(koz3);
        charlie(koz4);
        charlie(koz5);
        charlie(koz6);
        charlie(cap);
    }
    DDRB &= ~(_BV(PB0) | _BV(PB1) | _BV(PB2) | _BV(PB4));
}

int main(void) {
    setup();
    for (;;)
    loop();
}
| improve this question | | | | |
  • Both INPUT and LOW are defines as 0x0, so in your code no pin is ever set to OUTPUT LOW. Try creating a very simple sketch that just does the button reading, and turning on an led when pressed. That way you can see if there is actually a problem with your pull-up or not. – Gerben Dec 15 '19 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.