Download as pdf or txt
Download as pdf or txt
You are on page 1of 175

1222 CCS

Using the CCS C Compiler


for Rapid Development of
Microcontroller Applications
2008 Microchip Technology Incorporated. All Rights Reserved.

1222 CCS

Slide

Class Objective
When you finish this class you will:
Be able to use your C knowledge and
write applications using CCS C
Know the ways the CCS C Compiler
speeds up development time
Develop new application and coding
ideas

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

Agenda
O

Compiler Overview
Design Goals, Compiler Products

Compiler Methodology
Differences to Common C
Data Types

Programming Details
Required setup, Interrupts

Built-In Functions
GPIO, Delays, Serial (Asynch, I2C, SPI)

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

Agenda
Hands-on Labs
O

O
O

Lab 1: 0 to blinky LED in 60


seconds
Lab 2: The CCS IDE Debugger
Lab 3: Stopwatch using
interrupts
Lab 4: A/D

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

Compiler Overview

Design Goals
O
O

O
O

Ease of use compile and go!


Quick development time (with
built-in functions,
drivers/libraries, extensive
examples)
Easy PIC MCU to PIC MCU
migration
Optimization comparable vs.
assembly

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

CCS Command Line Compiler


O

Product Line:

PCB Baseline PIC MCUs


PCM Mid-Range PIC MCUs
PCH PIC18 Family PIC MCUs
PCD PIC24/dsPIC30/dsPIC33 Family
PCX PIC32 Family

Integrates into
O Linker
O

MPLAB

IDE

Cant create relocatable objects

Linux and Windows versions

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

PCW Windows IDE


O
O
O
O
O
O
O

C Aware Editor
Debugging (using a CCS ICD)
Integrated RTOS
Linker and Re-locatable objects
Document Generator
Statistics
Flow Chart Editor

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

CCS Support Policy


O

E-Mail / Phone Support


Duration is Unlimited!

Updates (new devices, drivers)


Requires Maintenance Contract
Maintenance can be extended at any
period

O
O

Large Customer Base


Rapid Releases
Average once per week

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

CCS ICD
O
O
O

In-Circuit Programmer/Debugger
Serial & USB
Control Software
Windows and Linux

O
O

CCS PCW IDE for debugging


ICD cable is compatible with
Microchip MPLAB ICD2

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

10

CCS Development Kits


O

A combined package that gives a


user:

C Compiler
Prototyping Board
ICD
Tutorial Book and Examples
Relevant Parts and Cables

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

11

Compiler
Demonstration

Hands On Lab #1
Blinky LED

Lab 1 Objectives
O

O
O

Hands-on experience with


compiler
Use tools and development
board
Use CCS Project Wizard
Create blinky-LED

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

14

Prototyping Hardware
O

PIC16F877A

ICD Connection
Pot on AN0
Button on RA4
LEDs on RA5, RB4 and RB5
RS232 driver and mini-connector
Header to all GPIO

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

15

Lab 1: Project Wizard


Project Wizard Overview
O
O

Creates an initial framework


Assists in the initial
configuration of internal
peripherals
Configuration screen for all
internal peripherals
Windows IDE Only

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

16

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. Class 1222 CCS

Slide

17

Lab 1: Project Wizard


Step 1 Start Compiler IDE
O

Start the CCS Compiler IDE


Double click on the icon labeled PIC
C Compiler

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

18

Lab 1: Project Wizard


Step 2 Start Project Wizard
O

Start the Project Wizard


File -> New -> Project Wizard

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

19

Lab 1: Project Wizard


Step 3 File Name Dialog
O
O

File name: C:\masters\1222\Lab1\lab1.c


Press Save

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

20

Lab 1: Project Wizard


Step 4 Configuration
O

You will now be in the Project


Wizard Dialog
In the General Section:
Select 16F877A under Device
Set oscillator to 20000000 Hz
Select High Speed Osc under fuse

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

21

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. Class 1222 CCS

Slide

22

Lab 1: Project Wizard


Step 5 Enter Code
O
O

Press the OK button in the Project Wizard


Inside main(), find the line marked:
//TODO: USER CODE

Replace this line with the following code:


while(TRUE)
{
delay_ms(500);
output_toggle(PIN_B5);
}

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

23

Lab 1: Project Wizard


Step 6 Compile Code
O

Compile Code
Compile -> Compile

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

24

Lab 1: Project Wizard


Step 7 Compile Results
O

Inspect the output window for


compile results
You should get 0 Errors

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

25

Lab 1: Project Wizard


Step 8 Load HEX
O

Load HEX file with CCS ICD


Click on the Program Chip icon on
the Compile ribbon, select ICD

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

26

Lab 1 Hints
O
O
O

LED will blink at 1Hz rate


Follow handout
Verify:

Correct PIC MCU Selected


