ECE372Prelabs

advertisement

Clemson ECE Laboratories

ECE 372 – Microcomputer

Interfacing Laboratory

1

Pre-labs for ECE 372

Created by Ryan Mattfeld 12/17/12

Last Updated:12/17/12

Clemson ECE Laboratories

2

Clemson ECE Laboratories

Instructor Information and Syllabus

Instructor Name: Nilim Sarma

Email: nsarma@clemson.edu

Office Hours: Email to set up appointment.

Lab Manual can be found at http://www.clemson.edu/ces/departments/ece/resourc es/lab_manuals.html

3

Clemson ECE Laboratories

Introduction

Objective:

Gain a better understanding of the functionality of a microcontroller.

Learn to interface devices with different modules on a microcontroller.

4

Experiments:

10 experiments involving various modules and a final design project.

Clemson ECE Laboratories

Final Design Project

Will combine at least two of the labs performed throughout the semester in a creative way

Worth 30% of final grade

Weekly Lab Reports for reference when making final design

Final lab report will be given on final design project

5

Clemson ECE Laboratories

Equipments

The National Instruments ELVIS system .

Freescale MCU Project Board Student Learning Kit –

Prototyping board with microcontroller interface.

Microcontroller - Freescale HCS12 Family, model

MC9S12DT256.

6 http://www.clemson.edu/ces/departments/ece/resources/ lab_manuals.html

Clemson ECE Laboratories

Software Development Environment.

CodeWarrior IDE.

Text Editor for writing code.

Cross compiler to generate executables.

Loader program to load the executable on the microcontroller.

Debugger to perform runtime debugging.

7

Clemson ECE Laboratories

Prerequisite

Familiarity with C programming language.

C tutorial available in lab manual.

8

Clemson ECE Laboratories

Freescale Board Connector J1

9

Clemson ECE Laboratories

Mandatory Safety Video

10

Clemson ECE Laboratories

LABORATORY 1 –

INTRODUCTION TO THE

MC9S12DP256B

11

Clemson ECE Laboratories

Laboratory 1 Program

Program will count from 1-16 in binary using LEDs and in decimal using the LCD display.

Download the program from our section on

Blackboard under the “information” section

Extract the program to your student (U:) drive

• Open the “Lab 1” folder and open the .mpc file to edit the program

Take a few minutes to read the code and figure out what it should do

12

Clemson ECE Laboratories

Laboratory 1 Program

Make sure to turn on both power switches on the NI-

ELVIS board and connect the USB cables

Attempt to compile the code (ctrl+F7 is keyboard shortcut)

Identify and fix the error

• Hit the “debug” button (green arrow with helicopter)

Run the code and see the result

Modify the code to count backwards

13

Clemson ECE Laboratories

Final Reminders!

Store each lab as a separate file on your student server (copying an old lab folder and renaming it for new labs)

This ensures all necessary files are linked and provides your access to your previous lab code when you begin your final design project.

You can also access your student drive from any computer with internet access at:

• https://netstorage.clemson.edu/

Read over Lab 2 in the lab manual to be prepared for next week!

14

Clemson ECE Laboratories

LABORATORY 2 – READING

AND WRITING USING RAM

15

Clemson ECE Laboratories

Device Memory Map

$0000 - $03FF – Registers.

$0000 - $0FFF – 4K Bytes EEPROM.

$1000 - $3FFF – 12K Bytes RAM.

External Memory

$FF00 - $FFFF – Vectors.

After reset the bottom 1k of the EEPROM ($0000 - $03FF) are hidden by the register space.

Refer to MC9S12DT256 Device User Guide.

16

Clemson ECE Laboratories

Laboratory 2 Preparation

Find the folder for Lab 1 that you used last week

(stored on your student drive)

Copy the folder and rename it for Lab 2

Open the .mpc file to edit and compile your code

In main.c, leave the header files and #pragma statement, but delete all code in void main(void)

We will define a command to read and write to the

RAM of our microprocessor

** We will begin writing at reading at memory location ***insert location here***

17

Laboratory 2 Code

