Example: marketing

Programming the PIC18/ XC8 Using C- Coding - …

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, IAR - EWB-PIC, CCS PIC18/ XC8 C Compiler, CCS Compiler n Provides C++ like reference parameters to functions n C Compiler Quick Start Webinar q q For more click here: ~stsgrimb/ C and Assembly Compilers or PICKit 2 or C18/XC8 n Mixed language Programming Using C-language with assembly language is supported by the C18/XC8 compiler.

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 …

Tags:

  Programming

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Advertisement

Transcription of Programming the PIC18/ XC8 Using C- Coding - …

1 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, IAR - EWB-PIC, CCS PIC18/ XC8 C Compiler, CCS Compiler n Provides C++ like reference parameters to functions n C Compiler Quick Start Webinar q q For more click here: ~stsgrimb/ C and Assembly Compilers or PICKit 2 or C18/XC8 n Mixed language Programming Using C-language with assembly language is supported by the C18/XC8 compiler.

2 Q Assembly blocks are surrounded with at _asm and a _endasm directives to the C18/XC8 compiler. q 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. The C18/XC8 Compiler The C Library Reference Guide n Read the practice exercise to see how you can combine C and ASM codes together For more information: #51 Note that we use: // for comments!

3 A bit must be defined! Note: It is possible to use extended assembly instructions Using C18/XC8 compiler must be purchased! (Brey ) C18 Only! 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 .aLinker . Compiler . (Librarian) MPLAB IDE Debug Tool C Source Files Assembly Source Files Assembly Source Files Object Files Object File Libraries (Archives) Linker Script Executable Memory Map Compiler Driver Program Assembler (.)

4 Asm or .s) (.lib or .a) (.lkr or .gld) (.asm or .s) Cof: Common object file format Development Tools Data Flow C Compiler Compiler C Header File Preprocessor .s Assembly Source File .h .c 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 / (crt = C RunTime) q / n User modifiable if absolutely necessary A little about C Programming .

5 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 #include < > #define PI int main(void) { float radius, area; //Calculate area of circle radius = ; area = PI * radius * radius; printf("Area = %f", area); } Header File Function Variable Declarations Constant Declaration (Text Substitution Macro) Comment Preprocessor Directives Comments Two kinds of comments may be used: Block Comment /* This is a comment */ Single Line Comment // This is also a comment /** * Program: * Author: A good man **/ #include < > /* Function: main() */ int main(void) { printf( Hello, world!)}

6 \n ); /* Display Hello, world! */ } Variables and Data Types A Simple C Program Example Variable Declarations Data Types Variables in use #include < > #define PI int main(void) { float radius, area; //Calculate area of circle radius = ; area = PI * radius * radius; printf("Area = %f", area); } Variables 41 10-44 0 15 Data Memory (RAM) int warp_factor; float length; char first_letter; A Variables are names for storage locations in memory Variable declarations consist of a unique identifier (name).. Data Types 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 Type Description Bits char int float double single character integer single precision floating point number double precision floating point number 16 8 32 64 Data Type Qualifiers Modified Integer Types Qualifiers.

7 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 -231 0 -231 255 127 65535 32767 65535 32767 -231 -231 232-1 264-1 8 8 16 16 16 16 32 64 32 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 Are "hard coded" values q May be numbers, characters, or strings q May be represented in a number of formats (decimal, hexadecimal, binary, character, etc.)

8 Q Always represent the same value (5 always represents the quantity five) Literal Constants Example unsigned int a; unsigned int c; #define b 2 void main(void) { a = 5; c = a + b; printf("a=%d, b=%d, c=%d\n", a, b, c); } Literal Literal Using IDE MPLAB C18/XC8 Directory Structure n MPLAB C18/XC8 can be installed anywhere on the PC. Its default installation directory is the C:\mcC18/XC8 folder. n MPLAB IDE should be installed on the PC prior to installing MPLAB C18/XC8. Program Examples n Make and Build the project n If you are Using standard outputs: q Select Debugger>Settings and click on the Uart1 IO tab.

9 The box marked q Enable Uart1 IO should be checked, and the Output should be set to Window n 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 1 MHz Another Example Assume Using 4 MHz internal clock (250 nsec) one instruction cycle is 1 usec Delay1 KTCYx(1) Delay of 1000 instruction cycles Delay100 TCYx(10) Delay of 1000 instruction cycles 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 Another Example Alternatively, we could have: While ( == 0) Time Delay Functions Function Example Note Delay1 TCY Delay1 TCY(); Inserts a single NOP instruction into the program Delay10 TCYx Delay10 TCYx(10).

10 Inserts 100 instruction cycles (number must be between 0 and 255) (0 causes a delay of causes 2560) Delay100 TCYx Delay100 TCYx(10); Inserts 1000 instruction cycles (number must be between 0 and 255) (0 causes a delay of 25,600) Delay1 KTCYx Delay1 KTCYx(3); Inserts 3000 instruction cycles (number must be between 0 and 255) (0 causes a delay of 256,000) Delay10 KTCYx Delay10 KTCYx(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) = 0; // enable PORTB internal pullups = 1; // enable pull up on RB0 ANSELH = 0x00; // AN8-12 are digital inputs (AN12 on RB0) = 1; // PORTB bit 0 // (connected to switch) is input (1) TRISB=0xFF; PORTD=0; seed = 1.}