Embedded C Programming
Embedded C Programming
Building Blocks
- Feature Overview - Block Diagram of Development Board - Components in Development Board - How to burn Programs on the Board - A sample Program to burn - How to write Programs for ATMEGA 16 - LED Pattern Example - Interfacing a simple Output Device with ATMEGA 16 - Seven Segment Example - Interfacing a Display Device with ATMEGA 16 - LCD Display Example -
LCD: device where you can display the string or character. Switch : can be used ON & OFF the power supply Can be used to drive the motors.
L293D IC is used to take 5 voltage in input and provide the voltage of 6 to 8v to the motors.
7805 IC is used to give 5 volt to the ATMEGA 16. Crystal oscillator:- Is used to gives
the fixed value of clock to the MC
Corporate Excellence Program ATMEGA 16: Overview Overview - Is an 8 Bit Microcontroller - Is a 40 Pin IC - Advanced RISC Architecture - 32x8 General Purpose Working Registers Memory - 16 K Bytes of In-system Self Programmable Flash - 512 Bytes of EEPROM - 1K Byte Internal SRAM I/O Peripherals - 32 Programmable I/O Lines - 8 Channel, 10 Bit ADC
Port C
1. 2.
(PC7...PC0)---- It is beginning from the (pin 29- 22) is an 8-bit bi-directional I/O port with internal pull-up resistor Port B also serves the functions
Port D (PD7...PD0)---- It is beginning from the (pin 21- 14) 1. is an 8-bit bi-directional I/O port with internal pull-up resistor 2. Port B also serves the functions
10
Corporate Excellence Program ATMEGA 16: Overview ADC system of Atmega16 microcontroller consists of following pins: i ADC0-ADC7: 8 Channels from Pin 40 to Pin 33 ii. AREF: Pin32 of Atmega16 microcontroller, - the voltage on AREF pin acts as the reference voltage for ADC conversion, - reference voltage is always less than or equal to the supply voltage, i.e., Vcc. iii. AVCC: - Pin30, - is the supply voltage pin for using PORTA and the ADC; - must be connected to Vcc to use PORTA and ADC. Note: External reference voltage source can be used at AREF pin. However, Atmega16 also has internal reference voltage options of 2.56V and Vref = Vcc.
www.buildingblocks.in | www.bulo.in | 9818403093 11
Burner: It provides an interface between computer and hardware(Development Board). Power Cable: It provides a voltage to the development board circuit. Serial Cable: It provides an interface between computer and burner. 6 Pin Female molex connecting cable: It provide an interface between Development Board and Burner. Development Board: It is board where instructions is resided and executed. the
13
Make File: It is a file which gives the information about the file to the compiler that which interface we used, which microcontroller we are using etc.
Make All: It is a command which is used to compile the program essentially it is a set of statements.
14
Make Clean: To remove all the executable files - .hex, .elf, .o etc. Whenever you make any changes to the code, you will have to execute this command.
Make Program: In order to burn the program into the chip, Program extension is used after make on the command prompt.
15
16
Serial programmer:- This device is used to burn the program from computer to the microcontroller. Serial Cable:- Is the cable used to interface the serial programmer with computer.
18
Following points should kept in mind to write program Programmer notepad- Open the programmer notepad - Select the c/c++ from the plan text tool bar - write the program - Click on File and save the program with C extension example : led.c in separate folder - minimize the programmer notepad MFile(WinAVR) - open the Mfile - Click on Makefile - click on main file name & give the file same file name without C extennsion. example : Main file led - Click on Mfie & click on MCU type & select the type of microcontroller ATMEG 16 - Click on Mfie & click on PON EYSER - Click on Mfie & click on File & Save as with name (Makefile) in the same folder. Programmer notepad- Maximize the programmer notepad & click on Tool - then click on (WinAVR)Makefile:- make is formed - Connect the hardware - then click on (WinAVR) program :- The program is burned in MCU.
19
Following header files are initialize while programming:a. <avr/io.h> :- This header file is used for giving input to the microcontroller & taking output from the microcontroller . Syntax:- #include<avr/io.h>
b. <util/delay.h> : - This is the header file used to initialize the delay function in the programming. Syntax:- #include<util/delay.h>
www.buildingblocks.in | www.bulo.in | 9818403093 20
// set of execution statements could be any: - Declaration statements; - Arithmetic Operations; - Function Calls; return (instance of <return_type>) ; } /* end of the main function */
4. Do Commenting - used to make any program readable/understandable for other programmers - is not an executable statement - should be given at the beginning or end of the line but not in middle of the line 5. Every statement in c should end with semicolon (;) Note: C is a case sensitive Language
21
6.Initialize the Data Direction Register:- this is the register used to indicate the microcontroller that programmer will used the particular port as either input or output in the programming. - is initialized at the beginning in the main function. - Example:- DDRA ox00-----For declare PORT A as input port. DDRA oxFF-----For declare PORT A as output port. 7. Write the PORTA/ PORTB/PORTC/PORTD for making output of the pin of a port of the MC Example:- PORTA=ob11110000;
22
LED Board Circuit diagram Home work:Make an input/output board containing 5 switch & 5 LEDs
23
#include<avr/io.h> #include<util/delay.h> Void main() { DDRD= 0xFF; PORTD=0b10000000; _delay_ms(500); PORTD=0b01000000; _delay_ms(500); PORTD=0b00100000; _delay_ms(500); PORTD=0b00010000; _delay_ms(1000); }
Assignment 1. Write a program to rotate the LEDs glow one by one in right shift. Assignment 2. Write a program to rotate the LEDs glow one by one in Left shift. Assignment 3. Write a program to glow all odd ON in one time & all Even LEDs on on www.buildingblocks.in second time. This pattern will glow continuously.| www.bulo.in | 9818403093
24
Embedded C Has Three major decision making instructions:1. If statement 2. If-else statement 3. if else if statement
If statement:
- is used to implement the decision control instruction. - Syntax: if (This condition is true) { execute this statement ; } - Generally Relational operators are used to compare two values to get a TRUE or FALSE - No semicolon at the end of the if statement - No bracket is needed if single statement has to be executed
www.buildingblocks.in | www.bulo.in | 9818403093
26
Assignment
1. Two ports, A & B are taken, One I/O card is connected at PORTA for input & second I/O card is connected at PORTB for output display. Then If input key 1 is press to input then all odd LEDs glow, 2 is press to input then all even LEDs glow, 3 is press to input then 1,2,3 LEDs glow, 4 is press to input then 4,5,6 LEDs glow, 5 is press to input then 7,8 LEDs glow. 2. Quest. Give the input on port D & output port A . By pressing first switch of I/P board then output will be display first 4 led glow on LED board connected on PORT A. How will use the LED board as input board. / PIN programming
27
If else if statement:
- is used to implement the decision control instruction. - Syntax: if ( This condition is true) { execute this statement ; } else if ( This condition is true) { execute this statement ; } - Generally Relational operators are used to compare two values to get a TRUE or FALSE - No semicolon at the end of the if statement - No bracket is needed if single statement has to be executed
28
29
30
31
32
33
35
37
Application of the Seven Segment: -used in digital clock , - electronic meters, and - other electronic devices for displaying numerical information.
Description of Seven-segment Display: Standard Common Cathode Seven-Segment Display It has 10 pins. Each pin is used to ON or OFF the segments. The Middle two pins of a seven segment is a cathode (-ve), which provides a ground to the seven segment. Decimal Point also connected Standard 5V control for Atmeaga16 controller. Easy to Connect Female Headers
www.buildingblocks.in | www.bulo.in | 9818403093 38
Important point to remember: 7-Segment Display can be attached to any PORT through connector. 8 pins can be connected to one port & 4 pins can be connected to second port. Ground is connected from the supply of the board. Connection with Development Board must be same as with the LED.
39
40
Void main() { DDRD= 0XFF; DDRA=0XFF; while(1) { PORTA=0b10000000; PORTD=0b01100000; delay_ms(2000); PORTA=0b01000000; PORTD=0b11011100; delay_ms(2000); PORTA=0b00100000; PORTD=0b11110100; delay_ms(2000); PORTA=0b00010000; PORTD=0b01100010; delay_ms(2000);
} }
//1
//2
//3
//4
41
42
Principal and working of motor : This motor works on Farad law of the electromagnetic induction.
Law:A current -carrying conductor generates a magnetic field. when a current carrying conductor is placed in the external magnetic field, it will experience a force proportional to the current in the conductor, and to the strength of the external magnetic field. Application Motor is applied where there is there motion is needed. - Used in car, used in washing machine, fans ets.
43
LCD PINS Pin 1 Pin2 Pin3 Pin4 Pin5 Pin6 Pin7 Pin8 Pin9 Pin10 Pin11 Pin12
SYMBOL
CONNECTION TO AVR
Pin13 Pin14
Pin15 Pin16
GND VCC PIN 22 GND EN NOT USED NOT USED NOT USED NOT USED PIN 28 PIN 27 PIN 26 PIN 29 VCC GND
45
46
47
48
49