Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 372 – Microcomputer Interfacing Laboratory

Similar presentations

Presentation on theme: "ECE 372 – Microcomputer Interfacing Laboratory"— Presentation transcript:

1 ECE 372 – Microcomputer Interfacing Laboratory
Lab Schedule reference (order matches order material is taught in class. The order is different than the lab manual.) Tentative Lab Schedule Lab 1 - Introduction & C Programming Examples Lab 2 - Reading and Writing Using Ram Lab 3 - Applications of a Digital Latch Lab 5 – Keypad Interfacing Lab 4 – Interrupts Lab 7 - Rotary Pulse Generator Lab 8 - Clock Pulse Generator (ECT) Lab 11 - Pulse Width Modulator Lab 6 - Serial Communications (SCI) Lab 9 and Lab 10 (combined) - Serial Communications, Part 2 (SPI) and Analog-to-Digital and Digital-to-Analog Conversions Design Projects Pre-labs for ECE 372 Created by Ryan Mattfeld 12/17/12 Last Updated:12/17/12


3 Instructor Information and Syllabus
Instructor Name: Nilim Sarma Office Hours: to set up appointment. Lab Manual can be found at Basic information, be sure to fill in details and be familiar with the lab manual. ***Make special note that the order that the labs will be performed does NOT match the lab manual. The sample syllabus gives the labs in an order that more closely reflects the order material is learned in class.*** Make sure you give your students a heads up about the fact that the labs will be taught according to the order given in the syllabus rather than the order in the lab manual.

4 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.

5 Weekly Lab Reports for reference when making final design
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 The final design project ends up heavily impacting some important things about this lab. Because it references previous labs, weekly lab reports are required to give students good references to the code and wiring used in previous labs. Make sure they know that the lab reports will be an important resource for them when they look back for their final lab report. Since the lab is 1 credit hour and lab reports are weekly, I recommend that you tell your students to limit their reports to 2 pages MAX. The should not be as time consuming as lab reports for other labs, since they are weekly rather than having only 3-4 per semester like other labs require.

6 Equipments The National Instruments ELVIS system . Freescale MCU Project Board Student Learning Kit – Prototyping board with microcontroller interface. Microcontroller - Freescale HCS12 Family, model MC9S12DT256.

7 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.

8 Prerequisite Familiarity with C programming language. C tutorial available in lab manual.

9 Freescale Board Connector J1

10 Mandatory Safety Video
Have your students watch the attached safety video.

11 Laboratory 1 – Introduction to the MC9S12DP256B
In this orientation lab, you will introduce students to the compiler and give them an introduction program. This lab, as a whole, is much more heavily programming based than most of the EE and CE labs. Electrical Engineers in particular may not have programmed since CP SC 111, an introduction to C programming. On the other hand, Computer Engineers will have had more programming classes. Because of this, there will likely be a discrepancy between the programming abilities of students in your lab. Keep that information in mind when working with students.

12 Extract the program to your student (U:) drive
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 I recommend posting the program for lab 1 on blackboard. You may be provided the program at the start of the semester or you may have to find a good example program on the lab computers. Lab 1 should be a premade program stored on each lab computer. However, since the programs are locally stored, students can edit them freely. Because of this it is best to give them an identical starting point by providing the program as a .zip file on blackboard. Simply find a program that works effectively, and per the lab manual, set it up so it is missing a semicolon and counts forwards. Your students should identify and correct the missing semicolon error and modify the code to count backwards. Students should save the project file on their student (U:) drive. The student drive can be identified by the student’s name. ***students must open the “.mpc” file, not the .c source file. ***

13 Attempt to compile the code (ctrl+F7 is keyboard shortcut)
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 Since this is the first lab and many Electrical Engineering students may not be familiar with programming techniques, I go over fine detail with this lab.

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: Read over Lab 2 in the lab manual to be prepared for next week! Having your students store their work on their student server is important because it provides three major benefits 1) No matter which computer they log in to, they will be able to access their code. 2) It will protect their code from other students 3) It will give them good reference code when they begin working on their final design lab.

15 Laboratory 2 – Reading and Writing Using RAM
Sample Code for Lab 2 FOR REFERENCE #include <hidef.h> #include <mc9s12dt256.h> #include "pbs12dslk.h" #include "lcd.h" #define start 0x1000 #define _P(ADDRESS) *(unsigned char volatile*)(ADDRESS) #pragma LINK_INFO DERIVATIVE "mc9s12dt256" void main(void) { int i,j,k; unsigned char array[]="Go CLEMSON TIGERS!"; LCDInit(); LCDClearDisplay(); for(i = 0; i <= 18; i++) _P(start + i) = array[i]; } for(i=0; i< 18; i++) if(i%8==0) LCDPutChar(_P(start + i)); for(j=0;j<300;j++) for(k=0;k<300;k++);

