1. Trang chủ >
  2. Giáo Dục - Đào Tạo >
  3. Cao đẳng - Đại học >

E.8 Experiment 7: LED/Switch IO and Introduction to Asynchronous Serial IO (Chapters 8, 9)

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (9.59 MB, 687 trang )


Appendix E: Suggested Laboratory Exercises



579



FIGURE E.5 LED/switch IO assignment.



4. Demo to the TA a spreadsheet that calculates the value to be written to the

SPBRG register given an oscillator frequency value, a target baud rate, and

either high-speed or low-speed baud rate mode. The PIC18F242 datasheet

section titled “USART Baud Rate Generator” should prove helpful.



580



Microprocessors



Lab Activity

1. Program your PIC18F242 with your LED/Switch IO solution and verify its

operation. Use a software delay (i.e., DelayMs(30)) after each test of a switch

input to protect against switch bounce.

2. In the echo.c program from the previous lab, the getch() subroutine waits

for a character to be ready in the TXREG. Modify this subroutine to detect

a framing error by checking the FERR bit in the USART status register

(RCSTA register). Turn on an LED when an error is detected. The LED

should be off after reset. Do the same for the overrun error (OERR) status

bit (this requires a second LED). Use any port bits you wish for the LED

outputs.

3. Change the baud rate from 19200 baud in HyperTerminal and verify that

a framing error condition is produced. Verify that you are checking the

correct bit in the RCSTA register. Framing errors occur when the stop bit

is detected as clear, which is most likely to happen when the actual baud

rate is lower than the expected baud rate.

4. Modify the echo.c program to prompt the user for a choice that forces

USART overrun by entering a software delay loop of five seconds or longer

in which the USART is not read. During this time, type in enough characters to cause overrun. Use the delay functions from the delay.h file.

5. Use the scope in single trigger capture mode to capture the character appearing on the RX pin of the PIC. Demonstrate that the character waveform you capture matches the waveform for the character you were

assigned in prelab.



E.9 EXPERIMENT 8: INTERRUPTS (CHAPTER 10)

This experiment covers material on interrupts from Chapter 10.

Prelab

1. Make a good faith effort to implement your assigned LED/Switch IO problem of the previous lab in an interrupt driven mode similar to that of Figure 10.15 using the INT0/INT1/INT2 interrupts. Use a software delay of 30

ms within the ISR to debounce the switch inputs (interrupts INT0, INT1,

INT2). This is not the most efficient way to debounce a switch input, but

is sufficient for this lab.



Appendix E: Suggested Laboratory Exercises



581



2. The root_rxfifo.c program inputs a decimal number in ASCII format, computes the floating-point square root, and displays the result. The serial port

is used for all I/O (19,200 baud is the default baud rate). This program uses

an interrupt approach and a receive software FIFO for serial data input as

discussed in Chapter 10. The size of the receive software FIFO is set by the

#define BUFMAX 2 statement. Read this program and ensure that you understand how the serial IO is performed as you will be modifying this program during lab. Table E.3 gives baud rate assignments that are needed

when modifying the root_rxfifo.c program.

TABLE E.3 Baud Rate Assignments

Last Digit of Student ID



Baud Rate Assignment



0 or 1



4800, 19200, 57600, 115200



2 or 3



4800, 19200, 38400, 115200



4 or 5



4800, 9600, 38400, 115200



6 or 7



4800, 9600, 57600, 115200



8 or 9



4800, 19200, 38400, 115200



Lab Activity

1. Program your PIC18F242 with your LED/Switch IO solution and verify its

operation.

2. Program your PIC with the root_rxfifo program and familiarize yourself

with its operation. Type in one number at a time and verify that the floatingpoint square root is computed. When compiling root_rxfifo.c, select the

printf library that supports both long and floating-point data types (see

Appendix C).

3. Open the file root_test1.txt in a text editor such as Notepad, select/copy a

range of input values, and use the HyperTerminal “Edit Paste-to-Host”

command to copy this selection into the terminal window. What happens

when a large number of entries are pasted at a time? Use the scope and determine the time it takes HyperTerminal to send several successive characters when pasting characters into the buffer. RECORD THIS TIME, you

will need it to answer questions in the report. You should observe that the

root_rxfifo program operates correctly for a small number of entries pasted



582



Microprocessors



4.



5.



6.



7.



into the window, but not for a large number. The failure is due to simultaneously receiving characters during transmission of the result string. While

characters are being transmitted, the receive port is not checked. At some

point, the RCREG input FIFO buffer fills to capacity, and characters are

lost. Note: When the Paste-to-Host command in HyperTerminal is used,

each character is sent at the specified baud rate. However, there is considerable dead time between each character that is sent. This explains why it

takes more than the expected number of input characters to cause a buffer

overrun problem.

Modify the root_rxfifo.c program to use the external DIP switch in your

parts kit to select between 4 different baud rates after reset (see Table E.3).

The DIP switch contains two switches allowing four different combinations. Use any two PORTB inputs you wish. Enable the weak-pullup on the

Port B pins so that external pullups are not required for these inputs. You

may need to use the low-speed mode to reach some of your assigned baud

