Upload
vimukthi-randeny
View
1
Download
0
Embed Size (px)
DESCRIPTION
Electonics
Citation preview
EN2852 – Applied Electronics4. Microcontrollers
By: Dr. S. ThayaparanBy: Dr. S. Thayaparan
Slide contents are extracted from the following source of origin :
1. http://en.wikipedia.org/wiki/Microcontroller
2. http://www.engr.sjsu.edu/bjfurman/courses/ME106/ME106pdf/MicroC_overview.pdf
3. http://www.mikroe.com/eng/chapters/view/64/chapter-1-introduction-to-microcontrollers/
4. http://www.mstracey.btinternet.co.uk/pictutorial/picmain.htm
5. http://www.codeproject.com/Articles/29528/Programming-the-PIC-Microcontroller-in-C-using-PIC
6. http://www.mikroe.com/eng/chapters/view/4/chapter-3-i-o-ports/
7. http://www.mikroe.com/eng/chapters/view/2/chapter-1-pic16f887-microcontroller-device-overview/
Applied Electronics Course Content
1. Op Amps
2. Sensors & Transducers
3. Electronic Instrumentation Systems
4. Microcontrollers
� Lectures are: 1.5 Hrs/Week
� Evaluation is : 60% Exam
40% CA
2
4.0 Microcontrollers
1. Introduction to Microcontrollers
2. Components of A Microcontroller
3. Microcomputer Architecture
4. Binary and Hexadecimal Numbers
5. Basic DSP Operations
A PIC 18F8720 microcontroller
in an 80-pin TQFP package
3
5. Basic DSP Operations
6. Programming Microcontrollers
Intel P8051 microcontroller
4.1 Introduction to Microcontrollers
� A microcontroller (sometimes abbreviated µC, uC or MCU) is a small
computer on a single integrated circuit (IC) containing a processor core,
memory, and programmable input/output peripherals
� Microcontrollers are used in automatically controlled products and
devices, such as automobile engine control systems, implantable medical
devices, remote controls, office machines, appliances, power tools, toys
and other embedded systems.
4
and other embedded systems.
� The first single-chip microprocessor was the 4-bit Intel 4004 released in
1971
� Even though there is a large number of different types of microcontrollers
and even more programs created for their use only, all of them have many
things in common.
� If you learn to handle one of them you will be able to handle them all.
4.1 Introduction to Microcontrollers
� A typical scenario on the basis of which it all functions is as follows:
1. Power supply is turned off and everything is still…the program is loaded
into the microcontroller, nothing indicates what is about to come…
2. Power supply is turned on and everything starts to happen at high speed.
The control logic unit keeps everything under control. It disables all
other circuits except quartz crystal to operate. While the preparations
are in progress, the first milliseconds go by.
3. Power supply voltage reaches its maximum and oscillator frequency
5
3. Power supply voltage reaches its maximum and oscillator frequency
becomes stable. Special Function Registers (SFR) are being filled with bits
reflecting the state of all circuits within the microcontroller. All pins are
configured as inputs. The overall electronics starts operation in rhythm
with pulse sequence. From now on the time is measured in µS or nS.
4. Program Counter is set to zero. Instruction from that address is sent to
instruction decoder which recognizes it, after which it is executed with
immediate effect.
5. The value of the Program Counter is incremented by 1 and the whole
process is repeated...several million times per second.
4.1 Introduction to Microcontrollers
6
4.1 Introduction to Microcontrollers
7
4.1 Introduction to MicrocontrollersPIC16F887 Block Diagram
8
4.1 Introduction to Microcontrollers
Read Only Memory (ROM)
• ROM is a type of memory used to permanently save the program.
• ROM size depends on the size of the program.
• ROM can be built in the microcontroller or added as an external chip.
• If ROM is external, the microcontroller is cheaper and the program can be
considerably longer.
• The internal ROM is usually smaller and more expensive, but leaves more
pins available for connecting to peripheral environment.
9
pins available for connecting to peripheral environment.
• The typical size of ROM ranges from 512B to 64KB.
Random Access Memory (RAM)
• RAM is used for temporary storing data and intermediate results created
and used during the operation of the microcontrollers.
• The content of this memory is cleared once the power supply is off.
• For example, if the program performs an addition, it is necessary to have a
register standing for the “sum”.
• The typical size of RAM goes up to a few KBs.
Electrically Erasable Programmable ROM (EEPROM)
• The EEPROM is a special type of memory not contained in all
microcontrollers.
• Its contents may be changed during program execution (similar to RAM ),
but remains permanently saved even after the loss of power (similar to
ROM).
• It is often used to store values, created and used during operation (such as
4.1 Introduction to Microcontrollers
10
• It is often used to store values, created and used during operation (such as
calibration values, codes, values to count up to etc.), which must be saved
after turning the power supply off.
• A disadvantage of this memory is that the process of programming is
relatively slow and it is measured in miliseconds.
4.2 Components of A Microcontroller
11
4.3 Microcomputer Architecture
12
4.3 Microcomputer Architecturei8051 microarchitecture
13
� Microcontrollers are fundamentally digital and use binarylogic
� Digital : Two states: high and low, 1 or 0, on or off
• Often 5V or 0V
� One binary digitis called a bit
� It can take on two possible states: 1or 0
� Four binary digits is called a nibble
4.4 Binary and Hexadecimal Numbers
14
� Four binary digits is called a nibble
� Eight binary digits is called a byte
4.4 Binary and Hexadecimal Numbers
15
4.4 Binary and Hexadecimal Numbers
16
4.4 Binary and Hexadecimal Numbers
17
4.5 Basic DSP Operations
DSP : Digital Signal Processing
• Everything that we do in analog domain can be done in digital domain
Example 1: Signal Amplification
Example 2: Filtering (Low pass, band pass, high pass)
• Some things very difficult to do in analog domain can be easily done in digital
domain
Example 1: Finding the average value
18
Example 1: Finding the average value
Example 2: Finding the maximum value
• DSP has a very wide scope. We will only have a very brief look
• In digital domain, we work with fixed point numbers and variables
• Each variable/value is represented by a fixed number of bits
Example 1: Interger – 8 bits (signed & unsigned)
Example 2: Long Integer – 16 bits (signed & unsigned)
Example 3: Float – 32 or 64 bits (signed)
• In micro controllers, floating point variables are not common. We usually
have to use integers
4.5 Basic DSP Operations
• In simple micro-controllers, numbers are 8 bit values.
• How we interpret the numbers is up to us.
Example 1: 1111 1111 = 255 (unsigned interpretation)
Example 2: 1111 1111 = -128 (signed interpretation)
Example 3: 1000 0000 = 128
Example 4: 1000 0000 = 0.5
• We can also use two 8-bit variables to create 16-bit variable in our program
• It is important to keep the same interpretation throughout our algorithm
19
• It is important to keep the same interpretation throughout our algorithm
• Addition, substraction and multiplication
Example 1: after each adc operation do {
new_sig = sig_adc1 + sig_adc2;
}
Example 2: after each adc operation do {
new_sig = sig_adc1 - sig_adc2;
}
Example 3: after each adc operation do {
new_sig = sig_adc1 * 2;
}
4.6 Programming Microcontrollers
Example: 1 (For a pic micro-controller)
Operation : Smoothing (Low Pass Filtering)
function (adc_complete) {
val0 = 0;
val1 = 0;
sum = 0;
20
sum = sum + val0 + val1;
new_val = sum/3;
val0 = val1;
val1 = new_adc;
}
Example: 2 (For a pic micro-controller)
Operation : High Pass Filtering
- Substract the original with the LPF component.
Example: 2
int ledPin = 13; // LED connected to digital pin 13
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
4.6 Programming Microcontrollers
21
}
void loop()
{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000); // waits for a second
}
4.6 Programming Microcontrollers
RA0-4 & RB0-7 : RA & RB are
bidirectional ports. Can be
configured as an input or an
output.
The number following RA is the
bit number (0 to 4). Each bit can
be configured as Input or Output.
VDD & VSS - Power supply pins
22
Pin-outs of the PIC 16F84
VDD & VSS - Power supply pins
VDD (+5V), VSS (0V)
OSC1/CLKIN & OSC2/CLKOUT
The microcontroller has some
kind of timing.
T0CK1: This is another clock
input, which operates an internal
timer. It operates in isolation to
the main clock.
MCLR - Used to erase the memory locations
inside the PIC (i.e. When re-program it). In
normal use it is connected to VDD.
INT - an input pin can be monitored. If it goes
high, we can do restart, stop or any other
function.
#include <htc.h>
#include <pic.h>
#include <delay.c>
main()
{
TRISB=0; // declare portb as output
for(;;) // do forever
4.6 Programming Microcontrollers
TRISB = %00000000 -> set all PORTB pins as output
TRISB = %11111111 -> set all PORTB pins as input
PORTB = %11111111 -> make all PORTB pins high
PORTB.0[i] = 1 -> make pin number i high
23
for(;;) // do forever
{
RB0=1; // LED ON
DelayMs(250); // delay for 0.25 seconds
RB0=0; // LED OFF
Delayms(250);
}
} Note: A. PORTB : I/O port name
B. TRISA, TRISB: data-direction register name
C. These names are common to most PIC micros.
D. When power up, all the I/O pins are configured as inputs.
4.6 Programming Microcontrollers
24
4.6 Programming Microcontrollers
25
Port A and TRISA Register
ANSEL register :
• determine whether the pins will act as analog inputs or digital inputs/outputs.
RA0 = AN0 (determined by bit ANS0 of the ANSEL register);
RA1 = AN1 (determined by bit ANS1 of the ANSEL register);
RA2 = AN2 (determined by bit ANS2 of the ANSEL register);
RA3 = AN3 (determined by bit ANS3 of the ANSEL register);
1. #include <htc.h>
2. #include <pic.h>
3. #include <delay.c>
4. int j; // general global variables
5. void Wait_These_Seconds(float s) { // only to 3 decimals
6. int milli_Equiv; // milli second equivalent integer
7. milli_Equiv=1000*s;
8. for(j=1; j<=milli_Equiv; j++) { DelayMs(1); }
9. }
10. main() {
11. TRISB=0xF0; // RB4:RB7 are input ; 11110000 in binary
12. for(;;) {
13. if(RB7==1) {
14. RB0=1; // LED ON
4.6 Programming Microcontrollers
26
14. RB0=1; // LED ON
15. Wait_These_Seconds(1.8); // delay for 1.8 seconds
16. RB0=0; // LED OFF
17. Wait_These_Seconds(0.9);
18. }
19. if(RB7==0) { // interrupt is connecting RB7 to ground
20. RB0=1; // LED ON
21. Wait_These_Seconds(0.7); // delay for 0.7 seconds
22. RB0=0; // LED OFF
23. Wait_These_Seconds(0.1);
24. }
25. }
26. }