16 Refer to MC9S12DT256 Device User Guide.
Device Memory Map $ $03FF – Registers. $ $0FFF – 4K Bytes EEPROM. $ $3FFF – 12K Bytes RAM. External Memory $FF00 - $FFFF – Vectors. After reset the bottom 1k of the EEPROM ($ $03FF) are hidden by the register space. Refer to MC9S12DT256 Device User Guide.

17 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*** Give your students reminders about how to access their student drive for the first couple of weeks as they likely have not used it before this lab. In order to ensure the correct files are linked, have students copy the entire folder from the previous week and just rename it. Make sure they open the .mpc file to allow for debugging and compiling. ***by looking at the MC9S12DT256 manual, you can find that RAM can be accessed at addresses 0x1000 – 0x3FFF on the microprocessor. Use a value in that range for your students to start writing. By changing this value from semester to semester, you can keep an eye on cheating (if they use an old program save to their computer, it will have the old TA’s starting address.) *******NOTE: a good way to ensure code is working (and also debug) is to look at the actual RAM addresses. You can see their contents in the interface given after the program is successfully compiled.*********

18 Conceptually, in your program, you can use
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. Make sure you note where the spaces are in the #define statement. A common error is for students to leave out the space after #define or the space after _P(ADDRESS).

19 You can write to the LCD using the command “LCDPutChar()”
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 **** create a unique phrase for your students to write. Changing the phrase each semester again is useful for preventing cheating by copying old labs. ****

20 Preparations for Next Week
Before next lab, read over Lab 3 in your lab manual, Application of a Digital Latch. Lab 3 will add hardware setup combined with coding

21 Laboratory 3 – Application of a Digital Latch
Sample Code for Lab 3 /* ALWAYS INCLUDE THESE LINES OF CODE */ #include <hidef.h> /* common defines and macros */ #include <mc9s12dt256.h> /* derivative information */ #include "pbs12dslk.h" /* I/O definitions and init routines */ #include "lcd.h" /* LCD definitions and init routines */ #pragma LINK_INFO DERIVATIVE "mc9s12dt256" /* END ALWAYS INCLUDE THESE LINES OF CODE */ void main(void) { // initialize ports and registers PTT = 0x00; PTM = 0x00; PORTB = 0x00; DDRT = 0x00; DDRB = 0xFF; DDRM = 0x01; // constantly scan for switch input, and pushbutton input to latch in new values from the switches while(1) // send signals to port B based on Port T's input from the switches if(PTT == 0) PORTB = 0xF0; if(PTT == 1) PORTB = 0xE0; if(PTT == 2) PORTB = 0xD0; if(PTT == 3) PORTB = 0xC0; // if the pushbutton is pressed, send a pulse to the clock signal in the latch to latch in a new value if((PTM & 0x02) == 0) PTM = PTM | 0x01; PTM = PTM & 0x00; }

22 Copy the folder and rename it for Lab 3
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) The D Flip-Flop acts as a buffer. The input, sent to the “D” side does not filter through to the output “Q” side until a clock pulse is sent to the “C” pin. When the clock pulse is sent, the input filters to the output pin.

23 74LS374 – D Flip Flop Latch data from Input Pin to Output Pin when there is a rising edge on Clock Pin.

24 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 There are a lot of different outputs and inputs, so make sure you go over each slowly. The reason for the number of outputs is this: One set of LEDs will light up immediately when the student flips a switch. These are the LEDs hard wired to Port B (located on the far left part of the board). Then, if the student pushes Pushbutton 1, the program will generate a clock pulse and the input will filter through the flip flop to reach the output LEDs. This demonstrates how to use input and output with the microcontroller and demonstrates how the flip-flop operates.

25 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! Breaking up the lab into wiring and coding helps students see how the different parts of the lab interact

26 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 Make sure you emphasize the Data Direction Registers. They will be used heavily throughout the rest of the labs. Understanding how they operate is essential. Input and output are determined from the perspective of the microcontroller. Input means a voltage is being read in to the microcontroller, output means a voltage is being sent out of the microcontroller.

27 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) Try to get students to make their outputs make sense given their inputs. There are 4 input options, 00, 01, 10, 11. The outputs should either light LEDs to rotate between 4 different LEDs in sequence to reflect the decimal equivalents of 0, 1, 2, 3 or light to match the switches directly (output can be binary or decimal)

28 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.

29 Flowchart

30 Preparations for Next Week
Before next lab, read over Lab 5 in your lab manual, Keypad Interfacing Next week’s keypad interfacing lab will take place the week after students learn about keypads in class

