go_away

Author Topic: AVR port config  (Read 6477 times)

0 Members and 1 Guest are viewing this topic.

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,665
  • Helpful? 169
    • Society of Robots
AVR port config
« on: June 19, 2007, 05:09:18 PM »
How do I set individual ports on the AVR to be output pins without doing a bit mask?

for example, this is my current code:
Code: [Select]
//configure ports for input or output - specific to ATmega8
void configure_ports(void)
{
DDRC = 0x00;  //configure all C ports for input
PORTC = 0x00; //make sure pull-up resistors are turned off
DDRD = 0xFF;  //configure all D ports for output
//PORTB0 =1;//???
//PORTB6 =1;//???
//PORTB7 =1;//???
}

and the pinout
http://www.societyofrobots.com/images/sbs_avr_schematic.png

The reason I dont want to do a DDRB = whatever mask is because I am afraid of messing up the MISO/MOSI stuff that is also on PORT B (a mistake could prevent me from doing future programming on my AVR)

Offline rgcustodio

  • Robot Overlord
  • ****
  • Posts: 217
  • Helpful? 0
  • Use "Search" and ye might find answers!
Re: AVR port config
« Reply #1 on: June 19, 2007, 05:17:13 PM »
Bit masking is the way to go. The register is setup like that by Atmel so we have no choice.

I think you don't have to worry about messing up MISO and MOSI (ie setting them up both as inputs or as outputs). Before the ISP programmer (and software) starts programming, the MCU is reset (RESET line goes low). So any settings you make in your program should not affect SPI programming.

What you should be wary of is setting the RESET line as an I/O pin in the fuse bit settings. This will render your MCU un-programmable by SPI. High-voltage programming will be your last option.
The best thing one can do when it's raining is to let it rain. - H. W. Longfellow

understanding is the path to enlightenment

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,665
  • Helpful? 169
    • Society of Robots
Re: AVR port config
« Reply #2 on: June 19, 2007, 05:57:14 PM »
whoa!

ok so I can use those ports both for the programmer and servos without interference issues?

Offline rgcustodio

  • Robot Overlord
  • ****
  • Posts: 217
  • Helpful? 0
  • Use "Search" and ye might find answers!
Re: AVR port config
« Reply #3 on: June 19, 2007, 06:11:35 PM »
Ooops sorry to get your hopes up, I need to rephrase my statements.

You can set them up one at a time only. You can program them as I/O in your application. Then when you want to reprogram the MCU via ISP, the protocol resets the robot and uses MISO and MOSI as specified in ISP programming. You can not however, program these pins as I/O then use them as MISO and MOSI at the same time.

Quote
ok so I can use those ports both for the programmer and servos without interference issues?
Actually, behind the scenes, the MCU is reset before programming. When the MCU is reset your application is not running so the ISP programmer/software can use the MOSI and MISO pins as expected.
The best thing one can do when it's raining is to let it rain. - H. W. Longfellow

understanding is the path to enlightenment

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,665
  • Helpful? 169
    • Society of Robots
Re: AVR port config
« Reply #4 on: June 19, 2007, 06:21:19 PM »
Quote
You can not however, program these pins as I/O then use them as MISO and MOSI at the same time.
yea I understood that part

So it just occured to me that if I output stuff to the MISO/MOSI lines, wouldnt that damage the programmer?

Offline dunk

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 1,086
  • Helpful? 21
Re: AVR port config
« Reply #5 on: June 19, 2007, 06:23:08 PM »
i find it good practice to put jumpers between the MISO and MOSI pins and any other electronics you are attaching to them.
thus way when you program, you can remove the jumpers and guarantee the other electronics in the circuit are not interfering with the programing.
in Admin's case where you want to connect a servo to that pin simply unplugging the servo before programming will have the same effect...

dunk.

Offline rgcustodio

  • Robot Overlord
  • ****
  • Posts: 217
  • Helpful? 0
  • Use "Search" and ye might find answers!
Re: AVR port config
« Reply #6 on: June 19, 2007, 06:28:00 PM »
So it just occured to me that if I output stuff to the MISO/MOSI lines, wouldnt that damage the programmer?

Not that I know of. It "might" damage other electronics on the MISO/MOSI line but it won't damage the programmer.

I have a Butterfly (AVR ATmega644 MCU) which has an LCD that is connected to the MISO/MOSI lines. When I program the Butterfly the LCD flickers :) I use the AVR ISP MkII.

dunk's suggestion is spot on, but then again you'll have  to remember to remove jumpers when programming. It's a bit tedious but it should protect your electronics.
The best thing one can do when it's raining is to let it rain. - H. W. Longfellow

understanding is the path to enlightenment

 


Get Your Ad Here