Correct FUSEs selected
Correct #use delay() specified
Fix any compile errors

Bored? Try Extra Credit

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

27

Compiler
Methodology

Differences to Common C
O

Case in-sensitive

#case will make it case sensitive

Variables not initialized to zero


#zero_ram inits all variables to 0

O
O

const places variable into ROM


By default, const strings cannot be
parameter
SomeFunc(Hello) is usually illegal
#device PASS_STRING=IN_RAM

#device ANSI

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

29

ANSI Non-Compliance
O

No fopen(), fclose(), etc


fprintf() supported, methodology a
little different

printf() formatting string must be


known at compile time
Recursion not allowed on all PIC
MCUs

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

30

Data Types
PCB/PCM/PCH
short
1bit
char
Unsigned 8bit
int
Unsigned 8bit
long
Unsigned 16bit
long long Unsigned 32bit
float
MCHP 32bit
double
NO SUPPORT
2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

PCD
1bit
Unsigned 8bit
Signed 16bit
Signed 32bit
Signed 64bit
IEEE 32bit
IEEE 64bit
Slide

31

Data Types
O

#type can change default size

#TYPE SHORT=8, INT=16, LONG=32

Optimization Tips

Use only one float type


Use int as much as possible

Non-standard data types

int1
int8
int32
int48*
float32 float48*
__address__

int16
int64*
float64* (PCD ONLY)

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

32

Fixed Point Decimal


O

Decimals with integers, not floats!


Faster, Smaller
100% precision

O
O

Stores value in 10y power


Declaration:
[type] _fixed(y) [declarator]
int16 _fixed(2) money

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

33

Fixed Point Examples


O

int16 _fixed(2) money;


Range: 0.00 to 655.35

O
O
O
O

money = 20.50;
money += 5;
//adds 5.00
money += value;
printf(%w, money);

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

34

RAM Allocation
O

RAM is allocated first come, first


served in this order:
1. globals
2. static
3. local

O
O
O

Call tree determines which RAM can


be reused
User can force RAM placement
Structure/Array cannot be larger than
bank

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

35

const vs. rom


O

const has two modes:


#device CONST=ROM DEFAULT
#device CONST=READ_ONLY

O
O
O
O

const is always read-only


rom places variable in ROM
rom writable if supported by PIC MCU
Examples
const int lookup[16]={0...15};
const char string[]=Hello;
rom char *cptr = Hello;

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

36

Borrowed from C++


Reference Parameters
O

Pass address, not value


More Efficient for large structures
Values can be changed in function

Declare reference parameters with &


void Inc(int &i) {
i++;

}
O

Many times compiler will inline

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

37

Borrowed from C++


Default Parameters
O

Default value passed to function


if no value specified
int1 Get(char *c, int time=200);
Get(&c);
time will be 200

Get(&c, 500);
time will be 500
2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

38

Borrowed from C++


Function Overloading
O
O
O

Multiple functions, same name


Different parameters
Example:
Three different functions called Inc
int Inc(int *i);
long Inc(long *l);
float Inc(float *f);

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

39

In-Line Assembly
O

#asm

#endasm

O
O

Ends an assembly block

_return_

Starts an assembly block

Assign return value


May be corrupted by any C code
after #endasm

Supports all opcodes


C variables can be accessed
Automatic banking

#asm ASIS - disables autobanking

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

int find_parity (int data)


{
int count;
#asm
movlw
0x8
movwf
count
movlw
0
loop:
xorwf
data,w
rrf
data,f
decfsz count,f
goto
loop
movlw
1
addwf
count,f
movwf
_return_
#endasm
}
Slide

40

Output Files
O
O
O
O
O
O
O

HEX Compiled Application


COF Debug Application
ERR Compile output messages
LST C to Assembly comparison
SYM Memory Map
STA Statistics
TRE Call Tree

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

41

Programming
Details
Required Setup

Pre-processor
O

Design Goal: All in front of you


Everything is in the source code
No linker scripts

Pre-processor commands
change compiler-behaviour:
Methodology
ROM/RAM placement
Library configuration

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

43

#device
O

Loads personality for the target PIC


MCU
#device PIC18F4520

Can be used to alter low-level


specifics
#device ICD=TRUE

O
O

PIC MCU header files have #device


If creating multiple compilation units
(linking), must be defined in each
file.
Put it in a common include file

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

44

#fuses
O

Define the configuration bits


#fuses HS, NOWDT, NOLVP

To get list of valid fuses:


In IDE, use View Valid Fuses window
Header file for the device has list

To get a description of each fuse:


In IDE, use View Valid Fuses window
fuses.txt

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

45

#use delay
O

Configure the built-in libraries for the


clock speed for your target
#use delay(clock=value)

O
O

value is system clock speed, after PLL


Only needed if using the following:
Serial
Delay

