Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8621
Скачиваний: 0
Data EEPROM Programming |
473 |
;7 6 5 4 3 2 1 0
;| | | | |_|_|_|_____ switch rows (output)
;|_|_|_|_____________ switch columns (input)
;rows must be defined as output and columns as input
movlw |
b’11110000’ |
|
movwf |
TRISB |
|
; TRIS Port-E for output |
||
movlw |
b’00000000’ |
|
movwf |
TRISE |
; TRIS Port-E |
; Enable Port-B pullups for switches in OPTION register
movlw |
b’00001000’ |
movwf |
OPTION_REG |
;Back to bank 0 Bank0
;Initialize serial Port-for 2400 baud, 8 bits, no parity
;1 stop
call |
InitSerial |
|
; Test serial transmission by sending “RDY-” |
||
movlw |
‘R’ |
|
call |
SerialSend |
|
movlw |
‘D’ |
|
call |
SerialSend |
|
movlw |
‘Y’ |
|
call |
SerialSend |
|
movlw |
‘-’ |
|
call |
SerialSend |
|
movlw |
0x20 |
|
call |
SerialSend |
|
; Clear all output lines |
||
movlw |
b’00000000’ |
|
movwf |
PORTD |
|
movwf |
PORTE |
|
; Wait and initialize HD44780 |
||
call |
delay_5 |
; Allow LCD time to initialize |
call |
initLCD |
; Then do forced |
; initialization |
||
call |
delay_5 |
|
; Clear character counter and line counter variables |
||
clrf |
LCDcount |
|
clrf |
LCDline |
|
; Set display address to start of first LCD line |
||
call |
line1 |
|
; Store address of display buffer |
||
movlw |
0x20 |
|
movwf |
bufAdd |
|
; Display “Receiving:” message prompt |
||
call |
blank20 |
; Clear buffer |
movlw |
0x00 |
; Offset in buffer |
474 |
Chapter 15 |
|
call |
storeMS1 ; Store message at offset |
|
call |
display20 |
; Display message |
; Start address of EEPROM |
||
clrf |
EEMemAdd |
|
; Setup for display in second line |
||
call |
line2 |
|
clrf |
LCDline |
|
incf |
LCDline,f; Set scroll control for line 2 |
|
;============================================================
;receive serial data, store, and display ;============================================================ receive:
;Call serial receive procedure
call SerialRcv
;HOB of newData register is set if new data
;received
btfss |
newData,7 |
|
goto |
scanExit |
|
; At this point new data was received. |
||
movwf |
EEByte |
; Save received character |
; Display character on LCD |
||
movf |
EEByte,w ; Recover character |
|
call |
send8 |
; Display in LCD |
call |
LCDscroll |
; Scroll at end of line |
; Store character in EEPROM at location in EEMemAdd |
||
call |
EEWrite ; Local procedure |
|
incf |
EEMemAdd,f |
; Bump to next EEPROM |
; Check for <Enter> key (0x0d) and execute display function
movf |
EEByte,w ; |
Recover last received |
sublw |
0x0d |
|
btfsc |
STATUS,Z ; |
Test if <Enter> key |
goto |
isEnter ; |
Go if <Enter> |
; Not <Enter> key, continue processing |
||
scanExit: |
||
goto |
receive |
; Continue |
;============================
;display EEPROM data ;============================
;This routine receives control when the <Enter> key is
;received.
;Action:
;1. Clear LCD
;2. Output is set to top LCD line
;3. Characters stored in EEPROM are displayed
;until 0x0d code is detected
isEnter:
call clearLCD
; Clear character counter and line counter variables
Data EEPROM Programming |
475 |
clrf LCDcount
clrf LCDline
;Read data from EEPROM memory, starting at address 0
;and display on LCD until 0x0d terminator
call |
line1 |
||
clrf |
EEMemAdd ; |
Start at EEPROM 0 |
|
readOne: |
|||
call |
EERead |
; |
Get character |
; Store character |
|||
movwf |
EEByte |
; |
Save character |
; Test for terminator |
|||
sublw |
0x0d |
||
btfsc |
STATUS,Z ; |
Test if 0x0d |
|
goto |
atEnd |
; Go if 0x0d |
|
;At this point character read is not 0x0d
;Display on LCD
movf |
EEByte,w ; Recover character |
|
; Display character on LCD |
||
call |
send8 |
; Display in LCD |
call |
LCDscroll |
; Scroll at end of line |
incf |
EEMemAdd,f |
; Next EEPROM byte |
goto |
readOne |
|
; End of execution |
||
atEnd: |
||
goto |
atEnd |
|
The Ser2EEP program can be tested with any PC serial communications program set for the program’s protocol parameters. We developed and tested the program using Windows Hyperterminal.
15.1 EEPROM Devices and Interfaces
In addition to onboard EEPROM memory that is available in many PICs, a circuit can contain EEPROM memory in separate integrated circuits. The reason for using separate EEPROM is the need for storing more data, since access to onboard EEPROM memory is usually faster, simpler, and requires less interface elements.
EEPROM devices are furnished in two different interface types: serial and parallel. Devices that use the parallel bus require an 8-bit data bus and an address bus wide enough to cover its entire memory space. Although parallel EEPROMS are faster than serial ones, in PIC and microcontroller technology, parallel devices are usually out of the question.
Serial EEPROMS also come in various flavors. In the PIC environment, the most used ones are I2C (Inter-Integrated Circuit), SPI (Serial Peripheral Interface), and
Microwire which is a subset of SPI. Another interface called 1-Wire, similar to I2C, finds some use in PIC systems.
476 |
Chapter 15 |
Although of different design and having unique architectures, the several types of EEPROM devices share many features. For example, they all operate on a three-phase system that includes an Opcode, an Address, and a Data phase. Although each type of device has a unique instruction set, the basic operations perform similar functions: enable write, enable read, get read status, get write status, read data, and write data. For this reason we have selected a single one of these interfaces: I2C. This interface, probably because of its minimal use of communications lines, seems to be the most popular one in the PIC environment.
15.1.1 The I2C Serial Interface
I2C (or I2C) is a serial computer bus and interface developed by Philips Electronics for use in TV receivers. I2C has found considerable use in embedded systems and is supported by many types of devices; including EEPROMs, thermal sensors, real-time clocks, RF tuners, video decoders, etc. I2C devices are made by Philips, National
Semiconductors, Microchip, and many others. The popularity of I2C is often attributed to its simplicity, low implementation cost, and minimal use of communications resources.
The typical use of I2C is for interfacing devices on a single board or in a closed system. The interface uses a two-wire bus and two signals: SDA (serial data line) and SCL (serial clock line). These signals support serial transmission 8-bits at a time with 7-bit address-space devices. In the I2C protocol the device that initiates a transaction is called the master and the device being addressed is the slave. Normally the master controls the clock signal, but the slave can hold-off the master in the middle of a transaction by pulling the SCL line low. This is called “.” Not all I2C slave devices support this feature.
The presence of a clock signal makes I2C a synchronous protocol. Since the clock signal is part of the transmission, it can vary without disrupting data. For this reason, I2C is used in systems with imprecise clocks, such as the PIC RC oscillator.
Every I2C hardware slave device has a predefined device address, although some part of this address can be defined at the board level. At the start of every transaction, the master sends the device address of the slave it intends to access. The slave device monitors the bus and responds only to commands that include its own address. The number of available user-configurable address bits limits the number of identical devices that coexist on the same bus.
15.1.2 I2C Communications
SDA and SCL I2C signals are open-drain, that is, the master and slave devices can only drive the lines low, or leave them open (high). In operation, a termination resistor pulls the line up to Vcc if no I2C device is pulling it down. It is this mechanism that allows a slave device to suspend communications by holding down the SCL line. Furthermore, I2C lines can only be in one of two states, called “float high” and “drive low.” Here again, it is the pull-up resistors that ensure that the line does not float in an unknown state.
Data EEPROM Programming |
477 |
The pull-up resistors used in I2C hardware vary according to communication speed. Typically, lines at 100 kbps require 4.7K pull-up resistors and lines at 400 kbps 1K resistors.
15.1.3 EEPROM Communications Conditions
The descriptions and examples that follow are limited to EEPROM IC2 device acess. I2C is a bidirectional interface, implemented by an Acknowledge or Ack system. This system allows data to be sent in one direction to one device on the I2C bus. The device indicates that data has been received by issuing an Ack signal. This action from the receiver eliminates any doubts about whether the transmission was received or not.
Several so-called “conditions” serve to explain I2C communications. The conditions refer to the various bus states during transmission, such as start, stop, data, and acknowledge.
The START condition (represented by the letter S) indicates that an R2C device is ready to transfer data on the bus. The device initializes the transmission by pulling the SDA line low. Recall that both lines are high in the normal state. So the S condition is detected by a low SDA and a high SCL.
The STOP condition (represented by the letter P) indicates that a device has finished transferring and is releasing the bus. The P condition is detected when the SDA line is released while the SCL line remains high. Thus, by action of the pull-up resistor, the P conditions places both lines high.
The RESTART condition (represented by the letter R) indicates that a device is ready to transmit more data without releasing the line. This condition is called a Repeated Start (condition Rs) in the technical literature. The typical scenario for an R condition is when a START must be sent, but a STOP has not occurred. The
RESTART condition issues a new START without releasing the line, as is the case when another data item must be sent. In the R condition, the SCL line is momentarily released while the SDA is held high.
The DATA TRANSFER condition (or just DATA condition) represents the transmission of 8 data bits by pulsing the SDA line while the SCL is high. The CLK signal and the SDA signal must be aligned so that the high and low bits on the SCL line coincide with the high state of the CLK line. The fact that the SCL line is meaningful only when the CLK signal is high allows the SCL line to change. A DATA byte can be a control code, an address, or an information element.
The ACK condition (represented by the letter A) is used to acknowledge a data reception. This condition is furnished by the device by bringing the SDS line low during the 9th clock pulse of a transmission sequence. The sequence starts with the S or R condition (one bit), followed by 8 data bits, and the 9th bit on the line requires that the SDA line be brought low, since the SDA line floats high.