Go Down

Topic: Writing directly to ports, now the Diecimila hangs (Read 2061 times) previous topic - next topic


So, tried some direct writing to the ports and I think I screwed up. Now I can't use the serial loading feature... It shouldn't be a problem though, because I believe the programmer I have at work can handle writing the bootloader to the atmega168.

Anyone mind having a look at my setup code? I'd like to know what I did wrong so I don't do it again.

Code: [Select]

DDRD = DDRD || B11111100  
DDRB = DDRB || B00111111  
DDRC = DDRC || B00000001  

void clearDisplay()
 PORTD = B11111100
 PORTB = B00111111
 PORTC = B00000001

void setup()


Port D0 and D1 are used by the uart to so you need to leave them alone. Also, pins 6 and 7 are not available on a standard board on ports B and C

My advice is to stick to as much of the arduino abstractions as you can. For example, you can use the Arduino pinMode commands to set the direction of the pins you have access to (but remeber to leave pin 0 and 1 alone). If you need to set the register bits directly you need to use a mask so that you are only changing the pins you intend.

I am curious to know why you are not using digitalWrite?


I'm interfacing with a display. So my initial thought was direct port access would make it easier to write to the display.

Instead of looping through an array of pins, I'd rather just set the pins high or low.

I should probably take a good, long look at the bitmath tutorial.



you are setting values on the rx and tx pins, that might be confusing things, and looks like you are writing 0 to the reset pin too, in a loop :)

need to & (and) with the existing pin values maybe?


I think it's easier using the arduino digitalWrite than setting pins using directo port IO.
Nothing wrong with looping through an array of pins  ;)

It has been my experience that stuff gets done better and quicker if it is kept as simple as possible. I only optimize when I need to, and use low level code like direct port io as a last resort.


dcb: Is the reset pin bit 6 of Port C?... I thought the only part of the program that looped was the void loop() section... Oh, but every time I set PC6 low, the program resets and executes the code over again. Hahah...

mem: That's great that you think I should stick with using digitalWrite, but I don't really want to. I'd like to understand and be able to work with this device at a deeper level, so when I run into a problem that can be solved more effectively by direct port manipulation, I'll know how to tackle it.

I can see now that I should be setting the bits more carefully.

Code: [Select]
PORTD &= B00000011;
This will turn bits D7..2 Off, while leaving D1 and D0 alone (serial Rx, Tx).

Code: [Select]
PORTD |= B11111100;
Likewise, using the OR operator, I can turn bits D7..2 On.


Oct 10, 2008, 05:37 pm Last Edit: Oct 10, 2008, 05:42 pm by mem Reason: 1
Yes, port C pin 6 is connected to reset.

If you have not already found a copy of the ATmega168 datasheet, you can download a copy here: www.atmel.com/dyn/resources/prod_documents/doc2545.pdf
For what you want to do, it has a lot of information that you will need.


Am I right in thinking that all beatmaster has to do is press his reset button and he will be able to load a new sketch, thereby restoring his Arduino back to a funtioning unit ?.

Go Up