For multiple clock speed applications,


you can define this more than once

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

46

Programming
Details
Memory Placement

#byte / #word
O

Force location of RAM


int val;
#byte val=0xA0

Overlaying variable onto SFR:


#byte STATUS=0x03
#word TMR1=0x0E

Repeat, but using getenv():


#byte STATUS=getenv(SFR:STATUS)

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

48

#bit
O

#bit ident=X.b
Declares boolean at address X, bit b
Examples:
O
O

#bit CARRY=STATUS.0
#bit CARRY=getenv(BIT:C)

Dont forget:
Compiler will pack int1 variables
You can use struct to pack bits

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

49

#org
O

Fix placement in ROM

#org start, end

Place following function/constant in


specified program memory

#org 0x400,0x4FF
SomeFunction() { /*code*/ }
#org 0x500, 0x5FF
const someStruct[] = { }
2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

50

Hands On Lab #2
Debugging a Calculator
Application

Lab 2 Objectives
O

Learn basic debugging use

Breakpoints
Watches
ROM and RAM view
Peripheral Status

Learn advanced debugging use


Logging
1Wire RS232 on RB3

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

52

Lab 2: CCS IDE Debugger


Step 1 Close All Open Files

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

53

Lab 2: CCS IDE Debugger


Step 2 Load Example Code
O

Load Example Code


File->Open
C:\masters\1222\Lab2\lab2.c

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

54

Lab 2: CCS IDE Debugger


Step 3 Start Debugger
O

Start Debugger
Debug -> Enable Debugger

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

55

Lab 2: CCS IDE Debugger


Debugger GUI Overview

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

56

Lab 2: CCS IDE Debugger


Step 4 Load Target Code
Compile Project
Compile -> Compile
Project will be loaded

Press GO

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

57

Lab 2: CCS IDE Debugger


Step 5 1-Wire RS232
O

#use rs232(debugger)
1-Wire RS232 using RB3
1-Wire Monitor in Debugger:

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

58

Lab 2: CCS IDE Debugger


Step 6 1-Wire RS232
O

Using the Monitor, experiment


with the application
Application is a simple calculator

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

59

Lab 2: CCS IDE Debugger


Step 7 Breakpoints
O
O

Press STOP
Add a Breakpoint to line 63
printf(\r\nThe result is %lu, result)
Put cursor on Line 63, Press + on
Break Tab

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

60

Lab 2: CCS IDE Debugger


Step 8 Watching
O

Open Watch Tab, Press +

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

61

Lab 2: CCS IDE Debugger


Step 9 Watch Dialog
O
O

Enter result into Expression Field


Press Add Watch

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

62

Lab 2: CCS IDE Debugger


Step 10 Break Logging
O

Logs an expression at breakpoint


Breakpoint doesnt stop execution

Enter result into Break Log

Press GO

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

63

BREAK TIME!

Programming Details
addressmod

Pop Quiz

You need to store 128 values


but your PIC MCU only
has 80 bytes of RAM.
What do you do?
2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

66

addressmod
O

Application defined storage


What is normal storage?

O
O

typemod, on steroids!
Can be used on any data type
Pointers, structs, unions and arrays
const and rom not allowed

ISO/IEC TR 18037 (Embedded C)

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

67

addressmod Syntax
O

addressmod(identifier,[read,write,]start,end)

identifier is the new qualifier


read(int32 addr, int8 *ptr, int8 len)
write(int32 addr, int8 *ptr, int8 len)
O
O

The IO method to access this memory


Optional

start/end are the memory range

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

68

addressmod Declaration
addressmod(nv, read_nv, write_nv,
0, NV_SIZE);
void read_nv(int32 addr,
int8 *ram, int8 n)
{ /* read n from addr */ }
void write_nv(int32 addr,
int8 *ram, int8 n)
{ /* write n from ram */ }

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

69

addressmod Usage
nv NVBuffer[8192];
nv NVID;
nv *NVPtr;
#locate NVID=0
NVBuffer[i]=55;
*NVPtr++ = 0;
2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

70

addressmod Ideas
O
O

External RAM / Flash


Character/Grahic LCD access
thru a multi-dimensional array
Debug/trap critical variables

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

71

Programming Details
Interrupts

Pop Quiz

What does your ISR


need to do?

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

73

Interrupt Service Routine


O

CCS C provides an ISR


Saves status and scratch registers
Checks enable/flags, goes to user
ISR function for that flag
Clears interrupt flag (if needed)
Restores status/scratch registers

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

74

Interrupt Service Routine


API
O

enable_interrupts(INT_*)
enable_interrupts(GLOBAL)
enable_interrupts(INT_TIMER0)

O
O
O

disable_interrupts(INT_*)
clear_interrupt(INT_*)
interrupt_active(INT_*)

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

75

Interrupt Service Routine


