You are on page 1of 38

2010

@Ware EduProto III- 4553


Embedded System Design and Programming Guide using Microchip PIC18F4553/4550 and @Ware Executive Subjacent System
A student guide that gives instructions on how to implement an educative embedded system platform based on Microchip PIC18F4553/4550, a high performance 12MIPS 8-bit microcontroller with USB 2.0 Full Speed support. Also includes @Ware ESS SDK documentation for application developers on @Ware Executive Subjacent System. In this guide students will discover an easy way to learn the electronics, informatics, digital signal processing and digital control involved in the design, development and testing of embedded system applications.

M. Sc. Pedro Arturo Cornejo Torres @Ware Systems 01/04/2010

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

@Ware EduProto III- 4553


Embedded System Design and Programming Guide using Microchip PIC18F4553/4550 and @Ware Executive Subjacent System

By M. Sc. Pedro Arturo Cornejo Torres

Abstract
A student guide that gives instructions on how to implement an educative embedded system platform based on Microchip PIC18F4553/4550, a high performance 12MIPS 8 -bit microcontroller with USB 2.0 Full Speed support. Also includes @Ware ESS SDK documentation for application developers on @Ware Executive Subjacent System. In this guide students will discover an easy way to learn the electronics, informatics, digital signal processing and digital control involved in the design, development and testing of embedded system applications.

QUEDAN PROHIBIDOS EL USO Y LA REPRODUCCIN TOTAL O PARCIAL DE ESTE DOCUMENTO POR CUALQUIER MEDIO SIN PERMISO POR ESCRITO O CONCENTIMIENTO DEL AUTOR Copyright M. en C. Pedro Arturo Cornejo Torres MMX pcornejo@gdl.cinvestav.mx Guadalajara, Jalisco, Mxico Microsoft, Microchip y dems marcas y productos mencionados en este documento, son nombres y marcas registradas por sus respectivas compaas.

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

Table of Contents
1 2 Introduction .......................................................................................................................... 5 Assembling the @Ware EduProto III-4553 microcontroller educative platform.................. 6 2.1 2.2 3 @Ware EduProto III-4553 Schematic Diagram ............................................................. 7 Part list .......................................................................................................................... 8

@Ware Executive Subjacent System v2.5 ............................................................................. 9 3.1 3.2 @Ware ESS v2.5 Features ............................................................................................. 9 @Ware ESS Memory Organization in a PIC18F4553................................................... 10

@Ware ESS Program Memory and User Application.......................................................... 10 4.1 4.2 4.3 4.4 4.5 User Application Entry Point ....................................................................................... 11 Interrupt Vectors and Interrupt Handlers ................................................................... 11 System Calls ................................................................................................................. 12 API Access .................................................................................................................... 13 Configuration settings for PIC18F4553 ....................................................................... 13

@Ware ESS Operation ........................................................................................................ 15 5.1 The ESSCONTROL flags ................................................................................................ 17

6 7

USART .................................................................................................................................. 18 Preparing a User Application Workspace in MPLAB ........................................................... 19 7.1 Workspace and project creation ................................................................................. 19 Step 1: Starting MPLAB IDE ................................................................................. 19 Step 2: Creating a PIC18F4553 workspace and developer tools association ...... 19 Step 3: PIC Model Selection. ............................................................................... 20 Step 4: Tool suite selection ................................................................................. 20 Step 5: Workspace and project name. ................................................................ 21 Step 6: Workspace naming and location............................................................. 21 Step 7: Workspace and Project information verification .................................... 22 Step 8: Adding existing files to the project ......................................................... 22 Step 9: Executing project creation and settings .................................................. 23

7.1.1 7.1.2 7.1.3 7.1.4 7.1.5 7.1.6 7.1.7 7.1.8 7.1.9 7.2 v2.5 7.3 Set

Adding required files into the project to develop applications based on @Ware ESS 24 Configuring Code and Data Models and Code Generation using Extended Instruction 30 3

@Ware Systems 8

M.Sc. Pedro Arturo Cornejo Torres

Simulating and debugging applications in MPLAB .............................................................. 33 8.1 8.2 8.3 8.4 Enabling MPLAB SIM ................................................................................................... 33 Microcontroller Configuration Bits used in simulation ............................................... 33 Debugger Settings ....................................................................................................... 35 Timing while application is simulated ......................................................................... 36

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

