Elements of IC n IC interface instructions l




















- Slides: 20
Elements of IC n IC interface instructions l file types, loading, debugging n Data Objects l data types, constants, variables, characters n Expressions and Operators n Statements n Program Flow Control n Functions l main( ), library, yours n LCD Printing n Multi-Tasking and Processes n Good Programming Practices
Good Programming Practices n Use LOTS of /* Comments */ l task description is a good start l no storage penalty n Use Descriptive Names l constants, variables, & functions n Add more /* Comments */ n Never put number constants in code n /* Comments */ Really help you & the team. n Use Text Formatting l indents, capitalization, etc. n Debugging is easier with /* Comments */ n Use Functions Extensively n Did I mention use of /* Comments */ ?
/*********************** Meaningful Encounter with a Wall Program JFY, 9/17/99; Go forward until I hit wall; stop, reverse and stop. ************************/ /* GLOBAL CONSTANTS */ int L_MOTOR = 0; /* motor port #s */ int R_MOTOR = 1; int BUMPER_SWITCH = 0; /* digital port # */ int OPEN = 0; int CLOSED = 1; float DELAY = 0. 5; /* Back-up time */ void main() { Go_Forward(); while(digital(BUMBER_SWITCH)==OPEN){; } /* wait until I hit the wall */ alloff(); /* stop the motors */ beep(); Go_Backward(); sleep(DELAY); /* back away from wall a bit */ alloff(); printf(“I found the wall! n”); beep(); } /* END of main() */
IC Interface n Program types l A-Program-File. c l Find-the-Wall. lis • • • l persistent variables. c constants. c go-forward 3. c go-backward 2. c my-main. c Location of files n IC Instructions l load file. c or file. lis l unload file. c [to get rid of main()] l list files, list functions, list globals l help l quit n Line Recall & Editing l , , etc.
Variables & Constants Type must be declared at 1 st Use n Data or Number Types: l integer: 16 bits, ± 32, 767 int m=12; 32 or 2 B long k=0 L; l long: 32 bits, ± 2 ± 32; 7 decimal l floating point, ± 10 digits • • l float foo = 70. ; float foo = 70. 0; float foo = 7 E 1; float Zero = 0. 0; Characters, 8 -bit ASCII Code • ‘x’ yields its ASCII value, 120 • char string[ ] = “Hello World!”; n Arrays, a sequence of data elements l int foo[6]; elements 0 to 5 l int foo[ ] = {4, 0, 5, 13, 7, 127}; • foo[3] has value 13
Variables & Constants n Names case sensitive l No spaces -- use under_score n Scope: l Global • declared outside a function • valid within all functions, all files. l Local: valid only within a function or {} • declared in that function, or as its argument. n Initialized when l new code loaded l main( ) is run l hardware reset (power on, or RESET) n Persistent Global Variables l Never initialized l persistent int m;
Operators n Assignment l a = a + 2; or a += 2; n Integers l Arithmetic: +, –, *, / l Increment & Decrement • b = a++; increment after assignment • b = ++a; increment before assignment l Boolean: 0 represents FALSE • OR ||, AND &&, , NOT ! l Comparison • >, <, ==, >=, <= l Bitwise Logic • OR |, AND &, XOR ^, NOT ~ n Long Integers: l Arithmetic, but No Division l Comparisons n Floating Point l Arithmetic & Comparisons; math functions
Statements & Expressions n Operators act upon objects of a certain type and specify what is to be done to them. n Expressions combine variables and constants using operators to create new values. l Association and precedence: ( ), [ ] l Object types must match • Changing type: (float) 3 n Statements are expressions, assignments, function calls, or flow control statements which make up C programs. l Every statement ends with ; • Not required for function definition, before { l Group into meta-statement blocks with { } • Each inside statement needs a ; • No ; following the right brace
Functions n A block of statements that performs a single, well defined task or computation. n Use of functions is critical to good programming practice, program development & debugging. n A function may return a value, & must be typed l void, int, long, float n Arguments optional, but must be typed. n One unique function: main( ) l Only one version may be loaded l Starts on reset, unless “Choose” n Example function definition: int square(int n) { return(n*n); } /* end of square() */
n Functions within functions: float hypotenuse(int a, int b) { /* define local variable */ float h; h = sqrt( (float) [square(a) + square(b)] ); return(h); } /* end of hypotenuse() */ l l l n sqrt() is a pre-defined library function of type float with argument type float. Note the coercion of integer type to float h, a, & b are variables local to hypotenuse() Function Libraries l Floating point math; Robo. Board; Yours.
Function Libraries n IClibsrobo. lis l floating point math functions l Input & Output functions l IR beacon functions l Time and Process functions l Sound functions n IClibsencoders. lis l shaft encoder functions n Test Programs: IClibs l testports. lis l QT. c n Your Function Definitions l In IClibs or l ICmy-lib [specify directory] l IClibsmy-lib [specify directory]
Floating Point Functions n Trigonometric, angles in radians l float sin(float angle) l float cos(float angle) l float tan(float angle) l float atan(float tangent) n Log and Exponential l float log 10(float num) l float exp 10(float num) l float log(float num) l float exp(float num) n Other l float sqrt(float num) l (float) a ^ (float) b • (float) 2 ^ (float) 3 returns 8
IC Library Functions n Motor Control l motor(p, s); forward(p); reverse(p); off(p); alloff(); ao(); motor_force(p); n Other Outputs l led_out 0(n); led_out 1(n); l digital_out(p, n); n External Sensor Input l digital(p); 0 <= p <= 7 & 30 l analog(p); 20 <= p <= 27 n On-Board Conditions l dip_switch(n); dip_switches(); robo_knob(); choose_button(); escape_button(); voltage(); n Time l reset_system_time(); seconds(); msleep(msec); sleep(sec);
IC Library Functions n Shaft Encoding l enable_encoder(p); read_encoder(p); reset_encoder(p); disable_encoder(p); l digital ports 0, 1, 2, & 3 n Sound l beep(); set_beeper_pitch(freq); beeper_on(); beeper_off(); tone(freq, sec); play(song); n IR Beacon l set_ir_transmit_frequency(freq); set_ir_receive_frequency(freq); • freq = 100 or 125 l l ir_transmit_on(); ir_transmit_off(); ir_receive_on(); ir_counts(p); ir_receive_off(); • digital ports 2, 3, 4, & 5 n Process Functions
Program Flow Control n while ( expression ) statement n if ( expression ) statement-1 else /* optional */ statement-2 n for ( expr-1; expr-2; expr-3 ) statement /* Example */ int n; for (n=0; n<100; n++) printf(“&dn”, n) n break
LCD Printing printf( “format-string”, arg-1, arg-2, … arg-N); n Format elements: l Character string: Hello World! l end of line: n l Integer number in decimal: %d l Integer number in hex: %x l Integer number in binary: %b • low byte only l l l Integer (low byte) as ASCII: %c Floating point number: %f NO formatting for long type n Arguments: constants, variables, expressions. n Treated as a single line even in twoline LCD; truncated if too long.
Multiple Processes n Processes can be created and destroyed dynamically to perform particular tasks with their own local variables. l Unique process id number: pid n Processes run sequentially for a given time, or “ticks” in milliseconds l default is 5 ticks n Processes can communicate through global variables n Commands l int start_process(funct( ), [ticks, ] [stack, ]); l int kill_process(pid); l void defer();
/************ Quick Test of Robo. Board, JFY 9/99; A version of "testports" that doesn't take so many button pushes. **********/ /* Global Definitions */ float DELAY = 0. 5; /* A timing delay parameter */ int BEACON_PORT = 2; /* port for IR sensor test */ int ON = 1; int OFF = 0; int n; /* General counter variable */ void main() { beep(); printf("Quick Test: Push Choose to Startn"); clear_board(); wait_for_choose(); /* Wait for Choose button to start; tests beeper, LCD, and Choose */
motor_test(); LED_test(); beacon_test(); digital_port_test(); analog_port_test(); Robo. Knob_test(); DIPswitch_test(); printf("End: Quick Testn"); } /* End of main() */ void clear_board() /* Turn off motors, LEDs, and beacon. */ { ao(); led_out 0(OFF); led_out 1(OFF); ir_transmit_off(); ir_receive_off(); } /* end clear_board() */ void wait_for_choose() /* Debounce "choose" button */ { while(!choose_button()); while(choose_button()); } /* End wait_for_choose() */
void digital_port_test() /* Read all the digital ports in sequence until "choose" */ { printf("Digital Ports Test: Choosen"); wait_for_choose(); while(!choose_button()) {for(n=0; n<8; n++) {printf("Port: %d = %dn", n, digital(n)); sleep(2. 0*DELAY); if(choose_button()) break; /* don't wait whole loop */ } /* end "for" */ } /* end "while" */ printf("End Digital Ports Testn"); sleep(2. 0*DELAY); } /* end digital_port_test() */