API
O

#int_*

The following function will be processed


for that interrupt
#int_timer0
void isr_timer0(void)
{
//HANDLE TIMER0 OVERFLOW
}

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

76

Interrupt Service Routine


Special Identifiers
O

#int_global
Function overrides CCS ISR
User must save and restore all
registers altered by their ISR (W,
STATUS, scratch used by compiler,
etc.)

#int_default
Traps unknown interrupt

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

77

Interrupt Service Routine


Options
O

These keywords may follow #int_*:

high (PIC18) Interrupt is high priority


fast (PIC18) Same as high, but no save
fast (PCD) Uses shadow registers
level=(0..7) (PCD) sets interrupt level
noclear ISR will not clear interrupt flags

#device HIGH_INTS=TRUE
Must set to use high/fast ISR on PIC18

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

78

Lab #3
Interrupts

Lab 3: Objectives
O
O
O
O

Learn how to use the CCS ISR


Learn how the CCS built-in functions
control the Timer0 and GPIO
Implement a stop watch that measures
button press time.
Open C:\masters\1222\Lab3\lab3.c
Complete the lines marked TODO

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

80

Lab 3 Hints
O
O

Handout has help and finished example


TIPS:
Press F1 to get compiler help manual
#int_timer0
O

Following function is called on a Timer 0 overflow

setup_timer_0(RTCC_DIV_256)
O

Configures Timer 0 to use a divide by 256 prescalar

enable_interrupts(int_xxx)
O
O

INT_TIMER0 Enable Timer 0 Interrupt


GLOBAL Enables Global Interrupt

input(PIN_XX)
O
O

Returns TRUE if PIN_XX is high, else returns FALSE


Button is RA4, or PIN_A4 in CCS

Bored? Try the Extra Credit

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

81

Programming
Details
Built-In Functions

Built-In Functions
O

Compiler provides functions to:


Control Peripherals (A/D, Serial, etc)
Perform logic optimized to PIC

O
O

Internal to compiler, not linked


Simplify PIC MCU to PIC MCU
migration
Follow any errata workarounds

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

83

Easy PIC MCU Migration


#if defined(__PIC18__)
#include <18F4520.h>
#elif defined(__PIC14__)
#include <16F877A.h>
#endif
#fuses HS, NOWDT, NOLVP
#use delay(clock=20000000)
Void main(void) {
while(TRUE) {
output_toggle(PIN_C0);
delay_ms(500);
}
}
2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

84

getenv()
O
O

Assists in making portable code


Returns chip information
Program Memory Size
Bulk Erase Size
Configuration Bits Set

Options can be conditionally compiled:


#if getenv(ADC_CHANNELS) > 0
setup_adc_ports(NO_ANALOGS);
#endif

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

85

Device Database and Editor

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. Class 1222 CCS

Slide

86

Dont forget Drivers and


Examples!
O

Drivers provided for complex features or


external peripherals
EEPROMs, LCDs, USB, FAT, etc

O
O
O

Many example programs provided to


showcase built-in functions/drivers
Always kept up to date with latest
peripherals
Before you start a project, examine the
libraries and example programs CCS
provides you to reduce your development
time

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

87

Built-In Functions
General Purpose Input / Output
O
O

output_high(PIN_XX)
output_low(PIN_XX)
Sets the pin to desired level
PIN_XX (ex PIN_C0, PIN_B5, etc) are
defined in the device header file

output_toggle(PIN_XX)
Toggle high/low state of the pin

bool=input(PIN_XX)
Read value of pin

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

88

Built-In Functions
General Purpose Input / Output
O

output_X(value)
Sets the port X (A to J) to the desired
value

byte=input_X()
Read value of port X

O
O

set_tris_X(value)
val = get_tris_X()
Get/Set the tristate setting

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

89

Built-In Functions
General Purpose Input / Output
O

#use standard_io(X)
Output functions set TRIS to output
Input functions set TRIS to input
This is the default operation

#use fast_io(X)
Compiler does not alter TRIS

#use fixed_io(port_outputs=pins)
#use fixed_io(d_outputs=PIN_D7)

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

90

Built-In Functions
Bit Manipulation
O

bool=bit_test(var, bit)
Returns the value of the specified bit

O
O

bit_clear(var, bit)
bit_set(var, bit)
Set/clear the specified bit

The above bit_XXX() functions


use the PIC MCUs bit operation
opcodes for maximum efficiency

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

91

Built-In Functions
Delays
O
O
O

delay_cycles(x)
delay_us(x)
delay_ms(x)
Uses a series of loops and NOPs
Timing based on #use delay()
Multiple #use delay() allowed

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

92

#use delay(clock=125000)
//1 second delay @ 125kHz
void Delay1s125khz(void) {
delay_ms(1000);
}
#use delay(clock=8000000)
//1 second delay @ 8MHz
Void Delay1s8Mhz(void) {
delay_ms(1000);
}
2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. Class 1222 CCS