1 Introduction
In the last years, microcontrollers have been used successfully in many applications such as communications, human interface devices, entertainment, commodities, mobile devices, remote controls, robots, automotive industry, heavy industry, medical devices, and more others. The reasons that justify the need of learning and understanding the great world of microcontrollers are more than clear. Embedded systems that are based on microcontrollers are everywhere, even in the space. Currently, powerful microcontrollers are available from multiple providers as Freescale, Microchip, Atmel, Intel and others. Firmware developers for these microcontrollers have adopted the C language to accelerate the development of complex embedded systems. These projects must accomplish both hardware and software requirements to be reliable, safe, efficient and precise. Embedded system design is not only firmware programming work, it also involves knowledge and practical skills in electronics and digital control, because most embedded system applications implies some kind of digital signal processing and control. You must be proficient in computer programming to build, in most cases in embedded design, time critical firmware. The proper use of computing power provided by the CPU cores, the correct and adequate use of the peripherals inside modern microcontroller and good circuitry design, will lead to cost effective and efficient solutions for industrial production, office and home applications. With this document, users can learn how to implement a powerful educative embedded system platform using Microchip PIC18F4553 microcontroller. This educative platform is called @Ware EduProto III-4553. With this microcontroller platform users can develop and test a considerable number of embedded applications. With 12MIPS of computing power and USB connectivity that PIC18F4553 microcontroller provides, users will find a modern and convenient way to learn embedded systems design and programming.

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

2 Assembling the @Ware EduProto III-4553 microcontroller educative platform


The purpose of this section is to give you guidance on what do you need to build your own hardware based on Microchip PIC18F4553 microcontroller. The special selection of components and their interconnection will make your new hardware compatible with @Ware Executive Subjacent System Firmware, or simply @Ware ESS. The hardware configuration has been called @Ware EduProto III-4553 and it is capable of connectivity with a personal computer by the use of microcontrollers on-chip USB and @Ware ESS firmware. Detailed information about Microchip PIC18F4553 can be found at www.microchip.com. Download the datasheet and use it as primary reference while you are assembling or programming the device. The @Ware EduProto III-4553 platform has the following features: Microchip PIC18F4553 as main microcontroller chip. Its RISC CPU core is based on Harvard architecture running at 48MHz and executes instructions as fast as 12 MIPS. It includes 32KB of Flash EEPROM accessible as program space, 2KB of static RAM accessible as data space and 256 bytes of EEPROM. The PIC18F4553 contains a USB 2.0 Full Speed transceiver, 5 I/O ports, 20 interrupt sources, 4 timers, 2 Capture-ComparePWM Modules, Enhanced USART RS-232, LIN capable, I2C or SPI Communication Module, A 12-bit ADC with 13 multiplexed channels, 3 oscillators for clock source, power managed operation modes and more. USB Type B Connector Receptacle. Supports mechanical and reliable USB connection with @Ware EduProto III-4550 and a host personal computer. You can use an USB A/B cable to connect a host personal computer and @Ware EduProto III-4553. This allows an embedded application to exchange information with a PC or to be used for developing purposes when @Ware ESS is executing. This is a cost-effective method for loading firmware from PC. To load, erase or execute programs on the PIC18F4553, please use @Ware Embedded SDK Tools. Autorun Switch. Allows automatic application program execution when powered up or user reset. The application program will start after @Ware ESS initializes chip and USB possible connection with a host is configured. A USB configured connection is not required for application program start. Reset Button. A button that is always required in development and test process. If something is wrong with the firmware execution or PIC18F4553 does not respond to user commands or simply a crash occurs, you will have to press it. Dual Power Source with on board 5V-1A regulator. An automatic switch circuit allows @Ware EduProto III-4553 to be powered directly from VUSB or an external 12V-35V power source. If the external power source is available then it will be used as primary source and no current will be drained from VUSB. If external power source is not present, VUSB will be used, draining current from the host device. If VUSB is used then the embedded application must not exceed the 500mA limit defined by the USB 2.0 specification. The maximum current provided by the switch transistor is 500mA.

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

2.1 @Ware EduProto III-4553 Schematic Diagram


This is the schematic diagram of the @Ware EduProto III-4553. You can use a prototyping breadboard or design your own printed circuit board. Take into account distances between certain circuit elements to avoid noise induction and erratic operation.

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

2.2 Part list


@Ware EduProto III-4553 is very simple and requires a few components to work. Please, download important datasheets related with listed semiconductors.
Table 1 @Ware EduProto III-4553 part list

Part Label R1,R2 R3,R4 R5,R9 R6,R7 R8 R10 C1,C2 C3 C4 C5 X1 D1,D4 D2,D3 Q1 SW1 SW2 J1 U1 U2

