Lab 5
Lab 5
The objective of this laboratory exercise is to utilize μC/OS-II in conjunction with the Dragon12 HCS12
microcontroller board for the purpose of interfacing a servo motor with an infrared range finder. Your task
will be to integrate an additional task within the μC/OS-II operating system. This task will be responsible
for periodically reading the analog values emitted by the infrared (IR) sensor. Based on these readings,
the task will adjust the rotation angle of the servo motor accordingly.
A practical application of this setup is the contactless monitoring and regulation of chemical liquid levels
within a storage tank. The infrared sensor will be employed to determine the height of the liquid within the
tank. Concurrently, the servo motor will be tasked with controlling the flow of the liquid into the tank. This
process is designed to operate in a real-time manner, which is crucial for ensuring a consistent and
steady supply of the chemical. The real-time operation is essential to maintain a balanced inflow and level
of the liquid, thereby ensuring the tank is neither overfilled nor underfilled. This setup exemplifies the
integration of embedded systems and real-time control in practical industrial applications.
In this laboratory session, we will be utilizing specialized Dragon boards that come equipped with the
HCS12 Serial Monitor firmware. This is a notable shift from the previously used D-Bug12 firmware. Our
development environment for this exercise will be the CodeWarrior Integrated Development Environment
(IDE). With the combination of the CodeWarrior IDE and the HCS12 Serial Monitor firmware, there is a
significant enhancement in our capability to interact and debug with the HCS12 microcontroller. One of
the key advantages is the ability to completely overwrite the flash memory of the HCS12. This means we
can now permanently install programs onto the microcontroller. Unlike before, where we needed to
repeatedly use 'load' and 'g 2000' commands for running programs after every reboot, the programs we
install now will persist and survive reboots. This improvement simplifies the development process
significantly, while increasing the wear-tear of the internal MCU flash.
Background:
μC/OS-II (read as MicroC OS) is the second generation of μC/OS which is a priority-based, preemptive and
real-time multitasking operating system written mainly in the C programming language. It is originally published
in a book by Jean J. Labrosse, μC/OS The Real-Time Kernel, which purpose was to describe the internals of a
portable operating system with a small footprint. It is now a product which is maintained by Micrium Inc., and
licenses are issued per product or royalty free for non-commercial educational uses. Even though the source
code of μC/OS is available, it is not by any means considered free opensource software! Please refer to
licenses (https://github.com/weston-embedded/uC-OS2).
μC/OS-II is an extremely detailed and highly readable design study which is particularly useful to the
embedded systems student. While documenting the design and implementation of the kernel, the book also
walks through the many related development issues such as how to adapt the kernel for a new microprocessor,
how to install the kernel, and how to structure the applications that run on the kernel.
μC/OS-II allows one to create new tasks and check the existing status of the tasks stack. Tasks can be deleted
or their priority can be changed. Also μC/OS-II provides general information about a specific task and allows
one to suspend or resume operation as well on a task.
The current version of μC/OS-II can manage up to 64 tasks. The four highest priority tasks and the four lowest
priority tasks are reserved for the OS itself. The lower the value of the priority, the higher the priority of the task.
The task priority number also serves as the task identifier μC/OS-II uses rate preemptive monotonic scheduling
such that the highest rate of execution is given to the highest priority task which is ready. Tasks are periodic
and do not synchronize with one another and
OSStart
OSStart function is used to start the multitasking process which lets μC/OS-II manages the task that you have
created. Before you can call OSStart(), you MUST have called OSInit() and you MUST have created at least
one task.
OSIntEnter
OSIntEnter function is used to notify μC/OS-II that you are about to service an interrupt service routine (ISR).
This allows μC/OS-II to keep track of interrupt nesting and thus only perform rescheduling at the last nested
ISR. You are allowed to nest interrupts up to 255 levels deep.
OSIntExit
OSIntExit function is used to notify μC/OS-II that you have completed servicing an ISR. When the last nested
ISR has completed, μC/OS-II will call the scheduler to determine whether a new, high-priority task, is ready to
run. You MUST invoke OSIntEnter() and OSIntExit() in pairs. In other words, for every call to OSIntEnter() at
the beginning of the ISR you MUST have a call to OSIntExit() at the end of the ISR. Please note that
rescheduling is prevented when the scheduler is locked (see OSSchedLock)
OSTaskCreate
OSTaskCreate function is used to have μC/OS-II manage the execution of a task. Tasks can either be created
prior to the start of multitasking or by a running task. A task cannot be created by an ISR.
Signature:
INT8U OSTaskCreate (void (*task)(void *p_arg), void *p_arg, OS_STK *ptos, INT8U
prio)
Arguments:
for (;;)
Task code;
Signature:
Arguments:
§ prio argument is the priority of the task to delete. Note that you can explicitely delete
the current task without knowing its priority level by seeng 'prio' to OS_PRIO_SELF.
Returns:
Please note:
//...
while (1)
OSTimeDly(1);
if (OSTaskDelReq(OS_PRIO_SELF) == OS_ERR_TASK_DEL_REQ)
Arguments:
§ Prio argument is the priority of the task to request the delete from
Returns :
§ OS_ERR_NONE: if the task exist and the request has been registered
§ OS_ERR_TASK_NOT_EXIST: if the task has been deleted. This allows the caller to know
whether the request has been executed.
§ OS_ERR_TASK_DEL: if the task is assigned to a Mutex.
§ OS_ERR_TASK_DEL_IDLE: if you requested to delete μC/OS-II's idle task
§ OS_ERR_PRIO_INVALID: if the priority you specify is higher that the maximum allowed (i.e.
>= OS_LOWEST_PRIO) or, you have not specified OS_PRIO_SELF.
§ OS_ERR_TASK_DEL_REQ: if a task (possibly another task) requested that the running task
be deleted.
OSTaskSuspend
OSTaskSuspend function is called to suspend a task. The task can be the calling task if the priority passed to
OSTaskSuspend() is the priority of the calling task or OS_PRIO_SELF. You should use this function with great
care. If you suspend a task that is waiting for an event (i.e. a message, a semaphore, a queue …) you will
prevent this task from running when the event arrives.
Arguments:
§ Prio arguments is the priority of the task to suspend. If you specify OS_PRIO_SELF, the
calling task will suspend itself and rescheduling will occur.
Returns:
OSTaskResume
OSTaskResume function is called to resume a previously suspended task. This is the only call that will remove
an explicit task suspension.
Returns:
OSTaskChangePrio
OSTaskChangePrio function allows you to change the priority of a task dynamically. Note that the new priority
MUST be available.
Returns:
OSSchedLock
OSSchedLock function is used to prevent rescheduling to take place. This allows your application to prevent
context switches until you are ready to permit context switching. You MUST invoke OSSchedLock() and
OSSchedUnlock() in pair. In other words, for every call to OSSchedLock() you MUST have a call to
OSSchedUnlock().
OSSchedUnlock
OSSchedUnlock function is used to re-allow rescheduling. You MUST invoke OSSchedLock() and
OSSchedUnlock() in pair. In other words, for every call to OSSchedLock() you MUST have a call to
OSSchedUnlock().
OSTimeDly
OSTimeDly function is called to delay execution of the currently running task until the specified number of
system ticks expires. This, of course, directly equates to delaying the current task for some time to expire. No
delay will result If the specified delay is 0. If the specified delay is greater than 0 then, a context switch will
result.
Arguments:
§ “ticks” is the time delay that the task will be suspended in number of clock 'ticks'. Note that
by specifying 0, the task will not be delayed.
OSTimeDlyHMSM
OSTimeDlyHMSM function is called to delay execution of the currently running task until some time expires.
This call allows you to specify the delay time in HOURS, MINUTES, SECONDS and MILLISECONDS instead
of ticks.
OS_ENTER_CRITICAL
OS_ENTER_CRITICAL() is a macro inserts the machine instruction into your code to block all interrupts.
OS_EXIT_CRITICAL
OS_EXIT_CRITICAL() is a macro inserts the machine instruction to enable interrupts.
OSSemCreate
OSSemCreate function creates a semaphore.
Arguments:
§ “cnt” is the initial value for the semaphore. If the value is 0, no resource is available (or no
event has occurred). You initialize the semaphore to a non-zero value to specify how many
resources are available (e.g. if you have 10 resources, you would initialize the semaphore to
10).
Returns:
§ (void *)0 if no event control blocks were available. Otherwise return is a pointer to the event
control block (OS_EVENT) associated with the created semaphore.
OSSemPost
OSSemPost function signals a semaphore Argument pevent is a pointer to the event control block associated
with the desired semaphore.
Returns:
§ OS_ERR_NONE: The call was successful and the semaphore was signaled.
§ OS_ERR_SEM_OVF: If the semaphore count exceeded its limit. In other words, you have
signalled the semaphore more often than you waited on it with either OSSemAccept() or
OSSemPend().
§ OS_ERR_EVENT_TYPE: If you didn't pass a pointer to a semaphore.
§ OS_ERR_PEVENT_NULL: If 'pevent' is a NULL pointer.
OSSemPendAbort
OSSemPendAbort function aborts & readies any tasks currently waiting on a semaphore. This function should
be used to fault-abort the wait on the semaphore, rather than to normally signal the semaphore via
OSSemPost().
Servo Motor
A Servo is a device that has an output shaft that can be positioned to a specific angular positions by sending
the servo a coded signal. As long as the coded signal exists on the input line, the servo will maintain the
angular position of the shaft. As the coded signal changes, the angular position of the shaft changes. Servos
are topically have 3 wires; one is for power (+5 volts), ground, and the yellow wire is the control wire.
The servo motor has a control circuit and a potentiometer (a variable resistor also known as pot) that is
connected to the output shaft. The pot allows the control circuitry to monitor the current angle of the servo
motor. If the shaft is at the correct angle, then the motor shuts off. If the circuit finds that the angle is not
correct, it will turn the motor the correct direction until the angle is correct. The output shaft of the servo is
capable of traveling somewhere around 180 degrees. A normal servo is mechanically not capable of turning
any farther due to a mechanical stop built on to the main output gear .
Lab 5
The goal is to have a task that every 300 ms checks the distance of the IR Rangefinder and change the servo's
position accordingly.
§ Connect the VCC and ground wires of Servo and IR Range Finder. Make sure you have +5
volts and have a common ground between the microprocessor and your power supply.
§ Connect PP7 pin to servo's input signal and PAD04 pin to IR-Range Finder output signal.
§ Double check your circuitry with your TA before powering up anything.
§ Recompile the project and transfer the binaries to Dragon12 board using CodeWarrior.
lab5
├── ReadMe
│ ├── 00-CW_CW816BITIDEUG.pdf
│ ├── 01-an1004 (The 10-Minute Guide to RTOS).pdf
│ ├── 02-AN-1456 (µC OS-II Dragon12 Development Board).pdf
│ ├── 03-uCOS-II-RefMan.PDF
│ ├── 04-Hitec HS422 Servo Motor Manual.pdf
│ ├── 04-LCD-Manual.pdf
│ ├── 04-SHARP GP2D12 IR Range Finder Manual.pdf
│ ├── 05-S12ATD10B8CV2.pdf
│ ├── 05-S12PWM8B8CV1.pdf
│ ├── LegalNotice
│ │ ├── LegalNotice_OS_ONLY.pdf
│ │ ├── Micrium-SLA-CPU.pdf
│ │ ├── Micrium-SLA-P1.pdf
│ │ └── Micrium-SLA-PL.pdf
│ └── OtherReferences
│ ├── AN1002 (Mutual Exclusion Semaphores).PDF
│ ├── AN1005 (Inter-Process Communication).pdf
│ ├── AN1007A (µC OS-II and Event Flags).pdf
│ ├── AN2000 (C Coding Standard).PDF
│ ├── QuickRefChart-Color.PDF
│ ├── ReleaseNotes.PDF
│ ├── Task-State-Diagram.pdf
│ ├── TaskAssignmentWorksheet.PDF
│ ├── WhatsNewSince-V200.PDF
│ ├── uC-OS2-github.zip
│ └── uCOS-II-CfgMan.PDF
├── Software
│ ├── EvalBoards
│ │ └── Freescale
│ │ └── MC9S12DG256B
│ │ └── Wytec Dragon12
│ │ └── Metrowerks
│ │ └── Paged
│ │ ├── BSP
│ │ │ ├── bsp.c
│ │ │ ├── bsp.h
│ │ │ ├── keypad.c
│ │ │ ├── keypad.h
│ │ │ ├── nvm.c
│ │ │ ├── nvm.h
│ │ │ ├── sevenSegDisp_OS.c
│ │ │ ├── sevenSegment.c
│ │ │ ├── sevenSegment.h
│ │ │ └── sevenSegment.s
│ │ └── OS-Probe-LCD
│ │ ├── C_Layout.hwl
│ │ ├── Default.mem
│ │ ├── HCS12_Serial_Monitor.ini
│ │ ├── OS-Probe-LCD.mcp
│ │ ├── P&E_Multilink_CyclonePro.ini
│ │ ├── Sources
│ │ │ ├── Start12.c
│ │ │ ├── app.c
│ │ │ ├── app_cfg.h
│ │ │ ├── app_hooks.c
│ │ │ ├── app_probe.c
│ │ │ ├── datapage.c
│ │ │ ├── includes.h
│ │ │ ├── os_cfg.h
│ │ │ ├── probe_com_cfg.h
│ │ │ └── vectors.c
│ │ ├── bin
│ │ │ ├── HCS12_Serial_Monitor.abs
│ │ │ ├── HCS12_Serial_Monitor.abs.phy
│ │ │ ├── HCS12_Serial_Monitor.abs.s19
│ │ │ ├── HCS12_Serial_Monitor.map
│ │ │ ├── P&E_Multilink_CyclonePro.abs
│ │ │ ├── P&E_Multilink_CyclonePro.abs.phy
│ │ │ ├── P&E_Multilink_CyclonePro.abs.s19
│ │ │ ├── P&E_Multilink_CyclonePro.map
│ │ │ ├── cpu_a.dbg
│ │ │ ├── os_cpu_a.dbg
│ │ │ ├── probe_rs232_ba.dbg
│ │ │ └── sevenSegment.dbg
│ │ ├── cmd
│ │ │ ├── HCS12_Serial_Monitor_Postload.cmd
│ │ │ ├── HCS12_Serial_Monitor_Preload.cmd
│ │ │ ├── HCS12_Serial_Monitor_Reset.cmd
│ │ │ ├── HCS12_Serial_Monitor_Startup.cmd
│ │ │ ├── P&E_Multilink_CyclonePro_Erase_unsecure_hcs12.cmd
│ │ │ ├── P&E_Multilink_CyclonePro_Postload.cmd
│ │ │ ├── P&E_Multilink_CyclonePro_Preload.cmd
│ │ │ ├── P&E_Multilink_CyclonePro_Reset.cmd
│ │ │ ├── P&E_Multilink_CyclonePro_Startup.cmd
│ │ │ ├── P&E_Multilink_CyclonePro_Vppoff.cmd
│ │ │ └── P&E_Multilink_CyclonePro_Vppon.cmd
│ │ ├── prm
│ │ │ ├── HCS12_Serial_Monitor_linker.prm
│ │ │ ├── P&E_Multilink_CyclonePro_linker.prm
│ │ │ └── burner.bbl
│ │ ├── readme.txt
│ │ └── tips.txt
│ ├── uC-CPU
│ │ ├── MC9S12
│ │ │ └── Metrowerks
│ │ │ ├── cpu.h
│ │ │ └── cpu_a.s
│ │ └── cpu_def.h
│ ├── uC-LCD
│ │ ├── Doc
│ │ │ ├── HD44780-Datasheet.PDF
│ │ │ └── LCD-Manual.pdf
│ │ ├── OS
│ │ │ └── uCOS-II
│ │ │ └── lcd_os.c
│ │ └── Source
│ │ ├── lcd.c
│ │ └── lcd.h
│ ├── uC-LIB
│ │ ├── Doc
│ │ │ ├── uC-LIB-Manual.pdf
│ │ │ └── uC-LIB-ReleaseNotes.pdf
│ │ ├── lib_ascii.c
│ │ ├── lib_ascii.h
│ │ ├── lib_def.h
│ │ ├── lib_mem.c
│ │ ├── lib_mem.h
│ │ ├── lib_str.c
│ │ └── lib_str.h
│ ├── uC-Probe
│ │ └── Target
│ │ ├── Communication
│ │ │ └── Generic
│ │ │ ├── OS
│ │ │ │ └── uCOS-II
│ │ │ │ └── probe_com_os.c
│ │ │ ├── RS-232
│ │ │ │ ├── OS
│ │ │ │ │ └── uCOS-II
│ │ │ │ │ └── probe_rs232_os.c
│ │ │ │ ├── Ports
│ │ │ │ │ └── Freescale
│ │ │ │ │ └── MC9S12
│ │ │ │ │ ├── probe_rs232_ba.s
│ │ │ │ │ ├── probe_rs232c.c
│ │ │ │ │ └── probe_rs232c.h
│ │ │ │ └── Source
│ │ │ │ ├── probe_rs232.c
│ │ │ │ └── probe_rs232.h
│ │ │ └── Source
│ │ │ ├── probe_com.c
│ │ │ └── probe_com.h
│ │ └── Plugins
│ │ └── uCOS-II
│ │ ├── os_probe.c
│ │ └── os_probe.h
│ └── uCOS-II
│ ├── Doc
│ │ ├── QuickRefChart-Color.PDF
│ │ ├── README.TXT
│ │ ├── ReleaseNotes.PDF
│ │ ├── Task-State-Diagram.pdf
│ │ ├── TaskAssignmentWorksheet.PDF
│ │ ├── WhatsNewSince-V200.PDF
│ │ ├── uCOS-II-CfgMan.PDF
│ │ └── uCOS-II-RefMan.PDF
│ ├── Ports
│ │ └── HCS12
│ │ └── Paged
│ │ └── Metrowerks
│ │ ├── OS_CPU.h
│ │ ├── SerialMonitor
│ │ │ ├── OS_CPU.h
│ │ │ ├── os_cpu_a.s
│ │ │ └── os_cpu_c.c
│ │ ├── os_cpu_a.s
│ │ └── os_cpu_c.c
│ └── Source
│ ├── os_core.c
│ ├── os_dbg_r.c
│ ├── os_flag.c
│ ├── os_mbox.c
│ ├── os_mem.c
│ ├── os_mutex.c
│ ├── os_q.c
│ ├── os_sem.c
│ ├── os_task.c
│ ├── os_time.c
│ ├── os_tmr.c
│ └── ucos_ii.h
└── lab5_codes_to_help_you.c