Slide

93

Built-In Functions
Serial Libraries

Pop Quiz

You need to add another


serial port to your project,
but you have used all of the

PIC MCUs peripherals.


What do you do?
2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

95

UART
O

Powerful UART library built into


compiler
#use rs232(baud=xxxx, xmit=PIN_XX,
rcv=PIN_XX, stream=yyyyy)
TX/RX pins can be any pins
Many more options exist
O

Enable bit, parity, collision detection,


open-collector mode, etc.

Timing is based on #use delay()


2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

96

UART (Continued)
O

Standard C I/O, last #use rs232()


printf(string, )
char=getc()
putc(char)

Standard C I/O, streams:


fprintf(stream, string, )
char=fgetc(stream)
putc(char, stream)

O
O

bool=kbhit(stream)
setup_uart(newBaud, stream)
Dynamically change the UART

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

97

Dual UART Example


#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,
stream=HW_UART)
#use rs232(baud=9600, xmit=PIN_D0, rcv=PIN_B0,
stream=SW_UART, disable_ints)
fprintf(HW_UART, HELLO HARDWARE UART\r\n);
fprintf(SW_UART, HELLO SOFTWARE UART\r\n);
if (kbhit(HW_UART)) {
c=fgetc(HW_UART);
fputc(c,SW_UART);
}
if (kbhit(SW_UART)) {
c=fgetc(SW_UART);
fputc(c,HW_UART);
}
2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

98

printf() Redirection
O

printf() can be redirected to a


user-defined function
Example:
void LCDPut(char c);
printf(LCDPut, %U, val);

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

99

2
I C
O
O

Library

Multiple I2C channels on any I/O pins


#use i2c(master, sda=pin_XX,
scl=pin_XX, address=YY,
stream=id)
The SDA and SCL pins can be any pins
Slave Mode is HW MSSP only
Address Is only needed in slave mode

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

100

2
I C
O

Library (Continued)

i2c_start()
Can also be used to send a re-start signal

char=i2c_read(ack)
Ack is an optional parameter

O
O

ack = i2c_write(char)
bool=i2c_available()
Can only be used with a hardware MSSP

i2c_stop()

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

101

SPI
O
O

Configurable SPI library


#use spi(parameters)
HW or SW pins
Multiple streams
Clock rate and clock mode
configurable

in = spi_xfer(STREAM_SPI, out)

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

102

Built-in Functions
A/D Converter

A/D Converter
O

setup_adc(mode)
Configure and the ADC; mode varies
for each type

setup_adc_ports(config)
Configure the pins for analog or
digital mode

#device ADC=X
Assign the ADC result size

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

104

A/D Converter (Continued)


O

set_adc_channel(channel)
Set the channel for subsequent reads

val = read_adc(mode)
ADC_START_ONLY Start a conversion
ADC_READ_ONLY Read last
conversion
ADC_START_AND_READ Full cycle

adc_done()
Returns TRUE if conversion is done

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

105

A/D Converter Example


//INIT
#device ADC=10
setup_adc(ADC_CLOCK_INTERNAL );
setup_adc_ports(AN0);
//APPLICATION
int16 result;
set_adc_channel(0);
read_adc(ADC_START_ONLY);
/* do something while A/D is busy */
while (!adc_done());
result=read_adc(ADC_READ_ONLY);
2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

106

Getting Help
So many peripherals,
how do I get help?
1.

2.

In PCW, Press F1 for context


sensitive help
Look for section labeled
Functional Overviews

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

107

Lab #4
Analog to Digital
Conversion

Lab 4
O
O

O
O

Read A/D conversion


Convert A/D result to 0 to 5 using
float
Light LEDs based upon voltage
C:\masters\1222\Lab4\lab4.c
Complete the lines marked TODO

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

109

Lab 4 Hints
O

C:\masters\1222\Lab4\lab4.c
Complete the lines marked TODO

O
O

Handout has help, finished example


ADC Troubles?
Did you enable A/D with setup_adc()?
Did you set the channel?
Is A/D correct, but fixed() math wrong?
Verify readings using debugger

Bored? Try Extra Credit

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

110

Class Summary

Class Summary
Leaving this class you should:
Be able to use your C knowledge and
write applications using CCS C
Know how to use the CCS software to
load your code onto a PIC MCU and
debug it
Know the ways the CCS C Compiler
speeds up development time with built-in
functions, drivers, example files and
context sensitive help
2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

112

Q&A

Thank You!
Please fill out evaluation
form.

Appendix
Frequently Asked
Questions

Why do I get out of ROM error,


when there is ROM left?
O
O
O

A function must fit into one bank