Clemson ECE Laboratories

We will create an easy method to access specific

RAM memory addresses by using a #define statement

#define _P(ADDRESS) *(unsigned char volatile *)(ADDRESS)

18

This command will allow you to both read and write to a specific address in RAM.

Conceptually, in your program, you can use

_P(**desired memory address**)

Like you would use a variable to store and use information.

Laboratory 2 Code

Clemson ECE Laboratories

Program goal: Write **** insert phrase here **** to

RAM, then read from RAM to the LCD

At the beginning of your program, make sure you initialize the LCD using “LCDInit()”

Then, make sure that after every 8 characters you clear the LCD using “LCDClearDisplay()”

You can write to the LCD using the command

“LCDPutChar()”

Use loops to write the phrase

Use conditional statement to clear display

19

Clemson ECE Laboratories

Preparations for Next Week

Before next lab, read over Lab 3 in your lab manual,

Application of a Digital Latch.

20

Clemson ECE Laboratories

LABORATORY 3 –

APPLICATION OF A DIGITAL

LATCH

21

Lab 3 Preparation

Clemson ECE Laboratories

Find the folder for Lab 2 that you used last week

(stored on your student drive)

Copy the folder and rename it for Lab 3

Open the .mpc file to edit and compile your code

This lab combines hardware with software

Uses 74LS374 Flip-Flop chip (D Flip-Flop)

22

Clemson ECE Laboratories

74LS374 – D Flip Flop

Latch data from Input Pin to Output Pin when there is a rising edge on Clock Pin.

23

Clemson ECE Laboratories

Laboratory 3 - Hardware Prep

The Input, 1D corresponds to the output, 1Q.

Reference chip diagram:

Input: Switches (SW1 1 and SW1 2)

Output: LEDs (LED 1 and 2)

Intermediate Output (Port B LEDs)

(Port B is hardwired to 4 LEDs)

Wire SW1 1 to 1D, SW1 2 to 2D

Wire 1Q to LED 1, 2Q to LED 2

24

Clemson ECE Laboratories

Laboratory 3 – Microcontroller Comm.

Must wire inputs to microcontroller to manipulate input with code.

Wire SW 1 to PTT 0 (reference page 50 in lab manual for Port wiring table)

Wire SW 2 to PTT 1

Clock Pulse Input: Pushbutton (PB1) to Port M 1

Clock Pulse Output: Port M 0 to clock of Flip-Flop

25

Wiring complete!

Clemson ECE Laboratories

Laboratory 3 – Software Prep

To use input and output registers on the microcontroller, must designate them for input or output in code.

DDR – Data Direction Register (0 for input, 1 for output)

Will use Port T, channels 0 and 1 for input, so we will set DDRT = 0x00;

Will use LEDs hard wired to Port B for output.

LEDs wired to Port B channels 4-7, set DDRB=0xF0;

Will use Port M channel 0 as input and Port M channel 1 as output, set DDRM = 0x01

26

Clemson ECE Laboratories

Laboratory 3 – Main Function

Constantly loop, looking for switch input. Port T 0 and Port T 1 will change between 00, 01, 10, 11 based on switch configuration.

For each possible state of Port T, light corresponding

LEDs (set Port B to appropriate values)

(HINT: Port B 4-7 are wired to LOW active LEDs)

Finally, must generate clock pulse for D Flip-Flop

Check if Port M channel 1 is 0 (Push Buttons are low active). If it is, generate clock pulse on Port M channel 0. (Set PTM = 0x01 then reset it to 0x00)

27

Clemson ECE Laboratories

Result

When complete, should have two displays:

1) When you flip one of the switches, the LEDs hard wired to Port B should immediately change

2) When you push Pushbutton 1, the switch values will travel through the Flip-Flop, and light the LEDs you wired your outputs to.

28

Flowchart

Clemson ECE Laboratories

29

Clemson ECE Laboratories

Preparations for Next Week

Before next lab, read over Lab 5 in your lab manual,

Keypad Interfacing

30

Clemson ECE Laboratories

