EE26 Digital Logic Systems - Tufts University



EE14 Microprocessor Architecture and Applications

Fall Semester 2011

Notes 16(11/22/2011)

Project #5 Programming HCS12 Microprocessor in C

Implementation due on Monday, December 5 for Monday section and Friday, December 9 for Friday section. The report due a week after.

SUMMARY

The goals of this lab are to get familiar with the Dragon12-Plus-USB board and CodeWarrior IDE, practice programming the microcontroller in C to communicate with and control the peripheral hardwares, i.e., seven segment display, LCD display, Infrared sensor and the onboard speaker.

INTRODUCTION

The Dragon 12-Plus-USB board is a training board for the Freescale HCS12 microcontroller family. The MC9S12DG256 microcontroller consists of a powerful 16-bit CPU, 256K bytes of flash memory, 12K bytes of RAM, 4K bytes of EEPROM and many on-chip peripherals. Check out the Dragon 12-Plus-USB user’s manual and the microcontroller’s datasheet on our course site on Trunk System, under Resource/Tutorials.

Traditionally microcontrollers have been programmed in assembly language. The advantage of assembly language is that it is closest to the hardware and can execute programs most efficiently. The disadvantage is that it is not portable. You have to rewrite your program when changing to a different microcontroller. The trend in recent years is to program microcontrollers in a high-level language – the most popular being C. CodeWarrior is a very powerful and professional IDE. The main feature of CodeWarrior IDE is the source level debugger in assembler and C.

CodeWarrior Special Edition is installed on the PCs in Lab 225. There is a file “HCS12(X) Quick start” on the Trunk Site under Resources/Tutorials which contains a simple CodeWarrior tutorial. Follow the tutorial to practice creating and running a project.

In this lab, you will implement a clock and show it on the 4-digit seven segment display. The 4 digits should display 00:00 first when the program starts to run. As soon as you press one of the four pushbutton switches, the clock starts to tick. Check object presence close to the board with the IR sensor every 10 seconds. If there is an object presents in front of the IR sensor, display “Object Nearby” on the first line of the LCD screen. Otherwise, display “No Object Detected” on the first line of the LCD screen. When objects are detected 10 times, sound the speaker as an alarm and clear the LCD screen.

7-segment diaplays: The Dragon 12-Plus-USB board has four common-cathode 7-segment displays that are connected as shown in Fig. 1. The common cathodes of the four digits are connected to the lower four bits of port P. These bits must be set to zero to enable the 7-segment displays. The segment a, b, c, d, e, f, g and decimal point are driven by the pins 0:7 of port B. In this common-cathode case, an output 1 will turn on a segment and an output 0 will turn it off. If all four bits of PTP[3:0] are zero, then all four digits will display the same segment pattern given by the output of port B. Port B is also connected to the red LEDs. So to use the 7-segment display correctly, enable 7-segment display and disable the LEDs before assigning values to port B.

Below is a list of C function calls for 7-segment displays.

Table 1. C function call for 7-segment displays

|C Function Call |Meaning |

|seg7_enable(); |Enables the 7-segment displays |

|seg7_disable(); |Disables the 7-segment displays |

|seg7_on(int s, int b); |Display the segments s on the 7-segment digit no. b |

|seg7dec(int i, int b); |Display the hex value i on the 7-segment digit no. b |

Pushbutton switches: There are four pushbutton switches, SW2, SW3, SW4 and SW5 on the Dragon 12-Plus-USB board. They are connected to bits 3:0 of port H as shown in Fig. 2. If the switches are not being pressed, the 100kΩ pullup resistors will cause the voltages at pins 3:0 to be 5 volts and therefore a read of port H will read these bits as 1. Closing a switch will cause the input to that bit of port H to be grounded and therefore that bit will read 0 when port H is read.

[pic]

We provide the C function calls for the pushbutton switches in Table 2.

Table 2. C function call for reading switches SW2 – SW5

