Académique Documents
Professionnel Documents
Culture Documents
PROGRAM STRUCTURE
Dr. Tran Thanh Hung Department of Automation Technology, College of Engineering, Can Tho University Email: tthung@ctu.edu.vn
Review
For software development, we have look on: super loop architecture in Seminar 1 switch interface in Seminar 2 Are there other factors we need to consider?
Review
No company can remain in business for a long time if it creates new code for every project, from the beginning Code must be reusable Embedded systems have a very long lifespan and need to be maintained or updated, BUT: - Programmers change very often - No programmer can remember what he/she has written for a long time Code must be easy to understand and maintain
Outline
In this seminar, we will consider the techniques to support above features Project header Port header Code reuse Useful functions
Seminar objectives
At the end of this seminar, by referring the lecture notes, students will be able to: understand issues of maintainable and reusable
Project header
Look at your program in Exercise 2.3 If the hardware was changed: - AT89C51 was used, instead of AT89S52 - XTAL 4MHz was used, instead of 12 MHz How do you modify your code?
Project header
1. Put all information about microcontroller in a Project header file /*--------- Main.H ---------*/ #ifndef _MAIN_H #define _MAIN_H #include <reg52.h> #define OSC_FREQ 12000000UL #define OSC_PER_INST 12 // Typedefs typedef unsigned char tByte; typedef unsigned int tWord; typedef unsigned long tLong; 2. In your program // Interrupts (Main.C), put an #define INTERRUPT_Timer_0_Overflow 1 include: #define INTERRUPT_Timer_1_Overflow 3 #include <Main.H> #define INTERRUPT_Timer_2_Overflow 5 #endif /*---------End Main.H ---------*/
Example 3.1
/*--------- Main.C ---------*/ #include <Main.H>
#define display sbit K1 = P1^0; P0
//function prototypes void delay(tWord ms); bit get_switch_K1(tWord ms); void main (void) { tByte count; while(1){
if(get_switch_K1(20)) count++; display = ~count;} }
Port header
Look at your program in Exercise 2.3 If the hardware was changed: SWs were connected to Port 3, instead of Port 1 LEDs were connected to Port 2, instead of Port 0
How do you modify your code? If there are over 100 of times the program access to Port 2 and Port 3, how do you change the program to adapt with the hardware?
Port header
Put all of port reference in a Port header file:
/*--------- Port.H ---------*/ #ifndef _PORT_H #define _PORT_H #define SW_PORT P1 #define LED_PORT P0 sbit K1 = SW_PORT ^0;
#endif
In the program, put an include: #include <Port.H> If the hardware changed, just modify the relevant definition in the file Port.H
Code reuse
In Exercise 2.3, you have written a segment of code to read the switches If the develop a new project, and you have to read the similar switches How do you write a code segment for that job? If you know a job needed for every project, how do you write code?
Exercise 3.1
Redesign your program in Exercise 2.3 so that code segment for reading 8 SWs on Port 1 can be reused in many other programs. Follow the rule for noting
/******************************************************************************* * @fn getSwitch * @brief Read switches * @param void * @return 0,1,2,3,4,5,6,7 if SW0..SW7 is pressed, * 0xFF if no SW is pressed or pass debounce process */
Exercise 3.2
Redesign your program in Exercise 2.4 so that code segment for reading 4x4 keypad can be reused in many other programs. Follow the rule for noting
/******************************************************************************* * @fn KeyScan * @brief Read switches * @param void * @return 0,1,,9,A,D if corresponding key is pressed, * 0xFF if no key is pressed or pass debounce process */
Exercise 3.3
Write (game) program to do following things - Let user choose a position (0-7) by pressing a SW on Port 1 - Turn on the first LED on Port 0 and let the LED ships from low to high position with a random of times - If the LED stops at the same position that user chose, turn on a green LED on P2.7 (You win) - Otherwise, turn on a red LED on P2.6 (Sorry. Try again).
Notes: *Use the function getSwitch in Exercise 3.1 to read the SWs *Use function rand() in <stdlib.h> to create a random number