C8051F04x-DK
4
Rev. 0.4
5. Example Source Code
Example source code and register definition files are provided in the “
SiLabs\MCU\Examples\C8051F04x
” directory
during IDE installation. These files may be used as a template for code development. Example applications include
a blinking LED example which configures the green LED on the target board to blink at a fixed rate. A Controller
Area Network (CAN) application example is also included with the C8051F04x development kit.
5.1. Register Definition Files
Register definition files
C8051F040.inc and C8051F040.h
define all SFR registers and bit-addressable control/
status bits. They are installed into the “
SiLabs\MCU\Examples\C8051F04x
” directory during IDE installation. The
register and bit names are identical to those used in the C8051F04x data sheet. Both register definition files are
also installed in the default search path used by the Keil Software 8051 tools. Therefore, when using the Keil 8051
tools included with the development kit (A51, C51), it is not necessary to copy a register definition file to each
project’s file directory.
5.2. Blinking LED Example
The example source files
blink.asm
and
blinky.c
show examples of several basic C8051F04x functions. These
include; disabling the watchdog timer (WDT), configuring the Port I/O crossbar, configuring a timer for an interrupt
routine, initializing the system clock, and configuring a GPIO port. When compiled/assembled and linked this pro-
gram flashes the green LED on the C8051F040 target board about five times a second using the interrupt handler
with a C8051F040 timer.
5.3. Controller Area Network (CAN) Application Example
Configuration and use of the CAN controller is documented in the Bosch CAN User’s Guide, located in the “
Documen-
tation
” directory on the CD-ROM. Accessing the CAN controller (i.e., accessing the CAN RAM, CAN registers, and
moving data to and from the CAN controller) is documented in Silicon Laboratories’ C8051F04x data sheet.
An example CAN application is included in the “
Examples\C8051F04x
” directory. Each C8051F040 target board fea-
tures a push button (labeled P3.7) and a LED (labeled P1.6). After the two target boards are connected together via
the provided CAN bus physical layer (i.e. cable, connectors, and CAN transceiver), the example application sends
CAN messages between the two target boards containing the state of the push buttons. In this example, each CAN
controller has two of the 32 message objects configured: one to send a control signal based on the state of its target
board push button, and one to receive a control signal from the other target to see if it should turn on/off its own LED.
When a target board receives a message that the push button on the other target board is depressed, it lights its own
LED. When a target board receives a message that the push button on the other target board is not depressed, it
turns off its own LED. In this way, the push button on one target board controls the LED on the other target board as
a virtual control link via a CAN bus.
5.3.1. Setting-up the Application
1. Connect the target boards together at the CAN DB-9 connectors using the CAN cable provided in the
development kit, as shown in Figure 2 on page 5. The correct cable has a male connector on both ends.
Take care not to connect the CAN cable to the RS232 DB-9 connector. See Figure 3 on page 6 for the
location of the CAN DB-9 connector.
2. Compile and link the can1.c example located in the “
Examples\C8051F04x
” directory on the CD-ROM.
Choose one of the target boards as Target Board #1. Connect to Target Board #1 and download the can1
project to the C8051F040, following the steps outlined in Section 4.4 on page 2. Once downloaded, close
this project in the IDE and disconnect the EC2 from Target Board #1.
3. Connect the EC2 to the other target board, Target Board #2. Open a new project in the IDE and load
can2.c into the C8051F040 device, just as was done in step 2 for Target Board #1. Take care not to load
can1.c into both devices. Disconnect the EC2 from this board.