1. Trang chủ
  2. » Kỹ Thuật - Công Nghệ

3 c programming essentials

31 74 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Nội dung

Microcontrollers C Programming Essentials Microcontrollers Listing 2.1 A program to output a binary code #include "16F877A.h" // MCU select void main() { output_D(255); } // Main block // Switch on outputs Microcontrollers MPLAB IDE Screenshot Microcontrollers ISIS dialogue to attach program Microcontrollers OUTBYTE.DSN test circuit with output LEDs Variables Microcontrollers #include "16F877A.h" void main() { int x; x=99; output_D(x); // Declare variable and type // Assign variable value // Display the value in binary } Listing 2.3 Endless loop Source code file: ENDLESS.C Author, date, version: MPB 11-7-07 V1.0 Program function: Outputs variable count Simulation circuit: OUTBYTE.DSN *******************************************************/ Microcontrollers /* #include "16F877A.h" void main() { int x; while(1) { output_D(x); x++; } } // Declare variable // Loop endlessly // Display value // Increment value Microcontrollers NBIT.DSN test circuit with input switch IF statement /* Source code file: IFIN.C Author, date, version: MPB 11-7-07 V1.0 Program function: Tests an input Simulation circuit: INBIT.DSN ******************************************************* / Microcontrollers #include "16F877A.h" void main() { int x; output_D(0); // Declare test var // Clear all outputs while(1) // Loop always { x = input(PIN_C0); // Get input if(x==1)output_high(PIN_D0); // Change out } } Conditional loop /* Source code file: WHILOOP.C Author, date, version: MPB 11-7-07 V1.0 Program function: Input controls output loop Simulation circuit: INBIT.DSN *******************************************************/ Microcontrollers #include "16F877A.h" #use delay (clock=1000000) void main() { while(1) { while(input(PIN_C0)); { output_high(PIN_D0); delay_ms(300); output_low(PIN_D0); delay_ms(500); } output_low(PIN_D0); } } // MCU clock = 1MHz // Repeat while switch open // Delay 0.3s // Delay 0.5s // Switch off LED 10 ASCII Codes Microcontrollers Low Bits 0000 High Bits 0010 Space 0011 0100 @ 0101 P 0110 ` 0111 p 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 ! " # $ % & ' ( ) * + , / : ; < = > ? A B C D E F G H I J K L M N O Q R S T U V W X Y Z [ \ ] ^ _ a b c d e f g h i j k l m n o q r s t u v w x y z { | } ~ Del 17 Arithmetic and Logical Operations OPERATION OPERATO R DESCRIPTIO N SOURCE CODE EXAMPLE RESUL T Increment ++ Add one to integer result = num1++; 0000 0000 0000 0001 Decrement Subtract one from integer result = num1 ; 1111 1111 1111 1110 Complement ~ Invert all bits of integer result = ~num1; 0000 0000 1111 1111 Add + Integer or Float result = num1 + num2; 0000 1010 + 0000 0011 0000 1101 Subtract - Integer or Float result = num1 - num2; 0000 1010 - 0000 0011 0000 0111 Multiply * Integer or Float result = num1 * num2; 0000 1010 * 0000 0011 0001 1110 Divide / Integer or Float result = num1 / num2; 0000 1100 / 0000 0011 0000 0100 Logical AND & Integer Bitwise result = num1 & num2; 1001 0011 & 0111 0001 0001 0001 Logical OR | Integer Bitwise result = num1 | num2; 1001 0011 | 0111 0001 1111 0011 Exclusive OR ^ Integer Bitwise result = num1 ^ num2; 1001 0011 ^ 0111 0001 1110 0010 Single operand Microcontrollers Arithmetic Operation Logical Operation 18 Microcontrollers Variable Operations 19 Microcontrollers Conditional Operators Operation Symbol EXAMPLE Equal to == if(a == 0) b=b+5; Not equal to != if(a != 1) b=b+4; Greater than > if(a > 2) b=b+3; Less than < if(a < 3) b=b+2; Greater than or equal to >= if(a >= 4) b=b+1; Less than or equal to

Ngày đăng: 06/11/2017, 20:54

w