C - pins

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

Hi there,

I am programing an ATtiny26. The circuit is ready and the materials are soldered. My problem is that the 8 data (connected in a parallel data flash) bus pins are not in series due to some pins limitations or peripherals. So I have connected some pins from PortA and some from PortC. for example:
AVR Pin Data bus pin
PB2 0
PB3 1
PA5 2
PA4 3
PB1 4
....
....

Could you suggest me a way to load an unsigned char on the 8 bit data bus in order to send data and an other way to load the 8 bit data bus in the unsigned char (in order to receive data)?

Thanks a lot.

Michael.

User of:
IAR Embedded Workbench C/C++ Compiler
Altium Designer

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

It will be difficult to provide an example you can use directly because you have not provided all 8-bits of the required port pin assignments. So, I'll make up my own assignments.

bit:0 PC2
bit:1 PC3
bit:2 PA5
bit:3 PA4
bit:4 PC1
bit:5 PA1
bit:6 PC7
bit:7 PA3

You'll have to arrange the above bits into two bytes in the following order for a write to PORTA and PORTC.
PA7:X PA6:X PA5:2 PA4:3 PA3:7 PA2:X PA1:5 PA0:X
PC7:6 PC6:X PC5:X PC4:X PC3:1 PC2:0 PC1:4 PC0:X

Let's say the data you want to write is in the variable unsigned char port_out and the unsigned char you'll write to PORTA is port_a_out and the unsigned char you'll write to PORTC is port_c_out.

Here's one way of doing this:


  /* Holds the data to write to the external device */
  unsigned char port_out;

  /* Accumulate the byte to write to PORTA here */
  unsigned char port_a_out;

  /* Accumulate the byte to write to PORTC here */
  unsigned char port_c_out;

  /* Assemble the bits for output to PORTA */
  port_a_out = (port_out >> 4) & (1 << PA3);
  port_a_out |= (port_out >> 4) & (1 << PA1);
  port_a_out |= (port_out << 1) & (1 << PA4);
  port_a_out |= (port_out << 3) & (1 << PA5);

  /* Assemble the bits for output to PORTC */
  port_c_out = (port_out << 1) & (1 << PC7);
  port_c_out |= (port_out >> 3) & (1 << PC1);
  port_c_out |= (port_out << 2) & (1 << PC3);
  port_c_out |= (port_out << 2) & (1 << PC2);
 
  /* Output the bits to PORTA and PORTC */
  /* Set the PORTA bits that should be 1 */
  PORTA |= port_a_out;
  /* Clear the PORTA bits that should be 0*/
  PORTA &= ~port_a_out;

  /* Set the PORTC bits that should be 1 */
  PORTC |= port_c_out;
  /* Clear the PORTC bits that should be 0 */
  PORTC &= ~port_c_out;

I haven't actually compiled and tested this code. There may be other approaches that are more efficient. Before you modify it for your particular port bit positions, get out your 'C Programming' book and work through it by hand. Let me know if it does or does not work.

-Tom

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

Not that it would help you in this particular situation (ATtiny26 with 2K flash), but a generic solution can be implemented with four 256 byte lookup tables. Two of them map the original value to the port A and port C bits; the other two map the incoming port A and port C bits to the combined value. Fast, but expensive in terms of real estate.

Chuck Baird

"I wish I were dumber so I could be more certain about my opinions. It looks fun." -- Scott Adams

http://www.cbaird.org

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

I got it.

Thank you all.

Michael.

User of:
IAR Embedded Workbench C/C++ Compiler
Altium Designer