Using The CCS C Compiler For Rapid Development of Microcontroller
Using The CCS C Compiler For Rapid Development of Microcontroller
1526 CCS
Slide
Class Objective
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
Agenda
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)
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
Agenda
Hands-on Labs
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
Compiler Overview
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
Design Goals
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
Product Line:
-
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
C Aware Editor
Debugging
Integrated RTOS
Linker and Relocatable Objects
Document Generator
Flow Chart Editor
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
Compiler
Demonstration
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
10
Hands-on Lab #1
Blinky LED
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
11
Lab 1 Objectives
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
12
Prototyping Hardware
PIC16F887
ICD Connection
Pot on AN0
Button on RA4
LEDs on RA5, RB4 and RB5
RS232 driver and mini-connector
Header to all GPIO
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
13
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
14
Lab 1 Hints
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
16
Compiler
Methodology
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
17
Differences to Common C
Case insensitive
#case will make it case sensitive
#device ANSI
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
18
ANSI Non-Compliance
PIC MCUs
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
19
Data Types
short
char
int
PCB/PCM/PCH
1-bit
Unsigned 8-bit
Unsigned 8-bit
PCD
1-bit
Unsigned 8-bit
Signed 16-bit
long
Unsigned 16-bit
Signed 32-bit
long long
Unsigned 32-bit
Signed 64-bit
float
MCHP 32-bit
IEEE 32-bit
double
NO SUPPORT
IEEE 64-bit
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
20
Data Types
Optimization Tips
Use int over char/long as much as possible
If you must use float, use only one type
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
int16
int64*
float64* (PCD ONLY)
1526 CCS
Slide
21
Bit Arrays
Array of bits:
int1 flags[30];
flags[i] = TRUE;
if (flags[10]) { /* some code */ }
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
22
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
23
money = 20.50;
money += 5;
//adds 5.00
money += value;
printf(%w, money);
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
24
RAM Allocation
first come, first served in this
Allocated
order:
- globals
- static
- local
Call tree finds RAM that can be reused
User can force RAM placement
Struct/array cannot be larger than bank
- Enhanced PIC16, PIC18, 16-bit PIC MCUs
exempt
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
25
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
26
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
27
i++;
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
28
Get(&c, 500);
time will be 500
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
29
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
30
In-Line Assembly
#asm
Starts an assembly block
#endasm
Ends an assembly block
_return_
Assign return value
May be corrupted by any C code
after #endasm
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
31
Output Files
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
32
Programming
Details
Required Setup
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
33
Pre-processor
Pre-processor commands
change compiler-behaviour:
Methodology
ROM/RAM placement
Library configuration
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
34
#device
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
35
#fuses
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
36
#use delay
Set PIC
- #use delay(clock=value)
1526 CCS
Slide
37
Programming
Details
Memory Placement
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
38
#byte / #word
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
39
#bit
#bit ident=X.b
Declares boolean at address X, bit b
Examples:
#bit CARRY=STATUS.0
#bit CARRY=getenv(BIT:C)
Dont forget:
Compiler will pack int1 variables
You can use struct to pack bits
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
40
#org
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
41
Hands-On Lab #2
Debugging a Calculator
Application
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
42
Lab 2 Objectives
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
43
Lab 2 Directions
calculator
Follow the directions on the
handout
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
44
BREAK TIME!
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
45
Programming
Details
Built-In Functions
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
46
Built-In Functions
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
47
#if defined(__PIC24__)
#include <24FJ64GA004.h>
#elif defined(__PIC18__)
#include <18F4520.h>
#elif defined(__PIC16__)
#include <16F887.h>
#endif
#fuses HS, NOWDT, NOLVP
#use delay(clock=20000000)
void main(void) {
while(TRUE) {
output_toggle(PIN_C0);
delay_ms(500);
}
}
getenv()
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
49
Built-In Functions
General Purpose Input / Output
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
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
51
Built-In Functions
General Purpose Input / Output
output_X(value)
Sets the port X (A to J) to the value
byte = input_X()
Read value of port X
set_tris_X(value)
val = get_tris_X()
Get/Set the tristate setting
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
52
Built-In Functions
General Purpose Input / Output
#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)
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
53
Built-In Functions
Bit Manipulation
bit_clear(var, bit)
bit_set(var, bit)
Set/clear the specified bit
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
54
Built-In Functions
Delays
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
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
55
#use delay(clock=125K)
#use timer()
defined by
TICKS_PER_SECOND
compiler
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
57
start = get_ticks();
while(
(get_ticks() start) <
(TICKS_PER_SECOND*3)
)
{
1526 CCS
Slide
58
Built-In Functions
Serial Libraries
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
59
Pop Quiz
1526 CCS
Slide
60
UART
1526 CCS
Slide
61
UART (Continued)
Standard C I/O
- printf(string, );
- char = getc();
- fprintf(stream, string, );
- char = fgetc(stream);
bool = kbhit(stream);
setup_uart(newBaud, stream)
- Dynamically change the UART
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
62
printf() Redirection
Example:
void LCDPut(char c);
printf(LCDPut, %U, val);
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
64
getc() Timeout
#use rs232(timeout=5)
v = getc();
If no character within 5ms:
- getc() returns 0x00
- RS232_ERRORS is 0x00
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
65
I2C Library
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
66
i2c_start()
Can also be used to send a restart signal
char = i2c_read(ack)
Ack is an optional parameter
ack = i2c_write(char)
bool = i2c_available()
Slave only command
i2c_stop()
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
67
SPI
in = spi_xfer(out)
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
68
Built-In Functions
Peripherals and More
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
69
#use touchpad()
boolean = touchpad_hit()
char = touchpad_getc()
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
70
Writing:
- write_eeprom(address, v)
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
71
A/D Converter
setup_adc(mode)
Configure the ADC; mode varies for each
PIC MCU
setup_adc_ports(config)
Configure the pins for analog or digital
mode
set_adc_channel(channel)
Set the channel for subsequent reads
val = read_adc()
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
72
Getting Help
So many peripherals,
how do I get help?
1.
2.
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
73
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
74
Lab #3
Analog to Digital
Conversion
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
75
Lab 3 Objective
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
76
Lab 3 Hints
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
77
Programming Details
Interrupts
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
78
Pop Quiz
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
79
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
80
enable_interrupts(INT_*)
enable_interrupts(GLOBAL)
enable_interrupts(INT_TIMER0)
disable_interrupts(INT_*)
clear_interrupt(INT_*)
bool = interrupt_active(INT_*)
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
81
#INT_*
The following function will be processed
for that interrupt
#INT_TIMER0
void isr_timer0(void)
{
//HANDLE TIMER0 OVERFLOW
}
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
82
#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
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
83
#device HIGH_INTS=TRUE
Must set to use high/fast ISR on PIC18
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
84
#include <18F46K22.H>
#device HIGH_INTS=TRUE
#INT_RDA HIGH
void IsrUartRx(void)
{
/* some code */
}
#INT_TIMER0 NOCLEAR
Void IsrTimer0(void)
{ clear_interrupt(INT_TIMER0);
//some code
}
void main(void)
{
enable_interrupts(GLOBAL);
enable_interrupts(INT_RDA);
enable_interrupts(INT_TIMER0);
// some code
}
Lab #4
Interrupts
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
86
Lab 4: Objectives
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
87
Lab 4 Hints
setup_timer_0(RTCC_DIV_256)
enable_interrupts(INT_xxxx)
input(PIN_XX)
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
88
Class Summary
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
89
Class Summary
1526 CCS
Slide
90
Q&A
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
91
Thank You!
Please fill out
the evaluation form
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
92
Appendix
Frequently Asked
Questions
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
93
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
94
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
95
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
96
Appendix
Programming Details
Miscellaneous
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
97
Pre-Processor Miscellaneous
__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
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
98
String Parameters
Example (ROM):
void LCDPuts(rom char *str);
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
99
#import
Type = RAW
location gets or sets location
size parameter gets size
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
100
Appendix
Output Files
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
101
Output Files
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
102
main.buffer
main.index
na indicates no RAM
Following sections include:
Other Input files
ROM memory map
PIC MCU / Compiler Settings
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
103
STA Statistics
Review ROM/RAM/Stack used
Statistics for each function:
Page ROM % RAM Functions:
---- --- --- --- ---------0
26
0
1 @delay_ms1
0
284
1
3 ee_reset
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
104
STA Statistics
Statistics for each segment:
Segment
Used
Free
---------------00000-00006
4
4
00008-000B2
172
0
000B4-03FFE
15826
378
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
105
TRE Statistics
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
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
106
Out of ROM
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)
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
107
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
108
Out of RAM
Full error message:
Not enough RAM for all
variables
Review SYM file
Tips:
1526 CCS
Slide
109
Programming Details
addressmod
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
110
Pop Quiz
1526 CCS
Slide
111
addressmod
typemod, on steroids!
Can be used on any data type
Pointers, structs, unions and arrays
const and rom not allowed
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
112
addressmod Syntax
addressmod(identifier,[read,write,]start,end)
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
113
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 */ }
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
114
addressmod Usage
nv char NVBuffer[8192];
nv char NVID;
nv char *NVPtr;
#locate NVID=0
NVBuffer[i]=55;
*NVPtr++ = 0;
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
115
addressmod Block
#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=
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
116
addressmod Ideas
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
117
Appendix
Other Built-In Functions
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
118
Built-In Functions
Byte Manipulation
swap(value)
Swaps nibble, saves to value
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
119
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
120
setup_ccpX(mode)
Mode contains configuration,
examine header for full options
setup_ccp1(CCP_PWM)
set_pwmX_duty(new_duty)
% = new_duty / (4 * period)
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
121
Timers
setup_timer_X(mode)
Mode contains configuration info, such as
prescalar, postscalar, period, etc.
setup_timer_1(T1_INTERNAL | T1_DIV_BY_4);
set_timerX(new_count)
current_count=get_timerX()
Set/Get Timer count
Word Safe
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
122
Peripheral Control
Parallel Slave Port
setup_psp(mode)
Mode is PSP_ENABLED or
PSP_DISABLED
full=psp_output_full()
avail=psp_input_full()
isOverflowed=psp_overflow()
psp_data
A variable mapped to the PSP I/O port
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
123
Built-In Functions
Miscellaneous
ext_int_edge(which, mode)
ext_int_edge(0, H_TO_L)
port_X_pullups(boolean)
setup_oscillator(mode, finetune)
finetune is optional
setup_oscillator(OSC_2MHZ)
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
124
Appendix
Fixed Memory Placement
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
125
#inline
Makes following function inline
Best if used with reference parameters
#separate
Makes following function separate (called)
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
126
#locate
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
127
#locate examples
1526 CCS
Slide
128
#byte ident=X
Same as #locate, but no allocation
#bit ident=X.b
Declares boolean at address X, bit b
Examples:
#bit CARRY=STATUS.0
#bit CARRY=get_env(BIT:C)
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
129
#rom
1526 CCS
Slide
130
#inline
Makes following function inline
#separate
Makes following function separate (called)
Disables stack overflow check
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
131
#org
#org start
Continue previous segment
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
132
#org Example
//Valid Protoype
#seperate void func1(void);
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
133
#org
#org DEFAULT
Terminate previous DEFAULT
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
134
#org Example 2
1526 CCS
Slide
135
#org
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
Used
4
250
190
1337
1526 CCS
Free
0
2
66
199
Slide
136
#build
Examples:
#build(reset=0x800, interrupt=0x808)
#build(memory=0x10000:0x1FFFF)
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
137
#rom
1526 CCS
Slide
138
Appendix
Bootloader Example
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
139
Bootloader Overview
Two programs:
Loader
Application
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
140
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)
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
141
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
}
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
142
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 */
}
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
143
Appendix
RTOS
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
144
RTOS Basics
Multitasking through
time-sharing
Tasks appear to run at same time
Real Time
Task is in one of three states:
Running
Ready
Blocked
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
145
Cooperative Multitasking
Tightly integrated with compiler
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
146
RTOS Setup
#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)
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
147
RTOS Tasks
#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
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
148
rtos_run()
Starts the RTOS
Will not return until rtos_terminate()
rtos_terminate()
Stops the RTOS
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
149
#use rtos(timer=1)
#task(rate=100ms, max=5ms)
void TaskInput(void)
{ /* get user input */ }
#task(rate=25ms)
void TaskSystem(void)
{ /* do some stuff */ }
void main(void) {
while(TRUE) {
rtos_run();
sleep();
}
}
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
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
151
RTOS Messaging
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
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
152
RTOS Yielding
rtos_yield()
Stops processing current task
Returns to this point on next cycle
rtos_await(expression)
rtos_yield() if expression not TRUE
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
153
#task(rate=100ms, max=5ms)
void TaskInput(void) {
if (KeyReady())
rtos_msg_send(TaskSystem, KeyGet());
}
#task(rate=25ms, queue=1)
void TaskSystem(void) {
SystemPrepare();
rtos_await(rtos_msg_poll());
SystemDo(rtos_msg_read());
rtos_yield();
SystemVerify();
}
RTOS Semaphores
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
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
155
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;
{
//
//
//
//
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
156
User I/O
Communication Protocols
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
157
Trademarks
The Microchip name and logo, the Microchip logo, dsPIC, KeeLoq, KeeLoq logo,
MPLAB, PIC, PICmicro, PICSTART, PIC32 logo, rfPIC and UNI/O are registered
trademarks of Microchip Technology Incorporated in the U.S.A. and other countries.
FilterLab, Hampshire, HI-TECH C, Linear Active Thermistor, MXDEV, MXLAB,
SEEVAL and The Embedded Control Solutions Company are registered trademarks
of Microchip Technology Incorporated in the U.S.A.
2011 Custom Computer Services, Inc., Microchip Technology Incorporated. All Rights Reserved.
1526 CCS
Slide
158