Upload
kt8815
View
10
Download
1
Embed Size (px)
DESCRIPTION
lap trinh nhung co ban
Citation preview
Programming embedded systems
Seminar 3
PROGRAM STRUCTURE
Dr. Tran Thanh Hung
Department of Automation Technology,
College of Engineering, Can Tho University
Email: [email protected]
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
• create and use “project header”, “port header”
• develop software that can be reused for many projects
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;// Interrupts #define INTERRUPT_Timer_0_Overflow 1#define INTERRUPT_Timer_1_Overflow 3#define INTERRUPT_Timer_2_Overflow 5#endif/*---------End Main.H ---------*/
2. In your program (Main.C), put an include: #include <Main.H>
Example 3.1
/*--------- Main.C ---------*/
#include <Main.H>#define display P0
sbit K1 = P1^0;
//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;}
}
void delay(tWord ms){ unsigned int x, y; for (x = 0; x <= ms; x++) { for (y = 0; y <= 120; y++); }}
bit get_switch_K1(tWord ms) { bit return_value = 0; if (K1 == 0) //switch K1 pressed?
{ delay(ms); //debounce if (K1 == 0) //K1 still pressed?
{ while (K1 == 0); return_value = 1; } }
return return_value }
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
/*---------End Port.H ---------*/
• 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?
Code reuse: Design
Code reuse: Design
• Put code segments for each job or relevant jobs in separate files: one name.h file and one name.c file
• name.h file is a header file, consists of definitions and function prototypes for name.c file. This file must be added to the Source Group1 area in KeilC window
• name.c file consists of all code segment for the job. This file must be added to Source Group1 area in KeilC window
• Put an include in name.c file and project’s main file:
#include <name.h>
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
*/
unsigned char getSwitch(void)
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
*/
unsigned char KeyScan(void)
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