Solved: Am I going crazy?!

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

I'm pretty well experienced in AVR and I have a 328 project with some odd behavior.  Any time I have an if statement that checks a pin, it will disable the port that is enabled with the pullup.  It's been a minute, but I've done this 100 times and can't figure this one out.

 

int main(void)
{
	DDRB = 0b00000001;    //Configure data directions (1 = output)
	DDRC = 0b00000000;
	DDRD = 0b00000000;

	PORTB = 0b11000000;   //Pullups on brightness controls
	PORTC = 0b00000000;
	PORTD = 0b00000000;

	PCICR |= 1<<PCIE0;    //enable Pin change interrupts controller 0
	PCMSK0 |= 1<<PCINT6;  //Enable interrupt on PCINT6
	PCMSK0 |= 1<<PCINT7;  // Enable Interrupt on PCINT7

	sei(); //set interrupts

	While (1)
	{

	}

ISR(PCINT0_vect)//interrupt routine
  {

	  if (!(PINB &= 1<<PINB6)) //Brightness Down selected
	    {
	      _delay_ms(25);
		  if (!(PINB&= 1<<PINB6))
		     {
				if(BrightnessValue >= 5)
				{
			      BrightnessValue = BrightnessValue - 5;
				}
				else
				{
				   BrightnessValue = 0;
				}
			 }
		}
	

This results in the corresponding port going low.  I'm using debugwire and a 328p  I have narrowed it to the if statement.

 

I don't get it.

Last Edited: Mon. Dec 30, 2019 - 09:49 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Are you saying the DDRB resets to 0x00?

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

No, When the if statement runs lets say for pinB6, PortB6 goes to 0. or from 0b11000000 to ob10000000

 

when the if statement is for PINB7, you end up with PortB = ob01000000.

 

it kills the pullup.

 

It doesn't make any sense.

Last Edited: Mon. Dec 30, 2019 - 05:28 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It makes perfect sense. You are assigning to the PIN register which toggles the corresponding bit in the PORT register, toggling the state of the pullup.

github.com/apcountryman/build-avr-gcc: a script for building avr-gcc

github.com/apcountryman/toolchain-avr-gcc: a CMake toolchain for cross compiling for the Atmel AVR family of microcontrollers

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

They are 2 different registers.  You read from the pin, you assign the pullup from the port when the DDR is set as an input.

 

An if statement on the pin register should not change the register on the Port.

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

You are writing to the PIN register in your if statements (bitwise AND assignments which I'm geussing should have just been bitwise ANDs) which, as I said, toggles the masked bits in the PORT register. From the datasheet: "Writing a '1' to PINxn toggles the value of PORTxn, independent of the value of DDRxn. The SBI instruction can be used to toggle one single bit in a port."

github.com/apcountryman/build-avr-gcc: a script for building avr-gcc

github.com/apcountryman/toolchain-avr-gcc: a CMake toolchain for cross compiling for the Atmel AVR family of microcontrollers

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

You're right.  That fixed it.

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

Don't you ONLY want to read the pin?...why are you ALSO changing it ( &= in your if)? get rid of =

 

writing a logic one to a bit in the PINx Register, will result in a toggle in the corresponding bit in the Data Register.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

Please do not modify the code in the OP, but post the corrected code in a new comment. Changing the OP code confuses the new reader as the comments after no longer make sense.

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

Also, modifying the title doesn't actually mark the topic as solved to the forum software.

 

@ chriscalandro : Please see Tip #5 (in my signature, below; may not be visible on mobile) for how to mark a topic as resolved ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...