Part Identifier or value 1K W 100K W 10K W 330 W 4.7K W 47K W 22pF Ceramic Capacitor 220nF Polyester Capacitor 10uF Electrolytic Capacitor 1uF Electrolytic Capacitor 4MHz Crystal 1N4001 1A Rectifier Diode LED PN2907 - 500mA or 2N3906 -100mA. General Purpose Amplifier PNP Transistor Push button Mini-switch SPST USB Type B Receptacle IC PIC18F4553-I/P Microcontroller programmed with @Ware Executive Subjacent System v2.5 or higher. IC 7805 Positive 5V regulator

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

3 @Ware Executive Subjacent System v2.5


The @Ware EduProto III-4553 platform uses a PIC18F4553 microcontroller programmed with @Ware Executive Subjacent System v2.5 firmware. This is required in order to be ready to load, erase and execute user programs into microcontroller directly from PC. @Ware ESS avoids the need of an expensive universal device programmer. Students only need access to a device programmer when they load the firmware. This also reduces cost in laboratory equipment using at least one device programmer for scheduled massive firmware programming. Some @Ware ESS portions are based on a customized version from Microchip USB Application Framework. The Microchip USB Application Framework can be downloaded from www.microchip.com.

3.1 @Ware ESS v2.5 Features


The @Ware ESS firmware has the following features: @Ware ESS sets a versatile chip configuration that enables the PIC18F4553 microcontroller functionality when powered up. This configuration includes activation of the extended instruction set to be used by optimized C compilations. @Ware ESS also activates a 48MHz clock generator to execute up to 12MIPS. More detailed information about chip configuration established by @Ware ESS, please see section 4.5 Configuration settings for PIC18F4553 in page 13. @Ware ESS enables users to upload, execute and erase user application firmware from the PC. This is done using @Ware ESS SDK tools. For more detailed information about these tasks, please see When @Ware EduProto III-4553 is connected with an USB host, @Ware ESS emulates a device connected to PC via a COM port. Users can develop applications for the PC that directly communicate with @Ware EduProto III-4553. @Ware ESS implements a state machine for a logical communication protocol using data packets with simple datagram structure. More detailed information about how to create PC applications that have communication with @Ware ESS and @Ware EduProto III-4553, please see ... @Ware ESS shares its communication protocol with both the USB emulated COM port and the on chip USART. When @Ware EduProto III-4553 is powered up, the USART is configured at 250,000bps allowing asynchronous mode communication with other nearby devices. The communication is done using ping-pong like byte transactions and time out count to avoid stall. @Ware ESS communication protocol and state machine can be extended to use I2C or other underlying hardware protocol. @Ware ESS does not handle any interrupt vector. @Ware ESS redirects interrupt vectors to user application. This is to avoid high latency when handling interruptions and improve application responsiveness when an interrupt event occurs. Also, allows users to manage interrupts in many ways improving design flexibility. See Section 4.2 Interrupt Vectors and Interrupt Handlers in page 11 for details. @Ware ESS supports power managed modes. Using @Ware ESS facilities, a simple system call can be done to change clock speed. @Ware ESS implements three power managed modes: Full Power Mode (48MHz), Low Power Mode (8MHz-31250Hz) and 9

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

Sleep Mode. The last mode is invoked by the USB host device or a proper execution of a sleep instruction. More information about power managed modes, please see

3.2 @Ware ESS Memory Organization in a PIC18F4553


Once firmware was programmed on a PIC18F4553 microcontroller, @Ware ESS requires the first 8192 bytes in the program memory and there are 24KB of free program memory for user application code. A detailed @Ware ESS memory organization is given in Figure 1. The application may use @Ware ESS facilities to initiate, use and terminate an USB connection with a host, reducing user application code size. Also, @Ware ESS reserves 512 bytes of RAM only if the user application intends to use @Ware ESS facilities. The first 8192 bytes of program space are not available for reading or writing while users application is in execution. Write protection and copy protection are enabled to avoid illegal copies and accidental overwrite on @Ware ESS code. Execution permission of @Ware ESS code is allowed through system calls. Only a device programmer can erase @Ware ESS.

Figure 1 @Ware ESS memory organization

Program Memory 0x000000 @Ware ESS Program Code 8KB 0x0000-0x03FF

Data Memory

Free 1KB

0x001FFF 0x002000

0x0400-0x05FF Free 24KB 0x0700-0x07FF