31 Laboratory 5 – Keypad Interfacing
Sample Code for Lab 5 FOR REFERENCE /* ALWAYS INCLUDE THESE LINES OF CODE */ #include <hidef.h> /* common defines and macros */ #include <mc9s12dt256.h> /* derivative information */ #include "pbs12dslk.h" /* I/O definitions and init routines */ #include "lcd.h" /* LCD definitions and init routines */ #pragma LINK_INFO DERIVATIVE "mc9s12dt256" /* END ALWAYS INCLUDE THESE LINES OF CODE */ void main(void) { int i,j; char K; 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','#'}; DDRT = 0x0F; PERT = 0xF0; PPST = 0x00; LCDInit(); LCDClearDisplay(); while(1) for(i=0;i<=15;i++) PTT = mask[i]; if(PTT == mask[i]) K=key[i]; } LCDPutChar(K); for(i=0;i<300;i++) for(j=0;j<300;j++);

32 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 Note that the wiring for the Keypad is essentially random. The keypad is wired strangely, but wiring it this way ends up allowing the lab to work.

33 Keypad Description/Operation
Here you can refresh the students memories about how a keypad works. Basically, 4 channels of Port T will be used as Output and 4 as input. We will pull the voltage of the input channels high, and we will very quickly run a voltage of 0 across each of the output channels. Then, if a key is pressed, the low voltage will connect with our input channel and pull down the value. We will create a couple of character arrays to tell us which key press corresponds with which mask.

34 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) Each week, new registers are introduced to correspond to each part of the microcontroller being introduced. This week, we introduce pulling. Pulling registers up or down will be used again. In the rotary pulse generator lab, they will be essential to get the rotary pulse generator operating

35 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','#'}; I give these arrays and explain them because even with the hardware wiring fix, the keypad still doesn’t operate as expected. Note: 0xE = 1110 0xD = 1101 0xB = 1011 0x7 = 0111 The mask array rotates low voltage through each output and input, one row and column at a time. A for loop can loop through each value of mask, as shown in the sample code given in the notes of the first slide for this lab. NOTE: it is up to you whether to give the two key lines of the code: PTT = mask[i]; If(PTT == mask[i]) { K = key[i]; } You could let students try to work on it for a while or just give it to them. Either way, make sure to explain WHY it works. (since 4 of the channels of port T are inputs, setting them in code does not actually change their value. Their value is only read in. Therefore, what we are really doing is going through the mask and setting the output values of Port T while checking our conditional statement for the corresponding input pins)

36 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 This is a good flowchart to summarize the code. Yes

37 Preparations for Next Week
Before next lab, read over Lab 4 in your lab manual, Interrupts Next week’s lab will introduce interrupts

38 Laboratory 4 – Interrupts
Sample Code for Lab 4 FOR REFERENCE /* ALWAYS INCLUDE THESE LINES OF CODE */ #include <hidef.h> /* common defines and macros */ #include <mc9s12dt256.h> /* derivative information */ #include "pbs12dslk.h" /* I/O definitions and init routines */ #include "lcd.h" /* LCD definitions and init routines */ #pragma LINK_INFO DERIVATIVE "mc9s12dt256" /* END ALWAYS INCLUDE THESE LINES OF CODE */ void interrupt_function(); void main(void) { unsigned char i='0'; int j,k; LCDInit(); LCDClearDisplay(); DDRP = 0x00; DDRB = 0xF0; PPSP = 0x03; PERP = 0x03; PIEP = 0x03; /* put your own code here */ EnableInterrupts; while(1) PORTB = 0xF0; if(i + 1 > '9') i = '0'-1; i++; LCDPutChar(i); for(j=0;j<200;j++) for(k=0;k<200;k++); } /* please make sure that you never leave this function */ void interrupt 56 interrupt_funtion() if(PIFP & 0x01 == 0x01) PORTB = 0xE0; PIFP = PIFP_PIFP0_MASK; else if (PIFP & 0x02 == 0x02) PORTB = 0xD0; PIFP = PIFP_PIFP1_MASK;

39 Laboratory 4 Wiring Diagram
+5V Port P 0 ***** IMPORTANT ***** Some of the code projects are not set up properly to include interrupts. During your lab prep, find a lab file that compiles with the interrupt code. If it isn’t set up properly, the compiler will give you a link error that mentions the word “fixup”. To get around this, find a working program on one of the machines and post it on Blackboard so all of your students can download a program file that is set up properly. This lab combines interrupts with a NAND gate debouncing circuit. The lab usually corresponds with an assignment from ECE 371 in which the students are asked to design a debouncer. This lab is helpful for them in that assignment. Have your students wire according to the above wiring diagram. MAKE SURE THEY ADD RESISTORS, otherwise there will be direct shorts to ground and the board will turn it self off for protection (look at the LEDs in the top left to determine whether the board detects a short. The LED corresponding to the voltage with a short will turn off if a short is detected. Port P 1 Gnd

40 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) Describe how interrupts work here (when voltage change is detected, interrupt is triggered, code jumps to corresponding interrupt vector, and corresponding interrupt flag is tripped). DDR, PER, and PPS have been used previously. PIE (Port Interrupt Enable) is a new register

