Embedded Notes - Iet.trichy

Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

Singaperumal Thayar Thunai

INNOVATIVE ELECTRICAL TECHNOLOGIES


Shaping Quality Engineers ____________________________________________________________________________

EMBEDDED

1. OVER VIEW OF EMBEDED SYSTEM 1.1System 1.2Embedded System 1.3Components of Embedded System 1.4Embedded System Hardware 1.5Developing Embedded System 2. HARDWARE DESIGN 2.1 Microcontroller 2.2 Processor 2.3 Controller and Processor Comparison 2.4 Popular Microcontrollers 2.5 Power Supply Section Designing 2.6 Signal Generation with Speed of Frequency 3. MICROCONTROLLER 8051 3.1 Basic Components of 8051 3.2 Features of 8051 3.3 Pin diagram 4. PIC MICROCONTROLLER 4.1 Features of PIC

Singaperumal Thayar Thunai

4.2 Pin Diagram 4.3 Pin Description 5. EMBEDDED C 5.1 Introduction of Embedded C 5.2 Failing of Hardware 5.3 Avoiding the Failure 5.4 Common Aim of Coding Standard 5.5 Sample Rules of Coding Standard 5.6 Example Programs 5.7 Exercise Programs

Singaperumal Thayar Thunai

Chapter 1: OVER VIEW OF EMBEDDED SYSTEM


1.1 SYSTEM A system is a way of working, organizing or doing one or many tasks according to a fixed plan, program or set of rules. A system is also an arrangement in which all its units assemble and work together according to the plan or program. Example: 1. WATCH It is a time display SYSTEM Parts: Hardware, Needles, Battery, Dial, Chassis and Strap. Rules All needles move clockwise only A thin needle rotates every second A long needle rotates every minute A short needle rotates every hour All needles return to the original position after 12 hours.

2. WASHING MACHINE It is an automatic clothes washing SYSTEM Parts: Status display panel, Switches & Dials, Motor, Power supply & control unit, Inner water level sensor and solenoid valve. Rules Wash by spinning

Singaperumal Thayar Thunai

Rinse Drying Wash over by blinking Each step display the process stage In case interruption, execute only the remaining

1.2 EMBEDDED SYSTEM An embedded system is a special purpose system that is used to perform one or few dedicated functions. Simply, we can call any electronic device that has a computer system embedded inside it an embedded system. An Embedded System is one that has computer hardware with software embedded in it as one of its important components. Some examples of embedded systems are: 1. Alarm / security system 2. Automobile cruise control 3. Heating / air conditioning thermostat 4. Microwave oven 5. Anti-skid braking controller 6. Traffic light controller 7. Vending machine 8. Gas pump 9. Handheld Sudoku game

Singaperumal Thayar Thunai

10. Irrigation system controller 11. Singing wall fish (or this gift seasons equivalent) 12. Mars Rover Embedded systems are made to perform few tasks only, after implementation you cant use them for another purposes. Ex: You cant watch movies using the microprocessor of your microwave oven!!

1.3 COMPONENTS OF EMBEDDED SYSTEM It has Hardware They are processor, Timers, Interrupt controller, I/O Devices, Memories, Ports, etc. It has main Application Software That may perform concurrently the series of tasks or multiple tasks. It has Real Time Operating System (RTOS) RTOS defines the way the system work which supervise the application software. It sets the rules during the execution of the application program. A small scale embedded system may not need an RTOS.

Singaperumal Thayar Thunai

1.4 EMBEDDED SYSTEM HARDWARE

1.5 DEVELOPING EMBEDDED SYSTEMS

Embedded System Development

Hardware Development

Software Development

Singaperumal Thayar Thunai

Chapter 2: HARDWARE DESIGN

2.1 MICROCONTROLLER Its a full computer system on a chip, even if its resources are far more limited than of a desktop personal computer designed for standalone operations. So.. Whats the difference between a microcontroller and a microprocessor system?

A microcontroller has a processor and many peripherals integrated with it on the same chip, like a flash memory, RAM, I/O ports, serial communication ports, ADC, Etc.

Singaperumal Thayar Thunai A timer module to allow the MCU to perform tasks for certain time periods. A serial I/O port to allow data to flow between the MCU and other devices such as a PC or another MCU. An ADC to allow the MCU to accept analog inputs for processing.

But a microprocessor cant do all the functions of a computer system on its own, and needs another circuits to support it like: I/O devices, RAM, ROM, DMA controllers, Timers, ADC, LCD drivers.. Etc.