@Ware ESS Data 512Bytes

0x0600-0x06FF

Free 256 Bytes Software Stack 256 Bytes Not implemented

0x007FFF 0x0F60-0x0FFF

SFR Bank

4 @Ware ESS Program Memory and User Application


@Ware ESS is written in the first 8192 bytes of program memory. This memory cannot be used to store user application code, because @Ware ESS configuration has write protected its code. Since user application code cannot be written in that memory block, the user application code

10

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

must be stored starting at program memory address 0x002000. @Ware ESS exposes an application interface to implement interrupt handlers and perform system calls.

4.1 User Application Entry Point


The special reset vector at address 0x000000 is always handled by @Ware ESS and it is not redirected. When @Ware ESS has initialized the PIC18F4553 and run application command is in progress, then @Ware ESS uses a call instruction to address 0x002000. The address 0x002000 is always the entry point for user applications. When user application ends and executes the outer return instruction, @Ware ESS will take execution control. It is not required by user application to return execution control to @Ware ESS when finished. This could be true because user application may stay in a loop or waiting for interrupts to occur.
Figure 2 User Application Entry Point

Program Memory 0x000000 @Ware ESS


If Autorun is activated or Run Application command is in progress, the execution is transferred to address 0x002000. In any case the PIC18F4553 has been initialized and Full Power is activated.

User Application Entry Point

0x002000

User Application

4.2 Interrupt Vectors and Interrupt Handlers


An interrupt vector is simply an address of a pointer to or an overlay to a function that handles or services an interrupt event. An overlay is generally a simple goto instruction that redirects execution control to another part of the program. Both overlays and interrupt handlers are required when using a PIC18F4553. The PIC18F4553 exposes only two interrupt vectors: a High Priority Interrupt vector and a Low Priority Interrupt vector. The High Priority Interrupt vector is located at program memory address 0x000008 and low priority interrupt vector is located at program memory address 0x000018. See Figure 3 to know how interrupt redirection is done. @Ware ESS implementation redirects the High Priority Interrupt vector from address 0x000008 to address 0x002008. The program memory address 0x002008 becomes the new vector to a High Priority Interrupt handler.

11

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

The Low Interrupt Vector is redirected from program memory address 0x000018 to address 0x002018. The address 0x002018 becomes the new vector to a Low Interrupt Priority Interrupt handler. The redirected vectors 0x002008 and 0x002018 are available to be defined by user application.
Figure 3 Interrupt vectors redirection

Program Memory
High Priority Interrupt Vector Low Priority Interrupt Vector

0x000008 0x000018

goto 0x002008 goto 0x002018

@Ware ESS

Redirected interrupt vectors

0x002008 0x002018

goto MyHPIHandler goto MyLPIHandler

Application must implement overlays and handlers if activates and uses interrupts. The application saves and restores application context in the interrupt handler. Microchip C18 compiler automatically generate code that saves and restores application context.

User Application

4.3 System Calls


User applications can invoke functionality provided by @Ware ESS. This can be done through a system call. A system call is performed using ESS_SystemCall macro. System calls are provided to perform common tasks that @Ware ESS already solves, like USB communication tasks, USART tasks, status Tx/Rx LEDs tasks, power management and more.

12

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

Program Memory

System Call Vector

0x000180

System Call Handler

@Ware ESS

User Application can use @Ware ESS functionality performing a sytem call when it needs.

User Application
call 0x000180

Note: A system call must be supplied with a command ID and its parameter using software stack. A system call eventually returns with a result placed in W register.

4.4 API Access


User application can use an API set to use even more granular functionality from @Ware ESS. An API set is a table of pointers to data or pointers to functions. These pointers can be used to perform direct calls to specific functions or for accessing data memory locations that @Ware ESS uses. Currently there are two API sets: the USB_API and USART_API. Lower level functionality like USB data transfers functions are only available through these API sets. See SDK for more details. To query for a pointer to an API set, the application must use the ESS_GetAPI macro and provide one of these values: API_INDEX_USB or API_INDEX_USART.

4.5 Configuration settings for PIC18F4553


When @Ware ESS is programmed into a PIC18F4553 an initial hardware configuration is established in order to operate. This initial configuration is stored in flash program memory when device is programmed. The Table 2 is an important reference when using and configuring microcontrollers peripherals. The configuration is applied by hardware each time the PIC18F4553 microcontroller is powered up, defining a hardware profile that cannot be changed during software execution. Write protection is enabled on those bits to ensure coherence with external components, and be able to execute @Ware ESS and user application code. Users can erase configuration using a device programmer, doing so, will also erase @Ware ESS.

