An Embedded C Program 1 Mainly from textbook: Embedded C Programming and the Atmel AVR 2 nd Edition Barnett, Cox and O’Cull.

Slides:



Advertisements
Similar presentations
Chapter 2 Representing and Manipulating Information Prof. Qi Tian CS 3843 Fall
Advertisements

1 Lab2: A/D Converter. 2 This circuit connects a variable voltage to an A/D port on the AVR mcu. Your software running on the AVR mcu will read the digital.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
1 Lecture 3 Bit Operations Floating Point – 32 bits or 64 bits 1.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Butterfly I/O Ports CS-212 Dick Steflik. I/O for our labs To get data into and out of our Butterfly its a little trickier than using printf and scanf.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
AVR Programming CS-212 Dick Steflik. ATmega328P I/O for our labs To get data into and out of our Arduino its a little trickier than using printf and.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
Railway Foundation Electronic, Electrical and Processor Engineering.
Bitwise Operations CSE 2451 Rong Shi. Working with bits – int values Decimal (not a power of two – used for human readability) – No preceding label –
Robotics Research Laboratory Louisiana State University.
 C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories. 
2015/8/221 Data Types & Operators Lecture from (Chapter 3,4)
Embedded Systems Programming 1 ETEE 3285 Topic HW3: Coding, Compiling, Simulating.
Simple Data Type Representation and conversion of numbers
Programmable Logic Controllers
C Summary Wilmer Arellano. References Excerpts from the book: Predko, Myke. (2005). 123 PIC MICROCONTROLLER EXPERIMENTS FOR THE EVIL GENIOUS. USA: McGraw-Hill.
1 Homework Turn in HW2 tonight HW3 is on-line already Questions?
Chapter 12 Variables and Operators. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Basic C Elements.
1 Lab 1: Introduction. 2 Configure ATMEL AVR Starter Kit 500 (STK500), a prototyping/development board for Lab1. ATmega16 ( V) is the chip used.
1 CS103 Guest Lecture Number Systems & Conversion Bitwise Logic Operations.
Lecture12. Outline Binary representation of integer numbers Operations on bits –The Bitwise AND Operator –The Bitwise Inclusive-OR Operator –The Bitwise.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Intro to Programming in C. Motivation for Using C  High level, compiler language  Efficiency over user-friendliness  Allows programmer greater freedom,
Bit Operations Horton pp Why we need to work with bits Sometimes one bit is enough to store your data: say the gender of the student (e.g. 0.
15-Nov-15 All the Operators. operators.ppt 2 Precedence An operator with higher precedence is done earlier (precedes) one with lower precedence A higher.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Chapter 12 Variables and Operators. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Basic C Elements.
CS115 FALL Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.
Programming Microcontrollers in C Lecture L7.1. C Data Types TypeSizeRange char1 byte-128 – 127 unsigned char1 byte0 – 255 Int2 bytes – unsigned.
LHO 22 C and the  The Silicon Labs ISE uses the Keil C51 compiler.  The code size is limiter to 2K  C has replaced PL/M (the original Intel high.
Expressions and Operators in C. Expressions and Operators Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); Two types: – Function calls – The expressions.
Arduino Mega Arduino Mega 2560 Arduino Mega 2560.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Expression and Operator. Expressions and Operators u Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); u Two types: –Function calls –The expressions formed.
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.
Bit Manipulation in 'C' 'C' bit operators
Embedded Systems Lecture 4 January 20, 2016.
Data Representation. Representation of data in a computer Two conditions: 1. Presence of a voltage – “1” 2. Absence of a voltage – “0”
CSE 220 – C Programming Bitwise Operators.
Chapter 12 Variables and Operators
Chap. 2. Types, Operators, and Expressions
Integer Real Numbers Character Boolean Memory Address CPU Data Types
Instructor: David Ferry
Bit Operations Horton pp
Chapter 12 Variables and Operators
Formatting Output.
Lecture 5 from (Chapter 4, pages 73 to 96)
Unit 2 Programming.
All the Operators 22-Nov-18.
More about Numerical Computation
All the Operators 4-Dec-18.
Embedded Programming in C
Conversion Check your class notes and given examples at class.
Homework Homework Continue Reading K&R Chapter 2 Questions?
Comp Org & Assembly Lang
All the Operators 6-Apr-19.
All the Operators 13-Apr-19.
Operations and Arithmetic
DATA TYPES There are four basic data types associated with variables:
Bit Manipulations CS212.
Bit Operations Horton pp
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

An Embedded C Program 1 Mainly from textbook: Embedded C Programming and the Atmel AVR 2 nd Edition Barnett, Cox and O’Cull