41 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(); The program will constantly count from 0-9 in a loop, even when an interrupt is pressed. The interrupt output will be shown in the LEDs. The counter will constantly run so the students can see that the main code is interrupted (as they can see by the LEDs changing) but it quickly returns to the main code (as seen by the counter constantly running)

42 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 One concept students have difficulty understanding is the concept of setting PIFP = 0x01 to reset the port interrupt flag for Port P channel 0. Typically people expect that setting something to 1 stores a 1 in the variable. Port Interrupt Flags work differently than variables. When you set them, you actually reset the flag, so if it is set as a 1, it resets to 0. If it is set to 0, no change occurs.

43 Laboratory 4 Code Flow Chart
This flow chart is a good summary of the code descriptions above.

44 Preparations for Next Week
Before next lab, read over Lab 7 in your lab manual, Rotary Pulse Generator Next week’s lab will examine the use of interrupts in more depth as well as the use of the Port Polarity Select register.

45 Laboratory 7 – Rotary Pulse Generator
Sample Code for Lab 7 FOR REFERENCE /* ALWAYS INCLUDE THESE LINES OF CODE */ #include <hidef.h> /* common defines and macros */ #include <mc9s12dt256.h> /* derivative information */ #include "pbs12dslk.h" #include "lcd.h" #pragma LINK_INFO DERIVATIVE "mc9s12dt256" void main(void) { DDRB = 0xF0; DDRP = 0x00; DDRJ = 0x00; PERP = 0x01; PERJ = 0x80; PORTB = 0xF0; if(PTP_PTP0 == 1) PPSP = 0x00; else if (PTP_PTP0 == 0) PPSP = 0x01; if(PTJ_PTJ7 == 1) PTJ = 0x00; else if (PTJ_PTJ7) PTJ = 0x01; PIFP = 0xFF; PIFJ = 0xFF; PIEP_PIEP0 = 1; PIEJ_PIEJ7 = 1; EnableInterrupts; while(1); } void interrupt 56 PORT_P_ISR() if(PTJ_PTJ7 == 0) PORTB = 0xB0; PORTB = 0xD0; PPSP_PPSP0 = 0; PORTB = 0x70; PORTB = 0xE0; void interrupt 24 PORT_J_ISR() if(PTP_PTP0 == 0) else if (PTJ_PTJ7 == 0)

46 Laboratory 7 Wiring Diagram
Rotary Pulse Generator Port P 0 Port J 7 This is all the wiring that needs to be done for the lab

47 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 The rotary pulse generator is an interesting device and provides the opportunity for students to figure out the logic. For example, assuming Port P 0 is connected to Output A and Port J 7 is wired to Output B, (as shown in circuit diagram on previous slide), if a Port P interrupt is triggered (interrupt vector 56), and Port P 0 == 1 and Port J 7 == 0, then we know that the generator is being rotated right and we transitioned from Position number 1 to Position number 2 as shown in the image in the slide.

48 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 Have students look up interrupt vector values for Ports P and J to make sure they know how to read the table in page 51 of the lab manual. Students should be familiar with the Data Direction Register (DDR) by this point

49 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) The outputs are relatively simple to initialize, simply set up Port B for use

50 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 Continuing the earlier example, assuming Port P 0 is connected to Output A and Port J 7 is wired to Output B, (as shown in circuit diagram on previous slide), if a Port P interrupt is triggered (interrupt vector 56), and Port P 0 == 1 and Port J 7 == 0, then we know that the generator is being rotated right and we transitioned from Position number 1 to Position number 2 as shown in the image in the slide. IN ADDITION, we know Port P’s interrupt was triggered and it is now high voltage. Therefore, we must pull Port P high (PPSP = 0x00) so that when it changes to low between states 3 and 4, the voltage change from high to low will trigger an interrupt. (If Port P 0 were instead pulled low, then the voltage change from high to low would not be detected because it would already be low and it can’t be pulled lower than 0V) This pulling of voltage low or high is essential to getting the rotary pulse generator to detect every state.

51 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 The output for this is provides a very good visual for the rotation of the rotary pulse generator. You can visualize this by essentially taking Position numbers and determining logic based on which position you are in. For example, in position 1, Port P 0 will be 0 and Port J 7 will be 0. Thus, if your logic detects these two values, then light the bottom LED (using Port B = 0xE0). Rotate the LEDs by following the positions and lighting a corresponding LED. One way of accomplishing this: PortP0 = 0 and PortJ7 = 0  Port B = 0xE0 PortP0 = 1 and PortJ7 = 0  Port B = 0xD0 PortP0 = 1 and PortJ7 = 1  Port B = 0xB0 PortP0 = 0 and PortJ7 = 1  Port B = 0x70

52 Laboratory 7 Flow Chart Summary of code.
Note, Port J interrupt is the same as the Port P interrupt but switching the places of Port P and Port J in the logic (see sample code in first slide)

