Part 1: Introduction to the C Language

Similar documents
Lecture 2: C Programming Basic

CMPE-013/L. Introduction to C Programming

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

An Introduction to Microchip's MPLAB IDE and an Example Application: LED Blinking

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

CSC 1600 Memory Layout for Unix Processes"

Programming (1.0hour)

Lecture 03 Bits, Bytes and Data Types

EC 413 Computer Organization

Introduction to Programming (Java) 4/12

Object-Oriented Programming

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Chapter 10. Programming in C

Preview from Notesale.co.uk Page 6 of 52

CS111: PROGRAMMING LANGUAGE II

Programming and Data Structure

Programming in C Quick Start! Biostatistics 615 Lecture 4

CMPE-013/L. Introduction to C Programming. Unit testing

C Programming Language

EE475 Lab #3 Fall Memory Placement and Interrupts

Lecture test next week

CSCI 171 Chapter Outlines

Software Project. Lecturers: Ran Caneti, Gideon Dror Teaching assistants: Nathan Manor, Ben Riva

C Programming Review CSC 4320/6320

Technical Questions. Q 1) What are the key features in C programming language?

Introduction to C Language

CS240: Programming in C

ECE 3740 SEP1: MPLAB Introduction and LED Blinking. An Introduction to Microchip's MPLAB IDE and

Programming in C. What is C?... What is C?

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

Motivation was to facilitate development of systems software, especially OS development.

CS313D: ADVANCED PROGRAMMING LANGUAGE

C Language, Token, Keywords, Constant, variable

o Code, executable, and process o Main memory vs. virtual memory

PESIT-BSC Department of Science & Humanities

Introduction to C Programming

A Fast Review of C Essentials Part I

MPLAB XC8 C Compiler Version 2.00 Release Notes for AVR MCU

C Introduction. Comparison w/ Java, Memory Model, and Pointers

BLM2031 Structured Programming. Zeyneb KURT

Motivation was to facilitate development of systems software, especially OS development.

C Language Programming

Programming. Data Structure

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

Class Information ANNOUCEMENTS

Today USING POINTERS. Functions: parameters and arguments. Todaywewilllookattopicsrelatingtotheuseofpointers

Course Outline Introduction to C-Programming

Holtek C and ANSI C Feature Comparison User s Guide

A Shotgun Introduction to C

Lectures 5-6: Introduction to C

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

MPATE-GE 2618: C Programming for Music Technology. Unit 4.1

Introduction to Computing Lecture 01: Introduction to C

The C standard library

ME 461 C review Session Fall 2009 S. Keres

Lecture 3: C Programm

Introduction. Keywords: MAXQ, IAR, memory allocation, flash data, flash storage, SRAM

ME 4447/ME Microprocessor Control of Manufacturing Systems/ Introduction to Mechatronics. Instructor: Professor Charles Ume

Introduction to C An overview of the programming language C, syntax, data types and input/output

Lectures 5-6: Introduction to C

embos Real Time Operating System CPU & Compiler specifics for ARM core with ARM RealView Developer Suite 3.0 Document Rev. 1

CS-211 Fall 2017 Test 1 Version Practice For Test on Oct. 2, Name:

CS201 - Introduction to Programming FAQs By

L2 - C language for Embedded MCUs

Storage Class Specifiers Scope and Lifetime of Variables CMPE-013/L. Modules and Scope. Storage Class Specifiers Automatic Variables

6.096 Introduction to C++ January (IAP) 2009

ET156 Introduction to C Programming

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Presented By : Gaurav Juneja

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

C Pointers. 6th April 2017 Giulio Picierro

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

A Shotgun Introduction to C

RL78 Development Environment Migration Guide

edunepal_info

CHW 469 : Embedded Systems

Macros and Preprocessor. CGS 3460, Lecture 39 Apr 17, 2006 Hen-I Yang

Computers Programming Course 5. Iulian Năstac

Binghamton University. CS-120 Summer Introduction to C. Text: Introduction to Computer Systems : Chapters 11, 12, 14, 13

Final CSE 131B Spring 2004

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

Programming for Engineers: Getting Started

Language Design COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science

Linking. Explain what ELF format is. Explain what an executable is and how it got that way. With huge thanks to Steve Chong for his notes from CS61.

CS Prof J.P.Morrison

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

CS241 Computer Organization Spring Data Alignment

EECS 388 Laboratory Exercise #9 Assembly Language Gary J. Minden Laboratory week of: April 24, 2017

Short introduction to C for AVR

MPLAB C1X Quick Reference Card

CS 61C: Great Ideas in Computer Architecture Introduction to C

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

C++ Tutorial AM 225. Dan Fortunato

A S H O R T I S H O V E R V I E W O F T H E A N S I C P R O G R A M M I N G L A N G U A G E

CS201 - Lecture 1 The C Programming Language

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

Transcription:

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