On 14-bit this is 2K
Split large functions, and main(),
into several smaller functions
This is good programming practice
any way!

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

116

How Can I Reduce Code


Space?
O
O
O
O
O

Use int1 or bit fields for flags


Use fixed point decimal, not float
Divide large functions
Avoid ->, move structure to local
Use access bank mode
#device *=8
read_bank(b,o), write_bank(b,o,v)

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

117

Why is My Math Clipped at


8-bits?
O

Examine the following code:


val16=val8 * val16;

The optimizer will use the


smallest data type for math, in
this case int8
Typecasting forces math into
proper mode:
val16=(int16) val8 * val16;

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

118

Appendix
Programming Details
Miscellaneous

addressmod Block
O

#type default=qualifier
Following declarations will use this
qualifier
If qualifier blank, goes back to default
#type default=nv
char buffer[8192];
#include <memoryhog.h>
#type default=

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

120

Pre-Processor Miscellaneous
O
O
O
O
O
O
O
O

__pcb__
__pcm__
__pch__
__pcd__

Returns the version of specified compiler

__date__
__time__

Time/Date project was compiled

#id CHECKSUM
#id value

Place this value (or checksum) into ID location

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

121

Bit Arrays
O

Array of bits:
int1 flags[30]={FALSE};
flags[i] = TRUE;
if (flags[10]) { /* some code */ }

O
O

Bits are packed


Pointers not supported

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

122

String Parameters
O
O
O

String can be function parameter


LCDPuts(Hello);
Example (RAM):
#device PASS_STRINGS=IN_RAM
void LCDPuts(char *str);

Example (Const RAM):


#device PASS_STRINGS=IN_RAM
#device CONST=ROM
void LCDPuts(const char *str);

Example (ROM):
void LCDPuts(rom char *str);

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

123

#import
O
O
O

Import various files into project.


#import(File=name, Type)
Type = Relocatable (.o, .cof)
only and except specifies what C
symbols

Type = HEX (.hex)

range parameter specifies range

Type = RAW

location gets or sets location


size parameter gets size

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

124

Appendix
Output Files

Output Files
O
O
O
O
O
O
O

HEX Compiled Application


COF Debug Application
ERR Compile output messages
LST C to Assembly comparison
SYM Memory Map
STA Statistics
TRE Call Tree

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

126

SYM Memory Map


O

First section is RAM memory map


005-014
main.buffer
na
main.index

O
O

na indicates no RAM
Following sections include:
Other Input files
ROM memory map
PIC MCU / Compiler Settings

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

127

STA Statistics
Review ROM/RAM/Stack used.
O Statistics for each function:
Page ROM % RAM Functions:
---- --- --- --- ---------0
26
0
1 @delay_ms1
0
284
1
3 ee_reset
O

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

128

STA Statistics
Statistics for each segment:
Segment
Used
Free
---------------00000-00006
4
4
00008-000B2
172
0
000B4-03FFE
15826
378
O

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

129

TRE Statistics
O

Review call tree


Function Name - Segment/ROM RAM
project
main 0/84 Ram=0
init 0/194 Ram=1
RELAY_INIT 0/16 Ram=0
RELAY1_OFF (Inline) Ram=0
@delay_ms1 0/26 Ram=1

Segment will be ? if it wont fit

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

130

Out of ROM
O

Full output:
Out of ROM, A segment or the program is too
large: XXXXXX
Seg w-x, y left, need z
Seg 0-3ff, 12C left, need 12F

Tips:

Be aware of processor segment size


Seg 0-3FF, 3FF left, need 412
Optimize code
Split large functions
Reduce stack usage (allows more CALLs)

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

131

How Can I Reduce Code


Space?
O
O
O
O
O

Use int1 or bit fields for flags


Use fixed point decimal, not float
Divide large functions
Avoid ->, move structure to local
Use access bank mode
#device *=8
read_bank(b,o), write_bank(b,o,v)

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

132

Out of RAM
O

O
O

Full error message:


Not enough RAM for all
variables
Review SYM file
Tips:
Be aware of PIC MCU bank size
Use bit flags
Use local variables (not global)

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

133

Appendix
Other Built-In Functions

Internal Data EEPROM


Access internal data EEPROM
O v = read_eeprom(address)
O write_eeprom(address, v)
O

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

135

Read / Write Program Memory


O

O
O

Treat program memory like data


EEPROM
v=read_program_eeprom(a)
write_program_eeprom(a, v)
Size of v depends on architecture

O
O
O

read_program_memory(a, ptr, num)


write_program_memory(a, ptr, num)
erase_program_memory(a)

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

136

Built-In Functions
Byte Manipulation
O

int8=make8(variable, offset)
Returns one byte from variable

O
O

int16=make16(i8MSB, i8LSB)
int32=make32(iMSB..iLSB)
Returns the combined value

