LCD interfacing with Microcontrollers tutorial - LCD Commands and Insturctions
►Commands and Instruction set
Only the instruction register (IR) and the data register (DR) of the LCD can be controlled by the MCU. Before starting the internal operation of the LCD, control information is temporarily stored into these registers to allow interfacing with various MCUs, which operate at different speeds, or various peripheral control devices. The internal operation of the LCD is determined by signals sent from the MCU. These signals, which include register selection signal (RS), read/write signal (R/W), and the data bus (DB0 to DB7), make up the LCD instructions (Table 3). There are four categories of instructions that:
Designate LCD functions, such as display format, data length, etc.
Set internal RAM addresses
Perform data transfer with internal RAM
Perform miscellaneous functions
Table 3: Command and Instruction set for LCD type HD44780
Although looking at the table you can make your own commands and test them. Below is a breif list of useful commands which are used frequently while working on the LCD.
No.
Instruction
Hex
Decimal
1
Function Set: 8-bit, 1 Line, 5x7 Dots
0x30
48
2
Function Set: 8-bit, 2 Line, 5x7 Dots 0x38
56
3
Function Set: 4-bit, 1 Line, 5x7 Dots 0x20
32
4
Function Set: 4-bit, 2 Line, 5x7 Dots 0x28
40
5 Entry Mode
0x06
6
6
Display off Cursor off
(clearing display without clearing DDRAM content)
0x08 8
7
Display on Cursor on
0x0E
14
8
Display on Cursor off
0x0C
12
9
Display on Cursor blinking 0x0F
15
10
Shift entire display left
0x18
24
12
Shift entire display right
0x1C
30
13
Move cursor left by one character
0x10 16
14
Move cursor right by one character
0x14
20
15
Clear Display (also clear DDRAM content)
0x01
1
16
Set DDRAM address or coursor position on display 0x80+add*
128+add*
17
Set CGRAM address or set pointer to CGRAM location
0x40+add** 64+add**
Table 4: Frequently used commands and instructions for LCD
* DDRAM address given in LCD basics section see Figure 2,3,4
** CGRAM address from 0x00 to 0x3F, 0x00 to 0x07 for char1 and so on..
The table above will help you while writing programs for LCD. But after you are done testing with the table 4, i recommend you to use table 3 to get more grip on working with LCD and trying your own commands. In the next section of the tutorial we will see the initialization with some of the coding examples in C as well as assembly.
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
Microcontrollers
Saturday, May 30, 2009
LCD interfacing with Microcontrollers tutorial - Basics
LCD interfacing with Microcontrollers tutorial - Basics
►DDRAM - Display Data RAM
Display data RAM (DDRAM) stores display data represented in 8-bit character codes. Its extended capacity is 80 X 8 bits, or 80 characters. The area in display data RAM (DDRAM) that is not used for display can be used as general data RAM. So whatever you send on the DDRAM is actually displayed on the LCD. For LCDs like 1x16, only 16 characters are visible, so whatever you write after 16 chars is written in DDRAM but is not visible to the user.
Figures below will show you the DDRAM addresses of 1 Line, 2 Line and 4 Line LCDs.
Figure 2: DDRAM Address for 1 Line LCD
Figure 3: DDRAM Address for 2 Line LCD
Figure 4: DDRAM Address for 4 Line LCD
►CGROM - Character Generator ROM
Now you might be thinking that when you send an ascii value to DDRAM, how the character is displayed on LCD? so the answer is CGROM. The character generator ROM generates 5 x 8 dot or 5 x 10 dot character patterns from 8-bit character codes (see Figure 5 and Figure 6 for more details). It can generate 208 5 x 8 dot character patterns and 32 5 x 10 dot character patterns. Userdefined character patterns are also available by mask-programmed ROM.
Figure 5: LCD characters code map for 5x8 dots
Figure 6: LCD characters code map for 5x10 dots
As you can see in both the code maps, the character code from 0x00 to 0x07 is occupied by the CGRAM characters or the user defined characters. If user want to display the fourth custom character then the code to display it is 0x03 i.e. when user send 0x03 code to the LCD DDRAM then the fourth user created charater or patteren will be displayed on the LCD.
►CGRAM - Character Generator RAM
As clear from the name, CGRAM area is used to create custom characters in LCD. In the character generator RAM, the user can rewrite character patterns by program. For 5 x 8 dots, eight character patterns can be written, and for 5 x 10 dots, four character patterns can be written. Later in this tutorial i will explain how to use CGRAM area to make custom character and also making animations to give nice effects to your application.
►BF - Busy Flag
Busy Flag is an status indicator flag for LCD. When we send a command or data to the LCD for processing, this flag is set (i.e BF =1) and as soon as the instruction is executed successfully this flag is cleared (BF = 0). This is helpful in producing and exact ammount of delay. for the LCD processing.
To read Busy Flag, the condition RS = 0 and R/W = 1 must be met and The MSB of the LCD data bus (D7) act as busy flag. When BF = 1 means LCD is busy and will not accept next command or data and BF = 0 means LCD is ready for the next command or data to process.
►Instruction Register (IR) and Data Register (DR)
There are two 8-bit registers in HD44780 controller Instruction and Data register. Instruction register corresponds to the register where you send commands to LCD e.g LCD shift command, LCD clear, LCD address etc. and Data register is used for storing data which is to be displayed on LCD. when send the enable signal of the LCD is asserted, the data on the pins is latched in to the data register and data is then moved automatically to the DDRAM and hence is displayed on the LCD.
Data Register is not only used for sending data to DDRAM but also for CGRAM, the address where you want to send the data, is decided by the instruction you send to LCD. We will discuss more on LCD instuction set further in this tutorial.
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
►DDRAM - Display Data RAM
Display data RAM (DDRAM) stores display data represented in 8-bit character codes. Its extended capacity is 80 X 8 bits, or 80 characters. The area in display data RAM (DDRAM) that is not used for display can be used as general data RAM. So whatever you send on the DDRAM is actually displayed on the LCD. For LCDs like 1x16, only 16 characters are visible, so whatever you write after 16 chars is written in DDRAM but is not visible to the user.
Figures below will show you the DDRAM addresses of 1 Line, 2 Line and 4 Line LCDs.
Figure 2: DDRAM Address for 1 Line LCD
Figure 3: DDRAM Address for 2 Line LCD
Figure 4: DDRAM Address for 4 Line LCD
►CGROM - Character Generator ROM
Now you might be thinking that when you send an ascii value to DDRAM, how the character is displayed on LCD? so the answer is CGROM. The character generator ROM generates 5 x 8 dot or 5 x 10 dot character patterns from 8-bit character codes (see Figure 5 and Figure 6 for more details). It can generate 208 5 x 8 dot character patterns and 32 5 x 10 dot character patterns. Userdefined character patterns are also available by mask-programmed ROM.
Figure 5: LCD characters code map for 5x8 dots
Figure 6: LCD characters code map for 5x10 dots
As you can see in both the code maps, the character code from 0x00 to 0x07 is occupied by the CGRAM characters or the user defined characters. If user want to display the fourth custom character then the code to display it is 0x03 i.e. when user send 0x03 code to the LCD DDRAM then the fourth user created charater or patteren will be displayed on the LCD.
►CGRAM - Character Generator RAM
As clear from the name, CGRAM area is used to create custom characters in LCD. In the character generator RAM, the user can rewrite character patterns by program. For 5 x 8 dots, eight character patterns can be written, and for 5 x 10 dots, four character patterns can be written. Later in this tutorial i will explain how to use CGRAM area to make custom character and also making animations to give nice effects to your application.
►BF - Busy Flag
Busy Flag is an status indicator flag for LCD. When we send a command or data to the LCD for processing, this flag is set (i.e BF =1) and as soon as the instruction is executed successfully this flag is cleared (BF = 0). This is helpful in producing and exact ammount of delay. for the LCD processing.
To read Busy Flag, the condition RS = 0 and R/W = 1 must be met and The MSB of the LCD data bus (D7) act as busy flag. When BF = 1 means LCD is busy and will not accept next command or data and BF = 0 means LCD is ready for the next command or data to process.
►Instruction Register (IR) and Data Register (DR)
There are two 8-bit registers in HD44780 controller Instruction and Data register. Instruction register corresponds to the register where you send commands to LCD e.g LCD shift command, LCD clear, LCD address etc. and Data register is used for storing data which is to be displayed on LCD. when send the enable signal of the LCD is asserted, the data on the pins is latched in to the data register and data is then moved automatically to the DDRAM and hence is displayed on the LCD.
Data Register is not only used for sending data to DDRAM but also for CGRAM, the address where you want to send the data, is decided by the instruction you send to LCD. We will discuss more on LCD instuction set further in this tutorial.
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
LCD interfacing with Microcontrollers tutorial
LCD interfacing with Microcontrollers tutorial
►Introduction
The most commonly used Character based LCDs are based on Hitachi's HD44780 controller or other which are compatible with HD44580. In this tutorial, we will discuss about character based LCDs, their interfacing with various microcontrollers, various interfaces (8-bit/4-bit), programming, special stuff and tricks you can do with these simple looking LCDs which can give a new look to your application.
For Specs and technical information HD44780 controller Click Here
►Pin Description
The most commonly used LCDs found in the market today are 1 Line, 2 Line or 4 Line LCDs which have only 1 controller and support at most of 80 charachers, whereas LCDs supporting more than 80 characters make use of 2 HD44780 controllers.
Most LCDs with 1 controller has 14 Pins and LCDs with 2 controller has 16 Pins (two pins are extra in both for back-light LED connections). Pin description is shown in the table below.
Figure 1: Character LCD type HD44780 Pin diagram
Pin No.
Name
Description
Pin no. 1 VSS
Power supply (GND)
Pin no. 2 VCC Power supply (+5V)
Pin no. 3 VEE
Contrast adjust
Pin no. 4 RS
0 = Instruction input
1 = Data input
Pin no. 5 R/W
0 = Write to LCD module
1 = Read from LCD module
Pin no. 6 EN
Enable signal
Pin no. 7 D0 Data bus line 0 (LSB)
Pin no. 8 D1 Data bus line 1
Pin no. 9 D2 Data bus line 2
Pin no. 10 D3
Data bus line 3
Pin no. 11 D4
Data bus line 4
Pin no. 12 D5
Data bus line 5
Pin no. 13 D6
Data bus line 6
Pin no. 14 D7
Data bus line 7 (MSB)
Table 1: Character LCD pins with 1 Controller
Pin No.
Name
Description
Pin no. 1
D7 Data bus line 7 (MSB)
Pin no. 2
D6 Data bus line 6
Pin no. 3
D5
Data bus line 5
Pin no. 4
D4
Data bus line 4
Pin no. 5
D3
Data bus line 3
Pin no. 6
D2
Data bus line 2
Pin no. 7
D1
Data bus line 1
Pin no. 8
D0
Data bus line 0 (LSB)
Pin no. 9
EN1 Enable signal for row 0 and 1 (1stcontroller)
Pin no. 10
R/W 0 = Write to LCD module
1 = Read from LCD module
Pin no. 11
RS 0 = Instruction input
1 = Data input
Pin no. 12
VEE Contrast adjust
Pin no. 13
VSS Power supply (GND)
Pin no. 14
VCC Power supply (+5V)
Pin no. 15
EN2 Enable signal for row 2 and 3 (2ndcontroller)
Pin no. 16
NC Not Connected
Table 2: Character LCD pins with 2 Controller
Usually these days you will find single controller LCD modules are used more in the market. So in the tutorial we will discuss more about the single controller LCD, the operation and everything else is same for the double controller too. Lets take a look at the basic information which is there in every LCD.
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
►Introduction
The most commonly used Character based LCDs are based on Hitachi's HD44780 controller or other which are compatible with HD44580. In this tutorial, we will discuss about character based LCDs, their interfacing with various microcontrollers, various interfaces (8-bit/4-bit), programming, special stuff and tricks you can do with these simple looking LCDs which can give a new look to your application.
For Specs and technical information HD44780 controller Click Here
►Pin Description
The most commonly used LCDs found in the market today are 1 Line, 2 Line or 4 Line LCDs which have only 1 controller and support at most of 80 charachers, whereas LCDs supporting more than 80 characters make use of 2 HD44780 controllers.
Most LCDs with 1 controller has 14 Pins and LCDs with 2 controller has 16 Pins (two pins are extra in both for back-light LED connections). Pin description is shown in the table below.
Figure 1: Character LCD type HD44780 Pin diagram
Pin No.
Name
Description
Pin no. 1 VSS
Power supply (GND)
Pin no. 2 VCC Power supply (+5V)
Pin no. 3 VEE
Contrast adjust
Pin no. 4 RS
0 = Instruction input
1 = Data input
Pin no. 5 R/W
0 = Write to LCD module
1 = Read from LCD module
Pin no. 6 EN
Enable signal
Pin no. 7 D0 Data bus line 0 (LSB)
Pin no. 8 D1 Data bus line 1
Pin no. 9 D2 Data bus line 2
Pin no. 10 D3
Data bus line 3
Pin no. 11 D4
Data bus line 4
Pin no. 12 D5
Data bus line 5
Pin no. 13 D6
Data bus line 6
Pin no. 14 D7
Data bus line 7 (MSB)
Table 1: Character LCD pins with 1 Controller
Pin No.
Name
Description
Pin no. 1
D7 Data bus line 7 (MSB)
Pin no. 2
D6 Data bus line 6
Pin no. 3
D5
Data bus line 5
Pin no. 4
D4
Data bus line 4
Pin no. 5
D3
Data bus line 3
Pin no. 6
D2
Data bus line 2
Pin no. 7
D1
Data bus line 1
Pin no. 8
D0
Data bus line 0 (LSB)
Pin no. 9
EN1 Enable signal for row 0 and 1 (1stcontroller)
Pin no. 10
R/W 0 = Write to LCD module
1 = Read from LCD module
Pin no. 11
RS 0 = Instruction input
1 = Data input
Pin no. 12
VEE Contrast adjust
Pin no. 13
VSS Power supply (GND)
Pin no. 14
VCC Power supply (+5V)
Pin no. 15
EN2 Enable signal for row 2 and 3 (2ndcontroller)
Pin no. 16
NC Not Connected
Table 2: Character LCD pins with 2 Controller
Usually these days you will find single controller LCD modules are used more in the market. So in the tutorial we will discuss more about the single controller LCD, the operation and everything else is same for the double controller too. Lets take a look at the basic information which is there in every LCD.
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
Seven-Segment Display Example
Seven-Segment Display Example
►Seven-Segment Display Example
The segments in a 7-segment display are arranged to form a single digit from 0 to F. We can display a multi-digit number by connecting additional displays. Even though LCD displays are more comfortable to work with, 7-segment displays are still standard in the industry. This is due to their temperature robustness, visibility and wide viewing angle. Segments are marked with non-capital letters: a, b, c, d, e, f, g and dp, where dp is the decimal point. The 8 LEDs inside each display can be arranged with a common cathode or common anode. With a common cathode display, the common cathode must be connected to the 0V rail and the LEDs are turned on with a logic one. Common anode displays must have the common anode connected to the +5V rail. The segments are turned on with a logic zero. The size of a display is measured in millimeters, the height of the digit itself (not the housing, but the digit!). Displays are available with a digit height of 7,10, 13.5, 20, or 25 millimeters. They come in different colors, including: red, orange, and green.
The simplest way to drive a display is via a display driver. These are available for up to 4 displays. Alternatively displays can be driven by a microcontroller and if more than one display is required, the method of driving them is called "multiplexing."
The main difference between the two methods is the number of "drive lines." A special driver may need only a single "clock" line and the driver chip will access all the segments and increment the display. If a single display is to be driven from a microcontroller, 7 lines will be needed plus one for the decimal point. For each additional display, only one extra line is needed. To produce a 4, 5 or 6 digit display, all the 7-segment displays are connected in parallel. The common line (the common-cathode line) is taken out separately and this line is taken low for a short period of time to turn on the display. Each display is turned on at a rate above 100 times per second, and it will appear that all the displays are turned on at the same time. As each display is turned on, the appropriate information must be delivered to it so that it will give the correct reading. Up to 6 displays can be accessed like this without the brightness of each display being affected. Each display is turned on very hard for one-sixth the time and the POV (persistence of vision) of our eye thinks the display is turned on the whole time. Therefore, the program has to ensure the proper timing, else the unpleasant blinking of display will occur.
Connecting a microcontroller to 7- segment displays in multiplex mode
Program "7seg.asm" displays decimal value of a number stored in variable D.
CODE:
Example:
movlw .21
movlw D ; number 21 will be printed on 7seg display
Displaying digits is carried out in multiplex mode which means that the microcontroller alternately prints ones digit and tens digit. TMR0 interrupt serves for generating a time period, so that the program enters the interrupt routine every 5ms and performs multiplexing. In the interrupt routine, first step is deciding which segment should be turned on. In case that the tens digit was previously on, it should be turned off, set the mask for printing the ones digit on 7seg display which lasts 5ms, i.e. until the next interrupt.
For extracting the ones digit and the tens digit, macro digbyte is used. It stores the hundreds digit, the tens digit, and the ones digit into variables Dig1, Dig2, and Dig3. In our case, upon macro execution, Dig1 will equal 0, Dig2 will equal 2, and Dig3 will equal 1.
The following example shows the use of the macro in a program. Program prints a specified 2-digit number on a 7seg display in multiplex mode.
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
►Seven-Segment Display Example
The segments in a 7-segment display are arranged to form a single digit from 0 to F. We can display a multi-digit number by connecting additional displays. Even though LCD displays are more comfortable to work with, 7-segment displays are still standard in the industry. This is due to their temperature robustness, visibility and wide viewing angle. Segments are marked with non-capital letters: a, b, c, d, e, f, g and dp, where dp is the decimal point. The 8 LEDs inside each display can be arranged with a common cathode or common anode. With a common cathode display, the common cathode must be connected to the 0V rail and the LEDs are turned on with a logic one. Common anode displays must have the common anode connected to the +5V rail. The segments are turned on with a logic zero. The size of a display is measured in millimeters, the height of the digit itself (not the housing, but the digit!). Displays are available with a digit height of 7,10, 13.5, 20, or 25 millimeters. They come in different colors, including: red, orange, and green.
The simplest way to drive a display is via a display driver. These are available for up to 4 displays. Alternatively displays can be driven by a microcontroller and if more than one display is required, the method of driving them is called "multiplexing."
The main difference between the two methods is the number of "drive lines." A special driver may need only a single "clock" line and the driver chip will access all the segments and increment the display. If a single display is to be driven from a microcontroller, 7 lines will be needed plus one for the decimal point. For each additional display, only one extra line is needed. To produce a 4, 5 or 6 digit display, all the 7-segment displays are connected in parallel. The common line (the common-cathode line) is taken out separately and this line is taken low for a short period of time to turn on the display. Each display is turned on at a rate above 100 times per second, and it will appear that all the displays are turned on at the same time. As each display is turned on, the appropriate information must be delivered to it so that it will give the correct reading. Up to 6 displays can be accessed like this without the brightness of each display being affected. Each display is turned on very hard for one-sixth the time and the POV (persistence of vision) of our eye thinks the display is turned on the whole time. Therefore, the program has to ensure the proper timing, else the unpleasant blinking of display will occur.
Connecting a microcontroller to 7- segment displays in multiplex mode
Program "7seg.asm" displays decimal value of a number stored in variable D.
CODE:
Example:
movlw .21
movlw D ; number 21 will be printed on 7seg display
Displaying digits is carried out in multiplex mode which means that the microcontroller alternately prints ones digit and tens digit. TMR0 interrupt serves for generating a time period, so that the program enters the interrupt routine every 5ms and performs multiplexing. In the interrupt routine, first step is deciding which segment should be turned on. In case that the tens digit was previously on, it should be turned off, set the mask for printing the ones digit on 7seg display which lasts 5ms, i.e. until the next interrupt.
For extracting the ones digit and the tens digit, macro digbyte is used. It stores the hundreds digit, the tens digit, and the ones digit into variables Dig1, Dig2, and Dig3. In our case, upon macro execution, Dig1 will equal 0, Dig2 will equal 2, and Dig3 will equal 1.
The following example shows the use of the macro in a program. Program prints a specified 2-digit number on a 7seg display in multiplex mode.
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
Addressing Modes of PIC Microcontroller
Addressing Modes of PIC Microcontroller
►Addressing modes
RAM memory locations can be accessed directly or indirectly.
1. Direct Addressing
Direct Addressing is done through a 9-bit address. This address is obtained by connecting 7th bit of direct address of an instruction with two bits (RP1, RP0) from STATUS register as is shown on the following picture. Any access to SFR registers is an example of direct addressing.
CODE:
Bsf STATUS, RP0 ;Bankl
movlw 0xFF ;w=0xFF
movwf TRISA ;address of TRISA register is taken from
;instruction movwf
2. Indirect Addressing
Indirect unlike direct addressing does not take an address from an instruction but derives it from IRP bit of STATUS and FSR registers. Addressed location is accessed via INDF register which in fact holds the address indicated by a FSR. In other words, any instruction which uses INDF as its register in reality accesses data indicated by a FSR register. Let's say, for instance, that one general purpose register (GPR) at address 0Fh contains a value of 20. By writing a value of 0Fh in FSR register we will get a register indicator at address 0Fh, and by reading from INDF register, we will get a value of 20, which means that we have read from the first register its value without accessing it directly (but via FSR and INDF). It appears that this type of addressing does not have any advantages over direct addressing, but certain needs do exist during programming which can be solved smoothly only through indirect addressing.
Such examples include sending a set of data via serial communication, working with buffers and indicators (which will be discussed further in a chapter with examples), or erasing a part of RAM memory (16 locations) as in the following instance.
Reading data from INDF register when the contents of FSR register is equal to zero returns the value of zero, and writing to it results in NOP operation (no operation).
Reading from EEPROM Memory
Setting the RD bit initializes transfer of data from address found in EEADR register to EEDATA register. As in reading data we don't need so much time as in writing, data taken over from EEDATA register can already be used further in the next instruction.
Sample of the part of a program which reads data in EEPROM, could look something like the following:
After the last program instruction, contents from an EEPROM address zero can be found in working register w.
Writing to EEPROM Memory
In order to write data to EEPROM location, programmer must first write address to EEADR register and data to EEDATA register. Only then is it useful to set WR bit which sets the whole action in motion. WR bit will be reset, and EEIF bit set following a writing what may be used in processing interrupts. Values 55h and AAh are the first and the second key whose disallow for accidental writing to EEPROM to occur. These two values are written to EECON2 which serves only that purpose, to receive these two values and thus prevent any accidental writing to EEPROM memory. Program lines marked as 1, 2, 3, and 4 must be executed in that order in even time intervals. Therefore, it is very important to turn off interrupts which could change the timing needed for executing instructions. After writing, interrupts can be enabled again.
Example of the part of a program which writes data 0xEE to first location in EEPROM memory could look something like the following:
NOTE: It is recommended that WREN be turned off the whole time except when writing data to EEPROM, so that possibility of accidental writing would be minimal. All writing to EEPROM will automatically clear a location prior to writing a new.
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
►Addressing modes
RAM memory locations can be accessed directly or indirectly.
1. Direct Addressing
Direct Addressing is done through a 9-bit address. This address is obtained by connecting 7th bit of direct address of an instruction with two bits (RP1, RP0) from STATUS register as is shown on the following picture. Any access to SFR registers is an example of direct addressing.
CODE:
Bsf STATUS, RP0 ;Bankl
movlw 0xFF ;w=0xFF
movwf TRISA ;address of TRISA register is taken from
;instruction movwf
2. Indirect Addressing
Indirect unlike direct addressing does not take an address from an instruction but derives it from IRP bit of STATUS and FSR registers. Addressed location is accessed via INDF register which in fact holds the address indicated by a FSR. In other words, any instruction which uses INDF as its register in reality accesses data indicated by a FSR register. Let's say, for instance, that one general purpose register (GPR) at address 0Fh contains a value of 20. By writing a value of 0Fh in FSR register we will get a register indicator at address 0Fh, and by reading from INDF register, we will get a value of 20, which means that we have read from the first register its value without accessing it directly (but via FSR and INDF). It appears that this type of addressing does not have any advantages over direct addressing, but certain needs do exist during programming which can be solved smoothly only through indirect addressing.
Such examples include sending a set of data via serial communication, working with buffers and indicators (which will be discussed further in a chapter with examples), or erasing a part of RAM memory (16 locations) as in the following instance.
Reading data from INDF register when the contents of FSR register is equal to zero returns the value of zero, and writing to it results in NOP operation (no operation).
Reading from EEPROM Memory
Setting the RD bit initializes transfer of data from address found in EEADR register to EEDATA register. As in reading data we don't need so much time as in writing, data taken over from EEDATA register can already be used further in the next instruction.
Sample of the part of a program which reads data in EEPROM, could look something like the following:
After the last program instruction, contents from an EEPROM address zero can be found in working register w.
Writing to EEPROM Memory
In order to write data to EEPROM location, programmer must first write address to EEADR register and data to EEDATA register. Only then is it useful to set WR bit which sets the whole action in motion. WR bit will be reset, and EEIF bit set following a writing what may be used in processing interrupts. Values 55h and AAh are the first and the second key whose disallow for accidental writing to EEPROM to occur. These two values are written to EECON2 which serves only that purpose, to receive these two values and thus prevent any accidental writing to EEPROM memory. Program lines marked as 1, 2, 3, and 4 must be executed in that order in even time intervals. Therefore, it is very important to turn off interrupts which could change the timing needed for executing instructions. After writing, interrupts can be enabled again.
Example of the part of a program which writes data 0xEE to first location in EEPROM memory could look something like the following:
NOTE: It is recommended that WREN be turned off the whole time except when writing data to EEPROM, so that possibility of accidental writing would be minimal. All writing to EEPROM will automatically clear a location prior to writing a new.
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
Port Programming of PIC Microcontroller
Port Programming of PIC Microcontroller
►Ports
All port pins can be designated as input or output, according to the needs of a device that's being developed. In order to define a pin as input or output pin, the right combination of zeros and ones must be written in TRIS register. If the appropriate bit of TRIS register contains logical "1", then that pin is an input pin, and if the opposite is true, it's an output pin. Every port has its proper TRIS register. Thus, port A has TRISA, and port B has TRISB. Pin direction can be changed during the course of work which is particularly fitting for one-line communication where data flow constantly changes direction. PORTA and PORTB state registers are located in bank 0, while TRISA and TRISB pin direction registers are located in bank 1.
1. PORTB and TRISB
PORTB has adjoined 8 pins. The appropriate register for data direction is TRISB. Setting a bit in TRISB register defines the corresponding port pin as input, and resetting a bit in TRISB register defines the corresponding port pin as output.
Each PORTB pin has a weak internal pull-up resistor (resistor which defines a line to logic one) which can be activated by resetting the seventh bit RBPU in OPTION register. These 'pull-up' resistors are automatically being turned off when port pin is configured as an output. When a microcontroller is started, pull-ups are disabled.
Four pins PORTB, RB7:RB4 can cause an interrupt which occurs when their status changes from logical one into logical zero and opposite. Only pins configured as input can cause this interrupt to occur (if any RB7:RB4 pin is configured as an output, an interrupt won't be generated at the change of status.) This interrupt option along with internal pull-up resistors makes it easier to solve common problems we find in practice like for instance that of matrix keyboard. If rows on the keyboard are connected to these pins, each push on a key will then cause an interrupt. A microcontroller will determine which key is at hand while processing an interrupt It is not recommended to refer to port B at the same time that interrupt is being processed.
CODE:
bsf STATUS, RP0 ;Bank1
movlw 0x0F ;Defining input and output pins
movlw TRISB ;Writing to TRISB register
bcf STATUS, RP0 ;Bank0
bsf PORTB, 4 ;PORTB <7:4>=0
bsf PORTB, 4
bsf PORTB, 5
bsf PORTB, 6
bsf PORTB, 7
The above example shows how pins 0, 1, 2, and 3 are designated input, and pins 4, 5, 6, and 7 for output, after which PORTB output pins are set to one.
2. PORTA and TRISA
PORTA has 5 adjoining pins. The corresponding register for data direction is TRISA at address 85h. Like with port B, setting a bit in TRISA register defines also the corresponding port pin as input, and clearing a bit in TRISA register defines the corresponding port pin as output.
It is important to note that PORTA pin RA4 can be input only. On that pin is also situated an external input for timer TMR0. Whether RA4 will be a standard input or an input for a counter depends on T0CS bit (TMR0 Clock Source Select bit). This pin enables the timer TMR0 to increment either from internal oscillator or via external impulses on RA4/T0CKI pin.
NOTE: RA4 pin can be designated output, but in that case it has to be externally connected to PULL-UP resistor.
Configuring port A:
CODE:
bsf STATUS, RP0 ;Bank1
movlw b'11111100' ;Defining input and output pins
movwf TRISA ;Writing to TRISA register
bcf STATUS, RP0 ;Bank0
Example shows how pins 0, 1, 2, 3, and 4 are designated input, and pins 5, 6, and 7 output. After this, it is possible to read the pins RA2, RA3, RA4, and to set logical zero or one to pins RA0 and RA1.
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
►Ports
All port pins can be designated as input or output, according to the needs of a device that's being developed. In order to define a pin as input or output pin, the right combination of zeros and ones must be written in TRIS register. If the appropriate bit of TRIS register contains logical "1", then that pin is an input pin, and if the opposite is true, it's an output pin. Every port has its proper TRIS register. Thus, port A has TRISA, and port B has TRISB. Pin direction can be changed during the course of work which is particularly fitting for one-line communication where data flow constantly changes direction. PORTA and PORTB state registers are located in bank 0, while TRISA and TRISB pin direction registers are located in bank 1.
1. PORTB and TRISB
PORTB has adjoined 8 pins. The appropriate register for data direction is TRISB. Setting a bit in TRISB register defines the corresponding port pin as input, and resetting a bit in TRISB register defines the corresponding port pin as output.
Each PORTB pin has a weak internal pull-up resistor (resistor which defines a line to logic one) which can be activated by resetting the seventh bit RBPU in OPTION register. These 'pull-up' resistors are automatically being turned off when port pin is configured as an output. When a microcontroller is started, pull-ups are disabled.
Four pins PORTB, RB7:RB4 can cause an interrupt which occurs when their status changes from logical one into logical zero and opposite. Only pins configured as input can cause this interrupt to occur (if any RB7:RB4 pin is configured as an output, an interrupt won't be generated at the change of status.) This interrupt option along with internal pull-up resistors makes it easier to solve common problems we find in practice like for instance that of matrix keyboard. If rows on the keyboard are connected to these pins, each push on a key will then cause an interrupt. A microcontroller will determine which key is at hand while processing an interrupt It is not recommended to refer to port B at the same time that interrupt is being processed.
CODE:
bsf STATUS, RP0 ;Bank1
movlw 0x0F ;Defining input and output pins
movlw TRISB ;Writing to TRISB register
bcf STATUS, RP0 ;Bank0
bsf PORTB, 4 ;PORTB <7:4>=0
bsf PORTB, 4
bsf PORTB, 5
bsf PORTB, 6
bsf PORTB, 7
The above example shows how pins 0, 1, 2, and 3 are designated input, and pins 4, 5, 6, and 7 for output, after which PORTB output pins are set to one.
2. PORTA and TRISA
PORTA has 5 adjoining pins. The corresponding register for data direction is TRISA at address 85h. Like with port B, setting a bit in TRISA register defines also the corresponding port pin as input, and clearing a bit in TRISA register defines the corresponding port pin as output.
It is important to note that PORTA pin RA4 can be input only. On that pin is also situated an external input for timer TMR0. Whether RA4 will be a standard input or an input for a counter depends on T0CS bit (TMR0 Clock Source Select bit). This pin enables the timer TMR0 to increment either from internal oscillator or via external impulses on RA4/T0CKI pin.
NOTE: RA4 pin can be designated output, but in that case it has to be externally connected to PULL-UP resistor.
Configuring port A:
CODE:
bsf STATUS, RP0 ;Bank1
movlw b'11111100' ;Defining input and output pins
movwf TRISA ;Writing to TRISA register
bcf STATUS, RP0 ;Bank0
Example shows how pins 0, 1, 2, 3, and 4 are designated input, and pins 5, 6, and 7 output. After this, it is possible to read the pins RA2, RA3, RA4, and to set logical zero or one to pins RA0 and RA1.
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
PIC Central Processing Unit and Registers
PIC Central Processing Unit and Registers
►Central Processing Unit
Central processing unit (CPU) is the brain of a microcontroller. That part is responsible for finding and fetching the right instruction which needs to be executed, for decoding that instruction, and finally for its execution.
Central processing unit connects all parts of the microcontroller into one whole. Surely, its most important function is to decode program instructions. When programmer writes a program, instructions have a clear form like MOVLW 0x20. However, in order for a microcontroller to understand that, this 'letter' form of an instruction must be translated into a series of zeros and ones which is called an 'opcode'. This transition from a letter to binary form is done by translators such as assembler translator (also known as an assembler). Instruction thus fetched from program memory must be decoded by a central processing unit. We can then select from the table of all the instructions a set of actions which execute a assigned task defined by instruction. As instructions may within themselves contain assignments which require different transfers of data from one memory into another, from memory onto ports, or some other calculations, CPU must be connected with all parts of the microcontroller. This is made possible through a data bus and an address bus.
Arithmetic logic unit is responsible for performing operations of adding, subtracting, moving (left or right within a register) and logic operations. Moving data inside a register is also known as 'shifting'. PIC16F84 contains an 8-bit arithmetic logic unit and 8-bit work registers.
In instructions with two operands, ordinarily one operand is in work register (W register), and the other is one of the registers or a constant. By operand we mean the contents on which some operation is being done, and a register is any one of the GPR or SFR registers. GPR is an abbreviation for 'General Purposes Registers', and SFR for 'Special Function Registers'. In instructions with one operand, an operand is either W register or one of the registers. As an addition in doing operations in arithmetic and logic, ALU controls status bits (bits found in STATUS register). Execution of some instructions affects status bits, which depends on the result itself. Depending on which instruction is being executed, ALU can affect values of Carry (C), Digit Carry (DC), and Zero (Z) bits in STATUS register.
►STATUS Register
bit 7 IRP (Register Bank Select bit)
Bit whose role is to be an eighth bit for purposes of indirect addressing the internal RAM.
1 = bank 2 and 3
0 = bank 0 and 1 (from 00h to FFh)
bits 6:5 RP1:RP0 (Register Bank Select bits)
These two bits are upper part of the address for direct addressing. As instructions which address the memory directly have only seven bits, they need one more bit in order to address all 256 bytes which is how many bytes PIC16F84 has. RP1 bit is not used, but is left for some future expansions of this microcontroller.
01 = first bank
00 = zero bank
bit 4 TO Time-out ; Watchdog overflow.
Bit is set after turning on the supply and execution of CLRWDT and SLEEP instructions. Bit is reset when watchdog gets to the end signaling that overflow took place.
1 = overflow did not occur
0 = overflow did occur
bit 3 PD (Power-down bit)
This bit is set whenever power supply is brought to a microcontroller : as it starts running, after each regular reset and after execution of instruction CLRWDT. Instruction SLEEP resets it when microcontroller falls into low consumption mode. Its repeated setting is possible via reset or by turning the supply off/on . Setting can be triggered also by a signal on RB0/INT pin, change on RB port, upon writing to internal DATA EEPROM, and by a Watchdog.
1 = after supply has been turned on
0 = executing SLEEP instruction
bit 2 Z (Zero bit) Indication of a zero result
This bit is set when the result of an executed arithmetic or logic operation is zero.
1 = result equals zero
0 = result does not equal zero
bit 1 DC (Digit Carry) DC Transfer
Bit affected by operations of addition, subtraction. Unlike C bit, this bit represents transfer from the fourth resulting place. It is set in case of subtracting smaller from greater number and is reset in the other case.
1 = transfer occurred on the fourth bit according to the order of the result
0 = transfer did not occur
DC bit is affected by ADDWF, ADDLW, SUBLW, SUBWF instructions.
bit 0 C (Carry) Transfer
Bit that is affected by operations of addition, subtraction and shifting.
1 = transfer occurred from the highest resulting bit
0 = transfer did not occur
C bit is affected by ADDWF, ADDLW, SUBLW, SUBWF instructions.
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
►Central Processing Unit
Central processing unit (CPU) is the brain of a microcontroller. That part is responsible for finding and fetching the right instruction which needs to be executed, for decoding that instruction, and finally for its execution.
Central processing unit connects all parts of the microcontroller into one whole. Surely, its most important function is to decode program instructions. When programmer writes a program, instructions have a clear form like MOVLW 0x20. However, in order for a microcontroller to understand that, this 'letter' form of an instruction must be translated into a series of zeros and ones which is called an 'opcode'. This transition from a letter to binary form is done by translators such as assembler translator (also known as an assembler). Instruction thus fetched from program memory must be decoded by a central processing unit. We can then select from the table of all the instructions a set of actions which execute a assigned task defined by instruction. As instructions may within themselves contain assignments which require different transfers of data from one memory into another, from memory onto ports, or some other calculations, CPU must be connected with all parts of the microcontroller. This is made possible through a data bus and an address bus.
Arithmetic logic unit is responsible for performing operations of adding, subtracting, moving (left or right within a register) and logic operations. Moving data inside a register is also known as 'shifting'. PIC16F84 contains an 8-bit arithmetic logic unit and 8-bit work registers.
In instructions with two operands, ordinarily one operand is in work register (W register), and the other is one of the registers or a constant. By operand we mean the contents on which some operation is being done, and a register is any one of the GPR or SFR registers. GPR is an abbreviation for 'General Purposes Registers', and SFR for 'Special Function Registers'. In instructions with one operand, an operand is either W register or one of the registers. As an addition in doing operations in arithmetic and logic, ALU controls status bits (bits found in STATUS register). Execution of some instructions affects status bits, which depends on the result itself. Depending on which instruction is being executed, ALU can affect values of Carry (C), Digit Carry (DC), and Zero (Z) bits in STATUS register.
►STATUS Register
bit 7 IRP (Register Bank Select bit)
Bit whose role is to be an eighth bit for purposes of indirect addressing the internal RAM.
1 = bank 2 and 3
0 = bank 0 and 1 (from 00h to FFh)
bits 6:5 RP1:RP0 (Register Bank Select bits)
These two bits are upper part of the address for direct addressing. As instructions which address the memory directly have only seven bits, they need one more bit in order to address all 256 bytes which is how many bytes PIC16F84 has. RP1 bit is not used, but is left for some future expansions of this microcontroller.
01 = first bank
00 = zero bank
bit 4 TO Time-out ; Watchdog overflow.
Bit is set after turning on the supply and execution of CLRWDT and SLEEP instructions. Bit is reset when watchdog gets to the end signaling that overflow took place.
1 = overflow did not occur
0 = overflow did occur
bit 3 PD (Power-down bit)
This bit is set whenever power supply is brought to a microcontroller : as it starts running, after each regular reset and after execution of instruction CLRWDT. Instruction SLEEP resets it when microcontroller falls into low consumption mode. Its repeated setting is possible via reset or by turning the supply off/on . Setting can be triggered also by a signal on RB0/INT pin, change on RB port, upon writing to internal DATA EEPROM, and by a Watchdog.
1 = after supply has been turned on
0 = executing SLEEP instruction
bit 2 Z (Zero bit) Indication of a zero result
This bit is set when the result of an executed arithmetic or logic operation is zero.
1 = result equals zero
0 = result does not equal zero
bit 1 DC (Digit Carry) DC Transfer
Bit affected by operations of addition, subtraction. Unlike C bit, this bit represents transfer from the fourth resulting place. It is set in case of subtracting smaller from greater number and is reset in the other case.
1 = transfer occurred on the fourth bit according to the order of the result
0 = transfer did not occur
DC bit is affected by ADDWF, ADDLW, SUBLW, SUBWF instructions.
bit 0 C (Carry) Transfer
Bit that is affected by operations of addition, subtraction and shifting.
1 = transfer occurred from the highest resulting bit
0 = transfer did not occur
C bit is affected by ADDWF, ADDLW, SUBLW, SUBWF instructions.
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
Subscribe to:
Comments (Atom)