C Summary Wilmer Arellano
References Excerpts from the book: Predko, Myke. (2005). 123 PIC MICROCONTROLLER EXPERIMENTS FOR THE EVIL GENIOUS. USA: McGraw-Hill. ISBN:
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 x = 47; For standard variables, two options exist that you should he aware of. The first is the ability to initialize the variable when it is declared. By adding an equals sign and a constant value, you can set your variable to 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 xConstant = 47;
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 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 = (iType)j; double result = (double)4/5;
C Data Types
Constant Formatting
main() { PORTA = 0; CMCON0 = 7; // Turn off Comparators ANSEL = 0; // Turn off ADC TRISA = 0x49; // Enable PORTA LED Outputs while(1 == 1) // Loop Forever { if (0 == (PORTA & (1 << 3))) PORTA = '3'; // Turn on Four LEDs else PORTA = 4; // Turn on Remaining Four LEDs } // elihw } // End cConfuse main() { PORTA = 0; CMCON0 = 7; // Turn off Comparators ANSEL = 0; // Turn off ADC TRISA = 0b001001; // Enable PORTA LED Outputs while(1 == 1) // Loop Forever { if (0 == RA3) PORTA = 0b010010; // Button Pressed, D0, D2, D4, D7 On else PORTA = 0b000100; // Button Released, D1, D3, D5, D6 On } // elihw } // End cClear
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.
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; while(1 == 1); } // End cAssign
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 * 0x00B; // Multiply by a Hex Value i = j * 0b ; // Multiply by a Binary Value i = '0' + j; // Load i with ASCII "3" // Change "Watch" Window // display Format to ASCII // to verify. // ASCII “O” IS 48; =51; ASCII “3” IS 51
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 i = j - 5 % 7; // Same As previous but no // Forced Order of Operations // i = j - 5 % 7 // = % 7 // = (5 % 7 = 5) // = 7 i = (j = i / 6) * 2; // Embedded assignment: // j = i / 6 // = 7 / 6 // = 1 // i = (i / 6) * 2 // = j * 2 // = 2 while (1 == 1); // Loop Forever } // End cExpression
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). — 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). ^ — 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). ~ — 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.
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 k = i ^ j; // XOR Values together k = ~j; // Invert the Bits in "j" k = (i * 2) - j + (1 << 7); // Mix Binary Operators with // Arithmetic while(1 == 1); } // End cBitwise
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;
Conditional Execution Using the If Statement if (Expression) // Test to see if “Expression” is Not Zero Statement // Statement executed if “Expression” !5 0 else // Optional “else” Statement which Statement // Executes if Expression is Zero if (0 != (j/ 3)) // Execute following statement // if “j / 3” is not zero if (j / 3) // Execute following statement // if “j / 3” is not zero.
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; } // Closing Brace to End true “statement”
Conditional Execution Using the If Statement main() { if (44 == i) // "i" equals a constant { n = n + 1; // Increment "n" if "i" == 44 } else { n = n - 1; // Decrement if Not Equals } // fi if ((j = (i / 3)) == 7) { j = j + 1; } // fi 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. while(1 == 1); } // End cIf
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” } // fi 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
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” } // fi