Table of Contents

Exercises

Exercise 1

For this exercise you will use the CAN board with the USB_CAN.hex program (board 1) and a second CAN board (board 2). Board 2 is similar to board 1, except that it is not programmed yet. You will also use the USB-CAN-Terminal program for the visualisation of CAN communication.

Figure 1.21: SetupFigure 1.21: alternative

Figure 1.21: Setup and alternative

The left picture shows the set-up you will use for this assignment. CAN-messages will be visualised by the CAN-USB Terminal program. The right picture shows an alternative set-up. CAN-messages could also be displayed on a LCD. Important: Follow the instructions given in “Testing the board” regarding power sources! Use the jumper TERM on board 2 to enable the terminating resistance! Use the jumpers JP1 on board 1 and board 2 to power board 2 over the CAN bus! Assignment: Use the provided 10-way ribbon cable to connect the two boards. Make sure to position the connector in the right way, as indicated by a “1” on the board. The red wire has to be connected with the pin marked by “1”. Program the Atmega32 on board 2 to send an arbitrary CAN message of your own choice. Use the “Code example for sending a CAN message”. Compile your source code and flash the program into the Atmega32. Use the CAN-USB Terminal program as described in “How to use the CAN-USB-Terminal” to display any CAN mes- sage received by board 1.

#include  <avr/io.h>
#include  <avr/pgmspace.h>
#include  <util/delay.h>
#include  "can.h"
 
int  main(void)
{
	//  insert  your  code  here
	while  (1)
	{
		//  insert  your  code  here
	}
}

Exercies 2

For this exersise you will use the CAN board with the USB_CAN.hex program (board 1) and a second CAN board (board 2), as in exercise 1. Assignment: Program the Atmega32 on board 2 to be able to receive a CAN message. Use the CAN-USB Terminal program to send CAN messages from board 1 to board 2. Create a structure of the type “can_t” for received messages. Use suitable methods provided by “can.h” such as “can_check_message” and “can_get_message” to store the content of the CAN message into this structure. Use the method “getled”, as provided by “led.h” and shown in “Code example for LED” to switch on a LED of your choice, depending on the content of the received message. Make sure to include the header file “led.h”.

Example: Board 1 sends a CAN message “aabb”. LED 1 on board 2 is being switched on. Board 1 sends a CAN message “ccdd”. LED 1 on board 2 is being switched off.

Exercies 3

For this exersise you will use two CAN boards, neither of which will be con- nected to the PC. Simply unplug the USB cable from board 1 and use the AC/DC- adaptor to power board 1. The boards will stay connected via the ribbon cable. Board 2 will be powered indirectly over the CAN bus.

Assignment: Merge your program from exercise 2 with your program from ex- ercise 1, so that it can both send and receive messages. The task is to flash both boards with the same program and establish communication between them. In the while-loop of the main-function, poll the states of pins PD6 and PD7 to detect if buttons KEY1 or KEY 2 are being pressed. Make sure to activate the pull-up resistors on these pins (outside the while-loop). Otherwise the pins will be “floating” without a definite state. If button KEY1 is being pressed, send a CANmessage that activates a LED on the other board. If button KEY2 is being pressed, send a CANmessage that deactivates the same LED on the other board.

Example:

Board 1 sends a CAN message “aabb”. LED 1 on board 2 is being switched on.

Board 1 sends a CAN message “ccdd”. LED 1 on board 2 is being switched off.

Board 2 sends a CAN message “aabb”. LED 1 on board 1 is being switched on.

Board 2 sends a CAN message “ccdd”. LED 1 on board 1 is being switched off.