AVR Programming in C Chapter 7

Slides:



Advertisements
Similar presentations
Introduction to Micro-controllers Anurag Dwivedi.
Advertisements

INSTRUCTION SET ARCHITECTURES
MICRO-CONTROLLER: A microcontroller is the brain of the robot: These are: 1. Integrated Circuits (ICs) 2. Programmable.
 | bit OR & bit AND ~ bit NOT ^ bit EXLUSIVE OR (XOR) > bit RIGHT SHIFT.
Programming the HC12 in C. Some Key Differences – Note that in C, the starting location of the program is defined when you compile the program, not in.
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.
Railway Foundation Electronic, Electrical and Processor Engineering.
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.
CS-280 Dr. Mark L. Hornick 1 EEPROM Memory Storing, Reading, and Writing.
Railway Foundation Electronic, Electrical and Processor Engineering.
Embedded Systems Programming 1 ETEE 3285 Topic HW3: Coding, Compiling, Simulating.
Timer/counter Chapter 12
© AJH 2013, V2.0 Computer Programming Embedded Systems Programming Stand-alone code, system initialisation, operating systems. Low-level operations; port.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To be able to use the bitwise logical operators in programs ❏ To be able to use.
RM2D Let’s write our FIRST basic SPIN program!. The Labs that follow in this Module are designed to teach the following; Turn an LED on – assigning I/O.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Lecture12. Outline Binary representation of integer numbers Operations on bits –The Bitwise AND Operator –The Bitwise Inclusive-OR Operator –The Bitwise.
Renesas Technology America, Inc. Flash!. CPU Rewrite CPU-rewrite is a term that refers to an Renesas MCU’s ability to erase/program its own internal Flash.
AVR Programming: Interrupts September 17, What are interrupts? An asynchronous signal indicating the need for an event to be handled A synchronous.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
Timers and Interrupts Anurag Dwivedi. Let Us Revise.
CS1372: HELPING TO PUT THE COMPUTING IN ECE CS1372 Some Basics.
BM-305 Mikrodenetleyiciler Güz 2015 (4. Sunu) (Yrd. Doç. Dr. Deniz Dal)
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.
Arduino Mega Arduino Mega 2560 Arduino Mega 2560.
Embedded Systems Lecture 4 January 20, 2016.
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
GUIDED BY PROFFESOR NILESH DESAI GROUP NO:-8 CHANDRASHIKHA SINGH( ) KURUP SHUBHAM VALSALAN( ) SAURAV SHUBHAM( )
Presentation By :- Nikhil R. Anande ( ) Electronic & Communication Engineering. 3 nd Year / 5 th Semester FACULTY GUIDE : RAHIUL PATEL SIR MICROCONTROLLER.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Interfacing of Input/Output devices with AVR Microcontroller Enrolment No. : Name of the student: SOMAIYA ISHA.
INTERFACING HARDWARE WITH MICROCONTROLLER GUIDED BY: Prof. ASIF SIR 1. AKSHAY KIRAN 2. DEEP PARIKH 3. JIGAR PATEL 4. TILAK PATEL ,05,D2D06,09.
1 Chapter 1: Basic Concepts Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine 9/6/2003.
Assembly Language with GCC
Lets Learn fundamentals !!
Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
BM-305 Mikrodenetleyiciler Güz 2016 (4. Sunu)
LCD Interfacing using Atmega 32
EKT 221 : Digital 2 Serial Transfers & Microoperations
Embedded Systems Programming Examples and Comparison
UNIVERSAL COLLEGE OF ENGINEERING & TECHNOLOGY
GANDHINAGAR INSTITUTE OF TECHNOLOGY
Chapter 3 Bit Operations
Chapter 1: An Overview of Computers and Programming Languages
The Arduino Microcontroller: Atmel AVR Atmega 328
Session 3,4.
COMP3221: Microprocessors and Embedded Systems
8051 Programming in C rhussin.
I/O Ports in AVR Sepehr Naimi
Introduction to Assembly Chapter 2
UART Protocol Chapter 11 Sepehr Naimi
Chapter 14 Bitwise Operators Objectives
I2C and RTC Chapter 18 Sepehr Naimi
AVR Programming in C Chapter 7
Embedded Programming in C
SPI Protocol Sepehr Naimi
Shift & Rotate Instructions)
LCD and Keyboard Sepehr Naimi
Embedded Programming in C
Introduction to Assembly Chapter 2
I/O Ports in AVR Sepehr Naimi
ADC and DAC Programming in AVR
Intro to Micro Controllers
Interrupt Chapter 10.
Programming Language C Language.
Operations and Arithmetic
Bit Manipulations CS212.
Presentation transcript:

AVR Programming in C Chapter 7 Sepehr Naimi www.NicerLand.com

Topics Data Types Time Delays IO Programming in C Logic Operation in C Data serialization in C

Languages High Level Languages C Language Low Level Languages Easy to develop and update C Language Acceptable performance Portable Low Level Languages High performance Not portable High Level Languages (like VB) C Language Machine Language

A simple C program Write a program that calculate the sum of {1,3,…,13,15} int main () { unsigned int sum; for (int i = 1; i <= 15; i+=2) sum += i; while (1); return 0; }