53 Preparations for Next Week
Before next lab, read over Lab 8 in your lab manual, Clock Pulse Generator Next week’s lab will use the Enhanced Capture Timer to generate musical notes through the piezoelectric buzzer built in to the NI_ELVIS board

54 Laboratory 8 – Clock Pulse Generator
Sample Code for Lab 8 #include <hidef.h> /* common defines and macros */ #include <mc9s12dt256.h> /* derivative information */ #include "pbs12dslk.h" #include "lcd.h" #pragma LINK_INFO DERIVATIVE "mc9s12dt256" #define C 3817 #define C_ 3610 #define D 3402 #define D_ 3216 #define E 3031 #define F 2866 #define F_ 2703 #define G 2551 #define G_ 2410 #define A 2273 #define A_ 2146 #define B 2025 #define q 350 #define e 175 #define num_notes 28 void main(void) { int i,j; int delay[num_notes] = {q,q,q,q,q,q,e,e,e,e,e,e,e,e,e,e,e,e,e,e,e,e,e,e,e,e,e,e}; int music_notes[num_notes] = {E,B,C,G,D,A,0,C,G,E,A,B,A_,G,E,G,A,F,G,E ,C,D,B,C,G,E,A,B }; DDRT = 0x01; TSCR1_TEN = 1; TIOS_IOS1 = 1; PTT = 0x01; TC1 = TCNT; for(i=0; i < num_notes; i++) TC1 += music_notes[i]; // initialize counter to period of current note for (j=0; j < delay[i]; j++) // for length of time to play note TFLG1 = TFLG1_C1F_MASK; // reset timer while(TFLG1_C1F == 0); // as long as flag is not tripped, wait if(PTT_PTT0 == 0) // then toggle clock pulse PTT_PTT0 = 1; else PTT_PTT0 = 0; TC1 += music_notes[i]; //add period of music note to timer (note, timer rolls // over to 0 automatically when its max value is reached) }

55 Laboratory 8 Hardware Preparation
The buzzer is hardwired to Port T 0, so no wiring is required! (Quick note generation review) This is also known as the Buzzer lab! This is generally accepted as one of the most entertaining labs. We use the Enhanced Capture Timer to produce musical notes. NOTE: note frequency table located in lab manual *** Review of note generation*** Musical notes are just sound waves with specific frequencies and amplitudes Amplitude determines loudness of note Frequency determines pitch of note We have scientifically determined what frequencies generate each note. The frequencies can be found online and are also located in the lab manual. The piezoelectric buzzer on our NI-ELVIS board converts the electrical signal we send into a sound wave with the same frequency as the electric signal we send it.

56 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 Note, no input, output is our signal sent to the buzzer. In addition, to allow students to play with the code more easily in this lab, can go over the code in more detail for this lab and show students the shortcuts they can use to make the code more programmer-friendly (#defines and arrays as seen in the sample code on slide 1 of this lab) **** Intro to ECT**** Enhanced Capture Timer, ECT, is a clock that runs in the background, even while the code is running. In the MC9S12 microprocessor we’re using, the speed of the clock is 2MHz, so the clock increments every ½ microsecond. We will use this to create a signal with the frequency of musical notes. **** Input Capture**** When an input signal is received, record the ECT’s current value **** Output Compare**** At a specified time, trigger a flag (and interrupt if timer interrupts are enabled (interrupts aren’t needed here)) ****We use output compare for our lab

57 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 We need to use periods for our notes because we are inverting our signal when certain amounts of time have passed. Thus, take the frequencies for the notes in the lab manual and convert to period (frequency = 1/period). Then, we need to invert our signal twice within the period to create the correct note, but the microcontroller simplifies this since the clock is 2 MHz. Since the clock increments every ½ microsecond, we can simply define the note as its period in microseconds. Then, the flag trips enough to produce a note with a period of 3817 microseconds (period of a middle C) if you set the target timer to increment by 3817 counts (3817/2 microseconds) and invert when target time is reached (by waiting until the flag is tripped)

58 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) Each note must be played for a certain length. The way the example program is set up, each note is played, in effect, for a certain number periods. Using 350 gives an approximate quarter note length, and all other notes can be determined by multiplying or dividing that by factors of two. The flow chart on the following slide summarizes how the program works

59 Laboratory 8 Software Flow Chart
Summary of code

60 Preparations for Next Week
Before next lab, read over Lab 11 in your lab manual, Pulse Width Modulation Next week’s lab will control a motor using the Pulse Width Modulation feature of the microprocessor.