13

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

Table 2 PIC18F4553 Configuration bits values established by @Ware ESS firmware

Configuration bits PLLDIV = 1 CPUDIV = OSC1_PLL2 USBDIV = 2 FOSC = XTPLL_XT FCMEN = ON IESO = ON PWRT = ON BOR = SOFT BORV = 3 VREGEN = ON WDT = OFF WDTPS = 256 MCLRE = ON LPT1OSC = OFF PBADEN = OFF CCP2MX = OFF STVREN = ON LVP = OFF ICPRT = OFF XINST = ON

Description of current configuration No prescale (4 MHz oscillator input drives PLL directly) CPU Clock = 48MHz USB clock source comes from the 96 MHz PLL divided by 2 XT oscillator, PLL enabled, XT used by USB Fail-Safe Clock Monitor enabled Oscillator Switchover mode enabled PWRT enabled Brown-out Reset enabled and controlled by software (SBOREN is enabled) 1 Minimum setting (2V) USB voltage regulator enabled Watchdog HW Disabled - SW Controlled 2 4ms*256 WDT Period MCLR pin enabled; RE3 input pin disabled Timer1 oscillator configured for higher power operation PORTB<4:0> pins are configured as digital I/O on Reset CCP2 input/output is multiplexed with RB3 Stack full/underflow will cause Reset Single-Supply ICSP disabled Dedicated In-Circuit Debug/Programming ICPORT disabled Instruction set extension and Indexed Addressing mode enabled

1 2

Brown-out Reset is disabled by firmware when @Ware ESS starts, users can enable BOR. Watch Dog Timer is disabled by default when @Ware ESS starts, users can enable WDT.

14

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

5 @Ware ESS Operation


The normal operation of @Ware ESS on @Ware EduProto III-4550 can be explained with the following pseudo code. During @Ware ESS operation, no interruptions are handled because it is designed for low resource consumption. In fact, @Ware ESS is implemented in a pooling basis. void ESSMain(void) { RCONbits.SBOREN=0; ESSCONTROL=0; ESSCONTROLbits.g_bRunProgram:=PORTAbits.RA5; //Autorun? InitializeStatusLEDs(); InitializeUSART(); InitializeADC(); while(1) //Do forever { ESS_USBDeviceTasks(); ESS_USARTTasks(); if( ESSCONTROLbits.g_bRunProgram || ESSCONTROLbits.g_bLoopProgram ) { ESSCONTROLbits.g_bRunProgram=TRUE; If(UserApplicationIsLoaded) { ExecuteUserApplication(); DisableAllInterrupts(); ReInitializeUSB(); ReInitializeUSART(); ReInitializeADC(); } } else if(USBDeviceIsNotConnected) { SleepAndWaitForUSBConnection(); } }// end while }//end while

When pressing Reset button or powering up the @Ware EduProto III-4553, the following occurs: If @Ware EduProto III-4553 is powered only by the USB host, @Ware ESS opens a connection with the host even if the Autorun switch was closed (enabled). User application must disable the USB SIE (clear UCON<USBEN> bit), if USB communication is not used. Also, if AutoRun switch was opened, @Ware ESS will take execution control, otherwise user application is executed. If @Ware EduProto III-4553 is powered by external power source and AutoRun switch was closed, @Ware ESS will disable USB SIE before user application starts. Also, if user 15

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

application is not loaded, @Ware ESS will enter in Sleep Mode waiting for USB connection. When USB connection occurs, @Ware ESS will open a connection and takes execution control.

16

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

5.1 The ESSCONTROL flags


ESSCONTROL is a private variable used by @Ware ESS to store control flags. These flags reflect the state of some features. There are six flags defined that are accessible to user application. Here is a description of these flags. The ESSCONTROL variable is accessible for reading or writing through a system call. See system call commands for more information.
Table 3 Description of the @Ware ESS control flags

Bit # 0 (LSB)

Bit Name g_bRunProgram

Description
This flag is set when one of the following conditions is meet: 1) When Autorun switch is closed when a reset event or power up event occurs. 2) When a Run Program command is received from a USB host. User application must not clear this flag. This flag is always set when user application is in execution. This flags indicates if low power mode is activated. When @Ware ESS starts this flag is clear and Full Power Mode. In Full Power Mode, CPU and peripherals are clocked at 48MHz. User application can set this bit to enter Low Power Mode. In Low Power Mode, CPU and peripherals are clocked at 8MHz using INTOSC. User application can modify OSCCON<IRCF2:IRCF0> bits to select different frequencies. Warning: Using frequencies below 2MHz may not be fast enough to hold or initiate a stable USB connection due to slow firmware execution. Note: Changing CPU and peripherals frequency does not affect USB SIE clock source. USB SIE uses only 48MHz clock source always. This bit is clear by default. User application sets this flag to activate Tx/Rx LED notifications. If user application clears this flag high impedance is seen in RE0 and RE1. This can be used for energy save. This flag is set by default. If this flag is set USB SIE hardware and firmware driver are enabled to initiate and hold USB connectivity with a host. If this bit is clear, no USB connections are allowed and USB SIE is disabled. In this case USB host will declare a disconnection even if PIC18F4553 microcontroller remains plugged. User application can clear and then set this flag to reinitialize USB driver in case of communication problems. This flag is set by default. The user application sets this flag if it is required that @Ware ESS executes user application again each time it returns. After each iteration @Ware ESS clears GIE/GIEH, GIEL/PEIE, IPEN, SWDTEN bits and then initializes ADC, USART and USB (if disabled). No RAM cleaning or other initializations are performed. This flag is clear by default. If this flag is set by user application, PIC18F4553 microcontroller will be allowed to enter Sleep Mode when USB host sleeps. By default, the user application execution will not be suspended when the USB host sleeps. This flag is clear by default. User application must not modify this flag. User application must not modify this flag.