Accessing I/O registers Example 1: Write an AVR C program to send value 0xAA to PORTD. #include <avr/io.h> int main () { DDRD = 0xFF; PORTD = 0xAA; while (1); return 0; }

Accessing I/O registers Example 2: Write an AVR C program to calculate PINB + PINC and send the result to PORTD. #include <avr/io.h> int main () { DDRB = 0x00; DDRC = 0x00; DDRD = 0xFF; while (1) PORTD = PINB + PINC; return 0; }

What is happening in machine language? int Fibonnaci (int n); int main () { unsigned int sum = 0; for(int j = 0; j < 11; j++) sum += Fibonnaci (j); } while(1); return 0; int Fibonnaci (int n) { int a1 = 1; int a2 = 1; int i; int temp; if(n == 0) return 0; else if(n <= 2) return 1; else{ for(i = 2; i < n; i++) temp = a2; a2 = a1 + a2; a1 = temp; } return a2;

Data Types unsigned char instead of unsigned int if you can Use unsigned whenever you can unsigned char instead of unsigned int if you can

What is the difference between int and unsigned int? Data types (cont.) c char c; int a1; long lng; unsigned int a; lng a1 a What is the difference between int and unsigned int?

int vs. unsigned int int a1 = 5; int a2 = 3; int a3 = 9; b = (a1 * a2) + a3; AVR is an 8-bit. How could we multiply 16-bit numbers?

Multiplications of Big Numbers 23 ab *49 *cd 27 b*d 18 ad+bc 27 ac 8

Choosing optimized data type unsigned int sum; for (int i = 1; i <= 15; i+=2) sum += i; unsigned char sum; for (char i = 1; i <= 15; i+=2) sum += i;

Accessing I/O registers #include <avr/io.h> int main () { DDRD = 0xFF; while(1) for (unsigned char i = 0; i <= 9; i++) PORTD = i; } return 0;

Time Delays in C You can use for to make time delay void delay(void) { volatile unsigned int i; for(i = 0; i < 42150; i++) { } } If you use for loop The clock frequency can change your delay duration ! The compiler has direct effect on delay duration!

Time Delays in C You can use predefined functions of compilers to make time delays In Atmel Studio: First you should include: #define F_CPU 8000000UL #include <util/delay.h> and then you can use _delay_us(200); //200 microseconds _delay_ms(100); //100 milliseconds It is compiler dependant

I/O programming in C Byte size I/O programming in C DDRB = 0xFF; while (1) { PORTB = 0xFF; _delay_ms(500); PORTB = 0x55; } Different compilers have different syntax for bit manipulations! It is better to use Bit-wise logical operators

Bit-wise logical operators 1110 1111 & 0000 0001 -------------- 0000 0001 1110 1111 | 0000 0001 -------------- ~ 1110 1011 -------------- 0001 0100

Shift operations in C data >> number of bits to be shifted right data << number of bits to be shifted left 1110 0000 >> 3 -------------- 0001 1100 0000 0001 <<2 -------------- 0000 0100

Setting a bit in a Byte to 1 We can use | operator to set a bit of a byte to 1 xxxx xxxx | 0001 0000 ------------- xxx1 xxxx xxxx xxxx | 1 << 4 ------------- xxx1 xxxx OR PORTB |= (1<<4); //set bit 4 (5th bit) of PORTB

Clearing a bit in a Byte to 0 We can use | operator to set a bit of a byte to 1 xxxx xxxx & 1110 1111 ------------- xxx0 xxxx xxxx xxxx & ~(1 << 4) ------------- xxx0 xxxx OR PORTB &= ~(1<<4); //clear bit 4 (5th bit) of PORTB

if( ((PINC & (1<<5)) != 0) //check bit 5 (6th bit) Checking a bit in a Byte We can use & operator to see if a bit in a byte is 1 or 0 xxxx xxxx & 0001 0000 ------------- 000x 0000 xxxx xxxx & (1 << 4) ------------- 00x0 0000 OR if( ((PINC & (1<<5)) != 0) //check bit 5 (6th bit)

Memory Types In AVR Flash Memory EEPROM RAM Not deleted when power is off Big in size Suitable for codes, tables and fixed data EEPROM Not deleted when power is off Not very big in size Suitable for small data that may be modified but should not be lost when power is off RAM deleted when power is off Suitable for storing the data we want to manipulate because we have fast access to read or modify them.

Accessing Flash #include <avr/pgmspace.h> const unsigned char PROGMEM lookup[] ={5,6,7,4}; ourData = pgm_read_byte(&lookup[i]); #include <avr/pgmspace.h> const unsigned char PROGMEM lookup[] ={5,6,7,4}; int main(void) { unsigned char a; a = pgm_read_byte(&lookup[i]); while (1); }

Accessing EEPROM #include <avr/io.h> #include <avr/eeprom.h> unsigned char EEMEM myVar; //reserve a location in EEPROM int main(void) { DDRC = 0xFF; if((PINB&(1<<0)) != 0) //if PB0 is HIGH eeprom_write_byte(&myVar,'G'); //read from EEPROM else PORTC = eeprom_read_byte(&myVar); //write to EEPROM while (1); }