Question 1 : MCO556 For the program shell on the following page, fill in the blanks and add the code required to create a program which flashes LEDs. The LED flashing must be controlled from the keypad and a PC running Hyperterminal connected to the SCI. The hardware configuration is identical to the EVB board in lab A4070. The flash rate of LED2 must be approximately 1 Hz and is controlled by Timer Overflow interrupts. The flash rate of LED5 must be 0.5 Hz and is controlled by Timer Channel 1 operating in output compare mode. Timer configuration Prescale factor for clock to divide by 8 (required by keypad driver) Use Output Compare channel 1 (channel numbers start at 0) 50 Hz output square wave on Port T.1 Serial communications configuration: Use SCI0 2400 bps 8 data bits no parity 1 stop bit receive interrupts enabled LED operation The Timer Overflow interrupt will be used to create a time delay allowing the LED2 of PORTB to flash with a frequency of approximately 1 Hz (50% duty cycle). Flashing will be enabled or disabled from the keypad as described below: The Output Compare interrupt will be used to create a time delay allowing the LED5 of PORTB to flash with a frequency of 0.5 Hz (50% duty cycle). Flashing will be enabled or disabled from the Hyperterminal as described below: Start up operation On start up, LED 0, 5, and 7 must be ON and all other LEDs must be OFF. LED2 Control When a 0 is pressed on the keypad, PORTB LED2 will turn off. The remaining LEDs must not be modified. When a 1 is pressed on the keypad, PORTB LED2 will flash with a frequency of 1 Hz (50% duty cycle). The remaining LEDs must not be modified. When a 2 is pressed on the keypad, PORTB LED2 will turn on. The remaining LEDs must not be modified. LED5 Control When a 0 is pressed on the PC keyboard, PORTB LED5 will turn off. The remaining LEDs must not be modified. When a 1 is pressed on the PC keyboard, PORTB LED5 will flash with a frequency of 0.5 Hz (50% duty cycle). The remaining LEDs must not be modified. When a 2 is pressed on the PC keyboard, PORTB LED5 will turn on. The remaining LEDs must not be modified. Notes 1) Only the depression of the 0, 1 and 2 keys on the keypad and the PC keyboard must affect the operation of LED2 and LED5. All other inputs from the keypad and PC keyboard must have no effect on the operation of the program. 2) You will need separate task counters for the Output Compare and Timer Overflow interrupts. 3) Use volatile global variables to communicate between interrupts and main. 4) LEDs are numbered 0-7 with LED0 as the right-most. 5) Assume the keypad driver files have been added to the project. Remember the driver uses Timer channel 7 interupts. G VandeBelt 091-1 -
#include <hidef.h> /* common defines and macros */ #include <mc9s12dp256.h> // register descriptions #include keypad.h #define PERIOD // time between output compare interrupts // with prescale = 8 void TOC1open(void); /* sets up timer 1 for output compare */ void SCIopen(void); /* sets up SCI */ // global variables as required #pragma CODE_SEG NEAR_SEG NON_BANKED /* interrupt function will cause LED5 to flash at 0.5 Hz rate */ interrupt void TOC1_isr(void) G VandeBelt 091-2 -
/* interrupt function will cause LED2 to flash at approximately 1 Hz rate */ interrupt void TOF_isr(void) /* SCI receive interrupt to control LED5 */ interrupt void SCIrx_isr(void) #pragma CODE_SEG DEFAULT G VandeBelt 091-3 -
G VandeBelt 091-4 - MCO556 void main(void) // local variables as required DDRB = ; TSCR1 = ; TSCR2 = ; // data direction register PORT B as output // enable timer // set prescale for divide by 8 with interrupt // more initialization while(1) // do forever loop
/* initialize Timer Channel 1 for Output Compare interrupt */ void TOC1open(void) TIOS = ; TCTL1 = ; TCTL2 = ; TIE = ; TFLG1 = ; TC1 = ; // sets counts to current + required delay/ /* initialize SCI for 2400 bps 8 bit, no parity, 1 stop bit and receive interrupts */ void SCIopen(void) SCI0BRL = ; SCI0BRH = ; SCI0CR2 = ; G VandeBelt 091-5 -
Question 2: MCO556 For the program in question 1, fill in the blanks for the linker file shell shown below. NAMES END SECTIONS MY_RAM = READ_WRITE 0x1000 TO 0x2FFF; MY_PSEUDO_ROM = READ_ONLY 0x3000 TO 0x3FFF; END PLACEMENT _PRESTART, STARTUP, ROM_VAR, STRINGS, NON_BANKED,DEFAULT_ROM, COPY INTO MY_PSEUDO_ROM; DEFAULT_RAM INTO MY_RAM; END STACKSIZE 0x100 // data RAM // code RAM VECTOR ADDRESS VECTOR ADDRESS VECTOR ADDRESS VECTOR ADDRESS KPisr VECTOR ADDRESS Startup // Timer Overflow vector // Timer channel 1 vector // SCI 0 vector // keypad vector // reset vector G VandeBelt 091-6 -
Question 3 : MCO556 For the program shell on the following page, fill in the blanks and add the code required to create a program which converts an analog voltage on a selected ADC channel to an 8 bit digital value. Keyboard commands from a PC running Hyperterminal specified the ADC channel. The results of the conversion are displayed on the PC screen. Also messages are displayed on the LCD module. The hardware configuration is identical to the EVB board in lab A4070. Serial communications configuration: Use SCI0 2400 bps 8 data bits no parity 1 stop bit receive interrupts enabled A to D converter configuration: Use one of ADC channels 0, 1, 2 and 3 with 4 samples per conversion cycle 10 bit resolution, unsigned result, left justified Single conversion cycle minimum sample time maximum frequency clock On start up, the LCD module must be blank. ADC operation When a 0, 1, 2 or 3 is sent from Hyperterminal to the micro, a conversion for ADC channel 0, 1, 2 or 3 will be started. Also, the message Channel n must be displayed on the first line of the LCD module where n is the channel requested. The rest of the display must be blank. When the ADC conversion sequence is complete, the four conversions for the cycle must be average, converted to ASCII and sent to the PC for display on the screen. All other keyboard characters must be ignored by the program. Notes: 1) The ADC conversion function must be called from main. 2) Every message displayed on the PC screen must be on a separate line. The message must be sent only once when a key is pressed. The messages must be sent in main. 3) Assume the LCD driver files have been added to the project. Remember the LCD uses Timer channel 6 interrupts. Hints: Use the _itoa library function to convert the 8 bit value into an ASCII string. Calling procedure _itoa(value, buffer, 10); // where buffer contains storage for the string The ASCII code for carriage return is 0x0D and the code for line feed is 0x0A G VandeBelt 091-7 -
Question 3 : Answer MCO556 #include <hidef.h> /* common defines and macros */ #include <mc9s12dp256.h> // register descriptions #include <stdlib.h> #include lcd.h void SCIopen(void); void SCIputch(unsigned char Data); void SCIputs(unsigned char Str[]); void ADCopen(void); unsigned char ADCgetSample(unsigned char chan); // global variables as required #pragma CODE_SEG NEAR_SEG NON_BANKED /*serial receive interrupt function */ interrupt void SCIrx_isr(void) #pragma CODE_SEG DEFAULT void main(void) unsigned char buffer[10]; DDRB = ; TSCR1 = ; TSCR2 = ; // software initialization G VandeBelt 091-8 - // message buffer // more local variables as required // data direction register PORT B as output // enable timer // set prescale for 1 MHz clock to timer
Question 3 : Continued while(1) // do forever loop G VandeBelt 091-9 -
Question 3 : Continued MCO556 // Initialize SCI void SCIopen(void) SCI0BDL=0xD0; /* 8000000/(16*2400)=>2400 bps 8MHz is Bus clock*/ SCI0BDH=0x00; /* 0x00,0x00D0 is divisor to produce 2400 bps */ SCI0CR2= ; /* enable SCI Rx interrupts */ // Output character to SCI void SCIputch(unsigned char Data) // Output a string to the SCI // Str[] is the string to be output void SCIputs(unsigned char Str[]) // Initialize ADC void ADCopen(void) ATD0CTL2 = ; // enable ADC ATD0CTL4 = 0x03; // Get a sample from the ADC unsigned char ADCgetSample(unsigned int chan) ATD0CTL5 = ; while( ); G VandeBelt 091-10 - // start sequence // wait for conversion complete // then take average of 4 conversions return (ATD0DR0H+ATD0DR1H+ATD0DR2H+ATD0DR3H)/4;
Question 4: MCO556 A DC level is applied to ADC channel 2 (channel numbers start at 0) via the level-shifting circuit in the lab. Note that full scale input range is -10V to +10V. a)the ADC converts this to an 8 bit value of hexadecimal 0xD5. What is the input voltage? b) The input voltage is changed to 4.0 Volts, what will be the hexadecimal value read from the ADC? Question 5: SCI 0 of a HCS12 microcontroller is connected a PC running Hyperterminal. The serial communications parameters are: Serial communications configuration: Use SCI0 9600 bps 8 data bits no parity 1 stop bit receive interrupts enabled a) What hex values must be written to the Baud Rate registers and the control registers of the SCI to properly open the port? b) Using the values written to the baud rate registers, what is the actual baud rate of the communications link? (Show result to 4 digits of accuracy). G VandeBelt 091-11 -
MC9S12DP256B Timer System Registers TSCR1 TEN 0 0 0 0 0 0 0 TEN = 1 : Timer enabled TSCR2 TOI 0 0 0 0 PR2 PR1 PR0 TOI = 1 : Overflow interrupt enabled PR2-0 : Pre-scale value TCTL1 OM7 OL7 OM6 OL6 OM5 OL5 OM4 OL4 TCTL2 OM3 OL3 OM2 OL2 OM1 OL1 OM4 OL0 OMn OLn Action 0 0 No output 0 1 Toggle 1 0 Output 0 1 1 Output 1 TIOS IOS7 IOS6 IOS5 IOS4 IOS3 IOS2 IOS1 IOS0 IOSn = 1 : Output compare IOSn = 0 : Input Capture TIE C7I C6I C5I C4I C3I C2I C1I C0I CnI = 1 : Channel n interrupt enabled TFLG1 C7F C6F C5F C4F C3I C2F C1F C0F CnF = 1 : Channel n event Write a 1 to clear TCn Channel n 16 bit capture/compare register G VandeBelt 091-12 -
MC9S12DP256B ADC Registers ATD0CTL2 ADPU 0 0 0 0 0 0 0 ADPU : 0 = power down 1 = normal ATD0CTL5 ATDCTL5 DJM DSGN SCAN MULT 0 CC CB CC ADPU 0 0 0 0 0 0 0 DJM : 0 = left justified DSGN: 0 = unsigned SCAN: 0 = single sequence MULT: 0 = single channel CC CB CA: channel ATD0STAT0 SCF 0 0 0 0 0 0 0 SCF : 1 = conversion complete Cleared on write to ATD0CTL5 ATD0DRnH high byte of result data for Sample n MC9S12DP256B RTI Registers RTICTL 0 RTR6 RTR5 RTR4 RTR3 RTR2 RTR1 RTR0 RTR6-4 : RTI Pre-scale RTR3-0 : modulus (1-16) CRGINT RTIE 0 0 LOCKIE 0 0 SCMIE 0 RTIE : 1 = RTI interrupt enabled CRGFLG RTIF PORF 0 LOCKIF LOCK TRACK SCMIF SCM RTIF : 1 = RTI event G VandeBelt 091-13 -
MC9S12DP256B SCI Registers SCI0BDH 0 0 0 SBR12 SBR11 SBR10 SBR9 SBR8 SBR12 0 = CLK/(16 * BR) SCI0BDL SBR7 SBR6 SBR5 SBR4 SBR3 SBR2 SBR1 SBR0 SCI0CR2 TIE TCIE RIE ILIE TE RE RWU SBK SCI0SR1 TDRE TC RDRF IDLE OR NF FE PF TE = 1 : Transmitter enabled RE = 1 : Receiver enabled RIE = 1 : Receive interrupt enabled TDRE = 1 : Transmitter empty RDRF = 1 : Receiver full SCI0DRL Transmit and receive data register Function prototypes for LCD driver void LCDopen(void); // initialize LCD driver void LCDputs(const char *s); // output a string void LCDputch(char data); // output a character void LCDclear(void); // clear display void LCDhome(void); // move cursor to home position void LCDline1(void); // move cursor to start of line 1 void LCDline2(void); // move cursor to start of line 2 void LCDline3(void); // move cursor to start of line 3 void LCDline4(void); // move cursor to start of line 4 Function prototypes for keypad driver void KPopen(void); char KPhit(void); char KPgetch(void); void KPflush(void); // initialize keypad driver // has a new key been pressed? // wait for and get a key press // discard possible keypress G VandeBelt 091-14 -
MC9S12DP256B Interrupt Vectors G VandeBelt 091-15 -