LABORATORY 5 – KEYPAD

INTERFACING

31

Clemson ECE Laboratories

Laboratory 5 Hardware Prep

Plug in Keypads with brass side of connector facing left

Keypad 1

Port T 1

Keypad 2

Port T 2

Keypad 3

Port T 5

Keypad 4

Port T 3

Keypad 5

Port T 6

Keypad 6

Port T 7

Keypad 7

Port T 4

Keypad 8

Port T 0

32

Clemson ECE Laboratories

Keypad Description/Operation

33

Clemson ECE Laboratories

Laboratory 5 software prep

Set Port T channels 0-3 as output and channels 4-7 as input using DDRT

New registers: PERx (Pull Enable Register) and

PPSx(Polarity select Register)

PER and PPS work together to pull desired ports either high or low.

Pull Port T channels 4-7 high.

Enable Port T channels 4-7 for pulling(PERT = 0xF0)

Pull Port T channels 4-7 high (PPST = 0x00)

(setting PPST to 0 pulls corresponding channel high)

34

Clemson ECE Laboratories

Laboratory 5 Software Tools

Arrays can make keyboard interfacing easier:

• unsigned char mask[16]={0xEE,0xDE,0xBE,0x7E,

0xED,0xDD,0xBD,0x7D,

0xEB,0xDB,0xBB,0x7B,

0xE7,0xD7,0xB7,0x77};

• unsigned char key[16]={'D','1','2','3',

'A','4','5','6',

'B','7','8','9',

'C','*','0','#'};

35

Clemson ECE Laboratories

Laboratory 5 Code Flow Chart

START

INITIALIZE (proper header files, declarations, and definition)

No

DETERMINE IF A SPECIFIED

COLUMN IS ACTIVE IN THE

ACTIVE ROW

INITIALIZE PORT T

TURN ON 1 ROW AT A TIME

36

Yes

SEND THE PRESSED KEY TO THE

LCD DISPLAY

Clemson ECE Laboratories

Preparations for Next Week

Before next lab, read over Lab 4 in your lab manual,

Interrupts

37

Clemson ECE Laboratories

LABORATORY 4 –

INTERRUPTS

38

Clemson ECE Laboratories

Laboratory 4 Wiring Diagram

Port P 0

+5V

Port P 1

Gnd

39

Clemson ECE Laboratories

Laboratory 4 Software Prep

INPUTS

Port P 0 and 1 should be used as interrupts

Port P generates interrupt vector 56 (reference lab manual page 51 for vector interrupt table)

Registers:

DDRP = 0x00; (interrupts are inputs)

PERP = 0x03; (Enable pulling for interrupt channels)

PPSP = 0x03; (Pull low so high active button will trip interrupt)

PIEP (Port Interrupt Enable Register) = 0x03; (This enables Port P 0 and 1 as interrupts)

40

Clemson ECE Laboratories

Laboratory 4 Software Prep

OUTPUTS

LEDs hard wired to Port B will be outputs

DDRB = 0xF0; (Channels 4-7 are wired to the LEDs so they should be outputs)

LCD will constantly count from 0-9

LCDInit();

LCDClearDisplay();

41

Clemson ECE Laboratories

Laboratory 4 Software Prep

LOGIC

Constantly loop to count from 0-9 on the LCD display

Create interrupt function (using interrupt 56, the interrupt vector corresponding to Port P)

Check PIFP to see which interrupt was triggered

Light LEDs to reflect which interrupt was triggered

Reset Port P flags by setting the PIF(Port Interrupt

Flag) register to a 1 for each channel you want to reset

42

Clemson ECE Laboratories

Laboratory 4 Code Flow Chart

43

Clemson ECE Laboratories

Preparations for Next Week

Before next lab, read over Lab 7 in your lab manual,

Rotary Pulse Generator

44

Clemson ECE Laboratories

LABORATORY 7 – ROTARY

PULSE GENERATOR

45

Clemson ECE Laboratories

Laboratory 7 Wiring Diagram

Rotary Pulse Generator

Port P 0

Port J 7

46

