C Summary Wilmer Arellano. References Excerpts from the book: Predko, Myke. (2005). 123 PIC...

20
C Summary Wilmer Arellano

Transcript of C Summary Wilmer Arellano. References Excerpts from the book: Predko, Myke. (2005). 123 PIC...

  • C SummaryWilmer Arellano

  • ReferencesExcerpts from the book:

    Predko, Myke. (2005). 123 PIC MICROCONTROLLER EXPERIMENTS FOR THE EVIL GENIOUS. USA: McGraw-Hill. ISBN: 0-07-145142-0

  • Variable Declaration Statementsint 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 toAnother 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 StatementsPlease try to make an effort to make them representative of what they are being used for.PWMV1 is much better than XEG4Variable types are important because of limited RAMBe careful when passing data among variablesC-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

  • Constant Formattingmain(){

    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

  • Assignment StatementsVariableName = Expression;Although C is somewhat tolerant of assigning different data types, you should always try to make sure that the expressions type is the same as the variables, and if it isnt, make sure you understand any potential issues.

  • Assignment Statementsint i; // Unitialized Variable Declarationint 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

  • Expressionsint 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 * 0b000001011; // 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; 48 + 3 =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 // = 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

    while (1 == 1); // Loop Forever

    } // End cExpression

  • Bitwise OperatorsThe 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 Operatorschar 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

  • Logical Expression

  • Logical Expressioni = 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 Statementif (Expression) // Test if Expression is // Not Zero {// Opening Brace to Collect Statements // Multiple Statements that // Execute if Expression // is Not ZeroStatement;Statement; Statement; }// Closing Brace to End true statement

  • Conditional Execution Using the If Statementmain(){

    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 Statementsif (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 }// 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 Statementsif (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