g_bLowPower

g_bStatusLEDEnable

g_bUSBEnable

g_bLoopProgram

g_bAllowSuspend

6 7 (MSB)

Reserved Reserved

The following sequence shows how to read and modify ESSCONTROL flags. 1. User application must first ESS_QueryESSCONTROL macro. read the current ESSCONTROL state using

17

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

2. Using logic masks user application can set or clear specific flags. 3. Finally, use ESS_WriteESSCONTROL macro to update @Ware ESS with the new configuration. Users can also use uESSCONTROL union type provided with @Ware ESS SDK. This is a C code example. #include interface.h //@Ware ESS Interface Header included ... unsigned char ESSCONTROL; ... //Read ESSCONTROL status first using a system call ESSCONTROL=ESS_QueryESSCONTROL(); //Modify flags, in this case, turn off USB Notification LEDs ESSCONTROL = ESSCONTROL & 0xFE; //Update ESSCONTROL register using a system call ESS_WriteESSCONTROL(ESSCONTROL);

6 USART
48000000 8000000 4000000 2000000 1000000 500000 250000 125000 31250 25 25 25 25 25 25 25 25 25 115384.615 19230.7692 9615.38462 4807.69231 2403.84615 1201.92308 600.961538 300.480769 75.1201923 115200 19200 9600 4800 2400 1200 600 300 75 416 416 416 416 416 416 416 416 416 1040 1040 1040 1040 1040 1040 1040 1040 1040

See @Ware Embeded Software Developer Kit. Every system call requires two parameters: a function ID (1 byte size) and its function parameter (3 bytes size). These parameters must be pushed into software stack, then perform a call instruction to address 0x000180. When returning from a system call, the application needs to restore the stack. The address 0x000180 is the entry point for a system call runtime in @Ware ESS program space.

18

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

7 Preparing a User Application Workspace in MPLAB


To develop and cross compile a solution based on @Ware ESS, you can use Microchip MPLAB IDE and Microchip C18 compiler. These software packages can be downloaded from www.microchip.com.

7.1 Workspace and project creation


The following examples are supposing that the developer user has already installed MPLAB IDE and the academic version of Microchip C18. 7.1.1 Step 1: Starting MPLAB IDE Start MPLAB IDE from the corresponding Microchips program group. When the IDE is ready, an untitled project is opened. In the next step, a new workspace will be created and corresponding tools will be activated.

7.1.2 Step 2: Creating a PIC18F4553 workspace and developer tools association From the Project menu, choose Project Wizard command. The following figure shows the Project Wizard presentation window. Press the Next button to continue.

19

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

7.1.3 Step 3: PIC Model Selection. From the device list, choose PIC18F4553. This informs to wizard to prepare tools related with this PIC model. Press the Next button to continue.

7.1.4 Step 4: Tool suite selection From the Active Tool Suite dropdown list, choose Microchip C18 Toolsuite. In the next step the project name and its location will be defined. Press the Next button to continue.

20

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