Clemson ECE Laboratories

Laboratory 7 Software Prep

INPUT

Port P 0 and Port J 7 are interrupts wired to Output A and Output B

Rotate Right

Increase position

Rotate Left

Decrease position

Change in voltage produces interrupt

Can determine which direction

Rotary Pulse Generator turns based on interrupt and state of

Port P 0, Port J 7

47

Clemson ECE Laboratories

Laboratory 7 Software Prep

INPUT

Interrupt Vector for Port P

Interrupt Vector for Port J

Two interrupt functions

Set Port P 0 and Port J 7 as inputs

Set PERP and PERJ to enable

Port P 0 and Port J 7 for pulling

48

Clemson ECE Laboratories

Laboratory 7 Software Prep

OUTPUT

Port B LEDs will be used to show rotation in rotary pulse generator

Set Port B 4-7 as output

Initialize Port B LEDs off (Low Active)

49

Clemson ECE Laboratories

Laboratory 7 Software Prep

LOGIC

Pulling registers high and low is essential

Interrupt trigger requires voltage change

As pulse generator rotates, voltage changes from low to high and high to low.

Pull high if next voltage change is high

 low

Pull low if next voltage change is low

 high

50

Clemson ECE Laboratories

Laboratory 7 Software Prep

LOGIC

In each interrupt function:

Determine which position the switch is in

Set LEDs to light such that rotating right causes the

LEDs to “scroll” up and rotating left causes the LEDs to “scroll” down

51

Clemson ECE Laboratories

Laboratory 7 Flow Chart

52

Clemson ECE Laboratories

Preparations for Next Week

Before next lab, read over Lab 8 in your lab manual,

Clock Pulse Generator

53

Clemson ECE Laboratories

LABORATORY 8 – CLOCK

PULSE GENERATOR

54

Clemson ECE Laboratories

Laboratory 8 Hardware Preparation

The buzzer is hardwired to Port T 0, so no wiring is required!

(Quick note generation review)

55

Clemson ECE Laboratories

Laboratory 8 Software Prep

LOGIC

Introduction to the Enhanced Capture Timer

Input Capture

*Output Compare*

Enable ECT: TSCR1 (Timer System Control

Register 1) Bit 7 (Enable bit) set to 1

TIOS (Timer Input capture/Output compare Select)

Set to 1 for Output Compare

56

Clemson ECE Laboratories

Laboratory 8 Software Prep

LOGIC

Initialize: DDRT (Port T 0 is our output to the buzzer)

TCNT holds current clock time

TC1 holds our target clock time (when time is reached, flag is tripped)

TFLG1_C1F is our flag

TFLG1 = TFLG1_C1F_MASK; resets timer flag

Invert voltage from Port T 0 when timer flag is tripped

Allows control based on real time

57

Clemson ECE Laboratories

Laboratory 8 Software Prep

LOGIC

Initialize our target time (TC1) to the current time

(TCNT)

For a certain note length (quarter note, eighth note, etc)

• Add the note’s period (in microseconds) to target time

Wait until timer flag is tripped

Invert signal to buzzer (through Port T 0)

58

Clemson ECE Laboratories

Laboratory 8 Software Flow Chart

59

Clemson ECE Laboratories

Preparations for Next Week

Before next lab, read over Lab 11 in your lab manual,

Pulse Width Modulation

60

Clemson ECE Laboratories

LABORATORY 11 – PULSE

WIDTH MODULATION

61

Clemson ECE Laboratories

Laboratory 11 PWM Lab Description

Pulse Width Modulation allows us to control the duty cycle of a signal

Can be used to control motor rotation speeds

Using our board, we must amplify the current in order to power our motor (current driver chip)

Control speed of our motor using interrupts to control duty cycle

Display duty cycle on LCD

62

Clemson ECE Laboratories

Laboratory 11 PWM Description

63

Clemson ECE Laboratories

Laboratory 11 PWM Description

64

Clemson ECE Laboratories

Laboratory 11 PWM Registers

PWMEx – Pulse Width Modulation Enable

