Cs201 Assignment No 1 Solution 2015 1040

Presentation on theme: "Engineering 1040: Mechanisms & Electric Circuits Winter 2015 Introduction to C Programming."— Presentation transcript:

1 Engineering 1040: Mechanisms & Electric Circuits Winter 2015 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. 1.For single lines://comment. 2.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. Declaration of global variables. Declaration of Function Prototypes. Function Declaration. Main function. //Header files #include //Declaring variables int a, b, c, par1, d; char zz, s3, temp; //Making prototypes int calc (int p); //Function int calc (int p) { p=p+1; //Add code return p; } //Main function void main(void) { //Add code a=calc(3); } 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 - Functions specific to the PIC #include - A/D converter functions. #include - Functions used for display by a LCD screen. #include - Delay functions in the program. #include - 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) { _asm goto _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 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 =200) { goforwardx(10); } Wall Follow Algorithm (1) Wall Left Sensor 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 =200) { goforwardx(10); } Wall Left Sensor

Enferno, - извиняясь, сказал Беккер.  - Я плохо себя чувствую.  - Он знал, что должен буквально вдавиться в пол. И вдруг увидел знакомый силуэт в проходе между скамьями сбоку.

0 Replies to “Cs201 Assignment No 1 Solution 2015 1040”

Lascia un Commento

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *