Scope & Register Access
Scope Scope Region of a program in which a defined object is visible Defined Objects Variables Functions Two types of regions Blocks Not in a block 2 tj
Scope Program Prototype Blocks Statements enclosed in { } Contents of Main Contents of Functions Not in a Block Global Area // comments #include <stdio.h> int foo; int fun1(int x, int y); // function prototype int main(void){ int x; int y; Main s Area float a; if( ){ float x = a * 3; float a; float b; Nested Block Area } else b = x * y; } // end of main int fun1 (int i, int j){ int x; int y; } // end of fun1 Global Area Function fun1 Area 3 tj
Scope Scope An objects scope extends from it s declaration to the end of it s block Global Scope Any object defined in the global area of a program Visible anywhere in the current program Local Scope Any object defined in a block area Includes Main and Functions Visible anywhere in the current block 4 tj
Scope Scope Local definitions supersede global definitions within a block // example #include <stdio.h> int x; int y; These x, y are not the same as these int main(void){ int x; float y; } 5 tj
Scope Scope // comments #include <stdio.h> int foo; int fun1(int x, int y); int main(void){ int x; int y; float a; if( ){ float x = a * 3; float a; float b; } else b = x * y; } // end of main int fun1 (int i, int j){ int x; int y; } // end of fun1 // function prototype foo is visible here this a- is visible here but this is a new a- 6 tj
Scope Scope 7 tj
Scope Scope 8 tj
IO Register Access
IO Structure Controlled by a series of registers 10 tj
IO Structure Output Path Direction Register 1 for output Data Register 0 or 1 11 tj
IO Structure Input Path Direction Register 0 for input Input Register 0 or 1 12 tj
IO Structure Input Path w/ Pull-up Pull Up/Down Register 1 for Enable Direction Register 0 for input Data Register 1 for pull-up Input Register 0 or 1 13 tj
IO Structure Input Path w/ Pull-down Pull Up/Down Register 1 for Enable Direction Register 0 for input Data Register 0 for pull-down Input Register 0 or 1 14 tj
Register Set (simplified) 8 bit registers 1 bit for each of 8 I/O pins 8 I/O pins assigned to a port 10 ports 1 through 10 Register Names P port x port number function port function P2DIR Port 2 Direction Register 15 tj
Register Set (simplified) PxDIR Direction register 0 for input, 1 for output PxIN Input Data Register Holds the value of the input pin 0 or 1 PxOUT Output Data Register Holds the value for the output pin 0 or 1 Holds the value of the pull-up/down when pull ups/down enabled 0 for pull down, 1 for pull up PxREN Enable Pull-Up/Down Register 1 for enabled 16 tj
Register Set (advanced) PxDS Drive Strength Register 0 for regular strength, 1 for high drive strength PxSEL0, PxSEL1 Mode Select Register PxIE Interrupt Enable Register 1 for enable PxIES Interrupt Edge Select Register 0 for high -> low, 1 for low -> high PxIFG Interrupt Flag Register Set to 1 on selected edge transition PxIV Interrupt Vector Register 17 tj
IO Port Configuration portmap(2, OUTPUT); Pin 2 is actually Port 6, bit 0 Need to set this pin to an output P6DIR = 0x01; P6DIR = b00000001; P6DIR = 1; // set port 6 bit 0 to an output // set port 6 bit 0 to an output // set port 6 bit 0 to an output 18 tj
IO Port Configuration P6DIR = 0x01; P6DIR = b00000001; P6DIR = 1; // set port 6 bit 0 to an output // set port 6 bit 0 to an output // set port 6 bit 0 to an output These set the other 7 pins to inputs this may not be OK P6DIR = 0x01; // set port 6 bit 0 to an output P6DIR = babcdefgh b00000001 P6DIR = babcdefg1 Only the bit we want to change is changed 19 tj
IO Port Configuration portmap(3, INPUT); Pin 3 is actually Port 3, pin 2 P3DIR &= ~0x04; // set port 3 bit 2 to an input P3DIR &= ~b0000100; // set port 3 bit 2 to an input P3DIR = babcdefgh & ~b00000100 P6DIR = babcdefgh & b11111011 P6DIR = babcde0fg Only the bit we want to change is changed 20 tj
IO Port Configuration portmap(4, INPUT_PULLUP); Pin 4 is actually Port 3, pin 3 P3DIR &= ~0x08; // set port 3 bit 3 to an input P3OUT = 0x08; // prepare to make pullup P3REN = 0x08 // enable pull_x for port 3 pin 3 21 tj
IO Port Configuration portmap(5, INPUT_PULLDOWN); Pin 5 is actually Port 4, pin 1 P4DIR &= ~0x02; // set port 4 bit 1 to an input P4OUT &= ~0x02; // prepare to make pulldown P4REN = 0x02 // enable pull_x for port 4 pin 1 22 tj
IO usage Write a square wave to pin 5 to drive an LED #include msp.h P4DIR = 0x02; // set port 4 bit1 (pin 5) to an output P4OUT &= ~0x02; // initialize output to 0 while(1){ _delay_cycles(460000); P4OUT = 0x02; _delay_cycles(460000); P4OUT &= ~0x02; } // high // low 23 tj
IO usage Read from pin 5 as an input #include msp.h P4DIR &= ~0x02; P4REN &= ~0x02; // set port 4 bit 1 (pin 5) to an input // Ensure pullx not enabled while(1){ int foo; foo = P4IN & 0x02; // check only bit 1 // if high foo has the value 0x02 NOT 1 // or foo = (P4IN & 0x02) && 0x01; // if high foo has the value 0x01 } 24 tj
IO usage Make a decision based on pin 5 as an input #include msp.h P4DIR &= ~0x02; P4REN &= ~0x02; // set port 4 bit 1 (pin 5) to an input // Ensure pullx not enabled while(1){ if ((P4IN & 0x02)!= 0) //do this if high else // do this if low // or THIS WILL NOT WORK if ((P4IN & 0x02) == 1) //do this if high else // do this if low } if (P4IN & 0x02) //do this if high else // do this if low 25 tj