Presentation is loading. Please wait.

Presentation is loading. Please wait.

MSP430 Intro. Why embedded systems? Big bang-for-the-buck by adding some intelligence to systems. Embedded Systems are ubiquitous. Embedded Systems more.

Similar presentations


Presentation on theme: "MSP430 Intro. Why embedded systems? Big bang-for-the-buck by adding some intelligence to systems. Embedded Systems are ubiquitous. Embedded Systems more."— Presentation transcript:

1 MSP430 Intro

2 Why embedded systems? Big bang-for-the-buck by adding some intelligence to systems. Embedded Systems are ubiquitous. Embedded Systems more common as prices drop, and power decreases.

3 Which Embedded System? We will use Texas Instruments MSP-430 + very popular (#2 next to microchip) + 16 bits (instead of 8) + low power + clean architecture + low cost (free) development tools - relatively low speed/capacity (i.e., no video or fancy audio) - low level tools (compared to Stamp, Arduino…) - 16 bits (instead of 32)

4 Today Brief overview of o logic o numbers o C o MSP430 digital I/O

5 Brief Review of Logic Logic circuits are either true (logical 1, for us this is 3.3V) or false (logical 0, for us this is 0V). To deal with these signal levels we develop a special form of mathematics, Boolean algebra.

6 Boolean Operators Operators: o AND : C=A·B (Read as C equals A and B). Input AInput BOutput C 000 010 100 111 logic gate images adapted from: http://www.ee.surrey.ac.uk/Projects/Labview/gatesfunc/index.html Sometimes the “·” is dropped: C=AB “Truth Table”

7 More logic… ABD 000 011 101 111 AF 01 10 ABH 001 010 100 110

8 … and even more. ABG 001 011 101 110 ABJ 000 011 101 110 eXclusive OR Note: XOR with B=1 inverts bit (J=~A), XOR with 0 passes bit (J=A).

9 Number Systems Binary: 00001101 2 = 1·2 3 + 1·2 2 + 0·2 1 + 1·2 0 = 8 + 4 + 1 = 13 Hex: 00101010 2 = 2A 16 = 0x2A = 2·16 1 + 10·16 0 = 32 + 10 = 42 (check 1·2 5 + 1·2 3 + 2·2 1 = 32 + 8 + 2 = 42) 8 bits = 1 byte 0000 0000 2 → 1111 1111 2 0x00 → 0xff 0 → 2 8 -1=255 ( or -128 → 127 ( -(2 7 ) → 2 7 -1) ) ) 16 bits = 2 bytes = 1 word 0000 0000 0000 0000 2 → 1111 1111 1111 1111 2 0x0000 → 0xffff 0 → 2 16 -1=65535 ( or -32768 → 32767 ( -(2 15 ) → 2 15 -1) ) ) 4 bits = 1 nybble(0 → 2 4 -1=15) DecimalBinaryHex 000000 100011 200102 300113 401004 501015 601106 701117 810008 910019 101010A 111011B 121100C 131101D 141110E 151111F

10 Basic Architecture of MSP430 (from Family User’s Guide)

11 MSP430G2533 (from device specific datasheet)

12 MSP430FG2533, 20 pin DIP (from datasheet)

13 Digital I/O (Family User’s Guide )

14 Some register functions (Family User’s Guide)

15 A typical I/O pin PxSELx=0, by default PxREN=0, by default Analog switch Inverted input Electronically controlled digital switch 2-1 Mux (multiplexer) Tri-State Output Schmitt trigger (hysteresis) Active low Transparent latch 4-1 Mux (multiplexer)

16 Effect of P1DIR 0 0 0 0 0 0 0 Hi-Z, Pin is input Input=q q P1IN.0=q 1 1 P1OUT.0=r Hi-Z r Output=r 0 Also – P1REN P1SEL, and P1SEL2 = 0 for I/O buffer enabled, pin is output 1 buffer enabled

17 #include void main(void) { volatile int i; WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer P1DIR |= 0x01; // Set P1.0 to output direction while (1) { //Do this forever P1OUT = P1OUT | 0x01; // Set P1.0 with “or”, | for (i=0; i<0x5000; i++) {}// Delay P1OUT = P1OUT & ~0x01;// Clear P1.0 for (i=0; i<0x5000; i++) {}// Delay } A simple C program Constants associated with our chip Every program needs a “main” routine (between braces) Don’t worry about for now. Set bit 0 in “P1DIR” - this makes it an output (next page). “1” is always true, so loop forever. Set bit 0 high (connected to LED) Loop to waste time Set bit 0 low (LED turns off) Loop to waste time Also note that every statement ends with “;” or “}” Comments start with “//” and go to end of line. Declare “i” as volatile so compiler doesn’t optimize it out of existence (or turn optimizations off). All variables must be declare before they are used.

18 Variant 1 (more readable) #include #define LED0x01 void main(void) { volatile int i; WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer P1DIR |= LED; // Set P1.0 (LED bit) to output while (1) { //Do this forever P1OUT |= LED; // Turn on LED for (i=0; i<0x5000; i++) {}// Delay P1OUT &= ~0x01;// Turn off LED for (i=0; i<0x5000; i++) {}// Delay } Give constants meaningful names. Equivalent to: P1OUT = P1OUT | LED;

