Programming the PIC18/ XC8 Using C- Coding Dr. Farahmand

Similar documents
Embedded C Programming

Displaying a Numerical Value on HyperTerminal (or equ.)

Programing the Microprocessor in C Microprocessor System Design and Interfacing ECE 362

Getting Started with PIC24F/PIC24H Programming and Interfacing in C

An Introduction to MPLAB Integrated Development Environment

Keil C51 Cross Compiler

Microcontroller Systems. ELET 3232 Topic 8: Slot Machine Example

The programming language C. sws1 1

Programming PIC Microcontrollers in PicBasic Pro Lesson 1 Cornerstone Electronics Technology and Robotics II

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

SKP16C62P Tutorial 1 Software Development Process using HEW. Renesas Technology America Inc.

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Freescale Semiconductor, I

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

2 ASCII TABLE (DOS) 3 ASCII TABLE (Window)

Pemrograman Dasar. Basic Elements Of Java

Simple C Programs. Goals for this Lecture. Help you learn about:

MPLAB C18 C Compiler

PC Base Adapter Daughter Card UART GPIO. Figure 1. ToolStick Development Platform Block Diagram

Informatica e Sistemi in Tempo Reale

Lecture 22: C Programming 4 Embedded Systems

The Answer to the 14 Most Frequently Asked Modbus Questions

AVR Butterfly Training. Atmel Norway, AVR Applications Group

Microcontroller Code Example Explanation and Words of Wisdom For Senior Design

ET-BASE AVR ATmega64/128

3. Programming the STM32F4-Discovery

DsPIC HOW-TO GUIDE Creating & Debugging a Project in MPLAB

1. The demonstration code is sending garbage to my terminal program. What are the recommended settings?

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

XMOS Programming Guide

Lab 1 Course Guideline and Review

13. Publishing Component Information to Embedded Software

SPI. Overview and Use of the PICmicro Serial Peripheral Interface. Getting Started: SPI

