Writing to a single output pin/bit without effecting others

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

Microcontroller: ATmega32
Goal: Want to write to output bits without having to overwrite existing bits.
Programmer: AVRisp MkII
Compiler: GCC/Win-AVR
IDE: AVR Studio 4, Version 4.15

How can I write to a single output bit without overwriting the existing values contained in the remaining bits on that particular port?

Example: Assume PORTD is an output and that PORTA has inputs that affect the bits on PORTD. If PORTD = 0b00110001, what process can I use to make PORTD = 0b00110000 without having to overwrite bits 2-8? In other words, is there a way to directly write to just the 1st bit of PORTD without touching the other bits on PORTD?

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

This is a trick question. Even the computer pioneers of the 50s and 60s couldnt solve this problem. John Von Neumann said it was a paradox that couldnt be solved except by Boolean Operations like AND and OR. If you can solve this conundrum, you might be able to move on to more complicated IO operations.

Imagecraft compiler user

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

So, what do I do about it? I take it that you're telling me I have to use Boolean expressions.

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

You really haven't done any basic searching have you? Have a look in the tutorial section.

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

PORTD |= 0b00000001 Sets bit 0
PORTD &= 0b11111110 Clears bit 0
PORTD ^= 0b00000001 toggles bit 0

like the others said, search and you shal find but i'm new here and not quite annoyed enough at this question to refuse answering :)

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

Thanks Nedward!!! I'll give that a shot.

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

Its a good thing the professor didnt give him Chandrikar's Conjecture to prove. Mean trick giving an undergrad a research level problem. When you write a bit into a port, its good if it doesnt Affect the adjacent bits. If you want to Effect the other bits, you might need an emulator.

Imagecraft compiler user

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

ElectronG,

To cut through Bob's sarcasm and wit, he's trying to tell you to look in the "Tutorials" forum of this site. Look for the "Programming 101" thread which gives an excellent rundown of how the OR, AND, XOR and NOT boolean expressions can be used to manipulate bits in registers in different (useful) ways.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!