61 Laboratory 11 – Pulse Width Modulation
Sample Code for Lab 11 #include <hidef.h> /* common defines and macros */ #include <mc9s12dt256.h> /* derivative information */ #include "pbs12dslk.h" #include "lcd.h" #pragma LINK_INFO DERIVATIVE "mc9s12dt256" volatile unsigned char duty = 50; void main(void) { int j,k; DDRP = 0x02; // DDRJ = 0x00; // PERJ = 0x80; PPSJ = 0x00; PIEJ = 0x80; PIFJ = 0x80; PTP_PTP1 = 1; PWMPRCLK = 0x07; PWMSCLA = 0x0F; PWMCLK_PCLK1 = 1; PWMPOL_PPOL1 = 1; PWMCAE_CAE1 = 0; PWMPER1 = 100;//equals 100 PWME_PWME1 = 1; PWMDTY1 = duty; //equals 50 LCDInit(); LCDClearDisplay(); LCDPutChar('5'); LCDPutChar('0'); EnableInterrupts; while (1) { switch(duty) { case 0: break; case 25: LCDPutChar('2'); case 50: case 75: LCDPutChar('7'); case 100: LCDPutChar('1'); } for(j=0;j<200;j++) for(k=0;k<200;k++); void interrupt 24 PORTJ_ISR() DisableInterrupts; duty += 25; duty = (duty > 100)? 0: duty; PWMDTY1 = duty;

62 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 It is a good idea to go over the remaining schedule at this point. Students will have to begin developing their ideas for the design project at this point. As can be seen in the sample program, a good way to use this lab to integrate previous labs and teach new concepts is to use interrupts to control the motor’s duty cycle and write to the LCD. It would be good to emphasize the practical application of PWM so students can see how useful the information learned here is. PWM is used in a variety of control mechanisms, and it is heavily used in motor control

63 Laboratory 11 PWM Description
There are a large number of hardware components and registers used to generate the PWM signal, so I created extra slides to go over the underlying hardware as well as each of the registers and describe how they all work together. This first PWM description slide is an overview of the PWM. The bus clock is the basis for the clock used in the PWM, but it is first altered in the clock select (explored in greater detail in the next slide). Also, 3 of the registers used are the Enable, Polarity Select, and Alignment registers. Those can be seen in this image in the hardware.

64 Laboratory 11 PWM Description
This second PWM slide shows the hardware of the clock select. The clock select is an important part of the PWM. It is used to control the frequency of the signal sent from the PWM. The bus clock enters and is scaled based on the value of a register (PWMPRCLK). Here it is scaled by a factor of two. Then, the clock is broken into clock A and clock B (Clock A runs to channels 0, 1, 4, and 5, Clock B runs to channels 2, 3, 6, and 7) Each clock can then be scaled again and result in Clock SA and SB (Scaled A and Scaled B). The PWMSCLA and PWMSCLB registers each scale their respective clocks based on the value sent to them. Each channel can then use either the scaled clock or the original clock for both A and B, based on the PWMCLK register. The registers will be covered in detail in the following slides.

65 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) These registers are the registers which select some key parts of the PWM clock features, including the enable, polarity select, clock select (scaled clocks, SA and SB, vs non-scaled clocks, A and B), and alignment (center align or left align). Each value is chosen to make the math and explanation simpler. We use left align because it is easier to conceptualize, SA because we will be scaling the clock heavily, high polarity because it simplifies the math later, and enable because we do wish to use the PWM.

66 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) These registers are all about clock scaling and creating the correct period. All of the information needed by your students to create a clock with the any possible frequency is given on this slide. This would be a good opportunity to ensure students are still listening by providing them a problem: take the 25 MHz bus clock and produce a frequency of 6104 Hz from clock SA (can hint that they should use the maximum scaling for PWMSCLA to accomplish this, or you can let them figure out the math)

67 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 The PWM control register

68 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 The hardware is relatively simple but has a few key elements that ensure that the lab runs correctly. First, it is important to note that the current amp also inverts the signal, so to make the math in our coding easier, we wire the output of the current amp to the negative terminal of the motor (the black wire). We also wire the positive wire for the motor to 3.3V because the motors are rated for a max voltage of 3.3V. If the motor is wired to 5V, it will shut off after running for a short time because it is over its recommended voltage. The final key element, wiring the common pin to +5V is necessary because (as seen on the circuit diagram) the common pin is connected to diodes. By raising the common voltage to 5V, we ensure that our signal being sent from the input pin to the output pin doesn’t go straight to ground through the diode.

69 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) Students should set up the PWM registers as discussed earlier in the slides, PWMPRCLK = 0x07; //scales clock A PWMSCLA = 0x0F; // scales clock SA PWMCLK_PCLK1 = 1; // selects clock SA PWMPOL_PPOL1 = 1; // selects high polarity PWMCAE_CAE1 = 0; // sets to left align PWMPER1 = 100; // allows us to use PWMDTY as a direct percentage representation of duty cycle PWMDTY1 = 0; //initializes duty cycle to 0