19 Variant 2 (macros) #include #define LEDBIT0 #define SETBIT(p,b) (p |= (b)) #define CLRBIT(p,b) (p &= ~(b)) void main(void) { volatile int i; WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer P1DIR |= LED; // Set P1.0 to output direction while (1) { //Do this forever SETBIT(P1OUT,LED); // Set P1.0 for (i=0; i<0x5000; i++) {}// Delay CLRBIT(P1OUT,LED); // Clear P1.0 for (i=0; i<0x5000; i++) {}// Delay } } Use Macros sparingly, but they can make code look much cleaner (see below) Expands to: (P1OUT |= (0x01)) Note “;” must be added. Can call bits by location

20 Variant 3 (shorter) #include #define LED BIT0 #define SETBIT(p,b) (p |= (b)) #define CLRBIT(p,b) (p &= ~(b)) #define TGLBIT(p,b) (p ^= (b)) void main(void) { volatile int i; WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer P1DIR |= LED; // Set P1.0 to output direction while (1) { //Do this forever TGLBIT(P1OUT,LED); // Toggle LED for (i=0; i<0x5000; i++) {}// Delay } New macro to toggle (xor) bit Loop is half as long as before

21 C Data Types (that we will use) TypeSize (bits) RepresentationMinimumMaximum char, signed char8ASCII-128+127 unsigned char, bool8ASCII0255 int, signed int162s complement-32 76832 767 unsigned int16Binary065 535 long, signed long322s complement-2 147 483 648 2 147 483 647 unsigned long32Binary 0 4 294 967 295 enum162s complement-32 76832 767 float32IEEE 32-bit±1.175 495e-38±3.40 282 35e+38

22 C Operators (Arithmetic) Arithmetic Operator nameSyntax Basic assignmenta = b Additiona + b Subtractiona - b Unary plus+a+a Unary minus (additive inverse)-a-a Multiplicationa * b Divisiona / b Modulo (remainder)a % b Increment Prefix++a Suffixa++ Decrement Prefix--a Suffixa--

23 More C Operators (Relational, Logical, Bitwise and Compound) Relational Operator nameSyntax Equal toa ==b Not equal toa !=b Greater thana > b Less thana < b Greater than or equal toa >=b Less than or equal toa <=b Logical Operator nameSyntax Logical negation (NOT)!a!a Logical ANDa &&b Logical ORa ||b Bitwise Operator nameSyntax Bitwise NOT~a~a Bitwise ANDa & b Bitwise ORa | b Bitwise XORa ^ b Bitwise left shifta <<b Bitwise right shifta >>b Compound Operator nameSyntax Addition assignmenta += b Subtraction assignmenta -= b Multiplication assignmenta *= b Division assignmenta /= b Modulo assignmenta %= b Bitwise AND assignmenta &= b Bitwise OR assignmenta |= b Bitwise XOR assignmenta ^= b Bitwise left shift assignmenta <<=b Bitwise right shift assignmenta >>=b

24 More C Statements a simple statement is a single statement that ends in a “;” a compound statement is several statements inside braces: { simple statement; … simple statement; } Indenting There are no rules about indenting code, but if you don’t adopt a standard style, your code becomes unreadable.

25 Even more C Array definition int a [100]; //Array elements are a[0] to a[99]. Don’t use a[100]! if…then…else if ( ) else if…then if ( ) may be a compound statement. if…then…else (shorthand) x = (y > 2) ? 3 : 4; // if y>2, then x=3, else x=4.

26 Yet more C Iteration (do…while while… for…) do while ( ); while ( ) for ( ; ; ) for (e1; e2; e3) s; is equivalent to e1; while (e2) { s; e3; } The break statement is used to end a for loop, while loop, do loop, or switch statement. Control passes to the statement following the terminated statement. Recall: for (i=0; i<0x5000; i++) {}// Delay

27 Again with the C switch (one choice of many) switch ( ) { case : case : break; default : } is compared against the label, and execution of the associated statements occur (i.e., if is equal to, are exectuted. No two of the case constants may have the same value. There may be at most one default label. If none of the case labels are equal to the expression in the parentheses following switch, control passes to the default label, or if there is no default label, execution resumes just beyond the entire construct. Switch statements can "fall through", that is, when one case section has completed its execution, statements will continue to be executed downward until a break; statement is encountered. This is usually not wanted, so be careful

28 Material taken from: http://en.wikipedia.org/wiki/C_syntax http://en.wikipedia.org/wiki/Indent_style http://en.wikipedia.org/wiki/Operators_in_C_and_C++ http://www.ti.com/lit/ug/slau144i/slau144i.pdf (Family User’s Guide; 658 pages) http://www.ti.com/lit/ug/slau144i/slau144i.pdf http://www.ti.com/lit/ds/symlink/msp430g2553.pdf (Datasheet; 70 pages) http://www.ti.com/lit/ds/symlink/msp430g2553.pdf


Download ppt "MSP430 Intro. Why embedded systems? Big bang-for-the-buck by adding some intelligence to systems. Embedded Systems are ubiquitous. Embedded Systems more."

Similar presentations


Ads by Google