PWMPOLx – PWM Polarity Register (one for each

PWM channel, if 1 - starts high at each period, if 0 – starts low)

PWMCLKx – PWM Clock Select Register (1- SA or

SB, 2- A or B) --- Note: channels 0,1,4,5 use A/SA and channels 2,3,6,7 use B/SB

PWMCAEx – PWM Center Align Enable Register

(1- Center Aligned, 0- Left Aligned)

65

Clemson ECE Laboratories

Laboratory 11 PWM Registers

PWMPRCLK – PWM Prescale Clock Select Register

(6-4 select prescaler for B, 2-0 select prescaler for A)

PWMSCLA – PWM Scale A Register

(Clock SA = Clock A/(2*PWMSCLA))

PWMPERx – PWM Channel Period Registers

(PWMx Period = Channel Clock Period*(2*PWMPERx)

(HINT: bus clock = 25 MHz)

66

Clemson ECE Laboratories

Laboratory 11 PWM Final Registers

PWMDTYx = PWM Duty Register

If Polarity (PWMPOL) = 1,

Duty Cycle = (PWMDTYx/PWMPERx)*100

If Polarity (PWMPOL) = 0,

Duty Cycle = ((PWMPERx-PWMDTYx)/PWMPERx)*100

67

Clemson ECE Laboratories

Laboratory 11 Hardware Preparation

62003 Current Amplifier

Wire GND to ground and Common to +5V

Output of current amplifier goes to the black motor wire

Red wire from motor goes to 3.3V on NI-ELVIS board

Wire PWM1 (pin can be located in chart on page 50 of manual) to the input of current amp

68

Clemson ECE Laboratories

Laboratory 11 Software Preparation

Initializations

Port P 1 (which corresponds to PWM 1) set to output

Port J 7 will be the interrupt used to control duty cycle

PIEJ set to turn on Port J 7 as interrupt

PERJ, PPSJ set to pull Port J 7 to high voltage (push buttons are low active)

EnableInterrupts;

LCDInit(); (will display motor duty cycle on LCD)

69

Clemson ECE Laboratories

Laboratory 11 Software Goals

When interrupt detected (pushbutton pressed)

Increment duty cycle by 25%

When duty passes 100%, reset it to 0%

Display current duty cycle on LCD display

70

Clemson ECE Laboratories

Preparations for Next Week

Before next lab, read over Lab 6 in your lab manual,

Serial Communications (SCI)

71

Clemson ECE Laboratories

LABORATORY 6 – SERIAL

COMMUNICATIONS (SCI)

72

Clemson ECE Laboratories

Laboratory 6 Description

Serial Communication Simulation (no communication with NI-ELVIS board at all)

Will use simulation to read in keyboard entry, alter it in code, and send it back out to terminal

Simulation can accurately simulate registers with proper set up.

Create structures and definitions to make register simulation easier

73

Clemson ECE Laboratories

Laboratory 6 Definitions

#define SCI_ADDR 0x00C8

Typedef struct{ unsigned char SCIBDH; unsigned char SCIBDL; unsigned char SCICR1; unsigned char SCICR2; unsigned char SCISR1; unsigned char SCISR2; unsigned char SCIDRH; unsigned char SCIDRL;

} SCIStruct;

#define SCI (*((SCIStruct*)(SCI_ADDR)))

74

Clemson ECE Laboratories

Laboratory 6 Initializations

SCIBDH and SCIBDL – SCI Baud Rate High and

Baud Rate Low registers (used to set baud rate)

SCI baud rate = SCI module clock/(16*BR)

BR = total of 12 bits from SCIBDH and SCIBDL:

0000 HHHH LLLL LLLL

Question: If module clock has speed of 8MHz, what should SCIBDH and SCIBDL be to produce a target baud rate of 9600 Hz.

75

Clemson ECE Laboratories

Laboratory 6 Initializations

SCICR1 = 0x00

SCICR2 = 0x0C (sets transmitter enable bit (bit 3) and receiver enable bit (bit 2))

SCISR1 is used to detect when data is sent and received.

SCISR1 bit 7

Transmit Data Register Empty Flag

SCISR1 bit 5

Receive Data Register Full Flag

SCIDRH and SCIDRL store data for transmitting and receiving. Because we set SCICR1 to 0x00, we only have 8 bits of data (SCIDRH is not used)

76

Clemson ECE Laboratories

Laboratory 6 Software

Create two functions, one to write to the terminal and one to read keyboard input:

Char TERMIO_GetChar(void)

Wait until data received (using SCISR1)

Return received character (using SCIDRL)

Void TERMIO_PutChar(char ch)

Wait until ready to transmit (using SCISR1)

Send character to terminal (using SCIDRL)

In main, convert received character from lowercase to uppercase (use ASCII table)

77

Clemson ECE Laboratories

Preparations for Next Week

Before next lab, read over Lab 10 in your lab manual,

A/D and D/A Conversion

78

Clemson ECE Laboratories

LABORATORY 9/10 – A/D AND

D/A CONVERSION

79

Clemson ECE Laboratories

Laboratory 9/10 Description

We will read in an analog signal from the function generator and read it into our microprocessor through a built in Analog to Digital Converter

The ATD Converter converts the signal by sampling it at a specified rate and estimating the value by quantizing it

We then send the digital signal out and use an external Digital to Analog converter to send the signal to the oscilloscope

80

Clemson ECE Laboratories

Laboratory 9/10 Hardware Preparation

81

Clemson ECE Laboratories

Laboratory 9/10 SPI Registers

SPI (Serial Parallel Interface) allows communication with external devices (DTA converter, here)

SPICR1 = 0x50; (Sets SPI Control Register 1 bits 6 and 4. Bit 6 enables SPI, Bit 4 sets SPI as master)

SPICR2 = 0x0000; (disables a number of unneeded features)

SPI1BR = 0x00; (SPI Baud Rate Register)

82

Clemson ECE Laboratories

Laboratory 9/10 ATD Registers

ATD0CTL2 = 0x80 (enables internal ATD converter)

ATD0CTL3 = 0x40 (Sets to take 8 conversions per sequence)

ATD0CTL4 = 0x0000 (Sets 10 bit resolution and sets sample rate as high as possible)

83

Clemson ECE Laboratories

Laboratory 9/10 Software

Port M channel 0 is used as a sync pin for the DTA converter chip. (Will be used as output)

Set DDRM = 0x03;

Create main loop to constantly read in data, convert it for writing, and then write it to the DTA converter.

While loop

Prepare ATD for data

ATD0CTL5 = ATD0CTL5_DJM_MASK;

Wait until data is read (Sequence Complete Flag)

While(ATD0STAT0_SCF == 0);

Prepare and Send Data

84

Clemson ECE Laboratories

Laboratory 9/10 Software

Preparing Data

Data Received as 10 bits from ATD

ATD0DR0H ATD0DR0L

0000 00AB CDEF GHIJ

Data Trasmitted through SCI as 12 bits, so desired output looks like this:

0000 ABCD EFGH IJ00

In order to maintain uppermost bits, must shift bits.

<< (shift bits left) >> (shift bits right)

& (binary AND) | (binary OR)

85

Clemson ECE Laboratories

Laboratory 9/10 Software

}

{

Sending Data volatile char temp; void write_dac(char upperbyte, char lowerbyte)

PTM = 0x00;

SPI1DR = upperbyte & 0x0F; while(SPI1SR_SPTEF == 0);

SPI1DR = lowerbyte; while(SPI1SR_SPTEF == 0); temp = SPI1SR; temp = SPI1DR;

PTM = 0x03;

86

Clemson ECE Laboratories

Laboratory 9/10 Function Generator

87

Clemson ECE Laboratories

Preparations for Next Week

Next week begins Final Design Project week. It will help to prepare your code before working on your design project next week.

88

Clemson ECE Laboratories

References

Images provided from ECE 372 lab manual and user’s manual for MC9S12DT256 and from personal notes.

89

Clemson ECE Laboratories

THE END

90

Download