mikrokontrolerski sistemi
DESCRIPTION
mikrokontrolerski sistemiTRANSCRIPT
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 1/175
1222 CCS
Using the CCS C Compilerfor Rapid Development of
Microcontroller Applications
© 2008 Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 1
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 2/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 2
Class ObjectiveWhen you finish this class you will:
−Be able to use your C knowledge andwrite applications using CCS C
− Know the ways the CCS C Compiler
speeds up development time
− Develop new application and codingideas
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 3/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 3
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)
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 4/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 4
Agenda
Hands-on Labs Lab 1: 0 to blinky LED in 60
seconds Lab 2: The CCS IDE Debugger
Lab 3: Stopwatch usinginterrupts
Lab 4: A/D
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 5/175
Compiler Overview
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 6/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 6
Design Goals Ease of use – compile and go!
Quick development time (withbuilt-in functions,drivers/libraries, extensiveexamples)
Easy PIC® MCU to PIC MCU
migration Optimization comparable vs.
assembly
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 7/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 7
CCS Command Line Compiler
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 MPLAB® IDE
Linker − Can’t create relocatable objects
Linux and Windows versions
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 8/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 8
PCW – Windows IDE
C Aware Editor
Debugging (using a CCS ICD)
Integrated RTOS
Linker and Re-locatable objects
Document Generator
Statistics
Flow Chart Editor
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 9/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 9
CCS Support Policy
E-Mail / Phone Support
−Duration is Unlimited!
Updates (new devices, drivers)
− Requires Maintenance Contract
− Maintenance can be extended at anyperiod
Large Customer Base Rapid Releases
− Average once per week
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 10/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 10
CCS ICD In-Circuit Programmer/Debugger
Serial & USB Control Software
−Windows and Linux
CCS PCW IDE for debugging
ICD cable is compatible withMicrochip MPLAB® ICD2
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 11/175
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 12/175
CompilerDemonstration
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 13/175
Hands On Lab #1
Blinky LED
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 14/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 14
Lab 1 Objectives
Hands-on experience with
compiler Use tools and development
board Use CCS Project Wizard
Create blinky-LED
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 15/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 15
Prototyping Hardware
PIC16F877A
−ICD Connection
− Pot on AN0
−Button on RA4
− LEDs on RA5, RB4 and RB5
−RS232 driver and mini-connector
− Header to all GPIO
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 16/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 16
Lab 1: Project Wizard
Project Wizard Overview Creates an initial framework
Assists in the initialconfiguration of internal
peripherals Configuration screen for all
internal peripherals Windows IDE Only
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 17/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. Class 1222 CCS Slide 17
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 18/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 18
Lab 1: Project Wizard
Step 1 – Start Compiler IDE Start the CCS Compiler IDE
−Double click on the icon labeled ‘PICC Compiler’
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 19/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 19
Lab 1: Project Wizard
Step 2 – Start Project Wizard Start the Project Wizard
−File -> New -> Project Wizard
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 20/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 20
Lab 1: Project Wizard
Step 3 – File Name Dialog File name: C:\masters\1222\Lab1\lab1.c
Press Save
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 21/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 21
Lab 1: Project Wizard
Step 4 – Configuration 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
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 22/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. Class 1222 CCS Slide 22
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 23/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 23
Lab 1: Project Wizard
Step 5 – Enter Code 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);
}
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 24/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 24
Lab 1: Project Wizard
Step 6 – Compile Code Compile Code
−Compile -> Compile
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 25/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 25
Lab 1: Project Wizard
Step 7 – Compile Results Inspect the output window for
compile results− You should get 0 Errors
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 26/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 26
Lab 1: Project Wizard
Step 8 – Load HEX Load HEX file with CCS ICD
−Click on the ‘Program Chip’ icon onthe ‘Compile’ ribbon, select ICD
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 27/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 27
Lab 1 Hints
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”
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 28/175
Compiler
Methodology
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 29/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 29
Differences to ‘Common’ C
Case in-sensitive− #case will make it case sensitive
Variables not initialized to zero− #zero_ram inits all variables to 0
const places variable into ROM By default, const strings cannot be
parameter
−SomeFunc(“Hello”) is usually illegal
− #device PASS_STRING=IN_RAM
#device ANSI
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 30/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 30
ANSI Non-Compliance
No fopen(), fclose(), etc
−fprintf() supported, methodology alittle different
printf() formatting string must beknown at compile time
Recursion not allowed on all PIC
®
MCUs
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 31/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 31
Data Types
1bit1bitshort
Unsigned 8bitUnsigned 8bitchar
Signed 64bitUnsigned 32bitlong long
IEEE 64bitNO SUPPORTdoubleIEEE 32bitMCHP 32bitfloat
Signed 32bitUnsigned 16bitlong
Signed 16bitUnsigned 8bitint
PCDPCB/PCM/PCH
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 32/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 32
Data Types
#type can change default size− #TYPE SHORT=8, I NT=16, LONG=32
Optimization Tips− Use only one float type
−
Use ‘int’ as much as possible Non-standard data types
− int1 int8 int16
−int32 int48* int64*
− float32 float48* float64* (PCD ONLY)
− __address__
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 33/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 33
Fixed Point Decimal
Decimals with integers, not floats!
−Faster, Smaller
− 100% precision
Stores value in 10y
power Declaration:
[type] _fixed( y ) [declarator]
int16 _fixed(2) money
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 34/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 34
Fixed Point Examples
int16 _fixed(2) money;
−Range: 0.00 to 655.35
money = 20.50;
money += 5; //adds 5.00 money += value;
printf(“%w”, money);
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 35/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 35
RAM Allocation
RAM is allocated first come, firstserved in this order:
1. globals2. static
3. local
Call tree determines which RAM canbe reused
User can force RAM placement
Structure/Array cannot be larger thanbank
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 36/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 36
‘const’ vs. ‘rom’
‘const’ has two modes:
− #device CONST=ROM DEFAULT
− #device CONST=READ_ONLY
‘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”;
B d f C
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 37/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 37
Borrowed from C++
Reference Parameters Pass address, not value
−More Efficient for large structures
− Values can be changed in function
Declare reference parameters with &
voi d I nc( i nt &i ) {
i ++;
}
Many times compiler will inline
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 38/175
B d f C++
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 39/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 39
Borrowed from C++
Function Overloading Multiple functions, same name
Different parameters Example:
Three different functions called Inc
− int Inc(int *i);
−long Inc(long *l);
− float Inc(float *f);
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 40/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 40
In-Line Assembly
#asm− Starts an assembly block
#endasm− Ends an assembly block
_return_ − Assign return value
−May be corrupted by any C codeafter #endasm
Supports all opcodes
C variables can be accessed
Automatic banking− #asm ASIS - disables auto-
banking
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 1addwf count,f
movwf _return_
#endasm
}
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 41/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 41
Output Files
HEX – Compiled Application
COF – Debug Application ERR – Compile output messages
LST – C to Assembly comparison
SYM – Memory Map
STA – Statistics
TRE – Call Tree
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 42/175
ProgrammingDetails
Required Setup
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 43/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 43
Pre-processor
Design Goal: All in front of you
−Everything is in the source code
− No linker scripts
Pre-processor commandschange compiler-behaviour:
− Methodology
−ROM/RAM placement
− Library configuration
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 44/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 44
#device
Loads personality for the target PIC®
MCU
−#device PIC18F4520
Can be used to alter low-levelspecifics
−#device ICD=TRUE
PIC MCU header files have #device If creating multiple compilation units
(linking), must be defined in eachfile.− Put it in a common include file
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 45/175
#
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 46/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 46
#use delay
Configure the built-in libraries for theclock speed for your target
−#use delay(clock=value)
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
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 47/175
ProgrammingDetails
Memory Placement
#b t / # d
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 48/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 48
#byte / #word
Force location of RAMint val;
#byte val=0xA0
Overlaying variable onto SFR:
#byte STATUS=0x03
#word TMR1=0x0E
Repeat, but using getenv():#byte STATUS=getenv(“SFR:STATUS”)
#bit
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 49/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 49
#bit
#bit ident=X.b
−Declares boolean at address X, bit b
− Examples: #bit CARRY=STATUS.0
#bit CARRY=getenv(“BIT:C”)
Don’t forget:
−Compiler will pack ‘int1’ variables
− You can use ‘struct’ to pack bits
#
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 50/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 50
#org
Fix placement in ROM
#org start, end− Place following function/constant in
specified program memory
#org 0x400,0x4FFSomeFunction() { /*code*/ }
#org 0x500, 0x5FFconst someStruct[] = { … }
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 51/175
Hands On Lab #2
Debugging a Calculator Application
L b 2 Obj ti
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 52/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 52
Lab 2 Objectives
Learn basic debugging use
− Breakpoints
− Watches
− ROM and RAM view
−Peripheral Status
Learn advanced debugging use
−Logging
− 1Wire RS232 on RB3
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 53/175
Lab 2: CCS IDE Debugger
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 54/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 54
Lab 2: CCS IDE Debugger
Step 2 – Load Example Code Load Example Code
−File->Open
− C:\masters\1222\Lab2\lab2.c
Lab 2: CCS IDE Debugger
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 55/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 55
gg
Step 3 – Start Debugger Start Debugger
−Debug -> Enable Debugger
Lab 2: CCS IDE Debugger
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 56/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 56
gg
Debugger GUI Overview
Lab 2: CCS IDE Debugger
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 57/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 57
gg
Step 4 – Load Target Code Compile Project
− Compile -> Compile
− Project will be loaded
Press GO
Lab 2: CCS IDE Debugger
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 58/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 58
gg
Step 5 – 1-Wire RS232 #use rs232(debugger)
−1-Wire RS232 using RB3
− 1-Wire Monitor in Debugger:
Lab 2: CCS IDE Debugger
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 59/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 59
gg
Step 6 – 1-Wire RS232 Using the Monitor, experiment
with the application Application is a simple calculator
Lab 2: CCS IDE Debugger
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 60/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 60
Step 7 – Breakpoints Press STOP
Add a Breakpoint to line 63− printf(“\r\nThe result is %lu”, result)
−Put cursor on Line 63, Press + onBreak Tab
Lab 2: CCS IDE Debugger
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 61/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 61
Step 8 – Watching Open Watch Tab, Press +
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 62/175
Lab 2: CCS IDE Debugger
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 63/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 63
Step 10 – Break Logging Logs an expression at breakpoint
−Breakpoint doesn’t stop execution
Enter result into Break Log
Press GO
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 64/175
BREAK TIME!
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 65/175
Programming Details
addressmod
Pop Quiz
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 66/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 66
Pop Quiz
You need to store 128 valuesbut your PIC® MCU only
has 80 bytes of RAM.
What do you do?
addressmod
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 67/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 67
Application defined storage
−What is ‘normal’ storage?
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)
addressmod Syntax
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 68/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 68
y
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) The IO method to access this memory
Optional
−start/end are the memory range
addressmod Declaration
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 69/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 69
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 */ }
addressmod Usage
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 70/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 70
g
nv NVBuffer[8192];
nv NVID;nv *NVPtr;
#locate NVID=0
NVBuffer[i]=55;*NVPtr++ = 0;
addressmod Ideas
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 71/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 71
External RAM / Flash
Character/Grahic LCD accessthru a multi-dimensional array
Debug/trap critical variables
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 72/175
Programming Details
Interrupts
Pop Quiz
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 73/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 73
What does your ISR
need to do?
Interrupt Service Routine
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 74/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 74
CCS C provides an ISR
−Saves status and scratch registers
− Checks enable/flags, goes to userISR function for that flag
− Clears interrupt flag (if needed)
− Restores status/scratch registers
Interrupt Service RoutineAPI
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 75/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 75
API
enable_interrupts(INT_*)
− enable_interrupts(GLOBAL)
− enable_interrupts(INT_TIMER0)
disable_interrupts(INT_*)
clear_interrupt(INT_*)
interrupt_active(INT_*)
Interrupt Service RoutineAPI
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 76/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 76
API
#int_*− The following function will be processed
for that interrupt
#int_timer0
void isr_timer0(void){
//HANDLE TIMER0 OVERFLOW
}
Interrupt Service RoutineSpecial Identifiers
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 77/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 77
Special Identifiers
#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
Interrupt Service RoutineOptions
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 78/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 78
p
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
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 79/175
Lab #3
Interrupts
Lab 3: Objectives
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 80/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 80
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
Lab 3 Hints
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 81/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 81
Handout has help and finished example TIPS:
− Press F1 to get compiler help manual
− #int_timer0 Following function is called on a Timer 0 overflow
− setup_timer_0(RTCC_DIV_256) Configures Timer 0 to use a divide by 256 prescalar
− enable_interrupts(int_xxx) INT_TIMER0 – Enable Timer 0 Interrupt
GLOBAL – Enables Global Interrupt
− input(PIN_XX) Returns TRUE if PIN_XX is high, else returns FALSE
Button is RA4, or PIN_A4 in CCS
Bored? Try the “ Extra Credit”
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 82/175
ProgrammingDetails
Built-In Functions
Built-In Functions
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 83/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 83
Compiler provides functions to:
−
Control Peripherals (A/D, Serial, etc)− Perform logic optimized to PIC
Internal to compiler, not linked Simplify PIC® MCU to PIC MCU
migration Follow any errata workarounds
Easy PIC® MCU Migration
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 84/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 84
#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);
}}
getenv()
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 85/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 85
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
Device Database and Editor
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 86/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. Class 1222 CCS Slide 86
Don’t forget Drivers andExamples!
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 87/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 87
Drivers provided for complex features orexternal peripherals
−EEPROMs, LCDs, USB, FAT, etc
Many example programs provided toshowcase built-in functions/drivers
Always kept up to date with latestperipherals
Before you start a project, examine the
libraries and example programs CCSprovides you to reduce your developmenttime
Built-In FunctionsGeneral Purpose Input / Output
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 88/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 88
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
Built-In FunctionsGeneral Purpose Input / Output
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 89/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 89
output_X(value)
− Sets the port X (A to J) to the desiredvalue
byte=input_X()
−Read value of port X
set_tris_X(value)
val = get_tris_X()− Get/Set the tristate setting
Built-In FunctionsGeneral Purpose Input / Output
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 90/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 90
#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)
Built-In FunctionsBit Manipulation
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 91/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 91
bool=bit_test(var, bit)
− Returns the value of the specified bit
bit_clear(var, bit)
bit_set(var, bit)
− Set/clear the specified bit
The above bit_XXX() functions
use the PIC® MCU’s bit operationopcodes for maximum efficiency
Built-In FunctionsDelays
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 92/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 92
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
#use delay(clock=125000)
//1 d d l @ 125kH
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 93/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. Class 1222 CCS Slide 93
//1 second delay @ 125kHz
void Delay1s125khz(void) {
delay_ms(1000);}
#use delay(clock=8000000)
//1 second delay @ 8MHz
Void Delay1s8Mhz(void) {delay_ms(1000);
}
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 94/175
Built-In Functions
Serial Libraries
Pop Quiz
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 95/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 95
You need to add another
serial port to your project,but you have used all of the
PIC®
MCU’s peripherals.
What do you do?
UART
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 96/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 96
Powerful UART library built intocompiler
#use rs232(baud=xxxx, xmit=PIN_XX,rcv=PIN_XX, stream=yyyyy)
−TX/RX pins can be any pins
− Many more options exist
Enable bit, parity, collision detection,
open-collector mode, etc.
− Timing is based on #use delay()
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 97/175
Dual UART Example
#use rs232(baud=9600, xmit=PIN C6, rcv=PIN C7,stream=HW UART)
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 98/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 98
# ( , _ , _ ,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);
}
printf() Redirection
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 99/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 99
printf() can be redirected to auser-defined function
Example:
void LCDPut(char c);
printf(LCDPut, “%U”, val);
I2C™ Library
2
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 100/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 100
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
I2C™ Library (Continued)
i2 ()
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 101/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 101
i2c_start()
− Can also be used to send a re-start signal
char=i2c_read(ack)
− Ack is an optional parameter
ack = i2c_write(char)
bool=i2c_available()
−Can only be used with a hardware MSSP
i2c_stop()
SPI
C fi bl SPI lib
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 102/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 102
Configurable SPI library
#use spi(parameters)− HW or SW pins
−
Multiple streams− Clock rate and clock mode
configurable
in = spi_xfer(STREAM_SPI, out)
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 103/175
Built-in Functions
A/D Converter
A/D Converter
t d ( d )
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 104/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 104
setup_adc(mode)− Configure and the ADC; mode varies
for each type
setup_adc_ports(config)
−Configure the pins for analog ordigital mode
#device ADC=X− Assign the ADC result size
A/D Converter (Continued)
t d h l( h l)
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 105/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 105
set_adc_channel(channel)− Set the channel for subsequent reads
val = read_adc(mode)− ADC_START_ONLY – Start a conversion
−
ADC_READ_ONLY – Read lastconversion
− ADC_START_AND_READ – Full cycle
adc_done()− Returns TRUE if conversion is done
A/D Converter Example
//INIT
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 106/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 106
//INIT
#devi ce ADC=10set up_adc( ADC_CLOCK_I NTERNAL ) ;set up_adc_por t s( AN0) ;
//APPLICATION
i nt 16 r esul t ;set _adc_channel ( 0) ;r ead_adc( ADC_START_ONLY) ;
/ * do somet hi ng whi l e A/ D i s busy */whi l e ( ! adc_done( ) ) ;r esul t =r ead_adc( ADC_READ_ONLY) ;
Getting Help
So many peripherals
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 107/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 107
So many peripherals,
how do I get help?
1. In PCW, Press F1 for context
sensitive help
2. Look for section labeled
‘Functional Overviews’
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 108/175
Lab #4
Analog to DigitalConversion
Lab 4
Read A/D conversion
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 109/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 109
Read A/D conversion
Convert A/D result to 0 to 5 usingfloat
Light LEDs based upon voltage
C:\masters\1222\Lab4\lab4.c
−
Complete the lines marked TODO
Lab 4 Hints
C:\masters\1222\Lab4\lab4 c
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 110/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 110
C:\masters\1222\Lab4\lab4.c
− Complete the lines marked TODO
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”
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 111/175
Class Summary
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 112/175
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 113/175
Q & A
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 114/175
Thank You!
Please fill out evaluationform.
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 115/175
Appendix
Frequently AskedQuestions
Why do I get out of ROM error,when there is ROM left?
A function must fit into one bank
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 116/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 116
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 practiceany way!
How Can I Reduce CodeSpace?
Use int1 or bit fields for flags
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 117/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 117
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)
Why is My Math Clipped at8-bits?
Examine the following code:
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 118/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 118
Examine the following code:
− val16=val8 * val16;
The optimizer will use thesmallest data type for math, in
this case int8
Typecasting forces math into
proper mode:− val16=(int16) val8 * val16;
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 119/175
Appendix
Programming DetailsMiscellaneous
addressmod Block
#type default=qualifier
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 120/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 120
yp q− Following declarations will use this
qualifier − If qualifier blank, goes back to default
#type default=nv
char buffer[8192];
#include <memoryhog.h>
#type default=
Pre-Processor Miscellaneous
__pcb__
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 121/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 121
__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
Bit Arrays
Array of bits:
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 122/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 122
y
− int1 flags[30]={FALSE};
− flags[i] = TRUE;
− if (flags[10]) { /* some code */ }
Bits are packed
Pointers not supported
String Parameters
String can be function parameter LCDP t (“H ll ”)
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 123/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 123
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);
#import
Import various files into project.
# ( )
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 124/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 124
#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
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 125/175
Output Files
HEX – Compiled Application
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 126/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 126
COF – Debug Application
ERR – Compile output messages
LST – C to Assembly comparison SYM – Memory Map
STA – Statistics TRE – Call Tree
SYM – Memory Map
First section is RAM memory map
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 127/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 127
005-014 main.buffer
na main.index
‘na’ indicates no RAM
Following sections include:− Other Input files
−ROM memory map
− PIC® MCU / Compiler Settings
STA Statistics
Review ROM/RAM/Stack used.
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 128/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 128
Statistics for each function:
Page ROM % RAM Funct i ons:
- - - - - - - - - - - - - - - - - - - - - - -0 26 0 1 @del ay_ms1
0 284 1 3 ee_r eset
STA Statistics
Statistics for each segment:
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 129/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 129
Segment Used Fr ee
- - - - - - - - - - - - - - - - - -
00000- 00006 4 400008- 000B2 172 0
000B4- 03FFE 15826 378
TRE Statistics
Review call treeF ti N S t/ROM RAM
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 130/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS
Slide 130
− Function Name - Segment/ROM – RAM
pr oj ect
mai n 0/ 84 Ram=0
i ni t 0/ 194 Ram=1
RELAY_I NI T 0/ 16 Ram=0RELAY1_OFF ( I nl i ne) Ram=0
@del ay_ms1 0/ 26 Ram=1
Segment will be ? if it won’t fit
Out of ROM
Full output:Out of ROM, A segment or the program is too
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 131/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 131
, g p glarge: XXXXXX
Seg w-x, y left, need zSeg 0-3ff, 12C left, need 12F
Tips:− Be aware of processor segment size
Seg 0- 3FF, 3FF l ef t , need 412
− Optimize code
− Split large functions
−Reduce stack usage (allows more CALLs)
How Can I Reduce CodeSpace?
Use int1 or bit fields for flags
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 132/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 132
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)
Out of RAM
Full error message:
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 133/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 133
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)
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 134/175
Appendix
Other Built-In Functions
Internal Data EEPROM
Access internal data EEPROM
d ( dd )
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 135/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 135
v = read_eeprom(address)
write_eeprom(address, v)
Read / Write Program Memory
Treat program memory like dataEEPROM
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 136/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 136
EEPROM
v=read_program_eeprom(a) write_program_eeprom(a, v)
−Size of v depends on architecture
read_program_memory(a, ptr, num)
write_program_memory(a, ptr, num)
erase_program_memory(a)
Built-In FunctionsByte Manipulation
int8=make8(variable, offset)
R f
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 137/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 137
− Returns one byte from variable
int16=make16(i8MSB, i8LSB)
int32=make32(iMSB..iLSB)
− Returns the combined value
swap(value)− Swaps nibble, saves to value
Timers
setup_timer_X(mode)
− Mode contains configuration info such as
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 138/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 138
− 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
Capture / Compare / PWM
setup_ccpX(mode)
M d t i fi ti
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 139/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 139
− Mode contains configuration,
examine header for full options
− setup_ccp1(CCP_PWM)
set_pwmX_duty(new_duty)− % = new_duty / (4 * period)
Peripheral ControlParallel Slave Port
setup_psp(mode)
Mode is PSP ENABLED or
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 140/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 140
− 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
Built-In FunctionsMiscellaneous
ext_int_edge(which, mode)
t i t d (0 H TO L)
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 141/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 141
− ext_int_edge(0, H_TO_L)
port_X_pullups(boolean)
setup_oscillator(mode, finetune)
− finetune is optional
− setup_oscillator(OSC_2MHZ)
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 142/175
Appendix
Fixed Memory Placement
#rom
Place raw data into program memory
#rom address={data data}
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 143/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 143
#rom address={data….data}
Application Ideas:
− Initialize the internal data EEPROM
−Manually set configuration bits
− Manually set ID location
−Place strings into ROM
#inline and #separate
#inline
− Makes following function inline
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 144/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 144
g
−Best if used with reference parameters
#separate
− Makes following function separate (called)
Generally you should let theoptimizer determine if a function
should be separate or inline
#locate
Force location of variable
#locate ident=X
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 145/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 145
#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)
#locate examples
Overlaying variable onto SFR:
#locate STATUS=5
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 146/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 146
#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
#byte and #bit
#byte ident=X
− Same as #locate but no allocation
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 147/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 147
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”)
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 148/175
#inline and #separate
#inline
− Makes following function inline
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 149/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 149
#separate− Makes following function separate (called)
− Disables stack overflow check
Generally you should let theoptimizer determine if a function
should be separate or inline
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 150/175
#org Example
Force following code into 0x100-0x1FF
#org 0x100, 0x1FFid f 1( id) {/* d */}
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 151/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 151
void func1(void) {/*code*/}
#org 0x100const cstring[]=“Hello”;
#org 0x100void func2(void) {/*code*/}
//Valid Protoype#seperate void func1(void);
#org
#org start, end DEFAULT
− Forces all following
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 152/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 152
Forces all following
function/constants into this segment
#org DEFAULT
−Terminate previous DEFAULT
#org start, end { }
−Reserve ROM
#org Example 2
Force following code into 0x100-0x1FF
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 153/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 153
#org 0x100, 0x1FF defaultvoid func1(void) {/*code*/}
const cstring[]=“Hello”;
void func2(void) {/*code*/}
#org default
#org
View .STA to view current
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 154/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 154
segment usage:Segment Used Free
0000-0003: 4 00004-00FF: 250 2
0100-01FF: 190 66
0200-07FF: 1337 199
#build
Can change reset and interrupt segment
#build(segment=start:end)
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 155/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 155
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)
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 156/175
Appendix
Bootloader Example
Bootloader Overview
Two programs:
− Loader
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 157/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 157
− Application
Each program #org’d
to their own space− Loader in low memory
(0-7FF)
− Application highmemory (800-end)
Bootloader/ApplicationCommon Code
#define BOOT_END (0x7FF)
#define APP_START (BOOT_END+1)
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 158/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 158
#define APP_ISR (APP_START+8)
#define PROGRAM_END
getenv(“PROGRAM_MEMORY”)
Bootloader ExampleLoader Code
//prevent bootloader from using application#org APP_START , PROGRAM_END { }
#int_global
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 159/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 159
_g
void isr(void) {//goto interrupt in applicationjump_to_isr(APP_ISR);
}
void main(void) {if (IsBootloadEvent())
Bootload();
#asm goto APP_START#endasm
}
Bootloader Example Application Code
#import(file=loader.hex, range=0:BOOT_END)
#build(reset=APP_START,interrupt=APP_ISR)
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 160/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 160
#org 0,BOOT_END { }
#int_timer0
void timer(void) { /* do timer0 isr */ }
Void main(void) {/* code */
}
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 161/175
Appendix
RTOS
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 162/175
The CCS RTOS
Cooperative Multitasking
Tightly integrated with compiler
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 163/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 163
Supports ALL PIC® MCUs with aTimer
Available to IDE customers
RTOS Setup
#use rtos(timer=X,[minor_cycle=cycle_time])
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 164/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 164
−Timer can be any timer available
− Minor_Cycle is rate of fastest task
−Example:
#use rtos(timer=1, minor_cycle=50ms)
RTOS Tasks
#task(rate=xxxx,[max=yyyy], [queue=z])
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 165/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 165
−Following function is RTOS task
− Will be called at specified rate
−Max is slowest execution time, usedfor budgeting
−
Queue defines RX message size
RTOS Start and Stop
rtos_run()
− Starts the RTOS
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 166/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 166
−Will not return until rtos_terminate()
rtos_terminate()
−Stops the RTOS
#use r t os( t i mer =1)
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 167/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 167
#t ask( r at e=100ms, max=5ms)voi d TaskI nput ( voi d)
{ / * get user i nput */ }
#t ask( r at e=25ms)
voi d TaskSyst em( voi d){ / * do some st uf f */ }
RTOS Task Control
rtos_enable(task)
rtos_disable(task)
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 168/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 168
− Dynamic task control
− Enable/Disable the specified task
−Task is the function name
− All tasks are enabled at start
RTOS Messaging
rtos_msg_send(task, char)
− Sends char to task
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 169/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 169
avail=rtos_msg_poll()
− TRUE if a char is waiting for this task
byte=rtos_msg_read()
− Read next char destined for this task
RTOS Yielding
rtos_yield()
− Stops processing current task
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 170/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 170
−Returns to this point on next cycle
rtos_await(expression)
−rtos_yield() if expression not TRUE
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 171/175
RTOS Semaphores
Semaphore− Determine shared resource availability
− A user defined global variable
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 172/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 172
−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
RTOS Timing Statistics
overrun=rtos_overrun(task)− TRUE if task took longer than max
t t t (t k t t t )
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 173/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 173
rtos_stats(task, rtos_stats)− Get timing statistics for specified task
typedef struct {
int32 total; // total ticks used by taskint16 min; // minimum tick time used int16 max; // maximum tick time used int16 hns; // us = (ticks*hns)/10
} rtos_stats;
RTOS Application Ideas
User I/O
Communication Protocols
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 174/175
© 2008 Custom Computer Services Inc.,, Microchip Technology Incorporated. All Rights Reserved. 1222 CCS Slide 174
7/21/2019 mikrokontrolerski sistemi
http://slidepdf.com/reader/full/mikrokontrolerski-sistemi 175/175