rates. Examine the serial_init() subroutine in the serial.c include file to

understand how to pass in values for the SPBRG register and how to select

between low- and high-speed mode. Use a multimeter to verify the

close/open positions of the DIP switch. Test all four baud rates. Use an oscilloscope, and capture a character waveform. Be prepared to show the TA

that the bit time of the captured waveform matches the expected bit time.

Modify the pic_isr() subroutine to turn on an LED if a software FIFO

buffer overrun error occurs. This is not the same as the USART overrun

error. This overrun error occurs if the pic_isr() subroutine is called, and

placing another character into the software FIFO causes it to appear empty

(overrun occurs if the head pointer is equal to the tail pointer after incrementing and wrapping the head pointer). After turning on the error LED,

enter sleep mode to halt the PIC18. Test the program by pasting several

numbers into the HyperTerminal window, causing buffer overrun.

Discover how many numbers it takes within approximately ±5 to cause

overrun for your second slowest baud rate.

Increase the software FIFO buffer size to 8. Test overrun again with the

same baud rate of the previous step and verify that the number of entries

required to cause overrun has increased. Because of how HyperTerminal

sends characters, you may find that even small increases in the software

FIFO size may greatly increase the number of entries required for overrun

or that you may not be able to force an overrun at all.

Overrun occurs in the root_rxfifo.c program when the input data rate exceeds the output data rate (characters needed to print the result). Using the

scope measurement you made of the time it took HyperTerminal to send

several successive characters, and justify the numbers you recorded for



Appendix E: Suggested Laboratory Exercises



583



buffer overrun based on input data rate vs. output data rate. Do this by

computing the time it takes to fill up the input buffer given the rate at

which characters are being sent by HyperTerminal, and compare this to the

time it takes to print the result string. (The PIC is sending characters back at

about the maximum rate that can be sustained by the TX channel to the PC.)



E.10 EXPERIMENT 9: MORE INTERRUPTS, THE I 2 C BUS, AND A

SERIAL EEPROM (CHAPTER 11)

This lab covers material on the I2C bus and the 24LC515 Serial EEPROM from

Chapter 11.

Prelab

1. Add the 24LC515 Serial EEPROM to your protoboard as shown in Figure

11.25. Do not forget the 10 k pullup resistors! Connect both A1 and A0

to ground.

2. Create a spreadsheet that calculates the correct SSPADD value for a desired

I2C bus speed.



Lab Activity

The lab activity first has you verify the correct operation of the 24LC515 EEPROM,

after which this is used to capture streaming ASCII data from the serial port.

Verification of the I2C Bus Hookup and i2cmemtst.c Modifications



1. Verify that the i2cmemtst.c program operates correctly on your protoboard.

This program is discussed in Section 11.8 (main() is shown in Figure

11.32).

2. Change the wiring of the A1/A0 pins to alter the address of the EEPROM.

Modify the i2cmemtst.c program to use this new address. Use the following

values for A1/A0 based on the last digit of your student ID: a) 0,1,2 use

“01”; b) 3,4,5 use “10”; c) 6,7,8,9 use “11”. The #define EEPROM 0xA0 statement in i2cmemtst.c must be changed to accommodate the new A1/A0 values. Review the address byte formatting in the 24LC515 datasheet to

determine the new value.



584



Microprocessors



3. Modify i2cmemtst.c to prompt the user to choose one of two different I2C

bus rates. One rate is 500 kHz (or as close as you can achieve), and the

other is based on the last digit of your student ID: a) 0 or 1 (50 kHz), b) 2

or 3 (100 kHz), c) 4 or 5 (175 kHz), d) 6 or 7 (250 kHz), e) 8 or 9 (325

kHz). Even though 500 kHz is above the maximum specification for the

memory part, it should still work, as datasheet specifications are very conservative. However, this is only being done for illustrative purposes. Never design a system that relies on a part working beyond its specified performance.

4. Demonstrate the operation of i2cmemtst.c using the modified EEPROM

address with your assigned I2C bus speed. Capture the first 5 bytes of a page

write or sequential read via the oscilloscope and explain the formatting/

purpose of each byte to the TA.



Interrupt-Driven Streaming Writes



1. Using i2cmemtst.c as a starting point, create a program that implements

streaming data capture as discussed in Section 11.9. Use the flowcharts of

Figures 11.36 and 11.37 to help your understanding of how to solve this

problem. Your program should prompt the user for either read or write

mode. In write mode, capture streaming text from the serial port and save

it to EEPROM. Test your program write mode by using the “Transfer

Send Text File” command in HyperTerminal to send a complete file. Do

not use large files for test cases, as HyperTerminal takes too long to send

text files even with a baud rate of 115200. In read mode, dump the contents

of the serial EEPROM to the screen; detect a keypress to start or stop the

EEPROM content listing. To allow HyperTerminal to display the text with

carriage return/line feeds, add the following check in your code for characters sent to the console:

if ((c == 0xd) || (c == 0xa))

pcrlf();

else putch(c);



2. Verify that your program works for your assigned I2C bus rates.



Xem Thêm
Tải bản đầy đủ (.pdf) (687 trang)

×