Part 1: Introduction to the C Language 1
Dennis Ritchie originally developed C at Bell Labs to write the UNIX operating system, 1974. C was designed to provide low level access to the hardware, which an OS requires. Also, C is suitable for embedded microcontroller development. Dennis Ritchie (1941-2011) Similar to assembly language, but much easier and faster to write programs in C. Most microcontroller platforms today have C compilers. C is known as the Engineer s (Programming) Language. 2
A Simple C Program (Following is 2 pages in file main.c) #include <xc.h> #include <limits.h> #pragma config ICESEL = ICS_PGx1 #define PI 314 //Associates 314 with PI unsigned int g_var1, g_var2; void initsystem(void) { g_var1 = 12; g_var2 = 34; void ledblink(unsigned char lednum, unsigned int delay) { unsigned int i; void main(void) { unsigned long int count = 0; initsystem(); while (1) { ledblink(1, 1000000); ledblink(2, 1000000); count++; if(count == ULONG_MAX) break; if(lednum == 1) { PORTSetBits(LED1_IOPORT, LED1_BIT); // LED1 on for (i = 0; i < delay; i++); // Delay some time PORTClearBits(LED1_IOPORT, LED1_BIT); // LED1 off else if(lednum == 2) { PORTSetBits(LED2_IOPORT, LED2_BIT); // LED1 on for (i = 0; i < delay; i++); // Delay some time PORTClearBits(LED2_IOPORT, LED2_BIT); // LED1 off 3
A Simple C Program (Continued) (in File Main.c) C Preprocessor Directives 1 st column of file Main.c Global variables Function Declaration (initsystem) #include <xc.h> #include <limits.h> #pragma config ICESEL = ICS_PGx1 #define PI 314 //Associates 314 with PI /*The #pragma directive is the method specified by the C standard for providing additional information to the compiler, beyond what is conveyed in the language itself. In this case, it gives the compiler information about how to setup the configuration bits of the PIC microcontroller.*/ unsigned int g_var1, g_var2; void initsystem(void) { g_var1 = 12; g_var2 = 34; Header (aka include files) 4
A Simple C Program (Continued) (in File Main.c) The function named ledblink requires two parameters (unsigned char lednum, unsigned int delay) and returns nothing (void) The function is defined in its body, which is delimited by the opening and closing braces {. A function does not execute (run) unless it is called by some other code (see the function invocation in main()). When code calls this function, it passes two arguments and expects nothing when the function returns. void ledblink(unsigned char lednum, unsigned int delay) { unsigned int i; if(lednum == 1) { PORTSetBits(LED1_IOPORT, LED1_BIT); // LED1 on for (i = 0; i < delay; i++); // Delay some time PORTClearBits(LED1_IOPORT, LED1_BIT); // LED1 off else if(lednum == 2) { PORTSetBits(LED2_IOPORT, LED2_BIT); // LED1 on for (i = 0; i < delay; i++); // Delay some time PORTClearBits(LED2_IOPORT, LED2_BIT); // LED1 off 5
A Simple C Program (Continued) (in File Main.c) void main(void) { unsigned long int count = 0; //Local variable initsystem(); //Calls the function Function Declaration (The main function: 1 st function is called by the OS when the program begins) while (1) { ledblink(1, 1000000); ledblink(2, 1000000); count++; if(count == ULONG_MAX) break; //Exits while loop //Exits the main function 6
A Project.c C Source.h C Header C Compiler.s Assembly Source Assembler Archiver.o Object.hex.map Executable Memory Map Library Object.a Linker.cof Debug 7
C Source C Compiler C Header Source Preprocessor.c.h Compiler 1. Preprocessor runs and scans.c and.h source files, looking for compiler directives. Example of a compiler directive: #define PI 314 Assembly Source.s Preprocessor will replace each instance of PI in the *.c files with 314. 2. After all directives have been processed, the actual compilation of the C statements occurs. 8
XC32 Compiler sets up a runtime environment Allocates space for stack and initializes the stack pointer Copies program and variables from Flash to RAM Clears uninitialized RAM Disables all interrupts Calls main() function (where your code starts) Runtime environment setup code (if any) is automatically linked into your application 9