Problem with reading pb2 but works fine with pb0 on attiny 85?

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

I am trying to use an attiny85 to filter through noise and wait for pb2 to go high for at least 2.25ms, then after this condition has occurred, as soon as pb2 is sampled low i want it to execute something.

 

This code works perfectly fine when replacing the two instances of pb2(or pinb2) with pb0(or pinb0), but when using pb2 it does not function at all.

 

This code uses timer 0 to check PB2 every 125μs using a timer0 compare isr.

once noise count = 18 or greater (2.25ms) it will hold its value of 18 and wait for it to go low on the next sample to turn on the test indicator led

if the input pulse is shorter than 18 counts, it resets the counter.

 

Tested twice each time, once with 5v wire connected to input at start, then disconnect to turn on test led. The second time with the 5v line disconnected at start (led off confirmation), then connect the 5v wire, then disconnect to light test led.

 

For the life of me I cannot understand why this wont work with pb2 but works fine with pb0, any help would be much appreciated, as i need pb2 for int0.

Tried using another attiny85 as well with the same results, and tested the "if (bit_is_set(PINB,PB2))" command alone in a separate test code in the while loop without the timer and it works fine on both attiny85s on pb2 and pb0. ( can post this test if needed)

does it have anything to with with the fact that pb2 is an adc while pb0 isnt? No clue at this point >.<

 

I tried to remove everything unnecessary to recreate the problem to post.

here is the shortened and re-tested working code with pb0

problem occurs when i change "DDRB &= ~(1 << PINB0);" to have PINB2, and when i change " if(bit_is_set(PINB,PB0))" tp have PB2

 

I really hope i am not missing something silly here.

Using Atmel Studio 7.0

 

#define F_CPU 8000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

volatile char noisecheck = 1;
volatile char noisecount = 0;
volatile char noisegood = 18; //(24times total in 3 ms with 125 us sample rate)
volatile char pinstate = 0;

int main(void)
{
	DDRB &= ~(1<<PINB0); // setpinb0 to input
	DDRB |= 1 << PINB1; //set pinb1 to output indicator
	PORTB &= ~(1 << PINB1); // turn off/clear indicator pin

	PORTB |= 1 << PINB1;
	_delay_ms(500);                   //toggle test led 1
	PORTB &= ~(1 << PINB1);
	_delay_ms(500);
	
			TCCR0A |= 1 << WGM01; //enable ctc (move if always on to only run once)
			TCCR0B |= 1 << CS01; //set prescaler to /8
			OCR0A = 125; //sample timer interrupt compare value set to sample every 125 microseconds
			TIMSK |= 1 << OCIE0A; //enable compare interupt
			sei();
	
    while (1) 
    {
    }
}

ISR(TIMER0_COMPA_vect)
{
	
	if(bit_is_set(PINB,PB0))
	{
		pinstate = 1;
	}
	else
	{
		pinstate = 0;
	}
	
	
	if(noisecheck == 1)
	{
		if (pinstate == 1)
		{
			
			noisecount ++;
			if (noisecount >= noisegood)
			{
				
				noisecount = noisegood;
			}
		}
		else
		{
			if (noisecount >= noisegood)
			{
				PORTB |= 1 << PINB1; //turn on test led to verify first stage compelete

			}
			noisecount = 0;
		}
	}
	


}

 

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

Here comes the really weird part!
If i place "PORTB |= 1 << PINB1; //turn on test led to verify first stage compelete" inside of 
"if(pinstate ==1)
{
if (noisecount >= noisegood)

{HERE}

}"

 

then the code functions just fine using the two stage test i described with bost pb0 and pb2!

I am just baffled O.O

 

Here is the described and tested code showing this phenomenon.

 

#define F_CPU 8000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

volatile char noisecheck = 1;
volatile char noisecount = 0;
volatile char noisegood = 18; //(24times total in 3 ms with 125 us sample rate)
volatile char pinstate = 0;

int main(void)
{
	DDRB &= ~(1<<PINB2); // setpinb0 to input
	DDRB |= 1 << PINB1; //set pinb1 to output indicator
	PORTB &= ~(1 << PINB1); // turn off/clear indicator pin

	PORTB |= 1 << PINB1;
	_delay_ms(500);                   //toggle test led 1
	PORTB &= ~(1 << PINB1);
	_delay_ms(500);
	
			TCCR0A |= 1 << WGM01; //enable ctc (move if always on to only run once)
			TCCR0B |= 1 << CS01; //set prescaler to /8
			OCR0A = 125; //sample timer interrupt compare value set to sample every 125 microseconds
			TIMSK |= 1 << OCIE0A; //enable compare interupt
			sei();
	
    while (1) 
    {
    }
}

ISR(TIMER0_COMPA_vect)
{
	
	if(bit_is_set(PINB,PB2))
	{
		pinstate = 1;
	}
	else
	{
		pinstate = 0;
	}
	
	
	if(noisecheck == 1)
	{
		if (pinstate == 1)
		{
			
			noisecount ++;
			if (noisecount >= noisegood)
			{
					PORTB |= 1 << PINB1; //turn on test led to verify first stage compelete
				noisecount = noisegood;
			}
		}
		else
		{
			if (noisecount >= noisegood)
			{
				

			}
			noisecount = 0;
		}
	}
	
}