70 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 This summarizes the goals of the software. One difficult issue is writing multiple characters to the LCD. Students may need some hints for this (there is a brute force method listed in the code given and a more thoughtful approach created by mathematically determining each digit of the output based on the duty cycle (using the modulus function, etc))

71 Preparations for Next Week
Before next lab, read over Lab 6 in your lab manual, Serial Communications (SCI) Next week’s lab will simulate a serial connection. The program is set up differently than a normal program, so find one that is created correctly. The program file is a simulation and doesn’t communicate with the NI-ELVIS board at all.

72 Laboratory 6 – Serial Communications (SCI)
Sample Code for Lab 6 #include <hidef.h> /* common defines and macros */ #include <terminal.h> #include <inout.h> #include <stdio.h> #include <mc9s12dt256.h> /* derivative information */ #pragma LINK_INFO DERIVATIVE "mc9s12dt256" #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))) char TERMIO_GetChar(void){ while(1){ if((SCI.SCISR1 & 0x20) != 0x00){ return SCI.SCIDRL; } void TERMIO_PutChar(char c){ if((SCI.SCISR1 & 0x80) != 0x00){ break; SCI.SCIDRL = c; void main(void) { char letter; SCI.SCIBDL = 52; SCI.SCICR2 = 0x0C; SCI.SCICR1 = 0x00; TERMIO_PutChar('S'); TERMIO_PutChar('T'); TERMIO_PutChar('A'); TERMIO_PutChar('R'); letter = TERMIO_GetChar(); if(letter == 0x20){ TERMIO_PutChar('E'); TERMIO_PutChar(‘N'); TERMIO_PutChar(‘D'); TERMIO_PutChar(' '); }else{ TERMIO_PutChar((letter-32));

73 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 This lab is very different than other labs in a couple of ways. First, there is no communication with the board at all (no need to power it on or connect to it, no hardware components). Second, we are using a project that is set up for simulation, so it will have a “terminal” window in the debug interface. By clicking on the terminal window, we can use the keyboard as input and the terminal as output. The compiler (CodeWarrior) is able to simulate the hardware including register operation. By correctly simulating the registers, the timing, flags, etc will all work as if hardware were being used. This lab can be difficult for students to grasp, so it may be a good idea to go over the code in pretty fine detail for this lab.

74 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))) These three definitions in sequence perfectly simulate the SCI registers. We must use 0x00C8 for the address because that is the memory location in the microprocessor where the SCI registers are stored. The order of definitions in the structure is also the order of registers in memory. The order and memory location of the registers an essential component of the simulation. Finally, the definition for SCI means that in code, we can access registers by typing SCI.SCIBDH (as an example, this allows access to the simulated SCIBDH register)

75 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: 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. Baud Rate – symbols (or pulses) per second Question included to make sure students are paying attention. Answer: Set SCIBDL to decimal 52 (or equivalent hex or binary values) and SCIBDH to 0 to produce a baud rate of 9600 Hz

76 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) SCICR1 (SCI control register 1 – controls features not needed in our simulation, details listed in manual) Note that while SCISR1 bits 7 and 5 seem to indicate different things (one trips when transmit is EMPTY and the other trips when receive is FULL), they really are saying the same thing: The serial communication is READY to transmit or read data. (If transmit data register is empty, it is ready to have data written and then transmitted. If receive data register is full, it means data is received and ready to be read) The SCIDRL register is the data register used for both transmitting and receiving. It may help to conceptually think of SCIDRL as the terminal (in effect.) If set up properly in code, when a keyboard entry is detected, the character pressed is received in SCIDRL, and when you want to write to the terminal, simply write a character to SCIDRL and it will be transmitted to the terminal

77 Char TERMIO_GetChar(void) Wait until data received (using SCISR1)
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) This breaks down the code requirements fairly well. Also mention that students should indicate when program begins and ends by writing to the terminal. ASCII tables can be found online. You could suggest more fun and interesting ways to manipulate input. A number of students may try to use “if” statements in the functions. These don’t work because if the terminal is not ready to transmit or receive, it will simply not perform the requested function and will leave the function having done nothing. Because of this the program will not work. Instead, you must wait (using a while loop) until the flag is tripped, then transmit or receive.

78 Preparations for Next Week
Before next lab, read over Lab 10 in your lab manual, A/D and D/A Conversion Next week’s lab will combine labs 9 and 10. Since lab 10 in the manual contains all the information in lab 9 as well as the information needed in lab 10, the two labs can be combined in to one week, allowing students more time to work on their design labs.

