hlxldb

LED光立方制作全过程(三十一)

0
阅读(1967)

Step 51Software: IO initialization

Software: IO initialization
Software: IO initialization
The first thing the ATmega does after boot, is to call the ioinit() function.

This function sets up IO ports, timers, interrupts and serial communications.
All IO ports on the ATmega are bi-directional. They can be used either as an input or an output. We configure everything as outputs, except the IO pins where the two buttons are connected. The RX pin for the serial line automatically becomes an input when USART RX is enabled.

1) DDRx sets the data direction of the IO pins. (Data Direction Register). 1 means output, 0 means input.

2) After directionality has been configured, we set all outputs to 0 to avid any blinking LEDs etc before the interrupt has started.

3) For pins configured as inputs, the PORTx bit changes its function. Setting a 1 in the PORTx register bit enables an internal pull up resistor. The port is pulled up to VCC. The buttons are connected between the port and GND. When a button is pressed the corresponding PINx bit reads a logic 0.

4) Timer 2 is configured and a timer interrupt enabled. This is covered in a separate step.

5) Serial communications is configured and enabled. This is also covered in a separate step.