1 / 32

C Summary

C Summary. Wilmer Arellano. References. Excerpts from the book: Predko, Myke. (2005). 123 PIC MICROCONTROLLER EXPERIMENTS FOR THE EVIL GENIOUS. USA: McGraw-Hill. ISBN: 0-07-145142-0. Variable Declaration Statements. int VariableName; VariableName is a label and can start with any:

mura
Download Presentation

C Summary

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. C Summary Wilmer Arellano

  2. References • Excerpts from the book: • Predko, Myke. (2005). 123 PIC MICROCONTROLLER EXPERIMENTS FOR THE EVIL GENIOUS. USA: McGraw-Hill. ISBN: 0-07-145142-0

  3. Variable Declaration Statements • int VariableName; • VariableName is a label and can start with any: • upper- or lowercase letter or • the underscore character. • After the starting character, the rest of the label can be any letter, number, or underscore character. Blank characters cannot be used in a label. • int time;

  4. For standard variables, two options exist that you should be aware of. The first is the ability to initialize the variable when it is declared. By adding an equal sign and a constant value, you can set your variable to: • int sample = 47; • Another option that is available to the declaration statement is the const keyword, which converts the declared value from a variable to a constant: • const int sampleConstant = 47;

  5. Variable Declaration Statements • Please try to make an effort to make them representative of what they are being used for. • PWMV1 is much better than XEG4 • Variable types are important because of limited RAM

  6. Variable Declaration Statements • Be careful when passing data among variables • C-style casts can be used to convert any type into any other type, potentially with unsafe results (such as casting an integer into a pointer type). • i = (Type)j; • double result = (double)4/5;

  7. C Data Types

  8. Constant Formatting

  9. void main() { PORTB = 0; U1MODE = 0x8400; ADPCFG = 0xFFFF; TRISB = 0x49; while(1) // Loop Forever { if ((PORTB & (1 << 3))==0) PORTB = '3'; else PORTB = 4; } } void main() { PORTB = 0; U1MODE = 0b1000010000000000; ADPCFG = 0b1111111111111111; TRISB = 0b0100 1001 ; while(1) // Loop Forever { if (PORTBbits.RB3==0) PORTB = 0b00110011; else PORTB = 0b000100; } } Constant Formatting

  10. Standard C or C++ • // Will not take 0b prefix, 0x will work and 0# is for octal; • #include <iostream> • using namespace std; • int main() • { • int x = 0x01; • x = x << 3; • cout << x; • system("pause"); • }

  11. Assignment Statements • VariableName = Expression; • Although C is somewhat tolerant of assigning different data types, you should always try to make sure that the expression’s type is the same as the variable’s, and if it isn’t, make sure you understand any potential issues.

  12. Assignment Statements • int i; // Unitialized Variable Declaration • int j = 23; // Variable Declared with Initial • // value assignment (Initialization) • main() • { • i = 47; // The variable "i" assigned (or loaded • // with) the constant value 47 • i = j; // The variable "i" assigned contents of • // of "j". • i = j = 1; // "i" and "j" assigned the same value. • i = i + 1; • }

  13. Expressions • int i, j; • main() • { • i = 3; // "3" is the Expression • j = i; // "i" is the Expression • i = j * 11; // Simple Arithmetic Expression • i = j * 0x0B; // Multiply by a Hex Value • i = j * 0b00001011; // Multiply by a Binary Value • i = '0' + j; // Load i with ASCII "3“ • // ASCII “O” IS 48; 48 + 3 =51; ASCII “3” IS 51

  14. Expressions • j = 12; • i = (j - 5) % 7; // Complex Arithmetic Expression • // Involving Two Operations • // and Forced Order of Operations • // i = (j - 5) % 7 • // = (12 - 5) % 7 • // = 7 % 7 • // = 0

  15. Expressions • i = j - 5 % 7; // Same As previous but no • // Forced Order of Operations • // i = j - 5 % 7 • // = 12 - 5 % 7 • // = 12 - 5 (5 % 7 = 5) • // = 7 • i = (j = i / 6) * 2; // Embedded assignment: • // j = i / 6 • // = 7 / 6 • // = 1 • // i = (i / 6) * 2 • // = j * 2 • // = 2

  16. Bitwise Operators • The four basic bitwise operators are as follows: • & bitwise AND. When two values are ANDed together, each bit in the result is loaded with the AND value of the corresponding bits in the two values (set if both the bits are set). • A = 0b00001111; • B = 0B00001001; • A&B = ? • bitwise (inclusive) OR. When two values are ORed together, each bit in the result is loaded with the OR value of the corresponding bits in the two values (set if either hit is set). • A = 0b00001111; • B = 0B01001001; • A | B = ?

  17. Bitwise Operators • ^ bitwise XOR. When two values are exclusively ORed together. each bit in the result is loaded with the XOR value of the corresponding hits in the two values (set if only one of the two parameter bits is set). • A = 0b00001111; • A ^ 0x0A = ?

  18. Bitwise Operators • ~ bitwise negation. This operator will return the negated or complementary value for each bit of the single input parameter (invert each bit). This operator must never be confused with the logical operator, which will he shown to invert the logical value, not the hitwise value. • B = 0B01001001; • ~ B = ?

  19. Bitwise Operators • char i, j, k; // Use 8 Bit Variables • main() • { • i = 47; // Initialize Values • j = 137; • k = i & j; // AND Values together • k = i | j; // OR Values together

  20. Bitwise Operators • k = i ^ j; // XOR Values together • k = ~j; // Invert the Bits in "j" • k = (i * 2) - j + (1 << 7); // Mix Binary // Operators with • // Arithmetic

  21. Logical Expression

  22. Logical Expression • i = 0xl234 * (j > 4) // if (j > 4) is True, then 1 returned • which is equivalent to: • if (j > 4) • i = 0x1234; • else • i = 0;

  23. Conditional Execution Using the If Statement • if (Expression) // Test to see if “Expression” is Not Zero • Statement // Statement executed if “Expression” <> 0 • else // Optional “else” Statement which Statement // Executes if Expression is Zero

  24. Conditional Execution Using the If Statement • if ((j/ 3) != 0) // Execute following statement • // if “j / 3” is not zero • if (j / 3) // Execute following statement • // if “j / 3” is not zero.

  25. Conditional Execution Using the If Statement • if (Expression) // Test if “Expression” is • // Not Zero • {// Opening Brace to Collect Statements • // Multiple Statements that • // Execute if Expression • // is Not Zero • Statement; • Statement; • Statement; • } • // Closing Brace to End true “statement”

  26. Conditional Execution Using the If Statement • main() • { • if (I == 44) // "i" equals a constant • { • n = n + 1; // Increment "n" if "i" == 44 • k = 23; • } • else • { • n = n - 1; // Decrement if Not Equals • k = 0; • } • }

  27. Conditional Execution Using the If Statement • if ((j = (i / 3)) == 7) • { • j = j + 1; • k = 2; • } • if (k = i) // "i" equals contents of Variable • n = n + 1; // Increment "n" if "i" == "k" • else if (j = i) // "i" equals contents of another Variable • n = n - 1; // Note that there is a single statement, • // so no braces required.

  28. Nested Conditional Statements • if (i > j) • { • if (k < n) • { • // Statement(s) Executed if “i > j” and “k < n” • } • else • { • // Statement(s) Executed if “i > j” and “k >= n” • } • } • if ((i > j) && (k < n)) • { • // Statement(s) Executed if “1 > j” and “k < n” • } • else if (i > j) • { • // Statement(s) Executed if “i > j” and “k >= n” ) • } // fi

  29. for loop • for(initial cond.; test cond.;increment) • Isntruction • for(initial cond.; test cond.;increment) • { • Isntructions • }

  30. for loop • int x = 0, i, j = 5; • for(i = 1; i < j; i = i + 1) • { • x = x + i; • } • for(i = 1; i < j; i++)

  31. while loop • int x = 0, i = 1, j = 5; • while(i < j) • { • x = x + i; • i++; • }

  32. do while loop • int x = 0, i = 1, j = 5; • do • { • x = x + i; • i++; • } while(i < j);

More Related