How To Program A Microcontroller Board (Eb064) With A Psp Microcontroller (B064-74) With An Ios 2.5V (Power) And A Ppt (Power Control) (Power Supply) (

RN-131-PICTAIL & RN-171-PICTAIL Evaluation Boards

Fondamenti su strumenti di sviluppo per microcontrollori PIC

C Programming Language

Tutorial - Creating Your Own Applications

ELEG3924 Microprocessor Ch.7 Programming In C

8051 MICROCONTROLLER COURSE

Sources: On the Web: Slides will be available on:

SmartFusion csoc: Basic Bootloader and Field Upgrade envm Through IAP Interface

First Bytes Programming Lab 2

Control III Programming in C (small PLC)

EasyPIC4 User s Manual

Develop a Dallas 1-Wire Master Using the Z8F1680 Series of MCUs

sqlite driver manual

LC-3 Assembly Language

Fall 2006 CS/ECE 333 Lab 1 Programming in SRC Assembly

Implementing SPI Master and Slave Functionality Using the Z8 Encore! F083A

Using C to Access Data Stored in Program Space Memory on the TMS320C24x DSP

Application Note: AN00141 xcore-xa - Application Development

Dear Reader. The latest tutorial will be available from now on from the website of my consulting company Argenox Technologies:

MPLAB TM C30 Managed PSV Pointers. Beta support included with MPLAB C30 V3.00

Using the HCS12 Serial Monitor on Wytec Dragon-12 boards. Using Motorola s HCS12 Serial Monitor on Wytec s Dragon-12 boards

Interrupts and the Timer Overflow Interrupts Huang Sections What Happens When You Reset the HCS12?

C Programming Structure of a C18 Program

Project Manager Editor & Debugger

Introduction to PIC Programming

Serial Communications

Developing an Application on Core8051s IP-Based Embedded Processor System Using Firmware Catalog Drivers. User s Guide

Application Note: AN00121 Using XMOS TCP/IP Library for UDP-based Networking

Evo Laser Firmware Developer s Manual

V850E2/ML4 APPLICATION NOTE. Performance Evaluation Software. Abstract. Products. R01AN1228EJ0100 Rev Aug. 24, 2012

8-bit Microcontroller. Application Note. AVR286: LIN Firmware Base for LIN/UART Controller. LIN Features. 1. Atmel LIN/UART Controller

Start A New Project with Keil Microcontroller Development Kit Version 5 and Freescale FRDM-KL25Z

Hi Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

GUIDE FOR THE INSTALLATION OF C18 COMPILER IN MPLAB IDE

C++ Language Tutorial

Introduction to Microcontrollers

Lecture N -1- PHYS Microcontrollers

ARM Thumb Microcontrollers. Application Note. Software ISO 7816 I/O Line Implementation. Features. Introduction

Voltage Measurement with A PIC Microcontroller

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

APPLICATION NOTE. AT07175: SAM-BA Bootloader for SAM D21. Atmel SAM D21. Introduction. Features

K8048 PIC PROGRAMMER BOARD

AVR033: Getting Started with the CodeVisionAVR C Compiler. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

Yun Shield Quick Start Guide VERSION: 1.0 Version Description Date 1.0 Release 2014-Jul-08 Yun Shield Quick Start Guide 1 / 14

The stack and the stack pointer

Lecture 7: Programming for the Arduino

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

The C Programming Language course syllabus associate level

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Software Serial Port for ROM/RAM Monitor

Using C to Access Data Stored in Program Memory on the TMS320C54x DSP

LCD I 2 C/Serial RX Backpack. Data Sheet. LCD to I2C/Serial RX Backpack I2C or Serial RX communication with Standard 16 Pin LCD modules

Tutorial for MPLAB Starter Kit for PIC18F

Opgui and OpenProg user s guide. v.1.2

EE8205: Embedded Computer System Electrical and Computer Engineering, Ryerson University. Multitasking ARM-Applications with uvision and RTX

Why using ATmega16? University of Wollongong Australia. 7.1 Overview of ATmega16. Overview of ATmega16

PICmicro tm Development Board

EXERCISE 3: String Variables and ASCII Code

ENGI E1112 Departmental Project Report: Computer Science/Computer Engineering

Chapter 12. Data Converters. à Read Section 19 of the Data Sheet for PIC18F46K20. Updated: 4/19/15

Hitchhiker's Guide to CodeWarrior EE371, EE475 Fall 2005

Chapter 5 Real time clock by John Leung

Transcription:

Programming the PIC18/ XC8 Using C- Coding Dr. Farahmand Updated: 3/14/12

The C Compiler n The C18/XC8 compiler is a free program for students used for programing the PIC in C- Language. n Programming in C-Language greatly reduces development time. n C is NOT as efficient as assembly q A good assembly programmer can usually do better than the compiler, no matter what the optimization level C WILL use more memory

PIC Compiler Third Party C-Compilers Available: HI-TECH - PICCTM, www.htsoft.com IAR - EWB-PIC, www.iar.com CCS PIC18/XC8 C Compiler, www.ccsinfo.com

CCS Compiler n Provides C++ like reference parameters to functions n C Compiler Quick Start Webinar q http://www.ccsinfo.com/videos/quickstart.asx q For more click here: http://www.personal.rdg.ac.uk/~stsgrimb/teaching/ programming_pic_microcontrollers.pdf

C and Assembly Compilers or C18/XC8 or PICKit 2

The C18/XC8 Compiler n Mixed language programming using C-language with assembly language is supported by the C18/ XC8 compiler. q q Assembly blocks are surrounded with at _asm and a _endasm directives to the C18/XC8 compiler. Assembly code can also be located in a separate asm file n Example: asm("movlw 0x1F"); n The compiler normally operates on 8-bit bytes (char), 16-bit integers (int), and 32-bit floating-point (float) numbers. n In the case of the PIC, 8-bit data should be used before resorting to 16-bit data. q Floats should only be used when absolutely necessary.

C18 Only! The C Library Reference Guide n Read the practice exercise to see how you can combine C and ASM codes together Note that we use: // for comments! A bit must be defined! Note: It is possible to use extended assembly instructions using C18/XC8 compiler must be purchased! (Brey p.414) For more information: http://ww1.microchip.com/downloads/en/devicedoc/c18/xc8_user_guide_51288j.pdf#51

Using Assembly in C Code

Setting up the IDE in MPLAB n When writing software in C-Language, the IDE is setup in a similar fashion to assembly language. n Start the IDE and perform the steps outlined on the next few slides.

Development Tools Data Flow C Source Files.c C Compiler Assembly Source Files (.asm or.s).s Compiler Driver Program Assembly Source Files (.asm or.s).s Assembler Object File Libraries (Archives) (.lib or.a) Archiver (Librarian).o.a Linker Object Files Executable.hex.map Memory Map Linker Script (.lkr or.gld).gld.cof MPLAB IDE Debug Tool Cof: Common object file format

Development Tools Data Flow C Compiler.c Preprocessor.h C Header File Compiler Assembly Source File.s

C Runtime Environment n C Compiler sets up a runtime environment q Allocates space for stack q Initializes stack pointer q Copies values from Flash/ROM to variables in RAM that were declared with initial values q Clears uninitialized RAM q Disables all interrupts q Calls main() function (where your code starts)

C Runtime Environment n Runtime environment setup code is automatically linked into application by most PIC compiler suites n Usually comes from either: q crt0.s / crt0.o (crt = C RunTime) q startup.asm / startup.o n User modifiable if absolutely necessary

A little about C Programming. n For the most part you are on your own! n Read the handouts in the library for more information.

Fundamentals of C A Simple C Program Example Preprocessor Directives Header File #include <stdio.h> #define PI 3.14159 int main(void) { float radius, area; Constant Declaration (Text Substitution Macro) Variable Declarations Function } //Calculate area of circle radius = 12.0; area = PI * radius * radius; printf("area = %f", area); Comment

Comments Two kinds of comments may be used: Block Comment /* This is a comment */ Single Line Comment // This is also a comment /******************************************************** * Program: hello.c * Author: A good man ********************************************************/ #include <stdio.h> /* Function: main() */ int main(void) { printf( Hello, world!\n ); /* Display Hello, world! */ }

Variables and Data Types A Simple C Program Example #include <stdio.h> #define PI 3.14159 Data Types int main(void) { float radius, area; Variable Declarations } //Calculate area of circle radius = 12.0; area = PI * radius * radius; printf("area = %f", area); Variables in use

Variables Variables are names for storage locations in memory Variable declarations consist of a unique identifier (name) 15 Data Memory (RAM) 0 int warp_factor; char first_letter; 41 A float length; 5.74532370373175 10-44

Data Types Type Description Bits char int float double single character integer single precision floating point number double precision floating point number 8 16 32 64 The size of an int varies from compiler to compiler. MPLAB-C30 int is 16-bits MPLAB-C18/XC8 int is 16-bits CCS PCB, PCM & PCH int is 8-bits Hi-Tech PICC int is 16-bits

Data Type Qualifiers Modified Integer Types Qualifiers: unsigned, signed, short and long Qualified Type Min Max Bits unsigned char char, signed char unsigned short int short int, signed short int unsigned int int, signed int unsigned long int long int, signed long int unsigned long long int long long int, signed long long int 0-128 0-32768 0-32768 0-2 31 0-2 31 255 127 65535 32767 65535 32767 2 32-1 -2 31 2 64-1 -2 31 8 8 16 16 16 16 32 32 64 64

Literal Constants n A literal is a constant, but a constant is not a literal q #define MAXINT 32767 q const int MAXINT = 32767; q Constants are labels that represent a literal q Literals are values, often assigned to symbolic constants and variables n Literals or Literal Constants q q q q Are "hard coded" values May be numbers, characters, or strings May be represented in a number of formats (decimal, hexadecimal, binary, character, etc.) Always represent the same value (5 always represents the quantity five)

Literal Constants Example unsigned int a; unsigned int c; #define b 2 Literal void main(void) { a = 5; Literal c = a + b; printf("a=%d, b=%d, c=%d\n", a, b, c); }

Using IDE

MPLAB C18/XC8 Directory Structure n n MPLAB C18/XC8 can be installed anywhere on the PC. Its default installation directory is the C: \mcc18/xc8 folder. MPLAB IDE should be installed on the PC prior to installing MPLAB C18/XC8.

Program Examples n n n Make and Build the project If you are using standard outputs: q Select Debugger>Settings and click on the Uart1 IO tab. The box marked q Enable Uart1 IO should be checked, and the Output should be set to Window Select large code model

Example #pragma is Directive instructing the compiler program indicating the setup for configuration bits

Configuration Bits Modified for PIC18/XC8F4xK20 #pragma is used to declare directive; Config directive allows configuring MCU operating modes; device dependent 1MHz

Another Example Basic idea: Initially RA4 is one When switch is pressed RA4 =0 Then RA & 16 = 0 à Start Counting & display the number on PORTB Note: 16d = 0x10 Assume using 4MHz internal clock (250 nsec)à one instruction cycle is 1 usec Delay1KTCYx(1)à Delay of 1000 instruction cycles Delay100TCYx(10)à Delay of 1000 instruction cycles

Another Example Alternatively, we could have: è While (PORTAbits.RA4 == 0)

Time Delay Functions Function Example Note Delay1TCY Delay1TCY(); Inserts a single NOP instruction into the program Delay10TCYx Delay10TCYx(10); Inserts 100 instruction cycles (number must be between 0 and 255) (0 causes a delay of causes 2560) Delay100TCYx Delay100TCYx(10); Inserts 1000 instruction cycles (number must be between 0 and 255) (0 causes a delay of 25,600) Delay1KTCYx Delay1KTCYx(3); Inserts 3000 instruction cycles (number must be between 0 and 255) (0 causes a delay of 256,000) Delay10KTCYx Delay10KTCYx(20); Inserts 20,000 instruction cycles (number must be between 0 and 255) (0 causes a delay of 2,560,000)

Random Number Generator Display a random number when RA4 is pressed Random number will be between 0-9

Random Number Generator Modified for PIC18/XC8F4xK20 int seed; void main (void) { TRISD = 0b00000000; // PORTD bits 7:0 are all outputs (0) INTCON2bits.RBPU = 0; // enable PORTB internal pullups WPUBbits.WPUB0 = 1; // enable pull up on RB0 ANSELH = 0x00; // AN8-12 are digital inputs (AN12 on RB0) TRISBbits.TRISB0 = 1; // PORTB bit 0 // (connected to switch) is input (1) } TRISB=0xFF; PORTD=0; seed = 1; while (1) { while (PORTBbits.RB0 == 0) { seed++; if (seed == 10) seed = 1; PORTD = seed; } }

#include <p18cxxx.h> #include <delays.h> #include <stdlib.h> /* Set configuration bits * - set HS oscillator * - disable watchdog timer * - disable low voltage programming */ #pragma config OSC = HS #pragma config WDT = OFF #pragma config LVP = OFF void main (void) { ADCON1 = 0x7F; // configure PORTS A and B as digital // this might need to be changed depending // on the microcontroller version. TRISB = 0; // configure PORTB for output PORTB = 0; // LEDs off srand(1); // Sets the seed of the random number while ( 1 ) // repeat forever } { } We can also use RAND() Delay10KTCYx(50); // wait 1/2 second PORTB = rand(); // display a random number // rand() returns a random value

Random Number Generator Modified for PIC18/XC8F4xK20 #include "p18f46k20.h" #include <stdio.h> void main (void) { TRISD = 0b00000000; // PORTD bits 7:0 are all outputs (0) INTCON2bits.RBPU = 0; // enable PORTB internal pullups WPUBbits.WPUB0 = 1; // enable pull up on RB0 ANSELH = 0x00; // AN8-12 are digital inputs (AN12 on RB0) TRISBbits.TRISB0 = 1; // PORTB bit 0 (connected to switch) is input (1) } srand(1); // seed the random number TRISB=0xFF; PORTD=0; while (1) { while (PORTBbits.RB0 == 0) { Delay10KTCYx(50); // wait 1/2 second PORTD = rand(); // display a random number } }

Common Conversion Functions in <stdlib.h> Function Example Note atob atob( buffer ) Converts the number from string form in buffer; returned as a byte signed number ( +127 to -128 ) atof atof( buffer ) Converts the number from string form in buffer; returned as a floating point number atoi atoi( buffer ) Converts the number from string form in buffer; returned as a 16-bit signed integer ( + 32,767 to -32, 768 ) atol atol( buffer ) Converts the number form string format in buffer; returned as a 32-bit signed integer ( + 2, 147, 483, 647 to 2, 417, 483, 648 ) btoa btoa( num, buffer ) Converts the signed byte into a string stored at buffer itoa itoa( num, buffer ) Converts the signed 16-bit integer to a string stored at buffer Itol itol( num, buffer ) Converts the 32-bit signed integer to a string stored at buffer rand rand() Returns a 16 bit random number ( 0 to 32, 767 ) srand srand( seed ) Sets the seed values to 16-bit integer seed tolower tolower( letter ) Converts byte-sized character letter from uppercase; returned as lowercase toupper toupper( letter ) Converts byte-sized character letter from lowercase, returns uppercase ultoa ultoa(num, buffer ) Same as itol, except num is unsigned The C Library Reference Guide http://www.acm.uiuc.edu/webmonkeys/book/c_guide/

Cool Example. Generating various patterns on PORTD

Read these! string.h

Copy data from program memory to data memory

Example of <string.h> and <stlib.h> n Using strlen() and atob() a File Register The program finds a number in the string Note: atob is defined in the stdlib.h table

Using ROM directives n Rom directive à tell the compiler to place data in program memory q Near ROM à 16-bit address q Far ROM à 21-bit address NEAR and FAR determine the address size in ROM (16 or 21 bit); Read the handout! Second row Program memory File register

Example of using Math Functions n n Math function uses significant amount of memory Use <math.h> Memory Usage with math.h

Verifying the Results Note: Without math.h the program does compile! However, correct results cannot be achieved!

Understanding Data Storage Using C18/ XC8 C compiler-example Answer the following questions (LAB): 1- where is mydata stored? Which register? 2- Where is Z variable located at? 3- Where is e variable located at? 4- where is midata? 5- where does the main program start at? Program: Second_C

Passing Parameters Between C and ASM Codes C Code ASM Code (Refer to Example Code: passing_parameters.c)

References n Microchip.com n Brey chapter 5 n Huang