An Embedded C Program the simplest form 2 void main() { while (1) //do forever ; } void main() { printf(“begin measuring speed”); while(1) //do forever ; }

Turn on/off LED 3 Output a 0 to turn on LED Output a 1 to turn off LED

4 #include // the LED 0 on PORTB will be on unsigned char led_status=0xFE; // Timer 1 overflow interrupt service routine interrupt [TIM1_OVF] void timer1_ovf_isr(void) { // Reinitialize Timer 1 value TCNT1H=0xF8; TCNT1L=0xFB; // Place your code here // move the LED led_status<<=1; led_status+=1; if (led_status==0xFF) led_status=0xFE; // turn on the LED PORTB=led_status; } // Declare your global variables here // Continue on next slide Example used in Lab 1

5 void main(void) { // Declare your local variables here // Input/Output Ports initialization // Port B initialization // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out // State7=1 State6=1 State5=1 State4=1 State3=1 State2=1 State1=1 State0=1 PORTB=0xFE; // PORTB is port B driver register DDRB=0xFF; // Data Direction Control Register, 1: output; 0: input // other initializations as needed by your program, such as A/D converter // usually all required work for initialization done by CodeVision Wizard // // Global enable interrupts #asm("sei") while (1) { // Place your code here }; }

6 In the past, some students can not display proper results even though they have the correct program structure, due to wrong data type, wrong operator. Now we have a short review of the fundamentals

Variable Types and Sizes 7 TypeSize(Bits)Range bit10,1 char8-128 to 127 unsigned char80 to 255 int to short int to unsigned int160 to signed int to long int32 unsigned long int32 signed long int32 float e-38 to e38 double e-38 to e38

Constants 8 Numerical Constants decimal 1234 binary0b hexadecimal0xff octal0777 Character Constants characterrepresentationEquivalent Hex Value TAB‘\t’‘\x09’ LF (new line)‘\n’‘\x0a’ CR‘\r’‘\x0d’ Backspace‘\b’‘\x08’ -- exampleprintf(“c = %d\n”, c) // printf(“c = %d\n\r”, c) //

Operators 9 Arithmetic Operators Multiply* Divide/ Modulo% Addition+ Subtraction- Negation- Bitwise Operators Ones complement~ Left Shift<< Right Shift>> AND& Exclusive OR^ OR| Beware division: If second argument is integer, the result will be integer (rounded): 5 / 10  0 whereas 5 / 10.0  0.5 Division by 0 will cause overflow

Bitwise Operations 10 Given an unsigned char y = 0xC9 operationresult x = ~yx = 0x36 x = y <<3x = 0x48 x = y>>4x = 0x0C x = y&0x3Fx = 0x09 x = y^1x = 0xC8 x = y | 0x10x = 0xD9 other examples: unsigned char z z = PINA & 0x06; PORTB = PORTB | 0x60; PORTB = PORTB & 0xfe;

Logical Operators 11 Logical operator AND&& OR|| x =5 and y =2 (x && y) is true, because both are non-zero (x & y) is false, because bitwise AND equal to zero (x || y) is true, because either value is non-zero (x | y) is true, b101 bitwise OR b010 is b111 (non-zero)

I/O Operations 12 unsigned char z; void main (void) { DDRB = 0xff; // set port B as output port DDRA = 0x00; // set port A as input port while (1) { z = PINA;// read port A PORTB = z + 1; // write to port B } // DDRx register is used to set which bits are to be used for output/input // DDRB = 0xc3; , upper two bits and lower two bits for // output

I/O operations 13 unsigned char i; // temporary variable DDRA = 0x00; // set PORTA for input DDRB = 0xFF; // set PORTB for output PORTB = 0x00; // turn ON all LEDs initially while(1){ // Read input from PORTA. // This port will be connected to the 8 switches i = PINA; // Send output to PORTB. // This port will be connected to the 8 LEDs PORTB = i; }

I/O operations 14 Turn on an LED connected to PB3 PORTB |= 0xF7; // b ; PORTB=0x00 initially; Must do the whole port Turn on an LED connected to PB3 PORTB.3 = 0 ; // access the bit 3 of port B, turn on the LED for (delay = 0; delay < 10000; delay++); // declare delay as int somewhere PORTB.3 = 1; // turn off the LED

I/O operation 15 Check if user pushed the button connected to PA5 swInput = PINA; swInput = ~PINA; if(swInput & 0x20) …

Division 16 Beware division: If second argument is integer, the result will be integer (rounded): 5 / 10  0 whereas 5 / 10.0  0.5 Division by 0 will cause a problem

Relational Operators 17 Relational Operators Is Equal to== Is Not equal to!= Less Than< Less Than or Equal to<= Greater than> Greater Than or equal to>= x = 3 and y =5 (x == y)FALSE (x != y)TRUE (x < y)TRUE (x<=y)TRUE (x>y)FALSE (x >= y)FALSE

Data format 18 Conversion specifier Description %ddisplay as a signed decimal integer %6dat least 6 characters wide %udisplay as an unsigned decimal integer %xdisplay as an unsigned hexadecimal integer %edisplay a floating point value in exponential notation, such as e2 %fdisplay a floating point value in fixed point notation, such as %6fat least 6 characters wide %.2f2 characters after decimal point %6.2fat least 6 characters wide and 2 after decimal point

Assignment Operators 19 x = y assign y to x x++ post-increment x ++x pre-increment x x-- post-decrement x --x pre-decrement x x += y assign (x+y) to x x -= y assign (x-y) to x x *= y assign (x*y) to x x /= y assign (x/y) to x x %= y assign (x%y) to x int x=5; int y; y = ++x; /* x == 6, y == 6 */ int x=5; int y; y = x++; /* x == 6, y == 5 */

Do While loop 20 do // mix up the numbers { // while waiting for button release. first ^= seed>>1; // Exclusive ORing in the moving seed second ^= seed>>2; third ^= seed>>3; seed++; // keep rolling over the seed pattern } while(PINA.0 == 0); // while the button is pressed

For Loop 21 for(count = 0; count < 5; count++) // flash light while moving.. { for(delay = 0; delay < 10000; delay++) ; // just count up and wait PORTB.1 = 0; // turn the LED on.. for(delay = 0; delay < 10000; delay++) ; PORTB.1 = 1; // turn the LED off.. }

If Then Else 22 if((first == 3) && (second == 3) && (third == 3)) printf("Paid out: JACKPOT!!\n");// Three "Cherries" else if((first == 3) || (second == 3) || (third == 3)) printf("Paid out: One Dime\n");// One "Cherry" else if((first == second) && (second == third)) printf("Paid out: One Nickle\n");// Three of a kind else printf("Paid out: ZERO\n");// Loser..