PIC16 Microcontrollers, Unit 4, Ch 2.3-2.4; Memory, Stack, and SFRs
Summary
TLDRIn this lecture, Clint Halstead introduces microprocessors, focusing on program memory, the stack, data memory, and special function registers using the book 'Designing Embedded Systems with PIC Microcontrollers' by Tim Wilmshurst. He explains flash memory, program counters, the stack's LIFO structure, and their roles in executing and storing code. Halstead also covers SRAM, special function registers, and their use in interfacing with peripherals. He concludes by discussing the configuration word, its significance in setting microcontroller features, and its protection mechanisms against code theft.
Takeaways
- 📚 The lecture is an introduction to microprocessors, specifically using 'Designing Embedded Systems with PIC Microcontrollers: Principles and Applications, Second Edition' by Tim.
- 💾 The program memory is composed of non-volatile flash memory, which retains the program code even when power is removed from the chip.
- 🔢 The program memory space ranges from 0 to 3FF, providing approximately 1000 memory locations for storing code.
- 👉 The program counter is essential as it points to the next instruction in the program memory and starts at address zero upon power-up or reset.
- 📚 Section 2.3 and 2.4 of the book are being discussed, which cover program memory, stack, data memory, and special function registers.
- 🔠 The stack is a part of SRAM and operates on a LIFO (Last In, First Out) basis, crucial for function calls and preserving the state of the program counter.
- 🔄 The stack facilitates the 'push' and 'pop' operations, which save and restore the program counter's state during function calls.
- 🔩 Data memory is stored in SRAM, which is volatile and loses its values when power is removed, typically used for values set during program execution.
- 🌐 Special function registers are used for interfacing with peripherals, controlling aspects such as timers, port directions, and EEPROM memory.
- 🛠 The configuration word, found in program memory, determines certain operating features of the microcontroller and can be set during the programming process.
- 🔒 Code protection bits within the configuration word can prevent unauthorized reading of the microcontroller's program memory, protecting intellectual property.
- ⚙️ Oscillator configuration is vital as it dictates how the microcontroller operates, with options for internal (RC) and external oscillators.
Q & A
What is the primary function of program memory in a microcontroller?
-The primary function of program memory in a microcontroller is to store the code, including assembler language code and C code, which the microcontroller executes.
Why is program memory often implemented using flash memory?
-Program memory is often implemented using flash memory because it is non-volatile, meaning it retains the stored values even when the power is removed from the chip.
What does the program counter in a microcontroller do?
-The program counter in a microcontroller points to the next instruction to be executed. It is set to zero upon power-up or reset, indicating the starting point of the code execution.
What is the significance of the stack in a microcontroller?
-The stack in a microcontroller is used to store the return addresses during function calls, allowing the program to resume execution at the correct location after the function completes. It operates on a LIFO (Last In, First Out) principle.
How does the stack assist during function calls in a program?
-During function calls, the current value of the program counter is pushed onto the stack to remember the current location in the code. After the function executes, the program counter is popped with the stored address, resuming the code execution from where it left off.
What is the role of the special function registers in data memory?
-Special function registers in data memory control various aspects of the microcontroller's operation, such as timers, the program counter, status, file select for indirect addressing, and port control.
Why is SRAM considered volatile memory?
-SRAM is considered volatile memory because it loses its stored values when power is removed from the chip, unlike non-volatile memory which retains values after power loss.
What is the purpose of the TRISC register in a microcontroller?
-The TRISC register controls the direction of Port C on a microcontroller, determining whether the port operates as an input or output.
How does the configuration word in a microcontroller affect its operation?
-The configuration word in a microcontroller determines certain operating features, such as code protection, power-up configuration, watchdog timer settings, and oscillator selection. It is set during the programming process and cannot be changed during normal operation.
What is the purpose of code protection bits in the configuration word?
-Code protection bits in the configuration word prevent unauthorized reading of the microcontroller's program memory, protecting the intellectual property of the software developed for the microcontroller.
Why is it important to check the watchdog timer and oscillator settings when troubleshooting a microcontroller?
-It is important to check the watchdog timer and oscillator settings when troubleshooting a microcontroller because incorrect settings can prevent the microcontroller from operating correctly. The watchdog timer may need to be disabled, and the oscillator must be correctly configured for the microcontroller to function.
Outlines
📘 Program Memory and Stack Overview
This paragraph introduces the concept of program memory in microprocessors, specifically focusing on the PIC microcontroller. It explains that program memory is composed of non-volatile flash memory, which retains the code even when power is removed. The memory is addressed from 0 to 3FF, providing approximately a thousand memory locations for code storage. The paragraph also introduces the program counter, which points to the next instruction in the program memory, and the stack, which is likened to a LIFO (Last In, First Out) structure used for function calls. The program counter's role in starting at address zero and the importance of the interrupt service routine starting at address 004 are also highlighted.
🔧 Understanding the Stack and Data Memory
The second paragraph delves into the functionality of the stack, which is a part of the SRAM and operates on a LIFO principle. It is used to store the program counter during function calls, ensuring that the program can return to the correct location after the function is executed. The explanation uses the analogy of stacking plates to clarify the concept of the stack. The paragraph also discusses data memory, which is volatile and stored in SRAM, and its role in setting values during program execution. Special function registers are introduced, which are crucial for the microcontroller's operation, including timer controls, status registers, and port controls.
🔄 Special Function Registers and Peripheral Interaction
This section explains the role of special function registers in interfacing with peripherals. It describes how these registers act as an interface between the microcontroller's core and external devices, such as LEDs or LCD panels. The paragraph outlines the process of configuring peripherals through software to operate in various modes and the importance of special function control registers in this process. It also touches on the data transfer process involving special function registers and the need for further control data during operation.
⚙️ Configuration Word and Microcontroller Settings
The final paragraph discusses the importance of the configuration word in determining the microcontroller's operating features. It is highlighted that the configuration word is written during the programming process and cannot be accessed during normal operation. The paragraph explains the various settings that can be adjusted in the configuration word, such as code protection bits to prevent software theft, power-up enable bits, watchdog timer settings, and oscillator configurations. The importance of correctly setting these options during the programming phase is emphasized, and an example of checking for an internal RC oscillator in the datasheet of the 16F-84A microcontroller is provided.
Mindmap
Keywords
💡Microprocessors
💡Program Memory
💡Stack
💡Program Counter
💡Data Memory
💡Special Function Registers
💡Peripherals
💡Configuration Word
💡Code Protection
💡Oscillator
Highlights
Introduction to microprocessors and usage of the textbook 'Designing Embedded Systems with PIC Microcontrollers'.
Overview of program memory and stack, sections 2.3 and 2.4 of the textbook.
Explanation of flash memory as non-volatile memory used for program storage.
Program memory space details with user memory locations from 0 to 3FF, allowing storage of up to 1000 instructions.
Introduction to program counter, stack, and program memory (flash).
Description of how the program counter points to the next instruction in program memory.
Explanation of interrupt service routines starting at address 004.
Details about the stack functioning as last in first out (LIFO) memory.
Explanation of how the stack is used during function calls and returns.
Introduction to data memory and special function registers (SFRs).
Description of SRAM as volatile memory for data storage.
Details about data memory sections, general-purpose registers, and special function registers.
Explanation of how to access banked memory sections using the bank select bit.
Overview of special function registers for controlling peripherals and data transfers.
Introduction to configuration word for setting microcontroller operating features during programming.
Explanation of code protection bits to prevent software theft.
Details about power-up enable bits, watchdog timer, and oscillator configuration.
Exercise suggestion to check the datasheet for the PIC16F84A for internal RC oscillator presence.
Next section to cover topics from 2.5 to 2.7 of the textbook.
Transcripts
hello this is Clint Halstead and this is
introduction to microprocessors I'm
using again the designing embedded
systems with pic microcontrollers
principles and applications second
edition with Tim well first today we're
going to be talking about program memory
and the stack will be going over section
2.3 and 2.4 of the book also will cover
data memory and special function
registers interfacing with peripherals
and talk about the configuration word so
the program memory and stack looks
similar to this diagram the program
memory is made up out of flash flash
memory which is non-volatile memory
which means that you know when the power
is removed from your from the chip then
it remembers the values the program the
program memory is a section of the chip
where you're you're actually your code
exists so your assembler language code
where your C code exists in this section
here called called the flash ok and you
can see here that we have from 0 to 3 FF
of this user memory space program memory
space your instructions are here each
you have each memory address location
even though they're not all shown these
numbers go from 0 0 0 0
hex - 0 0 0 1 0 0 2 0 0
0:04 and it just continues to go on up
even though they're not labeled so you
have a thousand memory locations in
order to store your code notice that
three F F remember F F is represents
eight ones so f f hex it represents
eight binary ones which also represents
256 decimal so 256 and notice that we
have we're going to have four of those
because we have zero one two and three
because it goes all way up to two three
F F that means it had you have 0 F 0 F F
which is 256 and you have one F F 2 F f
+ 3 F F so that's a 1000 you know if you
take 256 times 4 it gives you roughly a
thousand so you have a thousand
different memory locations in order to
store your code so as long as your code
isn't longer than that then it will fit
in this space if you need more code
space than you have to go up to a part
of different pic microcontroller that
has more program memory space or flash
so also notice here that we have we have
several features of the program memory
and the stack first thing we have is
there's three pieces three main pieces
we have the program counter we have the
stack which we'll talk about later and
then we have or we'll talk about in just
a few minutes so we have a program
counter the stack and then we have the
program memory which is the flash now
the program counter points to the
program memory so the the program
counter contains your next instruction
so when powers reset on the chip or when
you first powered up the program counter
is set to zero which means it's pointing
to the very first location which is zero
and then this is where your code has to
start
so also notice you have a also have an
interrupts interrupt service routine
that must start at zero zero four we'll
talk about that later on in the book but
not today
so if you have an external pin that's an
interrupt if that pin goes high
it creates an interrupt and then your
code will instantly go to this place and
so you need to put any sort of interrupt
code here so typically what people do is
they if they're not going to use
interrupts they they reset to zero and
then they put a go-to instruction as the
first instruction so say go to down here
and then you can start your code from
that point so you need to skip this
peripheral interrupt vector so that's
just a little tip we'll learn again more
about that later next thing that we want
to notice here is we've all read about
this a program must start here at zero
and the program counter points to the
locations in the program memory so the
program counter points here now the
question is what is this stack and the
best way I can describe the stack is
it's just a you've got some SRAM here
that will the program counter will be
copied to this stack and the stack is
just some some RAM some memory and this
memory is called a LIFO the book talks
about on page 37 it's a last in first
out last in first out just means that
the last thing that you put in the stack
is the first thing that comes out so if
you for example if you anytime describe
it as being stacking plates you know if
you stack a bunch of plates the last
plate you put down is also the first
plate you're gonna pick up when you grab
it you're not going to grab the plate
from the bottom you're gonna grab it
from the top so that's another reason
they call it a stack and the best way I
can describe the stack is the stack is
used during function calls so when
you're in here using your program and
you're you're running sequentially in
your code if you have a go-to statement
or a call statement I mean
rather a call statement what a call
statement does is it takes the he
remembers the place that you're at the
current value of the program counter
let's say that you I think about it also
as a reading a book you can think of it
as flipping through the pages of a book
and each page of the book you know how
can represent the address to your at so
let's say you're going out through here
and you're executing instructions to
address twenty you can think of that as
being reading through a book and then on
page 20 of the book it says well I want
you to go to page 50 so if you go to
page 50 you need to remember that you're
on page 20 so you need to remember that
you're at address location 20 so if you
jump down here to 50 you execute the
instructions at 50 but then when you get
a return statement you have to remember
where you you have to go back to so you
have to go back to 20 so that's kind of
a way to remember it's the same way when
you're reading a text book when you're
reading along and there's a reference to
another page you have to go to the other
page but you want to also remember where
you are at so that you can continue
reading your text later on so that's
kind of the way it works is is that the
the stack when there's a call function
it it jumps to the location where your
your program is that in the in the your
in your code so it's going to jump to
the new location but it needs to
remember so it pushes that we call that
pushing divided to the stack so let's
say that you're you're executing code to
get to 20 and then you get a call
function to another location it's going
to push that location of 20 onto the
stack now you're going to go execute
your code down here at a different
location and then when you get a return
it's going to what we call pop it's
going to pop that 20 value back to the
program counter so that your you can go
back and continue executing your code so
it's called when you put something on
the stack or when you put the program
counter on the stack it's called pushing
when you pull it back off then it's
called
popping so push and pop okay and go
ahead and read the textbook if you need
more information on that the next thing
we're talking about is data and memory
and special function register map now
the data memory is stored in the SRAM
portion SRAM is volatile memory which
means that these values are lost
you know once the powers are moved from
the chip okay so these do not get
remembered so that basically means your
your code your code typically sets these
values as the as the program is running
so as the program is running here and
your non-volatile memory in your flash
you're going to code that so that it
sets these values the way you want so it
doesn't really matter that these values
go away when power goes away because
your program is going to control these
registers anyway so from that
perspective you know you don't really
need to worry about it so you don't
really need this to be non-volatile
memory but anyway these are called your
special function registers so your your
data memory section is from 0 to 7f but
whether or not this part down here is
grayed out but pins on what type of chip
you have so if you have a larger chip
then you may have memory space all the
way down to 7f H or 7f hex but for the
16 f-84 we only have from 0 to 4 F okay
so means we have 68 general-purpose
registers if we had and notice that it
goes from it goes on both side you have
two banks so if you had a larger chip
that had all of these available than you
would have up to 256 because FF
represents 256 in 7f preferences 128 so
Bank 0 has 128 locations my coin has
another hundred twenty-eight locations
if you add those together it gives you
256 locations but notice that the first
few from zero to zero B are reserved the
rest of these you can write whatever
values you want to these and you can
define them to what you want but the
first 0 to 0 0 to 0 B are called special
function registers this is for the timer
this is for the this is the program
counter low value this is the status
register this is the file select
register for indirect addressing a
support a which controls your your port
a on your chip port B controls port B
which is your pins on your chip your
parallel port then you have EE data
that's your key EEPROM memory location
data and EEPROM address and then you
have some internet interrupt registers
then when you go to banker when you go
to this other side to bank one you
actually the only way you can get there
is you have to set the bank select bit
in the status register to 1 I believe
that's a bit bit 5 of the status
register you have to set two more in
order to go over here and then once you
get that makes a lot select register set
then you go ahead and you you you
address location 0 but that will give
you a 0 so it's a little confusing but
once you see if you have to have this
Bank select register set to 0 to get to
this side and you have to have the bank
select bit set to 1 to get to this side
over here once we do a lab we'll we'll
understand that a little bit better so
the option register you have to be you
have to have the bits so like the make
select bit set to 1 but once you do then
you can get over here option register
notice that some of these are the same
so notice that the program counter low
is the same whether I'm banks you or
Bank 1 same with the status
registers same with the file select
register but notice that for the port a
when you have the bank's select bit one
then you have this tryst a function
instead of port a so you have the tryst
a controls the direction for port a so
if you so what that means is if you want
do you want to report any to B inputs or
outputs so that's what's being set by
the tryst a whether or not the port a is
input or output because you can't really
have both you typically have your ports
or either one way or another it is
possible to have a by Direction report
but you would have to control that
somehow through software through a
read/write bit or something like that
but for a lot of simple app simple
applications they're usually either set
to either inputs or outputs so that's
the tryst a is how you control those and
then your e address EE prom data you
control with these EE control registers
1 & 2 and so that's about it for that
so that's your data memory and special
function register map now when you want
to interface with peripherals you do
that through the special function
control registers which means these
registers like tryst a tryst B and then
the data transfer special function
register which would be port a import B
so those are examples of this kind of
diagram this is a generic diagram of
showing you how do you get to the
outside world
you got this microcontroller core and
you got your program space over here in
your program code and you're wanting to
get to this outside world which may be
an LED or LCD panel liquid crystal
display or a variety of other things but
the way the interface between those the
outside world and the internal processor
core are your special function registers
and your data your data so your control
special function and your data transfers
special function so you've got two
registers here that you need to control
and then you know sometimes your outside
world you have some interrupts that come
into your my
processor so the peripherals can be
configured in software to operate a
number of modes and you do this to do
this certain control data must be
sitting set sit to them to set them up
in the desired way once in use there
will be data flow between core and
peripheral there may still be need for
further control data these needs are
commonly met by means of dedicated
memory memory registers sometimes called
special function registers this approach
gives a microcontroller manufacturer
greater flexibility to extend the
microcontroller family so now we're
going to continue on to the
configuration word configuration word is
important part of the microcontroller as
well it determines certain operating
features of the microcontroller it is in
program memory but cannot be accessed in
normal operation okay so you get this
special word in here it's written to
during the programming process so the
only way to control this configuration
word is when you program the chip when
you connect your picot 3 or pick it to
what are we using to your processor and
program it through your mplab software
there's a special menu at the top where
you can control this but this is not and
many times you can add this some code
some lines of code to your software
program your assembler or C and you can
get these bits to be changed as well
they're called assembler directives so
you can add these are similar directives
at the head of your program in order to
get these to be set but these are for
some pretty important bits here we have
code protect bits code protect bits will
prevent someone from stealing your your
software so if you spend a lot of time
designing some software for a pig
microprocessor or if a company does and
then you spend you know months and
months developing it and then you
competitor just simply takes your chip
and then just reads all your software
and they can just steal your code and
whereas you would put spend a lot of
money developing that then they could
just steal it so the code protection bit
prevents them from being able to read
that back it also prevents you from
being able to read that back but you
don't really need to read that back I
guess it's not really that important it
would be important during debug process
but once you've got the program working
the way you want it and in manufacturing
and you're gonna program all these chips
to go to sell them then you would
probably want to enable the code
protection so zero represents that all
the memory is code protected so if these
are all ones and it would there would be
no code protection so you also have bit
three you have a powerup enable bit you
have a watchdog timer in a little bit
many times the watchdog timer needs to
be disabled when you do many things so
this is a common thing that you would
disable would be the watchdog timer and
then everyone needs to know how to set
the oscillator because the chip won't
work if you don't have an oscillator so
if you have any errors universe first
start trying to use a chip I would check
the watchdog timer and the oscillator
the RC oscillator is the one that's
internal to the oscillator and then you
have these high speed options X TLP
low-power option so these last three
here are external oscillator options and
the RC one is the internal oscillator if
your processor has an internal
oscillator not all of them do so you
have to check your device datasheet to
see if you have an an RC oscillator so a
good exercise here would be to go for
the sixteen f-84 a would be to look at
the datasheet and tell me whether or not
this chip has an RC internal oscillator
if it doesn't you have to add an
external oscillator crystal and wire
that up and configure that that's it for
this section the next section is going
to cover section two point five to two
point seven
thank you very much
Ver Más Videos Relacionados
PIC16 Microcontrollers, Unit 3, Ch 2.1-2.2; Block Diagram & Status Register
Registers & Special Purpose Registers in Assembly Language Urdu/Hindi | Lecture # 2 | very Important
Lecture 5 Program structure, syntax and program to print single character in assembly language urdu
Fetch Decode Execute Cycle in more detail
Introduction to Registers | What is Shift Register? Types of Shift Registers
Pin Diagram of 8051 Microcontroller | PIN Configurations of 8051 | 8051 Microcontroller
5.0 / 5 (0 votes)