LED Cube/Custom Programs: Unterschied zwischen den Versionen

Aus Wiki CCC Göttingen
Zur Navigation springen Zur Suche springen
Zeile 33: Zeile 33:
<ol>
<ol>
<li>This first line of code is already quite interesting for someone who never used a microcontroller. If you read the [[LED Cube/Understanding The Electronics]] page, you know that the microcontroller contained the switches we talked about. The situation is actually much more complicated. The microcontroller is not only able to connect a pin to the positive supply voltage or nothing, it is also able to connect it to the negative pole. We will call the positive supply voltage ''Vcc'' and the negative ''GND'' (pronounced ''Ground'').
<li>This first line of code is already quite interesting for someone who never used a microcontroller. If you read the [[LED Cube/Understanding The Electronics]] page, you know that the microcontroller contained the switches we talked about. The situation is actually much more complicated. The microcontroller is not only able to connect a pin to the positive supply voltage or nothing, it is also able to connect it to the negative pole. We will call the positive supply voltage ''Vcc'' and the negative ''GND'' (pronounced ''Ground'').
Now, DDRC is a "register". This means that it has a fixed address in the memory of the microcontroller and is somewhat different from other memory regions. Also, it one byte or 8 bits wide. The difference of this register in particular is, that its value controls how the pins of the microcontroller behave. When each bit is zero, all switches controlled by this register are open. When each bit is one, all switches controlled by this register are closed and connect the pin to either GND or Vcc (more on that later).
</li>
</li>
</ol>
</ol>

Version vom 17. April 2012, 13:08 Uhr

This page introduces you to the software section of the LED Cube. It specifically shows you how you can write your own programs, for example if you do not want to use the pattern program or require additional functionality. This page assumes some basic knowledge about the C programming language and how to use a shell. It does not assume knowledge about the microcontroller architecture or the toolchain.

However, we are not taking care about how this works with Windows. If you want to use Windows anyway, you can figure it out by yourself (yes, it is possible) or get a decent operating system.

Requirements

First of all, you need an AVR Toolchain. You can skip installing avrdude if you do not have a programmer and want to update the program using the bootloader, which is probably what you want. This provides you with

  • avr-gcc: The C compiler for AVR microcontrollers
  • avr-libc: The C standard library for AVR microcontrollers
  • avr-binutils: Various tools for manipulating and working with outputs from avr-gcc

Also, you need the bootloader utility. To get this, you should follow the section Building the Bootloader Application from Building From Source. This time you can skip this step if you are using an external programmer. But this is probably not what you want. After following that step, you have an application called bootloadHID. This is used to update the firmware of your cube.

Getting Started

So, let's now create a first program. This will not do very much: It will simply light up the center LED of the cube - all the time. Create an empty folder, in this folder create a file led-cube.c and paste the following code into it:

// This includes the definitions for DDRC, DDRD, PC4, PD6, PORTC, PORTD:
#include <avr/io.h>

int main(void)
{
	DDRC |= (1 << PC4);	// {1}
	DDRD |= (1 << PD6);	// {2}

	PORTC |= (1 << PC4);	// {3}
	PORTD |= (1 << PD6);	// {4}

	while(1);		// {5}
}

First of all some explanation. The line numbers on the side are the same as the indices in the following enumeration:

  1. This first line of code is already quite interesting for someone who never used a microcontroller. If you read the LED Cube/Understanding The Electronics page, you know that the microcontroller contained the switches we talked about. The situation is actually much more complicated. The microcontroller is not only able to connect a pin to the positive supply voltage or nothing, it is also able to connect it to the negative pole. We will call the positive supply voltage Vcc and the negative GND (pronounced Ground). Now, DDRC is a "register". This means that it has a fixed address in the memory of the microcontroller and is somewhat different from other memory regions. Also, it one byte or 8 bits wide. The difference of this register in particular is, that its value controls how the pins of the microcontroller behave. When each bit is zero, all switches controlled by this register are open. When each bit is one, all switches controlled by this register are closed and connect the pin to either GND or Vcc (more on that later).