2.2 PROCESSOR Program Flow and data path Control Unit (CU): That includes a fetch unit for fetching instructions from the memory. Execution Unit (EU): Includes circuits for arithmetic and logical unit (ALU), and for instructions for a program control task, say, data transfer instructions, halt, interrupt, or jump to another set of instructions or call to another routine or sleep or reset.

Singaperumal Thayar Thunai

2.3 CONTROLLER AND PROCESSOR COMPARISION

COMPARISION Microcontroller Depend mainly on its peripherals like: Program memory, I/O ports, timers, interrupt circuitry, ADC, Etc. Used for a few dedicated functions determined by the system designer. Usually used as a part of a larger system Used in many applications, according to the program running on it Its in the heart of our PCs General purpose Microprocessor Depend mainly on other devices like: I/O devices, memory, DMA controllers, Etc.

2.4 POPULAR MICROCONTROLLERS 8051 (Intel and others) 80386 EX (Intel) PIC (Microchip) 68HC05 (Motorola) Z8 (Zilog)

Singaperumal Thayar Thunai

2.5 POWER SUPPLY SECTION DESIGNING You need to hook up 5V and GND to your micro. Your house outlet runs at 220V AC and is very bad for 5V DC (direct current) micros. So you'll need to convert the 220V AC from your outlet to a useable 5V DC. If you reverse the connection on your micro - bad things happen. Always make sure your 5V power supply is connected to the VCC pins and GND to GND. If you reverse this and connect 5V to GND on the micro and GND to VCC on the micro, things won't explode, probably no smoke, things will probably heat up like crazy. Before the regulator circuit, the 220V AC converted to 9V DC by using adapter. Then the 9V converted to 5V DC by the regulator circuit. The most common regulator is called the LM7805.

We can simply connect the terminals of the regulator LM7805 like in above circuit but there is some noise in the input pin. That noise may affect the output voltage so we should use filtering capacitors to get better output voltage.

Singaperumal Thayar Thunai

Another thing is, while designing the circuit as a beginner there may be a chance of making mistakes in designing so if any things heat up/smoke/spark, just unplug or turn off the system. So we need an on/off power switch/relay. Now the circuit modified as below. Remember all the warning about reversing VCC and GND and how that is bad? Well if you connect your power supply backwards, that's bad. So let's protect ourselves! Note that the diodes are polarized. Hence the diode can avoid reverse current.

Let us see how to connect the LED with this regulator circuit. If you connect the LED like in above circuit the LED will glow much brighter but burn out. The LED is a diode it can withstand only 20mA. So here we should use ohms law (V=IR) to calculate the resistance value to drop voltage and maintain the current 20mA. The calculation as follows, V = IR (this is Ohm's law) If we have 5V, and we only want 20mA flowing through the LED: 5V = 0.02 * R R = 250 Ohm Now this is not completely true because the LED has a forward voltage drop. But dont worry about that we can use 220 ohm, 330ohm and 1k.The final proper circuit is given below,

Singaperumal Thayar Thunai

2.6 SIGNAL GENERATION WITH SPEED OF FREQUENCY

Calculation of Machine Cycle for 8051: The formula for calculating the machine cycle is given by, Machine cycle=1/(Fosc/12). Find the machine cycle for Fosc = 11.0592 MHz Fosc = 16 MHz Solution: 1). 11.0592 MHz / 12 = 921.6 kHz; Machine cycle = 1 / 921.6 kHz = 1.085 s 2). 16 MHz / 12 = 1.333 MHz; Machine cycle = 1 / 1.333 MHz = 0.75 s

Singaperumal Thayar Thunai

Calculation of Machine Cycle for PIC16f877A: The formula for calculating the machine cycle is given by, Machine cycle=1/(Fosc/4).

Singaperumal Thayar Thunai

Chapter 3: MICROCONTROLLER 8051

3.1 BASIC COMPONENTS OF 8051 4K bytes internal ROM 128 bytes internal RAM Four 8-bit I/O ports (P0 - P3). Two 16-bit timers/counters One serial interface.

3.2 FEATURES OF 8051 only 1 On chip oscillator (external crystal) 6 interrupt sources (2 external , 3 internal, Reset) 64K external code (program) memory(only read)PSEN 64K external data memory(can be read and write) by RD,WR Code memory is selectable by EA (internal or external) We may have External memory as data and code.

Singaperumal Thayar Thunai

3.3 PIN DIAGRAM

