PIND4 pull-up not working on attiny2313a

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am so confused that something so simple is not working. As soon as I use an external pull-up resistor the pushbutton on PIND4 works as intended.

 


#define F_CPU   8000000
#define BUAD    9600
#define BRC     ((F_CPU/16/BUAD) - 1)

#include <util/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include <avr/pgmspace.h>
#include <avr/sleep.h>

const uint8_t PROGMEM displayBitmap[] = { 0b0111111, 0b00000110, 0b01011011, 0b01001111, 0b01100110, 0b01101101, 0b01111101, 0b00000111, 0b01111111, 0b01101111 };	//0 to 9

volatile uint8_t timerState = 0;	//0 SET, 1 RUNNING, 2 STOPPED
volatile uint8_t timerSecs = 0;		//Seconds of timer
volatile uint16_t timerMsecs = 0;	//Milliseconds of timer
uint8_t smalltimer = 0;				//Timer used to increment up to one millisecond in the OVF ISR



bool buttonState = 0;			//The state of the button (1 for pushed - 0 for open)
uint8_t timerButton = 0;			//Timer used to debouce the button

uint16_t timerSleep = 0;			//Timer used for sleeping the device after 60 seconds idle

inline void setupUART()
{
	UBRRH = (BRC >> 8);
	UBRRL =  BRC;
	UCSRB = (1 << TXEN);
	UCSRC = (1 << UCSZ1) | (1 << UCSZ0);
}

inline void setupPINS()	//Data direction registers and pull up resistor configuration
{
	GIMSK  = 0b00001000;	//Interrupts
	PCMSK1  = 0b00000011;	//Pin Change Mask Register
	
	DDRA	= 0b00000000;	//PINA0 sensorA input - PINA1 sensor B input
	PORTA	= 0b00000011;
	DDRB	= 0b01111111;	//PINB4..6 shift register control (4 data, 5 latch, 6 clock) - PINB0..3 digit select
	PORTB	= 0b00000000;
	DDRD	= 0b00000110;	//PIND2 beeper output - PIND4 button input
	PORTD	= 0b00010000;
}

inline void setupTIMERS()
{
	TIMSK  = 0b00000010;   //overflow interrupt
	TCCR0B = 0b00000100;   //clock source = CLK/256 , start PWM
	TCCR0A = 0b00000011;   //PWM

	MCUCR |= 0b01110000;  //Power down mode	
}

void SRlatch()
{
	PORTB |= 1 << PINB5;
	_delay_us(1);
	PORTB &= ~(1 << PINB5);
	_delay_us(1);
}

void SRclock()
{
	PORTB |= 1 << PINB6;
	_delay_us(1);
	PORTB &= ~(1 << PINB6);
	_delay_us(1);
}

void SRIN(bool x)
{
	if (x) PORTB |= 1 << PINB4;
	else PORTB &= ~(1 << PINB4);
	_delay_us(1);
}

void SRwriteByte(uint8_t data)
{
	for (uint8_t i=0; i<8; ++i)
	{
		SRIN(data & 0x80);
		SRclock();
		data <<= 1; 
	} 
}

void digitDisplayed(uint8_t d)		// no display	8.8.8.8. accepts 1-4 as input and displayes the said digit
{									// 0			1 2 3 4
	PORTB &= 0xf0;
	PORTB |= (1 << d) >> 1;
}

void beeper(bool x)					//Changes beeper pin to 1 or 0 effectively turning the beeper on and off
{
	if (x) PORTD = 1 << PIND2;
	else PORTD &= ~(1 << PIND2);
}

void BEEP()							//Produces a small BEEP using the beeper
{
	beeper(1);
	_delay_ms(20);
	beeper(0);
}

void buttonAction()
{
	timerSleep = 0;
	BEEP();
}

void buttonCheck(bool& bState, uint8_t& timer, volatile uint8_t* PINX, volatile uint8_t PINXno, const uint8_t msDebounce = 30) //Software debounced button check
{
	if (((*PINX & (1 << PINXno))) == 0)
	{
		if (!bState && timer > msDebounce)
		{
			bState = 1;
			timer= 0;
			
			//Action for button on here
			BEEP();
		}
	}
	else
	{
		if (bState && (timer > msDebounce))
		{
			bState = 0;
			timer = 0;
			
			//Action for button off here
			BEEP();
			_delay_ms(200);
			BEEP();
		}
	}
}

int main()
{
	//setupUART();	//Beeper cannot function using UART - for debuggig purposes
	setupTIMERS();
	setupPINS();
	
	sei();

	BEEP();	
	
	while(1)
    {
		//buttonCheck(buttonState, timerButton, &PORTD, PIND4);
		beeper(!(PIND >> PIND4 & 0x01));
	}
}


ISR (TIMER0_OVF_vect)
{
 	smalltimer++;
	
	if (smalltimer >= 122)
	{
		smalltimer = 0;
		
		if (timerState == 1) ++timerMsecs;
		if (timerMsecs >= 1000)
		{
			timerMsecs = 0;
			++timerSecs;
		}
		
		++timerButton;
		++timerSleep;
	}
}

ISR (PCINT1_vect)
{
	
}

 

Is some of the options I have chosen on the chip preventing the pull-up from being enabled? I dont think it is a problematic chip - I tried it with 3 different chips. Couldn't find something on datasheet so far.

This topic has a solution.

TO THE FINDER... THE ISLE OF KOHOLINT, IS BUT AN ILLUSION... HUMAN, MONSTER, SEA, SKY... A SCENE ON THE LID OF A SLEEPER'S EYE... AWAKE THE DREAMER, AND KOHOLINT WILL VANISH MUCH LIKE A BUBBLE ON A NEEDLE... CAST-AWAY, YOU SHOULD KNOW THE TRUTH!

Last Edited: Thu. Jan 21, 2021 - 08:18 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

I'm losing the pull-up here.
 

void beeper(bool x)					//Changes beeper pin to 1 or 0 effectively turning the beeper on and off
{
	if (x) PORTD = 1 << PIND2;
	else PORTD &= ~(1 << PIND2);
}

You should do this.

	if (x) PORTD |= 1 << PIND2;

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

oh God.. *facepalms* Thank you

TO THE FINDER... THE ISLE OF KOHOLINT, IS BUT AN ILLUSION... HUMAN, MONSTER, SEA, SKY... A SCENE ON THE LID OF A SLEEPER'S EYE... AWAKE THE DREAMER, AND KOHOLINT WILL VANISH MUCH LIKE A BUBBLE ON A NEEDLE... CAST-AWAY, YOU SHOULD KNOW THE TRUTH!