7.1.5 Step 5: Workspace and project name. In the following dialog box, define a new project name. It is recommended the use of Browse button to locate and create a known place for project files. Press the Browse button.

7.1.6 Step 6: Workspace naming and location Locate a known place and create a folder named ESSAPP01 in the Save As dialog, finally define the Workspace file name as ESSAPP01. Press the Save button to close the Save as dialog box.

21

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

7.1.7 Step 7: Workspace and Project information verification Now, the project name and its location have been defined. Press the Next button to continue. Please check file pathname to be sure about actions performed.

7.1.8 Step 8: Adding existing files to the project The project and workspace are almost ready. In the following figure, the wizard is asking for already existing files. Simply press Next.

22

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

7.1.9 Step 9: Executing project creation and settings This is the final step of the workspace creation and configuration. A summary will be presented. Please check for correct information. If something is not as expected, go back and modify the information provided. Press Finish button to continue.

23

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

7.2 Adding required files into the project to develop applications based on @Ware ESS v2.5
In the later section, a project and its workspace have been created and configured.

Now, it is required to add new files. A basic set of files must be copied where project is situated. The following files must be copied into projects folder. File name startup.c Brief description @Ware Systems modified startup code written in C language. This code will initialize any global variable that any source file declares and initializes. Must be compiled and linked with the other projects files. The code establishes the first sequence of instructions to be executed before main function executes. The startup code puts the first instruction at program address 0x002000. Do not exclude this file or the application will not be able to start from the main function. This file can be founded in the \startup folder of your @Ware ESS SDK package. ESSAPP_18F4553.lkr The required linker script. This file defines the memory maps for code and data. The linker must execute this script as part of the project. This informs the linker where code and data must be correctly located. Three important items are defined: The Software Stack Segment, Code Segment and Data Segments. This file can be founded in the \lkr folder of your @Ware ESS SDK package. command.h (Optional) If you intent to execute host commands into your @Ware EduProto III-4553, then this file must be added into your Visual Studio Solution and in your MPLAB workspace. It contains important structures and types related with command packing and communications between a PC and @Ware EduProto III-4553 using USB connection. interface.h (Optional) If you intent to use @Ware ESS facilities in your application, this file must be included in your source code. This header file also includes command.h header file into your implementation 24

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

automatically. It contains all the required structures, constants, macros and types required to perform invocations and callbacks using @Ware ESS v2.5 firmware. The following figure shows the projects folder with these new files as its contents.

Now that files have been copied into the projects folder, it is required to add them as part of the projects. Only copying those files into the projects folder does not mean that them will be compiled or executed. Projects configuration must be aware of those files too in order to process them. Right click on projects name ESSAPP01.mcp in workspace view and choose Add files... command from the emerging popup menu. See the figure below.

25

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

Once clicked, a dialog box will appear asking for files to add. Use the jump to dropdown list and choose Project Directory. Doing so will change the current directory view to the project folder. Also be sure to check the options showed in the following figure. These options must be checked: Remember this settings and User: Files where created especially for this project, use relative path. This will allow exporting, copying or transferring your project elsewhere, since it uses relative paths.

Now select the above mentioned files and add them into the project. You can use Ctrl key while clicking on specific files. Please, be sure to set the file filter to All files (*.*) to perform a single aggregation operation. You can use this method to add more files. It is recommended that existing files must be copied first into projects folder. When you are done, press the Open button to add the specified files. See the following figure to confirm how this selection is done.

26

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

After aggregating files is done, the IDE looks like this.

Finally, create a C source file called main.c. This will be your primary source file. Right click on the projects name at workspace view and choose Add new file... command. This is shown in the following figure.

27

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

In the Add new file to project dialog box at the Name write main.c as the new file name. Please, be sure to locate your projects folder before pressing Save button.

Now, after creating the main.c file, a new edition window will be opened. See following figure.

28

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

Please do not write any code or compile it yet. It is needed to configure the code and data models before to start. See next section to perform this final configuration.

29

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

7.3 Configuring Code and Data Models and Code Generation using Extended Instruction Set
@Ware ESS firmware had configured the PIC18F4553 microcontroller to be able to use the Extended Instruction Set. Therefore, the application must use Extended Instruction Set too. Some options must be configured before any compiling or linking process may occur. Again, right click on the projects name at workspace view and choose Build options... command. See the following figure that shows how to do this.

The following dialog box will appear after command selection. Click on the Directories tab and add the specified entry in the figure. This will inform to linker where standard libraries might be found. See following figure to see which entry must be added. The entry path may be totally different if you did choose a different directory when Microchip C18 was installed.

