PC3 doesn't switch PC4 on/off

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

If I switch PC3, PC4 doesn't change, why?

Attachment(s): 

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

What AVR model are you using? If it is one with JTAG debugging, then that function may be controlling pins on port C.

Turn off JTAG.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

I don't use JTAG
I use an AVR ATMEGA8

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

How are you switching it - is there a pullup/down on PINC3? The internal pullup is not enabled.

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

yes there is a pull-up resistor on PC3

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

Why do you expect PC4 to change when you switch PC3? I would switch PC4 to get an observable change on PC4.

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

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

PC4 determines the direction of the stepper motor. It should not be so hard, and seperate code works. In combination with the other code (stepping the motor) it doesn't work anymore and this is frustrating...

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

Quote:

doesn't work anymore

Can you tell us more about "doesn't work" ? For the benefit of others what we are talking about is:

	DDRC = 0xF0;
    int test;

	while(1)
	{

	    PORTC = (1<< PC5);		//PC5 high
		_delay_ms(1);			//wait

		PORTC &= ~(1<< PC5);	//PC5 low
		_delay_ms(1);			//wait


		if(PINC&(1<<PINC3)) 	//if PIN3 = 1
		PORTC |=(1<<PC4) ;		//PC4 high
		else 
		PORTC &= ~(1<<PC4) ;    //PC4 low
	}

Personally I would have said that as long as PC3 is connected to 1 then the output on PC4 should be 1 and as long as PC3 is connected to Gnd the output on PC4 should be 0. As we've been told that there is an external pull-up on PC3 the nett effect is that PC4 is going to be at 1 unless PC3 is held low in which case PC4 should reflect this.

So which bit of this picture is not right?

EDIT: SORRY, but I bet you mean:

	    PORTC |= (1<< PC5);		//PC5 high

not

	    PORTC = (1<< PC5);		//PC5 high

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

ok, thanks for the reply. I try to be more specific:
PC4 remains low no matter if I change PC3 from low to high.
The connections are as follow:
PC5-->pulse input to stepper driver
PC4-->direction input to stepper driver
PC3<--input to set direction of stepper driver

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

Did you see my EDIT: above - I'm pretty that IS your problem. You are blasting the entire contents of PORTC with that '=' rather than just setting one bit with '|='. As the setting of PORTC so that only bit 5 is set comes at the very top of the while() which will immediately follow your conditional stuff at the bottom of the while, even if you manage to get PC4 set it will only stay that way for 2 or 3 machine cycles during which the while() loops back.

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

Now it works, indeed the error was:
PORTC |= (1<< PC5); //PC5 high
instead of:
PORTC = (1<< PC5); //PC5 high
Thanks!