One of the most useful features of the 8051 is that it contains four I/O ports (P0 - P3) Port 0 pins 32-39P0P0.0P0.7 8-bit R/W - General Purpose I/O Or acts as a multiplexed low byte address and data bus for external memory design Port 1 pins 1-8 P1P1.0P1.7

Only 8-bit R/W - General Purpose I/O Port 2 pins 21-28P2P2.0P2.7 8-bit R/W - General Purpose I/O Or high byte of the address bus for external memory design Port 3 pins 10-17P3P3.0P3.7 General Purpose I/O

Singaperumal Thayar Thunai

If not using any of the internal peripherals (timers) or external interrupts. Each port can be used as input or output (bi-direction). The port3 has alternative functions as follows.

Singaperumal Thayar Thunai

Chapter 4: PIC MICROCONTROLLER

4.1 FEATURES OF PIC There are varieties of choice in PIC from 8 bit to 32 bit. Low Power Reasonable Size Convenient Packaging Through Hole (Dip) Surface Mount (QFN/SPDIP)

Resources and References Sleep mode Watchdog timer (WDT) Code protection In-circuit serial programming In-circuit debugger

Singaperumal Thayar Thunai

4.2 PIN DIAGRAM

Pin diagram of PIC16F877A.

4.3 PIN DESCRIPTION


As seen in Figure above, the most pins are multi-functional. For example, designator RA3/AN3/Vref+/C1IN+ for the fifth pin specifies the following functions:

RA3 Port A third digital input/output AN3 Third analog input Vref+ Positive voltage reference C1IN+ Comparator C1positive input

Singaperumal Thayar Thunai

This small trick is often used because it makes the microcontroller package more compact without affecting its functionality. These various pin functions cannot be used simultaneously, but can be changed at any point during operation. PIC16F877 has 5 basic input/output ports. They are usually denoted by PORT A (R A), PORT B (RB), PORT C (RC), PORT D (RD), and PORT E (RE). These ports are used for input/ output interfacing. In this controller, PORT A is only 6 bits wide (RA-0 to RA-7), PORT B , PORT C,PORT D are only 8 bits wide (RB-0 to RB-7,RC-0 to RC-7,RD-0 to RD-7), PORT E has only 3 bit wide (RE-0 to RE-7).

PORT-A PORT-B PORT-C PORT-D PORT-E

RA-0 to RA-5 RB-0 to RB-7 RC-0 to RC-7 RD-0 to RD-7 RE-0 to RE-2

6 bit wide 8 bit wide 8 bit wide 8 bit wide 3 bit wide

All these ports are bi-directional. The direction of the port is controlled by using TRIS(X) registers (TRIS A used to set the direction of PORT-A, TRIS B used to set the direction for PORT-B, etc.). Setting a TRIS(X) bit 1 will set the corresponding PORT(X) bit as input. Clearing a TRIS(X) bit 0 will set the corresponding PORT(X) bit as output. (If we want to set PORT A as an input, just set TRIS(A) bit to logical 1 and want to set PORT B as an output, just set the PORT B bits to logical 0.) Analog input port (AN0 TO AN7) : these ports are used for interfacing analog inputs. TX and RX: These are the USART transmission and reception ports. SCK: these pins are used for giving synchronous serial clock input. SCL: these pins act as an output for both SPI and I2C modes. DT: these are synchronous data terminals.

Singaperumal Thayar Thunai

CK: synchronous clock input. SD0: SPI data output (SPI Mode). SD1: SPI Data input (SPI mode). SDA: data input/output in I2C Mode. CCP1 and CCP2: these are capture/compare/PWM modules. OSC1: oscillator input/external clock. OSC2: oscillator output/clock out. MCLR: master clear pin (Active low reset). Vpp: programming voltage input. THV: High voltage test mode controlling. Vref (+/-): reference voltage. SS: Slave select for the synchronous serial port. T0CK1: clock input to TIMER 0. T1OSO: Timer 1 oscillator output. T1OS1: Timer 1 oscillator input. T1CK1: clock input to Timer 1. PGD: Serial programming data. PGC: serial programming clock. PGM: Low Voltage Programming input. INT: external interrupt. RD: Read control for parallel slave port. CS: Select control for parallel slave. PSP0 to PSP7: Parallel slave port. VDD: positive supply for logic and input pins. VSS: Ground reference for logic and input/output pins.

Singaperumal Thayar Thunai

Chapter 5: EMBEDDED C

5.1 INTRODUCTION OF EMBEDDED C