swap(value)
Swaps nibble, saves to value

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

137

Timers
O

setup_timer_X(mode)
Mode contains configuration info, such as
prescalar, postscalar, period, etc.
setup_timer_1(T1_INTERNAL | T1_DIV_BY_4);

O
O

set_timerX(new_count)
current_count=get_timerX()
Set/Get Timer count
Word Safe

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

138

Capture / Compare / PWM


O

setup_ccpX(mode)
Mode contains configuration,
examine header for full options
setup_ccp1(CCP_PWM)

set_pwmX_duty(new_duty)
% = new_duty / (4 * period)

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

139

Peripheral Control
Parallel Slave Port
O

setup_psp(mode)
Mode is PSP_ENABLED or
PSP_DISABLED

O
O
O
O

full=psp_output_full()
avail=psp_input_full()
isOverflowed=psp_overflow()
psp_data
A variable mapped to the PSP I/O port

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

140

Built-In Functions
Miscellaneous
O

ext_int_edge(which, mode)
ext_int_edge(0, H_TO_L)

O
O

port_X_pullups(boolean)
setup_oscillator(mode, finetune)
finetune is optional
setup_oscillator(OSC_2MHZ)

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

141

Appendix
Fixed Memory Placement

#rom
O
O
O

Place raw data into program memory


#rom address={data.data}
Application Ideas:
Initialize the internal data EEPROM
Manually set configuration bits
Manually set ID location
Place strings into ROM

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

143

#inline and #separate


O

#inline
Makes following function inline
Best if used with reference parameters

#separate
Makes following function separate (called)

Generally you should let the


optimizer determine if a function
should be separate or inline

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

144

#locate
O
O

Force location of variable


#locate ident=X

Assigns the C variable ident to location X


X can be a literal, or variable identifier
If not specified, ident is treated as a byte
Compiler allocates X
Can be any structure or type (not const)

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

145

#locate examples
O

Overlaying variable onto SFR:


#locate STATUS=5

Repeat, but using get_env():


#locate STATUS=get_env(SFR:STATUS)

Overlaying two variables:


char buffer[512];
struct { /*protocol */} header;
#locate header=buffer+2

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

146

#byte and #bit


O

#byte ident=X
Same as #locate, but no allocation

#bit ident=X.b
Declares boolean at address X, bit b
Examples:
O
O

#bit CARRY=STATUS.0
#bit CARRY=get_env(BIT:C)

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

147

#rom
O
O
O

Place raw data into program memory


#rom address={data.data}
Application Ideas:
Place strings into ROM
Initialize the internal data EEPROM
Manually set configuration bits
Manually set ID location

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

148

#inline and #separate


O

#inline
Makes following function inline

#separate
Makes following function separate (called)
Disables stack overflow check

Generally you should let the


optimizer determine if a function
should be separate or inline

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

149

#org
Create segment, force code
into segment.
O #org start, end
O

Place following function/constant in


this segment
O

#org start
Continue previous segment

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

150

#org Example
O

Force following code into 0x100-0x1FF

#org 0x100, 0x1FF


void func1(void) {/*code*/}
#org 0x100
const cstring[]=Hello;
#org 0x100
void func2(void) {/*code*/}

//Valid Protoype
#seperate void func1(void);
2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

151

#org
O

#org start, end DEFAULT


Forces all following
function/constants into this segment

#org DEFAULT
Terminate previous DEFAULT

#org start, end { }


Reserve ROM

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

152

#org Example 2
O

Force following code into 0x1000x1FF

#org 0x100, 0x1FF default


void func1(void) {/*code*/}
const cstring[]=Hello;
void func2(void) {/*code*/}
#org default
2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

153

#org
O

View .STA to view current


segment usage:
Segment
0000-0003:
0004-00FF:
0100-01FF:
0200-07FF:

Used
4
250
190
1337

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Free
0
2
66
199

Slide

154

#build
O
O
O

Can change reset and interrupt segment


#build(segment=start:end)
Valid segments:
reset the location of the reset vector
interrupt the location of the interrupt vector
memory external memory for CPU mode

Examples:
#build(reset=0x800, interrupt=0x808)
#build(memory=0x10000:0x1FFFF)

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

155

Appendix
Bootloader Example

Bootloader Overview
O

Two programs:
Loader
Application

Each program #orgd


to their own space
Loader in low memory
(0-7FF)
Application high
memory (800-end)

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

157

Bootloader/Application
Common Code
#define BOOT_END

(0x7FF)

#define APP_START

(BOOT_END+1)

#define APP_ISR

(APP_START+8)

#define PROGRAM_END
getenv(PROGRAM_MEMORY)

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

158

Bootloader Example
Loader Code
//prevent bootloader from using application
#org APP_START , PROGRAM_END { }
#int_global
void isr(void) {
//goto interrupt in application
jump_to_isr(APP_ISR);
}
void main(void) {
if (IsBootloadEvent())
Bootload();
#asm
goto APP_START
#endasm
}
2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

