Presentation is loading. Please wait.

Presentation is loading. Please wait.

Elements of IC n IC interface instructions l file types, loading, debugging n Data Objects l data types, constants, variables, characters n Expressions.

Similar presentations


Presentation on theme: "Elements of IC n IC interface instructions l file types, loading, debugging n Data Objects l data types, constants, variables, characters n Expressions."— Presentation transcript:

1 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

2 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 */ ?

3 /********************************************** 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(); beep(); beep(); }/* END of main() */

4 IC Interface n Program types l A-Program-File.c l Find-the-Wall.lis persistent variables.c constants.c go-forward3.c go-backward2.c my-main.c l 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.

5 Variables & Constants Type must be declared at 1st Use n Data or Number Types: l integer: 16 bits, ±32,767 int m=12; l long: 32 bits, ±2 32 or 2B long k=0L; l floating point, ±10 ±32 ; 7 decimal digits float foo = 70.; float foo = 70.0; float foo = 7E1; float Zero = 0.0; l 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

6 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; l Place at beginning of code to preserve l Use for calibration values

7 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

8 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 Local variables may be assigned

9 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() */

10 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() */ sqrt() is a pre-defined library function of type float with argument type float. l Note the coercion of integer type to float l h, a, & b are variables local to hypotenuse() n Function Libraries l Floating point math; RoboBoard; Yours.

11 Function Libraries n IC\libs\robo.lis l floating point math functions l Input & Output functions l IR beacon functions l Time and Process functions l Sound functions n IC\libs\encoders.lis l shaft encoder functions n Test Programs: IC\libs\ l testports.lis l QT.c n Your Function Definitions l In IC\libs\ or l IC\my-lib [specify directory] l IC\libs\my-lib [specify directory]

12 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 log10(float num) l float exp10(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

13 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_out0(n); led_out1(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(); mseconds(); msleep(msec); sleep(sec);

14 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 ir_transmit_on(); ir_transmit_off(); l ir_receive_on(); ir_counts(p); ir_receive_off(); digital ports 2, 3, 4, & 5 n Process Functions

15 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(“&d\n”, n) n break

16 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 Integer (low byte) as ASCII: %c l Floating point number: %f l NO formatting for long type n Arguments: constants, variables, expressions. n Treated as a single line even in two-line LCD; truncated if too long.

17 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();

18 /************************ Quick Test of RoboBoard, 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 Start\n"); clear_board(); wait_for_choose(); /* Wait for Choose button to start; tests beeper, LCD, and Choose */

19 motor_test(); LED_test(); beacon_test(); digital_port_test(); analog_port_test(); RoboKnob_test(); DIPswitch_test(); printf("End: Quick Test\n"); }/* End of main() */ void clear_board() /* Turn off motors, LEDs, and beacon. */ { ao(); led_out0(OFF); led_out1(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() */

20 void digital_port_test() /* Read all the digital ports in sequence until "choose" */ { printf("Digital Ports Test: Choose\n"); wait_for_choose(); while(!choose_button()) {for(n=0;n<8;n++) {printf("Port: %d = %d\n", n, digital(n)); sleep(2.0*DELAY); if(choose_button()) break; /* don't wait whole loop */ }/* end "for" */ }/* end "while" */ printf("End Digital Ports Test\n"); sleep(2.0*DELAY); }/* end digital_port_test() */


Download ppt "Elements of IC n IC interface instructions l file types, loading, debugging n Data Objects l data types, constants, variables, characters n Expressions."

Similar presentations


Ads by Google