In embedded programming usually programmer have to be much more aware of the resources consumed in embedded systems programming than you have to in ordinary programs time, space, communication channels, files, ROM (Read-Only Memory), Flash memory, etc.. Programmer must take the time to learn about the way your language features are implemented for a particular platform hardware, operating system and Libraries. A lot of this kind of programming is looking at specialized features of an RTOS (Real Time Operating System), Using a Non-hosted environment (thats one way of saying a language right on top of hardware without an operating system), Involving (sometimes complex) device driver architectures and Dealing directly with hardware device interfaces.

5.2 FAILING OF HARDWARE


In general, we cannot know how the failing happening was. In practice, we can assume that some kinds of errors are more common than others but sometimes a memory bit just decides to change. Why because there is more number of failure can occur, Power surges/failure The connector vibrated out of its socket Falling debris Falling computer X-rays

Singaperumal Thayar Thunai

The transient errors are the worst (E.g., only when the temperature exceeds 100 F. and the cabinet door is closed) and the errors that occur away from the lab are the worst (E.g., on Mars).

5.3 AVOIDING THE FAILURE


Replicate -> In emergency, use a spare Self-check-> Know when the program (or hardware) is misbehaving Have a quick way out of misbehaving code ->Make systems modular, Have some other module, computer, part of the system responsible for serious errors, In the end, maybe a person i.e., manual override. Monitor (sub) systems-> In case they cant/dont notice problems themselves

5.4 COMMON AIM OF CODING STANDARD


Reliability Portability Maintainability Testability Reusability Extensibility Readability

Singaperumal Thayar Thunai

5.5 SAMPLE RULES OF CODING


No function shall have more than 200 lines (30 would be even better) that is, 200 non-comment source lines Each new statement starts on a new line (E.g., int a = 7; x = a+7; f(x, 9); // violation!) No macros shall be used except for source control (using #ifdef and #ifndef Identifiers should be given descriptive names) May contain common abbreviations and acronyms When used conventionally, x, y, i, j, etc., are descriptive Use the number_of_elements style rather than the numberOfElements style Type names and constants start with a capital letter(E.g., Device_driver and Buffer_pool ) Identifiers shall not differ only by case (E.g., Head and head // violation!)

Identifiers in an inner scope should not be identical to identifiers in an outer scope (E.g., int var = 9; { int var = 7; ++var; } // violation: var hides var )

Declarations shall be declared in the smallest possible scope Variables shall be initialized (E.g., int var; // violation: var is not initialized) Casts should be used only when essential Code should not depend on precedence rules below the level of arithmetic expressions (E.g., x = a*b+c; // ok) if( a<b || c<=d) // violation: parenthesize (a<b) and (c<=d)

Increment and decrement operations shall not be used as sub expressions ( E.g., int x = v[++i]; // violation (that increment might be overlooked))

Singaperumal Thayar Thunai

5.6 EXAMPLE PROGRAMS 1). Requirement: LED blinking- 8 LEDs are connected in PORTB. Coding:
void main() { TRISB = 0; do { PORTB = 0x00; Delay_ms(1000); PORTB = 0xFF; Delay_ms(1000); } while(1); } // Turn OFF LEDs on PORTB // 1 second delay // Turn ON LEDs on PORTB // 1 second delay // set direction to be output

// Endless loop

2).Requirement: Running glow of LEDs- 8 LEDs connected in PORTA Coding:


char counter; void wait() {

Singaperumal Thayar Thunai

Delay_ms(100); } void main() { TRISA = 0x00; PORTA = 0x00; while (1) { for (counter=0; counter<8; counter++) { PORTA |= 1 << counter; wait(); } counter = 0; while (counter<8) { PORTA &= ~(1 << counter); wait(); counter++; } } // set direction to be output // turn OFF the LATD leds

Singaperumal Thayar Thunai

3).Requirement: Controlling the LEDs by using a switch. 4 LEDs are connected in PORTC
and a switch connected in PORTB.

Coding:
char oldstate; void main() { TRISB0_bit = 1; TRISC = 0x00; PORTC = 0xAA; oldstate = 0; do { if (Button(&PORTB, 0, 1, 1)) { oldstate = 1; } if (oldstate && Button(&PORTB, 0, 1, 0)) { PORTC = ~PORTC; // Invert PORTC // Detect one-to-zero transition // Update flag // Detect logical one // set RB0 pin as input // Configure PORTC as output // Initial PORTC value // Old state flag

Singaperumal Thayar Thunai

oldstate = 0; } } while(1); }

// Update flag

// Endless loop

4).Requirement: LED blinking using timer interrupt. 6 LEDs are connected in PORTB. Coding:
unsigned char timerint=0; void main() { PORTB=0; TRISB=0xc0; PIR1.f0=0; PIE1.f0=1; INTCON=0xc0; T1CON=0x01; while(1) { if(timerint) { PORTB=~PORTB;

Singaperumal Thayar Thunai

timerint=0; } } } void interrupt() { if(PIR1.f0) { timerint=1; PIR1.f0=0; TMR1L=TMR1L+176; TMR1H=TMR1H+60; } }

5).Requirement: LEDs controlled by 3 switches. 4 LEDs and 3 switches connected in


PORTB. The user press the switch2 after pressing switch1 means LED will glow otherwise after pressing switch3 means LED will off.

Coding:
// Program for controlling single LED by 3 switches #define switch_1 PORTB.f0

Singaperumal Thayar Thunai

#define switch_2 PORTB.f1 #define switch_3 PORTB.f2 #define LED PORTB.f4 #define ON 1 #define OFF 0 #define IO_config 0x0f #define Port_config TRISB #define Digital_port PORTB #define clear 0 #define set 1 #define pressed 1 #define releassed 0 #define status_1 status.f0 #define status_2 status.f1 #define status_3 status.f2 void main() { unsigned char status,ABC=1,GHI=1; status_1=clear; status_2=clear; status_3=clear;

Singaperumal Thayar Thunai

Digital_port=clear; Port_config=IO_config; delay_ms(100); while(1) { if(switch_1==pressed && status_1==clear) { status_1=set; GHI=1; } if(switch_1==released && status_1==set) { ABC=0; status_1=clear; } if(switch_2==pressed && status_2==clear ) { if(ABC==0) LED=ON; if(GHI==0) LED=OFF;

Singaperumal Thayar Thunai

status_2=set; } if(switch_2==released && status_2= =set) { status_2=clear; ABC=1; } if(switch_3==pressed && status_3= =clear) { status_3=set; ABC=1; } if(switch_3==released && status_3= =set) { GHI=0; status_1=clear; } } }

Singaperumal Thayar Thunai

6).Requirement: EEPROM handling. Store the value 150 in EEPROM. Coding:


#define lcd_port PORTB #define clear 0 #define set 1 unsigned char data=0; void digit_disp(unsigned int digit,unsigned char row,unsigned char col,unsigned char count) { unsigned char temp; if(count==3) { temp=digit/100; digit=digit%100; lcd_custom_chr(row,col,(temp+48)); col++; } temp=digit/10; digit=digit%10; lcd_custom_chr(row,col,(temp+48)); col++; lcd_custom_chr(row,col,(digit+48));

Singaperumal Thayar Thunai

} void init() { lcd_port=clear; lcd_custom_config(&PORTB,1,2,3,4,&PORTB,7,6,5 ); lcd_custom_cmd(lcd_clear); lcd_custom_out(1,1,"WELCOME"); delay_ms(1000); lcd_custom_cmd(lcd_clear); } void EEPROM_handling() { soft_i2c_config(&PORTC,4,3); soft_i2c_start(); soft_i2c_write(0xA0); soft_i2c_write(7); soft_i2c_write(150); soft_i2c_stop(); delay_ms(2); soft_i2c_start(); soft_i2c_write(0xA0);

Singaperumal Thayar Thunai

soft_i2c_write(7); soft_i2c_start(); soft_i2c_write(0xA1); data=soft_i2c_read(0); soft_i2c_stop(); } void main() { init(); EEPROM_handling(); lcd_custom_cmd(lcd_clear); digit_disp(data,1,1,3); }

5.7 EXERCISE PROGRAMS


1) Write a program to make 4 LEDs blink in alternative pins of PORTD. 2) Write a program to make running glow of LEDs in PORTA, PORTB, PORTC and PORTD continuously. 3) Write a program to make the LED glow while pressing the switch1, off the LED while pressing the switch2 and blink the LED while pressing switch3. PORT configuration is your wish. 4) Write a program to blink 8 LEDs in both forward and reverse direction. The LEDs are connected in PORTC. 5) Write a program to make LED glow after 5minites from switch pressed by using timer

Singaperumal Thayar Thunai

interrupt. 6) Write a program to handling the menu using 4 switches. Switch1, switch2, switch3 and switch4 are used as up arrow, down arrow, select and cancel button respectively. 7) Write a program to store the password in an array using keypad. 8) Write a program to write the password into the EEPROM. 9) Write a program to display time out when the user didnt give the password for 5minites. 10) Write a program to compare the password given by user and the password stored in EEPROM.

You might also like