DOC PREVIEW
NMT EE 308 - A software delay

This preview shows page 1-2-20-21 out of 21 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

EE 308 Spring 2009A software delay• To enter a software delay, put in a nested loop, just like in assembly.– Write a function delay(num) which will delay for num millisecondsvoid delay(unsigned int num){volatile unsigned int i; /* volatile so compiler does not optimize */while (num > 0){i = XXXX;/* ------------------------ */while (i > 0) /* */{ /* Want inner loop to delay */i = i - 1; /* for 1 ms */} /* *//* ------------------------ */num = num - 1;}}• What should XXXX be to make a 1 ms delay?1EE 308 Spring 2009• Look at assembly listing generated by compiler:0000206f <delay>:206f: 34 pshx2070: 7c 10 02 std 1002 <_.z>2073: 27 1e beq 2093 <delay+0x24>------------------------------------------------------------------------| 2075: 18 00 80 03 movw #XXX <__bss_size+0xXXX>, 0,SP| 2079: e8| 207a: ec 80 ldd 0,SP| 207c: 27 0a beq 2088 <delay+0x19>| ---------------------------------------------------| inner | 207e: ed 80 ldy 0,SPouter | loop | 2080: 1a 5f leax -1,Yloop | takes | 2082: 6e 80 stx 0,SP| 13 cycles | 2084: ec 80 ldd 0,SP| | 2086: 26 f6 bne 207e <delay+0xf>| ---------------------------------------------------| 2088: fc 10 02 ldd 1002 <_.z>| 208b: c3 ff ff addd #ffff <_stack+0xc3ff>| 208e: 7c 10 02 std 1002 <_.z>| 2091: 26 e2 bne 2075 <delay+0x6>------------------------------------------------------------------------2093: 30 pulx2094: 3d rts2EE 308 Spring 2009• Inner loop takes 13 cyles.• One millisecond takes 24,000 cycles(24,000,000 cycles/sec × 1 millisecond = 24,000 cycles)• Need to execute inner loop 24,000/13 = 1,846 times to delay for 1 millisecondvoid delay(unsigned int num){volatile unsigned int i; /* volatile so compiler does not optimize */while (num > 0){i = 1846;/* ------------------------ */while (i > 0) /* */{ /* Inner loop takes 13 cycles */i = i - 1; /* Execute 1846 times to */} /* delay for 1 ms *//* ------------------------ */num = num - 1;}}3EE 308 Spring 2009Program to increment LEDs connected to PORTB, and delay for 50 msbetween changes#include "hcs12.c"#define D_1MS (24000/13) // Inner loop takes 13 cycles// Need 24,000 cycles for 1 msvoid delay(unsigned int num);main(){DDRB = 0xff; /* Make PORTB output */PORTB = 0; /* Start with all off */while(1){PORTB = PORTB + 1;delay(50);}}void delay(unsigned int num){volatile unsigned int i; /* volatile so compiler does not optimize */while (num > 0){i = D_1MS;while (i > 0){i = i - 1;}num = num - 1;}}4EE 308 Spring 2009Program to display a particular pattern of lights on PORTB#include "hcs12.c"#define D_1MS (24000/13) // Inner loop takes 13 cycles// Need 24,000 cycles for 1 msvoid delay(unsigned int num);main(){const char table[] = {0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};int i;DDRB = 0xff; /* Make PORTB output */PORTB = 0; /* Start with all off */i = 0;while(1){PORTB = table[i];delay(100);i = i + 1;if (i >= sizeof(table)) i = 0; /* Start over when *//* end is reached */}}5EE 308 Spring 2009Operators in COperator | Action | example-----------------------------|---------------------------------------| | Bitwise OR | %00001010 | %01011111 = % 01011111& | Bitwise AND | %00001010 & %01011111 = % 00001010^ | Bitwise XOR | %00001010 ^ %01011111 = % 01010101~ | Bitwise COMP | ~%00000101 = %11111010% | Modulo | 10 % 8 = 2| ||| | Logical OR | %00000000 || %00100000 = 1&& | Logical AND | %11000000 && %00000011 = 1| %11000000 && %00000000 = 0Setting and Clearing Bits in Cassembly | C | action----------------------|-------------------------------|---------------------bset DDRB,$0F | DDRB = DDRB | 0x0f; | Set 4 LSB of DDRBbclr DDRB,$F0 | DDRB = DDRB & ~0xf0; | Clear 4 MSB of DDRB| |l1: brset PTB,$01,l1 | while ((PTB & 0x01) == 0x01) | Wait until bit clear| |l2: brclr PTB,$02,l2 | while ((PTB & 0x02) == 0x00) | Wait until bit setPointers in CTo read a byte from memory location 0xE000:var = *(char *) 0xE000;To write a 16-bit word to memory location 0xE002:*(int *) 0xE002 = var;6EE 308 Spring 2009Program to count the number of negative numbers in an array in memory/* Program to count the number of negative numbers in memory ** Start at 0xE000, go through 0xEFFF ** Treat the numbers as 8-bit **/#include "hcs12.h"unsigned int num_neg; /* Make num_neg global so we can find it in memory *//* Use type int so can hold value larger than 256 *//* Unsigned because number cannot be negative */main(){char *ptr,*start,*end;start = (char *) 0xE000; /* Address of first element */end = (char *) 0xEFFF; /* Address of last element */num_neg = 0;for (ptr = start; ptr <= end; ptr = ptr+1) {if (*ptr < 0) num_neg = num_neg + 1;}}7EE 308 Spring 2009MC9S12 Built-In Hardware• The MC9S12 has lots of useful pieces of hardware built into the chip.• Different versions of the MC9S12 have different pieces of hardware. Information aboutthe hardware modules is found in data sheet for the modules.• We are using the MCMC9S12DP256 chip.• Here is some of the hardware available on the MCMC9S12DP256:– General Purpose Input/Output (GPIO) Pins: These pins can be used toread the logic level on an MC9S12 pin (input) or write a logic level to an MC9S12pin (output). We have already seen examples of this – PORTA and PORTB. EachGPIO pin has an associated bit in a data direction register which you use to tellthe MC9S12 if you want to use the GPIO pin as input or output. (For example,DDRA is the data direction register for PORTA.)– Timer-Counter Pins: The MC9S12 is often used to time or count events. Forexample, to use the MC9S12 in a speedometer circuit you need to determine thetime it takes for a wheel to make one revolution. To keep track of the numb e rof people passing through a turnstile you need to count the numb e r of times theturnstile is used. To control the ignition system of an automobile you need tomake a particular spark plug fire at a particular time. The MC9S12 has hardwarebuilt in to do these tasks.∗ For information, see the ECT 16B8C Block User Guide.– Pulse Width Mo dulat io n (PWM) Pins: To make a motor turn at a particularspeed you need to send it a pulse width modulated signal. This is a signal at aparticular frequency (which diff ers for different motors), which is high for part ofthe period and low for the rest of the period. To have the motor turn slowly, thesignal might be high for 10% of the time and low for 90% of the time. To have themotor turn fast, the signal might be high for 90% of the time and low for 10% ofthe time.∗ For information, see the PWM 8B8C Block User Guide.8EE 308 Spring 2009– Serial


View Full Document

NMT EE 308 - A software delay

Documents in this Course
Load more
Download A software delay
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view A software delay and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view A software delay 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?