Society of Robots - Robot Forum

Software => Software => Topic started by: Admin on June 19, 2007, 05:09:18 PM

Title: AVR port config
Post by: Admin 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)
Title: Re: AVR port config
Post by: rgcustodio 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.
Title: Re: AVR port config
Post by: Admin 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?
Title: Re: AVR port config
Post by: rgcustodio 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.
Title: Re: AVR port config
Post by: Admin 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?
Title: Re: AVR port config
Post by: dunk 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.
Title: Re: AVR port config
Post by: rgcustodio 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.