159

Bootloader Example
Application Code
#import(file=loader.hex, range=0:BOOT_END)
#build(reset=APP_START,interrupt=APP_ISR)
#org 0,BOOT_END { }
#int_timer0
void timer(void) { /* do timer0 isr */ }
Void main(void)
/* code */
}

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

160

Appendix
RTOS

RTOS Basics
O

Multitasking thru time-sharing


Tasks appear to run at same time

O
O

Real Time
Task is in one of three states:
Running
Ready
Blocked

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

162

The CCS RTOS


O
O
O

Cooperative Multitasking
Tightly integrated with compiler
Supports ALL PIC MCUs with a
Timer
Available to IDE customers

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

163

RTOS Setup
O

#use rtos(timer=X,
[minor_cycle=cycle_time])
Timer can be any timer available
Minor_Cycle is rate of fastest task
Example:
#use rtos(timer=1, minor_cycle=50ms)

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

164

RTOS Tasks
O

#task(rate=xxxx,
[max=yyyy], [queue=z])
Following function is RTOS task
Will be called at specified rate
Max is slowest execution time, used
for budgeting
Queue defines RX message size

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

165

RTOS Start and Stop


O

rtos_run()
Starts the RTOS
Will not return until rtos_terminate()

rtos_terminate()
Stops the RTOS

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

166

#use rtos(timer=1)
#task(rate=100ms, max=5ms)
void TaskInput(void)
{ /* get user input */ }
#task(rate=25ms)
void TaskSystem(void)
{ /* do some stuff */ }
2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

167

RTOS Task Control


O
O

rtos_enable(task)
rtos_disable(task)

Dynamic task control


Enable/Disable the specified task
Task is the function name
All tasks are enabled at start

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

168

RTOS Messaging
O

rtos_msg_send(task, char)
Sends char to task

avail=rtos_msg_poll()
TRUE if a char is waiting for this task

byte=rtos_msg_read()
Read next char destined for this task

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

169

RTOS Yielding
O

rtos_yield()
Stops processing current task
Returns to this point on next cycle

rtos_await(expression)
rtos_yield() if expression not TRUE

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

170

#task(rate=100ms, max=5ms)
void TaskInput(void) {
if (KeyReady())
rtos_msg_send(TaskSystem,
KeyGet());
}
#task(rate=25ms, queue=1)
void TaskSystem(void) {
2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

171

RTOS Semaphores
O

Semaphore

Determine shared resource availability


A user defined global variable
Set to non-zero if used
Set to zero if free

rtos_wait(semaphore)

rtos_yield() until semaphore free


Once free, sets semaphore as used

rtos_signal(semaphore)
Release semaphore

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

172

RTOS Timing Statistics


O

overrun=rtos_overrun(task)

TRUE if task took longer than max

rtos_stats(task, rtos_stats)

Get timing statistics for specified task

typedef struct
int32 total;
int16 min;
int16 max;
int16 hns;
} rtos_stats;

{
//
//
//
//

total ticks used by task


minimum tick time used
maximum tick time used
us = (ticks*hns)/10

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

173

RTOS Application Ideas


O
O

User I/O
Communication Protocols

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

174

Trademarks
The Microchip name and logo, the Microchip logo, Accuron, dsPIC, KeeLoq,
KeeLoq logo, MPLAB, PIC, PICmicro, PICSTART, PRO MATE, rfPIC and
SmartShunt are registered trademarks of Microchip Technology Incorporated in
the U.S.A. and other countries.
FilterLab, Linear Active Thermistor, MXDEV, MXLAB, SEEVAL, SmartSensor
and The Embedded Control Solutions Company are registered trademarks of
Microchip Technology Incorporated in the U.S.A.
Analog-for-the-Digital Age, Application Maestro, CodeGuard, dsPICDEM,
dsPICDEM.net, dsPICworks, dsSPEAK, ECAN, ECONOMONITOR, FanSense,
In-Circuit Serial Programming, ICSP, ICEPIC, Mindi, MiWi, MPASM, MPLAB
Certified logo, MPLIB, MPLINK, mTouch, PICkit, PICDEM, PICDEM.net, PICtail,
PIC32 logo, PowerCal, PowerInfo, PowerMate, PowerTool, REAL ICE, rfLAB,
Select Mode, Total Endurance, UNI/O, WiperLock and ZENA are trademarks of
Microchip Technology Incorporated in the U.S.A. and other countries.
SQTP is a service mark of Microchip Technology Incorporated in the U.S.A.
All other trademarks mentioned herein are property of their respective
companies.
2008, Microchip Technology Incorporated. All Rights Reserved.

2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS

Slide

175

You might also like