30

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

Now in the MPLAB C18 tab, be sure the following configuration meets. This will activate an adequate code, data and stack model.

31

@Ware Systems And the following too, to generate optimized code.

M.Sc. Pedro Arturo Cornejo Torres

Now, it is time to enable extended instruction set. See the following figure. Please be sure to mark Extended mode check box. This will enable the linker to generate code that uses the Extended Instruction Set. If you do not activate this, the generated code will be unable to execute in @Ware EduProto III-4553 platform.

At this point, press OK and you will be ready to develop. Get Fun! 32

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

8 Simulating and debugging applications in MPLAB


Before to upload the firmware into microcontroller, it is possible to simulate and debug the application. This can be done using MPLAB SIM. Simulation is a very effective technique to analyze the execution evolution and can be used for timing the application without using a real microcontroller. This can save time and reduce developing costs.

8.1 Enabling MPLAB SIM


To enable MPLAB SIM, just choose the MPLAB SIM debugger tool under Debugger menu. This is shown in the figure below.

Once MLAB SIM is enabled you can set any number of breakpoints in the applications source code using the source editor. To set or clear a breakpoint, just double click on the source line of interest.

8.2 Microcontroller Configuration Bits used in simulation


It is required to define witch set of instructions will be simulated. PIC18F4550/4553 microcontroller has two instruction sets: Traditional Instruction Set and Extended Instruction Set. @Ware ESS is implemented using Extended Instruction Set and applications must be implemented using the same instruction set. This is also required when simulating the application. To enable simulation of the Extended Instruction Set, open the Configuration Bits window under Configure menu as shown in the figure below.

33

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

When the Configuration bits is open, look for the Extended Instruction Set Enable bit and set the configuration to Enable at the dropdown list as shown in the next figure.

Be sure that Configuration Bits set in code checkbox is not checked. If it is checked, the developer user must provide explicit configuration in source code level. Please see the Microchip C18 users Manual for more information about Configuration Bits in Source Code. The other bits can be configured as show in the figure above. This will simulate the hardware environment established by @Ware ESS v2.5. The relevant configuration bytes are from address 0x300000 to address 0x300006. As you can see, these addresses are not in the scope

34

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

of CPU program space and they can be set only when the chip is programmed by a device programmer. Please, refer to PIC18F4550 datasheet for more information.

8.3 Debugger Settings


Now, it is required to configure some simulation options in order to properly debug the application. The most important option is the simulation clock speed. When the application is executed, the microcontrollers clock speed may be different than 48MHz, so it is required to set the proper clock speed. Users can change the clock speed as required. @Ware ESS uses 48MHz clock speed by default when @Ware EduProto III is powered up. See the next figure that shows how to open the Simulation Settings window.

Once Simulation Setting window is open, change simulator clock frequency to 48MHz as shown in the next figure.

35

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

At this point, you are able to simulate and debug your application. If breakpoints are activated in the source code, you can press the play button at the top bar in the main window. The play command will rebuild your application if required.

8.4 Timing while application is simulated


Timing the application while it is simulated can be used to measure the computation time required by any portion of the application code. MPLAB SIM provides a Stopwatch for this purpose. This stopwatch is very easy to use and it behaves like a real life stopwatch. When application simulation starts, the stopwatch will also start to count the amount of instructions cycles. See the next figure to show the Stopwatch window.

36

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

The stopwatch looks as shown in the next figure. When the stopwatch is shown by first time and no simulation is in progress, the stopwatch is in reset state. The stopwatch will be reinitialized when a new simulation starts.

In the next example, a breakpoint is defined. The simulation starts when F9 key is pressed or play button is clicked. The simulation breaks when a breakpoint is reached and time in instruction cycles and its equivalent in seconds, milliseconds or microseconds are shown. In the example below, the first breakpoint reached is at Delay function invocation. At that point, some instruction cycles have been counted because startup.c code has been simulated before main.

37

@Ware Systems

M.Sc. Pedro Arturo Cornejo Torres

From now on, you can press F7 key if you want to debug into the Delay function, press F8 key if you want to debug over the Delay function, or press F9 key if you want to execute up to the next breakpoint. The stopwatch can be zeroed from the point you want to measure. See the next figure that shows the stopwatch in action. In the figure above the simulation is paused in the breakpoint reached first. The stopwatch also indicates the total instruction cycles simulated and time in microseconds elapsed.

38

You might also like