|C Function Call |Meaning |

|SW_enable(); |Enable switches SW2 – SW5 |

|SW2_down(); |Returns true if SW2 is down |

|SW3_down(); |Returns true if SW3 is down |

|SW4_down(); |Returns true if SW4 is down |

|SW5_down(); |Returns true if SW5 is down |

|SW2_up(); |Returns true if SW2 is up |

|SW3_up(); |Returns true if SW3 is up |

|SW4_up(); |Returns true if SW4 is up |

|SW5_up(); |Returns true if SW5 is up |

IR sensor: The IR transmitter and receiver on the Dragon 12-Plus-USB board are controlled by bits PS3 and PS2 of port S. PS3 is an output that turns on the IR transmitter when it is set to 0. PS2 is an input that goes low when the receiver detects a reflected IR signal from the transmitter. To turn on the transmitter you would set the data direction register of port S to 0x08 using the statement DDRS = 0x08; and then bring PS3 low using the statement PTS = 0.

LCD: The diagram in Fig. 3 shows how the LCD connector on the Dragon 12-Plus-USB board is connected to port K of the microcontroller. Since in this lab, you are programming in C, we provide C callable assembly routines for you to use to display things on the LCD screen.

[pic]

Table 3 shows all available C functions calls for LCD display.

Table 3. C function call for LCD display

|C Function Call |Meaning |

|void lcd_init(void); |Initialize LCD display (clears display) |

|void set_lcd_addr(char); |Set cursor address |

|void data8(char); |Write ASCII character to display at cursor location |

|void instr8(char); |Write instruction to display |

|void clear_lcd(void); |Clear LCD display |

|void hex2lcd(char); |Write hex digit (0 – F) to LCD display |

|char hex2asc(char); |Convert hex digit (0 – F) to ASCII code |

|void type_lcd(char*); |Display ASCII string on LCD display at cursor location |

Before you can write to the LCD, you must initialize it with the lcd_init() function. This function initializes it for 4-bit, 2 line, 5 x 7 dot operation, display on, cursor off, no blinking, then clears the display and sets the cursor to the home position.

You can set the cursor to any position on the display by calling the set_lcd_addr(char ad) function, where ad is an 8-bit hex address whose display position is shown in Fig. 4.

|00 |01 |

|servo54_init(); |Initialize PWM5 with 20 ms period |

|servo76_init(); |Initialize PWM7 with 20 ms period |

|set_servo54(int width); |Set position of servo5 (3300 – 5700) |

|set_servo76(width); |Set position of servo7 (3300 – 5700) |

In this lab, you will be programming the microcontroller in C. There is a CodeWarrior sample project with several examples on Trunk Site under Resources. These examples are to demonstrate how to program the peripheral hardware in C. You are provided with all the

ASSIGNMENT

1. There is an sample project named Examples1_4 posted on the Trunk Site under Resources\Labs. In the project, we provide all the assembly language routines that can be called by the C functions that you are going to use. Try compiling and run the example project on the Dragon 12-Plus-USB board, replace the “main.c” file with other example programs that are provided in the file examples1_4_sources. We included simple programs of controlling the 7-segment displays, button push switches, LCD and PWM.

2. The figure on the right side shows a table of hex codes for displaying hex values with the 7-segment display. Design your own table for a clock display and implement the clock on the 4-digit 7-segment display. Use the left two digits for minutes display and the right two digits for seconds display. To display different digits on the four 7-segment displays, the displays must be multiplexed in time. Refer to example 1c for information about how to multiplex the 7-segment displays.

3. Implement object detection with the IR sensor and display results on LCD screen.

4. Write a small program to sound the speaker. Try to make different sounds out of the speaker.

5. Integrate assignment 2 through 4 to implement the complete project as described in INTRODUCTION.

Lab Report

Write a report individually, follow the “Lab Report Guidelines” on Trunk Site. Include the source code as always.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download