ECE3720 Prelabs
Transcript of ECE3720 Prelabs
1Clemson ECE Laboratories
ECE 372 – Microcomputer Interfacing Laboratory
Pre-labs for ECE 372Created by Ryan Mattfeld 12/17/12
Last Updated:12/17/12
2Clemson ECE Laboratories
ORIENTATION
3Clemson ECE Laboratories
Instructor Information and Syllabus
• Instructor Name: Nilim Sarma• Email: [email protected]• Office Hours: Email to set up appointment.• Lab Manual can be found at
http://www.clemson.edu/ces/departments/ece/resources/lab_manuals.html
4Clemson ECE Laboratories
Introduction
Objective:• Gain a better understanding of the functionality of a
microcontroller.• Learn to interface devices with different modules on a
microcontroller.
Experiments:10 experiments involving various modules and a final
design project.
5Clemson 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
6Clemson 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.
http://www.clemson.edu/ces/departments/ece/resources/lab_manuals.html
7Clemson ECE Laboratories
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.
Software Development Environment.
8Clemson ECE Laboratories
Prerequisite
• Familiarity with C programming language.
• C tutorial available in lab manual.
9Clemson ECE Laboratories
Freescale Board Connector J1
10Clemson ECE Laboratories
Mandatory Safety Video
11Clemson ECE Laboratories
LABORATORY 1 – INTRODUCTION TO THE MC9S12DP256B
12Clemson 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
13Clemson 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
14Clemson 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!
15Clemson ECE Laboratories
LABORATORY 2 – READING AND WRITING USING RAM
16Clemson 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.
17Clemson 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***
18Clemson ECE Laboratories
Laboratory 2 Code
• We will create an easy method to access specific RAM memory addresses by using a #define statement
• #define _P(ADDRESS) *(unsigned char volatile *)(ADDRESS)
• 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.
19Clemson ECE Laboratories
Laboratory 2 Code
• 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
20Clemson ECE Laboratories
Preparations for Next Week
• Before next lab, read over Lab 3 in your lab manual, Application of a Digital Latch.
21Clemson ECE Laboratories
LABORATORY 3 – APPLICATION OF A DIGITAL LATCH
22Clemson ECE Laboratories
Lab 3 Preparation
• 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)
23Clemson ECE Laboratories
74LS374 – D Flip Flop
• Latch data from Input Pin to Output Pin when there is a rising edge on Clock Pin.
24Clemson 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
25Clemson 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
• Wiring complete!
26Clemson 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
27Clemson 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)
28Clemson 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.
29Clemson ECE Laboratories
Flowchart
30Clemson ECE Laboratories
Preparations for Next Week
• Before next lab, read over Lab 5 in your lab manual, Keypad Interfacing
31Clemson ECE Laboratories
LABORATORY 5 – KEYPAD INTERFACING
32Clemson 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
33Clemson ECE Laboratories
Keypad Description/Operation
34Clemson 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)
35Clemson 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','#'};
36Clemson ECE Laboratories
Laboratory 5 Code Flow Chart
START INITIALIZE (proper header files, declarations, and definition) INITIALIZE PORT T
TURN ON 1 ROW AT A TIME
SEND THE PRESSED KEY TO THE LCD DISPLAY
DETERMINE IF A SPECIFIED COLUMN IS ACTIVE IN THE
ACTIVE ROW
No
Yes
37Clemson ECE Laboratories
Preparations for Next Week
• Before next lab, read over Lab 4 in your lab manual, Interrupts
38Clemson ECE Laboratories
LABORATORY 4 – INTERRUPTS
39Clemson ECE Laboratories
Laboratory 4 Wiring Diagram
Port P 0
Port P 1
Gnd
+5V
40Clemson 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)
41Clemson 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();
42Clemson 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
43Clemson ECE Laboratories
Laboratory 4 Code Flow Chart
44Clemson ECE Laboratories
Preparations for Next Week
• Before next lab, read over Lab 7 in your lab manual, Rotary Pulse Generator
45Clemson ECE Laboratories
LABORATORY 7 – ROTARY PULSE GENERATOR
46Clemson ECE Laboratories
Laboratory 7 Wiring Diagram
Port P 0
Port J 7
Rotary Pulse Generator
47Clemson 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 turnsbased on interrupt and state ofPort P 0, Port J 7
48Clemson 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
49Clemson 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)
50Clemson 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
51Clemson 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
52Clemson ECE Laboratories
Laboratory 7 Flow Chart
53Clemson ECE Laboratories
Preparations for Next Week
• Before next lab, read over Lab 8 in your lab manual, Clock Pulse Generator
54Clemson ECE Laboratories
LABORATORY 8 – CLOCK PULSE GENERATOR
55Clemson ECE Laboratories
Laboratory 8 Hardware Preparation
• The buzzer is hardwired to Port T 0, so no wiring is required!
• (Quick note generation review)
56Clemson 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
57Clemson 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
58Clemson 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)
59Clemson ECE Laboratories
Laboratory 8 Software Flow Chart
60Clemson ECE Laboratories
Preparations for Next Week
• Before next lab, read over Lab 11 in your lab manual, Pulse Width Modulation
61Clemson ECE Laboratories
LABORATORY 11 – PULSE WIDTH MODULATION
62Clemson 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
63Clemson ECE Laboratories
Laboratory 11 PWM Description
64Clemson ECE Laboratories
Laboratory 11 PWM Description
65Clemson 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)
66Clemson 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)
67Clemson 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
68Clemson 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) tothe input ofcurrent amp
69Clemson 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)
70Clemson 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
71Clemson ECE Laboratories
Preparations for Next Week
• Before next lab, read over Lab 6 in your lab manual, Serial Communications (SCI)
72Clemson ECE Laboratories
LABORATORY 6 – SERIAL COMMUNICATIONS (SCI)
73Clemson 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
74Clemson ECE Laboratories
Laboratory 6 Definitions
#define SCI_ADDR 0x00C8Typedef 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)))
75Clemson 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.
76Clemson 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)
77Clemson 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)
78Clemson ECE Laboratories
Preparations for Next Week
• Before next lab, read over Lab 10 in your lab manual, A/D and D/A Conversion
79Clemson ECE Laboratories
LABORATORY 9/10 – A/D AND D/A CONVERSION
80Clemson 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
81Clemson ECE Laboratories
Laboratory 9/10 Hardware Preparation
82Clemson 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)
83Clemson 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)
84Clemson 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
85Clemson ECE Laboratories
Laboratory 9/10 Software
• Preparing Data• Data Received as 10 bits from ATD
ATD0DR0H ATD0DR0L0000 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)
86Clemson ECE Laboratories
Laboratory 9/10 Software
• Sending Datavolatile 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;}
87Clemson ECE Laboratories
Laboratory 9/10 Function Generator
88Clemson 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.
89Clemson ECE Laboratories
References
• Images provided from ECE 372 lab manual and user’s manual for MC9S12DT256 and from personal notes.
90Clemson ECE Laboratories
THE END