1 / 20

Engineering 1040: Mechanisms & Electric Circuits Fall 2011

Engineering 1040: Mechanisms & Electric Circuits Fall 2011. Introduction to C Programming. Basic Rules When Programming in C Language. At the end of each line with instructions a semi-colon (;) has to be placed. Eg: a=a+3; There are two methods of indicating comments.

hang
Download Presentation

Engineering 1040: Mechanisms & Electric Circuits Fall 2011

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. Engineering 1040: Mechanisms & Electric Circuits Fall 2011 Introduction to C Programming

  2. Basic Rules When Programming in C Language • At the end of each line with instructions a semi-colon (;) has to be placed. Eg: a=a+3; • There are two methods of indicating comments. • For single lines: //comment. • For multiple lines: /* This is comment 1; This is comment 2; */. • Functions, statements, etc., should be between { }. Eg: void main(void) //Function { //Add code }

  3. The Structure of a C Program //Header files #include <18f4550.h> //Declaring variables int a, b, c, par1, d; charzz, s3, temp; //Making prototypes int calc (intp); //Function int calc (intp) { p=p+1; //Add code return p; } //Main function void main(void) { //Add code a=calc(3); } • Header files. • Declaration of global variables. • Declaration of Function Prototypes. • Function Declaration. • Main function. Main function is the most important part of the C program. Most of the time we make modifications in main function

  4. Header files • Header files include the standard functions that will used elsewhere in the program. • Text from the specified file is used at this point of the compilation. • These will call the library functions. #include < 18f4550.h > - Functions specific to the PIC #include < adc.h > - A/D converter functions. #include < xlcd.h > - Functions used for display by a LCD screen. #include < delays.h > - Delay functions in the program. #include < stdio.h > - Standard input and output functions. • All header files used in standard C programming is valid in microcontroller programming .

  5. Variable types (1)

  6. Variable types (2) • Possible ways to give variable a the decimal value 15: a = 15; //Decimal a = 0b0000 1111; //Binairy a = 0x0F; //Hexadecimal

  7. Operators (1) • Relational operators • Bit operators

  8. Operators (2) • Mathematical operators • Increasing and decreasing: x--; //This is the same as x = x – 1; x++; //This is the same as x = x + 1; • Example: int a, b, c; a = 19; //a 00010011 b = 6; //b 00001110 c = a & b; //c 00000010 -> 2

  9. Statements (1) • IF...ELSE if (a==0) //If a is similar to 0... { b++; //...than increase b by 1 } else //otherwise { b--; //decrease b by 1 } • WHILE while (a>3) //As long as a is higher than 3 { b = b + 3; //Add 3 to b a--; //Decrease a by 1 }

  10. Statements (2) • FOR for (i = 0 ; i < 100 ; i++) //From i=0 to i=100 with step size 1 { b++; //Increase b by 1 a--; //Decrease a by 1 }

  11. Bootloader • When a bootloader is used, a special part of code has to be included in the program, after the #include statements. //Always use this code when using with a boot oader #pragma code _RESET_INTERRUPT_VECTOR = 0x000800 void _reset (void) { _asmgoto _startup _endasm } #pragma code #pragma code _HIGH_INTERRUPT_VECTOR = 0x000808 void _high_ISR (void) { ; } #pragma code #pragma code _LOW_INTERRUPT_VECTOR = 0x000818 void _low_ISR (void) { ; } #pragma code //End bootloader code

  12. Programming Pins & Ports (1) • If PORTB is used for writing to and reading from pins on port B. • Writing a, • ‘1’ to one of the bits of PORTB results in a high voltage on the output pin. • ‘ 0’ will result in a low voltage. • The MSB (most significant bit, the most left one) is used for pin B7, the LSB for pin B0.

  13. Programming Pins & Ports (2) • Declaring all the pins of a port: • Declaring a specific pin of a port to high or low:

  14. Programming Pins & Ports (3) • Instead of using PORT the command LAT could also be used in declaring pins. Example: • LATD = 0x00; // Set all port D to low • LATCbits.LATC0 = 0; //Set pin C0 to low • LATCbits.LATC1 = 1; //Set pin C1 to high

  15. Programming Pins & Ports (4) • Declaring a port as an output port: • Use ‘1’ for Input • Use ‘0’ for Output Example: • TRISD = 0b11111111; //Set all pins in port D as inputs • TRISD = 0b00000000; //Set all pins in port D as outputs • TRISD = 0b10101010; //Set pins 0,2,4,6 of port D as outputs //Set pins 1,3,5,7 of port D as inputs

  16. Coding the Wall-Following Mobile Robot Platform • goForward(void) –go forward by 1 step. • turnRight(int stepNum) – turn to the right side by the angle specified by the integer stepNum • readFrontSensor(void) – read the value of the front sensor Homework: • Using the above given functions write a code in C language, • Write a function for the robot to turn left (call this function turnLeft(int stepNum) ) • Write a function for reading the left sensor (call this function readLeftSensor (int stepNum) ) • Write a function for the robot to move forward a number of steps given by x (call this function goForwardx( (int x) )

  17. void turnRight(int stepNum) { int i = 0; while(I < stepNum) { rotateCW(2); // Rotate motor two by one step rotateCW(1); // Rotate motor by one step Delay1KTCYx(50); // Changing the argument in the delay function will change the speed at which // the motor rotates. Delay equals 1000 x 50 x 1/12 = 4.167 ms i++; LATD = 0x00; // Set all port D to 0 } return; }

  18. void goForward(void){ rotateCCW(2); // Rotate motor two step rotateCW(1); // Rotate motor one step Delay1KTCYx(50); // Changing the argument in the delay function will change the // speed at which the motor rotates. Delay equals 1000 x 50 x // 1/12 = 4.167 ms LATD = 0x00; // Set all port D to 0 }

  19. Wall Follow Algorithm (1) Write a pseudo code for the “wallee” robot to follow a wall on the left hand side using the left sensor. If (leftsensor > sensorreading_close) { rightturn(10); goforwardx(5); } If (leftsensor < sensorreading_far) { leftturn(10); goforwardx(5); } else (leftsensor <= sensorreading_close) && (l sensorreading_far > =200) { goforwardx(10); } Left Sensor Wall Sensorreading_close Sensorreading_far

  20. Wall Follow Algorithm (2) • Write a pseudo code for the “wallee” robot to follow a wall on the left hand side using the left sensor. If (leftsensor > 400) { rightturn(10); goforwardx(5); } If (leftsensor < 200) { leftturn(10); goforwardx(5); } else (leftsensor <= 400) && (leftsensor > =200) { goforwardx(10); } Left Sensor Wall

More Related