1 / 20

Elements of IC

Elements of IC. IC interface instructions file types, loading, debugging Data Objects data types, constants, variables, characters Expressions and Operators Statements Program Flow Control Functions main( ), library, yours LCD Printing Multi-Tasking and Processes

shay
Download Presentation

Elements of IC

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Elements of IC • IC interface instructions • file types, loading, debugging • Data Objects • data types, constants, variables, characters • Expressions and Operators • Statements • Program Flow Control • Functions • main( ), library, yours • LCD Printing • Multi-Tasking and Processes • Good Programming Practices

  2. Good Programming Practices • Use LOTS of /* Comments */ • task description is a good start • no storage penalty • Use Descriptive Names • constants, variables, & functions • Add more /* Comments */ • Never put number constants in code • /* Comments */ Really help you & the team. • Use Text Formatting • indents, capitalization, etc. • Debugging is easier with /* Comments */ • Use Functions Extensively • 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 • Program types • A-Program-File.c • Find-the-Wall.lis • persistent variables.c • constants.c • go-forward3.c • go-backward2.c • my-main.c • Location of files • IC Instructions • load file.c or file.lis • unload file.c [to get rid of main()] • list files, list functions, list globals • help • quit • Line Recall & Editing • , , etc.

  5. Variables & ConstantsType must be declared at 1st Use • Data or Number Types: • integer: 16 bits, ±32,767 int m=12; • long: 32 bits, ±232 or 2B long k=0L; • floating point, ±10±32; 7 decimal digits • float foo = 70.; • float foo = 70.0; • float foo = 7E1; • float Zero = 0.0; • Characters, 8-bit ASCII Code • ‘x’ yields its ASCII value, 120 • char string[ ] = “Hello World!”; • Arrays, a sequence of data elements • int foo[6]; elements 0 to 5 • int foo[ ] = {4, 0, 5, 13, 7, 127}; • foo[3] has value 13

  6. Variables & Constants • Names case sensitive • No spaces -- use under_score • Scope: • Global • declared outside a function • valid within all functions, all files. • Local: valid only within a function or {} • declared in that function, or as its argument. • Initialized when • new code loaded • main( ) is run • hardware reset (power on, or RESET) • Persistent Global Variables • Never initialized • persistent int m; • Place at beginning of code to preserve • Use for calibration values

  7. Operators • Assignment • a = a + 2; or a += 2; • Integers • Arithmetic: +, –, *, / • Increment & Decrement • b = a++; increment after assignment • b = ++a; increment before assignment • Boolean: 0 represents FALSE • OR ||, AND &&,, NOT ! • Comparison • >, <, ==, >=, <= • Bitwise Logic • OR |, AND &, XOR ^, NOT ~ • Long Integers: • Arithmetic, but No Division • Comparisons • Floating Point • Arithmetic & Comparisons; math functions

  8. Statements & Expressions • Operators act upon objects of a certain type and specify what is to be done to them. • Expressions combine variables and constants using operators to create new values. • Association and precedence: ( ), [ ] • Object types must match • Changing type: (float) 3 • Statements are expressions, assignments, function calls, or flow control statements which make up C programs. • Every statement ends with ; • Not required for function definition, before { • Group into meta-statement blocks with { } • Each inside statement needs a ; • No ; following the right brace • Local variables may be assigned

  9. Functions • A block of statements that performs a single, well defined task or computation. • Use of functions is critical to good programming practice, program development & debugging. • A function may return a value, & must be typed • void, int, long, float • Arguments optional, but must be typed. • One unique function: main( ) • Only one version may be loaded • Starts on reset, unless “Choose” • Example function definition: int square(int n) { return(n*n); } /* end of square() */

  10. 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. • Note the coercion of integer type to float • h, a, & b are variables local to hypotenuse() • Function Libraries • Floating point math; RoboBoard; Yours.

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

  12. Floating Point Functions • Trigonometric, angles in radians • float sin(float angle) • float cos(float angle) • float tan(float angle) • float atan(float tangent) • Log and Exponential • float log10(float num) • float exp10(float num) • float log(float num) • float exp(float num) • Other • float sqrt(float num) • (float) a ^ (float) b • (float) 2 ^ (float) 3 returns 8

  13. IC Library Functions • Motor Control • motor(p,s); forward(p); reverse(p); off(p); alloff(); ao(); motor_force(p); • Other Outputs • led_out0(n); led_out1(n); • digital_out(p,n); • External Sensor Input • digital(p); 0 <= p <= 7 & 30 • analog(p); 20 <= p <= 27 • On-Board Conditions • dip_switch(n); dip_switches(); robo_knob(); choose_button(); escape_button(); voltage(); • Time • reset_system_time(); seconds(); mseconds(); msleep(msec); sleep(sec);

  14. IC Library Functions • Shaft Encoding • enable_encoder(p); read_encoder(p); reset_encoder(p); disable_encoder(p); • digital ports 0, 1, 2, & 3 • Sound • beep(); set_beeper_pitch(freq); beeper_on(); beeper_off(); tone(freq, sec); play(song); • IR Beacon • set_ir_transmit_frequency(freq); set_ir_receive_frequency(freq); • freq = 100 or 125 • ir_transmit_on(); ir_transmit_off(); • ir_receive_on(); ir_counts(p); ir_receive_off(); • digital ports 2, 3, 4, & 5 • Process Functions

  15. Program Flow Control • while ( expression ) statement • if ( expression ) statement-1 else /* optional */ statement-2 • for ( expr-1; expr-2; expr-3 ) statement /* Example */ int n; for (n=0; n<100; n++) printf(“&d\n”, n) • break

  16. LCD Printing printf( “format-string”, arg-1, arg-2, … arg-N); • Format elements: • Character string: Hello World! • end of line: \n • Integer number in decimal: %d • Integer number in hex: %x • Integer number in binary: %b • low byte only • Integer (low byte) as ASCII: %c • Floating point number: %f • NO formatting for long type • Arguments: constants, variables, expressions. • Treated as a single line even in two-line LCD; truncated if too long.

  17. Multiple Processes • Processes can be created and destroyed dynamically to perform particular tasks with their own local variables. • Unique process id number: pid • Processes run sequentially for a given time, or “ticks” in milliseconds • default is 5 ticks • Processes can communicate through global variables • Commands • int start_process(funct( ), [ticks,] [stack,]); • int kill_process(pid); • 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(); 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() */

More Related