Martin Bates Elsevier 2008 Programming 8-bit PIC Microcontrollers in C.

Slides:



Advertisements
Similar presentations
EMS1EP Lecture 4 Intro to Programming Dr. Robert Ross.
Advertisements

Verilog.
Simulation executable (simv)
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
 | bit OR & bit AND ~ bit NOT ^ bit EXLUSIVE OR (XOR) > bit RIGHT SHIFT.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Railway Foundation Electronic, Electrical and Processor Engineering.
Railway Foundation Electronic, Electrical and Processor Engineering.
Embedded Programming and Robotics Lesson 2 C Programming Refresher C Programming1.
Programming 8-bit PIC Microcontrollers in C Martin Bates Elsevier 2008.
Working with Arduino: Lesson #1: Getting Acquainted with the Kit EGN1007.
 C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories. 
chipKit Sense Switch & Control LED
Introduction to Computer Algorithmics and Programming Ceng 113 Variables and Operators in C.
Manipulating Characters In today’s lesson we will look at: how text is stored inside the computer how we can use BASIC functions to manipulate and encipher.
C Summary Wilmer Arellano. References Excerpts from the book: Predko, Myke. (2005). 123 PIC MICROCONTROLLER EXPERIMENTS FOR THE EVIL GENIOUS. USA: McGraw-Hill.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
ECE 2372 Modern Digital System Design
DAT602 Database Application Development Lecture 5 JAVA Review.
Chapter 4 Program Control Statements
Chapter 2 A Loop in the Pattern Designing the Main Loop and Timing.
Information Representation. Reading and References Reading - Computer Organization and Design, Patterson and Hennessy Chapter 2, sec. 2.4, 2.9 (first.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
CPS120: Introduction to Computer Science Decision Making in Programs.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
Slide 1 PHP Operators and Control Structures ITWA 133.
1 Workshop Topics - Outline Workshop 1 - Introduction Workshop 2 - module instantiation Workshop 3 - Lexical conventions Workshop 4 - Value Logic System.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
CPS120: Introduction to Computer Science Decision Making in Programs.
MICROCONTROLLER SYSTEMS Part 1. Figure 1.1Elements of a digital controller CPU Central Processing Unit Input Peripherals Output Peripherals ROM Read Only.
ITEC113 Algorithms and Programming Techniques
Chapters 1-5 Review C++ Class. Chapter 1 – the big picture Objects Class Inheritance Reusability Polymorphism and Overloading.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Chapter 3: Loops and Logic. 2 Control Statements If statement Example NumberCheck.java Relational operators (, >=, ==, !=) Using code blocks with If.
By Mr. Muhammad Pervez Akhtar
Reference Learning Java DDC publishing Herst, Yamauchi, Adler COP 3331 Object Oriented Analysis and Design Java Part I.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Microcontrollers, Microcomputers, and Microprocessors
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Control Unit.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Presentation By :- Nikhil R. Anande ( ) Electronic & Communication Engineering. 3 nd Year / 5 th Semester FACULTY GUIDE : RAHIUL PATEL SIR MICROCONTROLLER.
Chapter 1 Introduction Digital Systems Digital systems: computation, data processing, control, communication, measurement - Reliable, Integration.
Arduino & its hardware interfacing
C Short Overview Lembit Jürimägi.
11/10/2018.
Starting JavaProgramming
Arithmetic operations, decisions and looping
Introduction to Programming
Working with Arduino: Lesson #1: Getting Acquainted with the Kit
Java - Data Types, Variables, and Arrays
Topics: Programming Constructs: loops & conditionals Digital Input
Differences between Java and C
C Programming Getting started Variables Basic C operators Conditionals
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Fundamental Programming
Decision making and control functions
C Language B. DHIVYA 17PCA140 II MCA.
Selection Control Structure
Presentation transcript:

Martin Bates Elsevier 2008 Programming 8-bit PIC Microcontrollers in C

This presentation contains illustrations from the book ‘ Programming 8-bit PIC Microcontrollers in C ’ by Martin Bates Part 1Microcontroller Systems describes in detail the internal architecture and interfaces available in the PIC 16F887A, a typical PIC chip, as well as outlining the main features of the development system Part 2 C Programming Essentials provides simple example programs for the microcontroller which show the basic principles of C programming, and interfacing to basic I/O devices Part 3C Peripheral Interfaces provides example programs for operating PIC chips with a full range of peripherals, using timers and interrupts

C PROGRAMMING ESSENTIALS Part 2

Listing 2.1A program to output a binary code /* Source code file:OUTNUM.C Author, date, version:MPB V1.0 Program function:Outputs an 8-bit code Simulation circuit: OUTBYTE.DSN *******************************************************/ #include "16F877A.h"// MCU select void main()// Main block { output_D(255);// Switch on outputs }

fFigure 2.1MPLAB IDE Screenshot

Figure 2.2ISIS dialogue to attach program

Figure 2.3OUTBYTE.DSN test circuit with output LEDs

Listing 2.2 Variables /* Source code file:VARI.C Author, date, version:MPB V1.0 Program function:Outputs an 8-bit variable Simulation circuit: OUTBYTE.DSN *******************************************************/ #include "16F877A.h" void main() { int x;// Declare variable and type x=99;// Assign variable value output_D(x);// Display the value in binary }

Listing 2.3Endless loop /* Source code file:ENDLESS.C Author, date, version:MPB V1.0 Program function:Outputs variable count Simulation circuit: OUTBYTE.DSN *******************************************************/ #include "16F877A.h" void main() { int x;// Declare variable while(1)// Loop endlessly {output_D(x);// Display value x++;// Increment value }

Figure 2.4INBIT.DSN test circuit with input switch

Listing 2.4IF statement /* Source code file:IFIN.C Author, date, version:MPB V1.0 Program function:Tests an input Simulation circuit: INBIT.DSN ******************************************************* / #include "16F877A.h" void main() { int x;// Declare test var. output_D(0);// Clear all outputs while(1)// Loop always { x = input(PIN_C0);// Get input if(x==1)output_high(PIN_D0);// Change out }

Listing 2.5Conditional loop /* Source code file:WHILOOP.C Author, date, version:MPB V1.0 Program function:Input controls output loop Simulation circuit: INBIT.DSN *******************************************************/ #include "16F877A.h" #use delay (clock= )// MCU clock = 1MHz void main() { while(1) { while(input(PIN_C0));// Repeat while switch open { output_high(PIN_D0); delay_ms(300);// Delay 0.3s output_low(PIN_D0); delay_ms(500);// Delay 0.5s } output_low(PIN_D0);// Switch off LED }

Listing 2.7Siren Program /* Source code file:SIREN.C Author, date, version:MPB V1.0 Program function:Outputs a siren sound Simulation circuit: INBIT.DSN *******************************************************/ #include "16F877A.h" #use delay (clock= ) void main() { int step; while(1) { while(!input(PIN_C0))// loop while switch ON { for(step=0;step<255;step++)// Loop control { output_high(PIN_D0);// Sound sequence delay_us(step); output_low(PIN_D0); delay_us(step); }

Listing 2.8Program Blank /*Source Code Filename: Author/Date/Version: Program Description: Hardware/simulation: ***************************************************************/ #include "16F877A.h"// Specify PIC MCU #use// Include library routines void main()// Start main block { int // Declare global variables while(1)// Start control loop { // Program statements } }// End main block

Table 2.1A basic set of CCS C components Compiler Directives #include source files Include another source code or header file #use functions(parameters) Include library functions C Blocks main(condition) {statements } Main program block while(condition) {statements } Conditional loop if(condition) {statements } Conditional sequence for(condition) {statements } Preset loop C Functions delay_ms(nnn) Delay in milliseconds delay_us(nnn) Delay in microseconds output_x(n) Output 8-bit code at Port X output_high(PIN_nn) Set output bit high output_low(PIN_nn) Set output bit low input(PIN_nn) Get input

Table 2.1Integer Variables NameTypeMinMax int1 1 bit01 unsigned int8 8 bits0255 signed int8 8 bits unsigned int16 16 bits signed int16 16 bits unsigned int32 32 bits signed int32 32 bits

Table 2.2Microchip/CCS Floating Point Number Format ExponentSignMantissa xxxx xxxx xxxx xxxx xxxx xxxx xxxx 8 bits123 bits Table 2.4Example of 32-bit floating point number conversion Mantissa: Exponent: Sign: 1 = negative number FP number:

Figure 2.5Variable Types

Table 2.5ASCII Codes Low Bits High Bits Spac e 0001!1AQaq 0010"2BRbr 0011#3CScs 0100$4DTdt 0101%5EUeu 0110&6FVfv 0111'7GWgw 1000(8HXhx 1001)9IYiy 1010*:JZjz 1011+;K[k{ 1100,<L\l| 1101-=M]m} 1110.>N^n~ 1111/?O_oDel

Table 2.6Arithmetic and Logical Operations OPERATION OPERATORDESCRIPTIONSOURCE CODEEXAMPLERESULT Single operand Increment++Add one to integer result = num1++; Decrement--Subtract one from integer result = num1--; Complement~Invert all bits of integer result = ~num1; Arithmetic Operation Add+Integer or Float result = num1 + num2; Subtract-Integer or Float result = num1 - num2; Multiply*Integer or Float result = num1 * num2; * Divide/Integer or Float result = num1 / num2; / Logical Operation Logical AND&Integer Bitwise result = num1 & num2; & Logical OR|Integer Bitwise result = num1 | num2; | Exclusive OR^Integer Bitwise result = num1 ^ num2; ^

Figure 2.6Variable Operations

Table 2.7Conditional Operators Operation Symbol EXAMPLE Equal to== if(a == 0) b=b+5; Not equal to!= if(a != 1) b=b+4; Greater than> if(a > 2) b=b+3; Less than< if(a < 3) b=b+2; Greater than or equal to>= if(a >= 4) b=b+1; Less than or equal to<= if(a <= 5) b=b+0;

Conditio n True? Statement Block Conditio n True? Statement Block (a) While loop (b) Do..While loop Figure 2.3.1Comparison of While and Do..While Loop

Listing 2.9DOWHILE.C contains both types of ‘ while ’ loop //DOWHILE.C //Comparison of WHILE and DO WHILE loops #include "16F877A.H" main() { int outbyte1=0; int outbyte2=0; int count; count=0;// This loop is not while (count!=0)// executed {output_C(outbyte1); outbyte1++; count--; } count=0;// This loop is do// executed { output_C(outbyte2); outbyte2++; count--; } while (count!=0); while(1){}; }

label Statement Block Continue Goto Break Figure 2.8Break, continue and goto

Listing 2.10Continue, Break & Goto //CONTINUE.C //Continue, break and goto jumps #include "16F877A.H" #use delay(clock= ) main() { int outbyte; again: outbyte=0;// Goto destination while(1) { output_C(outbyte);// Loop operation delay_ms(10); outbyte++; if (!input(PIN_D0)) continue;// Restart loop if (!input(PIN_D1)) break;// Terminate loop delay_ms(100); if (outbyte==100) goto again;// Unconditional jump }

Figure 2.9Comparison of If and If..Else If block Condition True? YES NO Condition True? If block Else block YESNO

Test Variable Value = 3? Procedure 3 YES NO Value = n? Procedure n YES NO Default Procedure Value = 2? Procedure 2 YES NO Value = 1? Procedure 1 YES NO Figure 2.10Switch..case branching structure

Listing 2.11Comparison of Switch and If..Else control //SWITCH.C //Switch and if..else sequence control //Same result from both sequences #include "16F877A.h" void main() { int8 inbits; while(1) { inbits = input_D();// Read input byte // Switch..case option switch(inbits)// Test input byte { case 1: output_C(1);// Input = 0x01, output = 0x01 break;// Quit block case 2: output_C(3);// Input = 0x02, output = 0x03 break;// Quit block case 3: output_C(7);// Input = 0x03, output = 0x07 break;// Quit block default:output_C(0);// If none of these, output = 0x00 } // If..else option if (input(PIN_D0)) output_C(1);// Input RD0 high if (input(PIN_D1)) output_C(2);// Input RD1 high if (input(PIN_D0) && input(PIN_D1)) output_C(7);// Both high else output_C(0);// If none of these, output = 0x00 }

Figure 2.11Hierarchical C program structure Main() { statements fun1() statements.... statements fun2(arg) statements } void fun1() { statements... } void fun2(arg) { statements... fun3... return(val) } void fun3 { statements... } LEVEL 0LEVEL 1LEVEL 2

Listing 2.12Basic function call // FUNC1.C // Function call structure #include "16F877A.H" int8 outbyte=1; int16 n; void out()// Start of function block { while (outbyte!=0)// Start loop, quit when output =0 { output_C(outbyte);// Output code 1 – 0xFF outbyte++;// Increment output for(n=1;n<500;n++);// Delay so output is visible } main() { out();// Function call while(1);// Wait until reset }

Listing 2.13Passing a parameter to the function // FUNC2.C #include "16F877A.H" int8 outbyte=1; // Declare global variables int16 n,count; void out()// Function block { while (outbyte!=0) {output_C(outbyte); outbyte++; for(n=1;n<count;n++); } main() { count=2000; out();// Call function while(1); }

Listing 2.14Local variables // FUNC3.C // Use of local variables #include "16F877A.H" int8 outbyte=1;// Declare global variables int16 count; int out(int16 t)// Declare argument types { int16 n;// Declare local variable while (input(PIN_D0))// Run output at speed t { outbyte++; for(n=1;n<t;n++); } return outbyte; // Return output when loop stops } main() { count=50000; out(count);// Pass count value to function output_C(outbyte);// Display returned value while(1); }