C8051F04x-DK
4 Rev. 0.6
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 C8051F
04
x 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.
C8051F04x-DK
Rev. 0.6 5
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 6. 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 7 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 Debug Adapter from Target Board #1.
3. Connect the Debug Adapter 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 Debug Adapter from this board.
You should now have can1.c loaded into Target Board #1, and can2.c loaded into Target Board #2. The CAN cable
should be connected to both boards at the CAN DB-9 connectors.
5.3.2. Running the Application
1. Start the application by resetting the device on each target board. Do this by depressing the RESET push
button on each target board. As can1.c executes on Target Board #1, and can2.c runs on Target Board #2,
the devices are now nodes on a CAN bus.
2. Pressing the P3.7 push button on Target Board #1 will light the LED on Target Board #2. Likewise, when
the push button on Target Board #1 is released, the LED on Target Board #2 will turn off. This will work on
either target board.
Once this example is running, you have established a simple CAN network with two nodes. When one C8051F040
device senses the push button on its target board is pressed, it sends a “0x11” in the first byte of a CAN message
data field. When the button is released, the first byte of the CAN message data field is “0x00”. When a C8051F040
device receives a message, it checks whether this byte has a value of “0x11” or “0x00”. When the byte is “0x11”,
the device turns on its LED by setting P1.6 high. When the byte is “0x00”, the device turns off its LED by setting
P1.6 low.
C8051F04x-DK
6 Rev. 0.6
You may run the example with the Debug Adapter connected to view CAN registers, and CAN message objects in
CAN RAM. While connected to one target board, run the code. Depress the RESET button on the other target
board. You may use debug and view features of the Silicon Laboratories IDE and on-chip debug logic. To view the
CAN SFRs, click View
Debug WindowsSFRsCAN0. To view message objects in CAN RAM, click
View
Debug WindowsCAN0 Message Registers. To view SFRs and message registers, the device must be
in a halt state to update the debug view windows.
Important Note: To view a Message Object in the CAN Message Registers window, you must set its Message Valid
bit to 1 in the Message Object's associated Message Arbitration 2 Register (Bit 15, ARBT2). This can be done in
code by configuring the IF1 and IF2 registers to set the associated Message Objects’ ARBT2 register. A second
method to set this bit is available while viewing the Message Object registers in the IDE CAN0 Message Registers
view. Click on, and change, the associated Message Objects’ ARBT2 register directly. Working in the background
the IDE will set the register for you via the IF1 and IF2 registers.
Figure 2. CAN Application Hardware Setup
Serial or USB Port
Debug
Adapter
Ribbon
Cable
Target
Board
PC
CAN
JTAG
RS232
Target
Board
AC/DC
Adapter
RS232
JTAG
CAN
Serial or USB
Cable
Serial
Cable
AC/DC
Adapter

C8051F040DK-E

Mfr. #:
Manufacturer:
Silicon Labs
Description:
C8051F04X EVAL BRD
Lifecycle:
New from this manufacturer.
Delivery:
DHL FedEx Ups TNT EMS
Payment:
T/T Paypal Visa MoneyGram Western Union