Problem with bit's manipulation when using PORTx

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

Hello guys,

 

 

I have been working on a school project using an ATmega324p and I have never experienced this error before.

 

I am trying to use 2 consecutive bits like PC3 and PC4 as an output and to accomplish that i use the DDRC|=(1<<PC3)|(1<<PC4); then i want the PC3 to have "0" and the PC4 to have "1" and i do PORTC|=(1<<PC4); PORTC&=(0<<PC3);. Until this point everything is fine but  when i check the logical state on both pins, they have both the LOW level (0).

 

 

I have already disabled the JTAG fuse bit, I've tried to use an other port and the same thing happens when using the port A, for example. I've already changed my ATmega to a new one, configured the fuse bit all over again (because I'm running it at 16MHz using an external crystal) and the same thing still happen.

 

I already tried to send information using for example PORTC=0b11110111 and it works just fine, but I can use that method because I want to manipulate pin by pin.

Can someone help with this situation? I have already used an ATmega88 and 128p and it never happened.

 

Many thanks for you attention in advance and happy new year!  wink
 

This topic has a solution.
Last Edited: Sat. Dec 30, 2017 - 02:54 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

0<<anything is always 0.
.
The way you clear a bit is with 1<<position but then apply ~ to that to invert it and use &

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

PORTC&=(0<<PC3); will clear the register as the value of (0 << anyshift) is 0x00.

I believe you need PORTC &= ~(1 << PC3);

Also, adding white space in your statements makes for much clearer reading...

David (aka frog_jr)

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

My error happens when I write:

 

DDRC|=(1<<PC3)|(1<<PC4);

PORTC&=(0<<PC3);

PORTC|=(1<<PC4);

 

 

and the PC4 has a 0 anyway. Shouldn't it be 1?

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

frog_jr wrote:

PORTC&=(0<<PC3); will clear the register as the value of (0 << anyshift) is 0x00.

 

So, is the PORTC&=(0<<PC3) that it putting all my PORTC to 0?

 

 

frog_jr wrote:

Also, adding white space in your statements makes for much clearer reading...

 

 

Already edited the text on my 1st post.. Thanks for the advice wink

Last Edited: Sat. Dec 30, 2017 - 02:40 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

MastrusC wrote:
So, is the PORTC&=(0<<PC3) that it putting all my PORTC to 0?

Yes

David (aka frog_jr)

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

frog_jr wrote:

MastrusC wrote:
So, is the PORTC&=(0<<PC3) that it putting all my PORTC to 0?

Yes

@frog_jr  You were right!! It is working now!

Many many many thanks!!!! You just saved my life and my semester yeslaugh

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

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...