79 Laboratory 9/10 – A/D and D/A Conversion
Sample Code for Lab 6 #include <hidef.h> /* common defines and macros */ #include <mc9s12dt256.h> /* derivative information */ #include "pbs12dslk.h" #include "lcd.h" #pragma LINK_INFO DERIVATIVE "mc9s12dt256" void write_dac(char upperbyte, char lowerbyte); void main(void) { char result_high, result_low; ATD0CTL2=0x80; ATD0CTL3=0x40; ATD0CTL4=0x0000; DDRM = 0x03; SPI1CR1 = 0x50; SPI1CR2 = 0x00; SPI1BR = 0x00; for(;;) ATD0CTL5 = ATD0CTL5_DJM_MASK; while(ATD0STAT0_SCF==0); result_high = ((ATD0DR0H & 0x03) << 2) | ((ATD0DR0 & 0xC0) >> 6); result_low = ((ATD0DR0 & 0x3F) << 2); write_dac(result_high,result_low); } volatile char temp; void write_dac(char upperbyte, char lowerbyte) PTM = 0x00; SPI1DR = upperbyte & 0x0F; while(SPI1SR_SPTEF == 0); SPI1DR = lowerbyte; temp = SPI1SR; temp = SPI1DR; PTM = 0x03;

80 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 ATD and DTA conversion is an essential component in many applications. It allows real world signals to be read in and manipulated with software. Software manipulation of signals is hugely important in radio, and almost everything having to do with communication. To use the oscilloscope, you need BNR cables, which can be picked up in the storage room on the second floor of Riggs The DTA chip is a small chip mounted on a green circuit board with 6 pins that can be put in to the breaboard.

81 Laboratory 9/10 Hardware Preparation
The images speak for themselves, for the function generator, wire to the pin labeled “FUNC OUT” in the segment of pins labeled function generator.

82 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) SPI uses Master/Slave communication routines. Setting SPI as the master allows us to control the communication sequence with the microcontroller SPICR2 includes features such as “buffer output”, “operate normally in wait mode”, and other features that are unneeded in our code. We don’t need to alter the baud rate for this lab, so baud rate register = 0x00.

83 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) Table refers to ATD0CTL3 *****A really good way to demonstrate that we are in fact sampling and altering the signal in code is to have your students change ATD0CTL4 to 0x001F when they have completed the lab. This decreases the sample rate to extremely slow sampling. When students initially complete the lab (with high sample rate), the output signal very closely mirrors the original signal. By slowing the sample rate, students can see that the signal is being reproduced so accurately only because of the high sample rate.

84 Wait until data is read (Sequence Complete Flag) Prepare and Send Data
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 The way the sync pin works is, when a 0 is sent, the DTA converter will convert whatever data is being sent. When 1 is being sent, the DTA converter will not convert any data. Preparing and sending data is gone over in more detail on the following slide

85 Data Received as 10 bits from ATD ATD0DR0H ATD0DR0L
Laboratory 9/10 Software Preparing Data Data Received as 10 bits from ATD ATD0DR0H ATD0DR0L AB CDEF GHIJ Data Trasmitted through SCI as 12 bits, so desired output looks like this: ABCD EFGH IJ00 In order to maintain uppermost bits, must shift bits. << (shift bits left) >> (shift bits right) & (binary AND) | (binary OR) Data is read in as 10 bits and should be read out as 12 bits. To maintain integrity of data, most significant bits should be read to and least significant bits ignored. To accomplish this, bit shifting is a required tool. This is a good opportunity for students to familiarize themselves with bit shifting SOLUTION: result_high = ((ATD0DR0H & 0x03) << 2) | ((ATD0DR0 & 0xC0) >> 6); result_low = ((ATD0DR0 & 0x3F) << 2); Then, send result_high and result_low to SPI data register

86 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; temp = SPI1SR; temp = SPI1DR; PTM = 0x03; } Explain code snippet to students: Set PTM to 0x00 in order to turn on DTA converter (sync pin) Send the upper byte of data Wait until flag indicates data was sent Send the lower byte of data Wait until flag indicated data was sent Reset status register (reset by reading it) Reset data register (reset by reading it) Set PTM to 0x03 to turn off the DTA converter (sync pin) NOTE: temp is a volatile character because it’s data is never used. Since the data is never used, the compiler will remove the variable to optimize the code unless it is declared as a volatile character.

87 Laboratory 9/10 Function Generator
This gives sample setting for the function generator. NOTE: the DTA converter clips the signal below 0V and above 5V. After further testing, the best wave to use for clear output is a sine wave with a frequency of 50 Hz, 3V peak to peak and a DC offset of 1.5V. This creates a signal entirely in the range of the DTA converter. NOTE2: Make sure you go back and change the value of ATD0CTL4 to 0x001F in order to create more digitized looking output.

88 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. Next week begins the final design lab! One suggestion is to break up the final design lab grade into a weighted combination of the final design presentation and the final design lab report. Perhaps 2/3 of the grade from getting the project working and 1/3 from the final lab report. Either way, final design labs should combine at least two previous labs in creative ways and the paper should be a more detailed version of a standard lab report, giving design details.

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


Download ppt "ECE 372 – Microcomputer Interfacing Laboratory"

Similar presentations

Ads by Google