* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Introduction to Micro
Survey
Document related concepts
Transcript
Simple Microcontroller Programming with PIC16F88. ECE 3102/ ECE 4102 International Islamic University Malaysia 1 ABOUT MICROCONTROLLER 2 Microcontroller in Common Modern Appliances Microcontrollers typically have the same functions like Computer yet it is much smaller and cheaper. It can be used in everyday appliances such as washing machine, modern car, mobile robot and home appliances. Computer: big size Microcontroller: mini size 4cm x 2 cm or smaller. Microcontroller also consumes less energy. 3 Some Appliances Using Microcontroller AC Rice Cooker Washing machine Electric chair Auto gate Modern car 4 Introduction to Microcontroller Microcontroller, also called as MCU or C, is an integrated electric circuit (IC) having some properties to perform like computer i.e: CPU (Central Processing Unit), code memory, data memory and I/O port. Its small size allows to be embedded into various home and industrial appliances, robotic applications and etc. RAM ROM Digital I/O Serial I/O ADC Timer/ Counter function Small Layout PWM Cheap High Speed Low Power 5 Microcontroller: Practical in Use The command/ program is written in BASIC or C and transferred into the memory of the Microcontroller via programmer or writer device. Computer Blank Microcontroller Programmer/ writer After the Microcontroller is programmed, then it is ready to use in the application. Application unit 6 Preparation to Microcontroller Programming Software: Compiler MikroC can be downloaded freely from: http://www.mikroe.com Data transformer to Microcontroller: Winpic800. Download free from: http://www.winpic800.com Hardware: Programmer Application circuit) Programmer Example of application circuit 7 Programmer Circuit 5volt power supply Resistor: 100 W or 56W This Programmer uses external 5V power supply. 8 Simple Programmer This Programmer does not use external power supply but sources the power directly from serial port. More convenient. 9 Simple Application Circuit The available ports can be directly connected to LED, Transistor, Buzzer, Sensor and Actuator. Oscillator (10k – 20MHz) Minimal component to drive Microcontroller: •IC Microcontroller •External Power supply, 5 V The Oscillator seems to have same function as heart in our body. It is to trigger constant pulses to execute the program in Microcontroller. •Fixed Frequency Oscillator 10 Important Notice in Microcontroller Operation 1. Microcontroller PIN location must be correct. Check properly the Socket direction by looking at PIN mark or number. 2. Switch Off the power supply when assembling and dismantling the Microcontroller to and from the Socket. 3. Don’t touch the Microcontroller on the PINs since our body include static electricity. The IC may be sensitive. Use pincer or small screwdriver to assist the IC removal from socket. 4. Check the voltage of the supply for safety. Input voltage can not be more than the specified (e.g. 5volt, 9volt). 5. Work carefully during IC dismantling since the PINs of IC and socket are fragile. 11 Input – Output (I/O) Microcontroller I/O configuration can be seen on the Datasheet of the Microcontroller. Input of Microcontroller can be digital or analog. If digital, then 0V input for low (0) and 5V for high (1). If analog, then input range is from 0V to 5V. Output of Microcontroller is only digital; 0V for state 0 and 5V for state 1. 12 Port I/O Microcontroller Example: From datasheet Microcontroller PIC16F88: Pin Input Analog (AN0-AN6): 17,18,1,2,3,12,13 Pin Input Digital: PORT A (RA0-RA4): 17,18,1,2,3 PORT B (RB0-RB7): 6,7,8,9,10,11,12,13 Pin Output only digital: PORT A (RA0-RA4): 17,18,1,2,3 PORT B (RB0-RB7): 6,7,8,9,10,11,12,13 PIN can be assigned as input or output depending on the programming setting. 13 Digital Value of Port I/O Microcontroller 14 Microcontroller Kit Microcontroller Kit is designed for training in Microcontroller programming. MINI18 PIC Training Kit is used here to design, test and develop PIC microcontroller programming. This Kit consists of Programmer and Application Circuit in one unit. • uses PIC16F88 microcontroller. • has many applications in addition to the low price that is suitable for education and robotic competition. • has 14 pin I/O and also ADC and PWM functions. 15 Microcontroller Kit Circuit Application circuit Programmer Circuit 16 PIC 16F88 Microcontroller Kit Application socket Programmer socket Programming circuit can be connected directly to serial port of PC or using extension cable. In the application circuit above, there are 2 input switches, 6 LEDs, 1 LDR sensor and 1 Buzzer Alarm. 17 Kit Connection to PC This is used when downloading the program from PC to PIC. 18 Kit Connection to PC Serial extension cable can also be used. 19 Transfer from Programmer to Application After the programming download is completed, The PIC can be removed from programmer socket to application socket. Be careful since the PINs are fragile. Then connect 9V supply to the terminal and switch ON test the circuit. 20 Basic Command in C 21 Statement/Comment Comment is made only to help the programmer as explanation, it will not be executed by computer. Using // for one line statement or /*.....statement........*/ for one paragraph statement or comment. Example: PORTB = 0; //Setting all values of PORTB = 0 22 Programming Structure For each program, it must begin with command: void main() Each command line ends with semicolon: ; 23 Operator Assignment operator It uses (“=”). example: input = 80; //variable “input” is equal to 80 A = x * y; //variable “A” has value of multiplicaion between x and y. Arithmatic operator * : multiplication / : division + : addition - : subtraction 24 Operator (contd.) Relational Operator < <= > >= == != less than Less than or equal to Greater than Greater than or equal to Equal to Not equal to Logic Operator && || ! Logic AND Logic OR Logic NOT 25 Conditions if…. and if….else In the structure if.....else the are at least two statement. If the given expression is true then statement-1 will be executed, otherwise if the given expression is false then stetement2 will be executed. It has common form as: If (condition) {…..} if(condition){statement-1} else {statement-2 } Example: if (m == 1) {x = x + 20} else {y = y – 10}; // If m=1 then the new value of x is x+20, otherwise the new value of y is y-10. 26 Looping Looping with do..while The looping process will continue if the given condition is true otherwise looping will be stopped. do {statement} while (condition); Example: int s, i; s = i = 0; do { s = s + 2; i = i + 1; } while (i < 7); //This will add value of variable s by 2 in 7 times; at the end the value of s will be 14. 27 PIN and PORT Assignment Byte Mapping in PORT PORTB has 8 pins: B0, B1, B2, B3, B4, B5, B6, B7 With the order from right: PORTB = RB7 RB6 RB5 RB4 RB3 RB2 RB1 RB0 For example to set only pin RB2 and RB4 to high state 1 (ON), the command is: PORTB=0b00010100; PORTB = 0 0 0 1 0 1 0 0 28 PIN and PORT Assignment PIN or PORT values can be determined per PIN or per PORT. The command below are the same: PORTB.F0 = 0; PORTB.F1 = 0; PORTB.F2 = 1; PORTB.F3 = 0; PORTB.F4 = 1; PORTB.F5 = 1; PORTB.F6 = 0; PORTB.F7 = 1; Per PIN Same as: PORTB= 0b10110100; Per PORT 29 Per PORT Command Per PORT command can be in decimal, binary or hexadecimal form. Bilangan Operator Example Value Binary 0b PORTB=0b00111111; Value of PORT B: B0=1, B1=1, B2=1, B3=1, B4=1, B5=1, B6=0, B7=0 Hexadecimal 0x PORTB=0x0F ; 0F is equivalent to 00001111, that means PORT B value: B0=1, B1=1, B2=1, B3=1,B4=0,B5=0, B6=0, B7=0 PORTB =12 ; Decimal 12 is equal to 00001100 in binary, thus PORT B: B0=0,B1=0, B2=1, B3=1, B4=0,B5=0, B6=0, B7=0 Decimal 30 Defining Input or Output PORT/PIN To assign a PORT or PIN as Input or Output, command TRIS is assigned: PORTB as output TRISB = 0 ; //same as 0b00000000 PORTB as input TRISB = 0b11111111; Only PIN B1 as output TRISB.F1 = 0; Only PIN B3 as input TRISB.F3 = 1; 31 Digital Input Checking To check digital input to the PIC (0V or 5 V), this command can be given: If (expression) {statement…} Example: If Port RA1 = 5V then Output of Port RB1 will be 1 (5V). If (PORTA.F1 == 1) {PORTB.F1=1} To delay the next command to be executed, the command is: Delay_ms(1000); //To delay the next command by 1s (1000 ms) 32 PIN Setting as Digital or Analog Command ANSEL can only be used by PIN with label AN. For example AN6 (Pin B7) will be set as analog, thus the command: ANSEL.F6=1; If let say PIN AN3 (Pin A2) is set to be digital: ANSEL.F3=0; For all PINs AN to be digital input: ANSEL=0b0000000; 33 ADC Activation To activate function conversion from analog to Digital (ADC), if PIN AN (Input Analog) is used. The command is ADCON0. To make the ADC function active: ADCON0 = 1; To make ADC function inactive, the command is: ADCON0 = 0; 34 First Project: Exercise In this first project, we will try to turn on LED with the sequence as follows: • All 6 LEDs connected to PORT B (RB0 to RB5) are ON in the first 1s. • In the next 1s, only LED on PORT B0, B2, B4 are ON (or Red-GreenYellow). • Then, in the next 1s after that, only LED on PORT B1, B3 and B5 are ON (Yellow-Green-Red). • The process is repeated to sequence 1. 35 First Project: Algorithm For this project, the algorithm is as follows: 1. Define PORT that will be used as output. 2. Define initial value of PORT. 3. Start the program. 4. Set output HIGH (1) for PIN on PORT B0-B5 5. Delay for 1s (1000ms). 6. Set output HIGH for PORT B0, B2, B4 only, the rest are LOW. 7. Delay for 1s (1000ms). 8. Set output HIGH for PORT B1, B3 and B5, the others are LOW. 9. Delay for 1s (1000ms). 10.Repeat to number 3. 36 First Project: Programming Programming is done using MicroC software. Click ProjectNew, then specify the name of the project and folder location. 37 Write the project name as “First_project” Specify the Project Path to save the files. Discription, we can leave it as blank (optional). Select Device, select PIC16F88. In Clock, write 20.000000 (20 MHz). Click default to use default settings. Then click OK. Then, MikroC compiler will prepare the project file and displays the window editor to write the source code program. 38 /* Name: First_project written in C Connection: LED B0-Red, B1-Yel, B2-Gre, B3-Red, B4-Yel, B5-Gre B6_SW1, B7-SW2 A1=Buzzer, A0=LDR */ void main() { PORTB = 0; //Initial setting PORTB=0 TRISB = 0; //Set PORTB as Output do { PORTB = 0b00111111; //output PORTB in binary 00111111 Delay_ms(1000); PORTB = 0b00101010; //output PORTB in binary 00101010 Delay_ms(1000); PORTB = 0b00010101; // output PORTB in binary 00010101 Delay_ms(1000); } while(1) ; } 39 Command Definition Command Explanation void main() To begin the main program {or } The bracket is used only to identify command into the blocks. To make the program easy to undertand. // or /*…..*/ Comment that will not be executed by computer. // is for one line statement. /* in the beginning of paragraph and */ in the end of paragraph. PORTB = 0b00000000; Set initial value PORTB=0. TRISB = 0b00000000; Set PORTB as Output. Example: TRISB = 0x00 is also to set PORT B as output and TRISB = 0b11111111 is to set PORT B as input. Partly input and output can also be set, for example: TRISA= 0b00001111 means PORTA pin A0,A1,A2,A3 as input (since set to 1) and pin A4,A5,A6,A7 as output (since set to 0). 40 Command Definition (contd.) do… while(1); To set the loop without ending. While (1) if the condition is TRUE or 1, then repeat to do again. PORTB = 0b00111111; On PORTB the output is given as: B0=1,B1=1,B2=1,B3=1,B4=1,B5=1, B6=0, B7=0. (bit sequence from right) Delay_ms(1000); To pause the next exceution by 1000ms or 1s. 41 Program Compiling After the program is wriiten in the editor, then save Then click Compile Icon the file. or build project. It takes a while for MikroC to compile the program, a .hex file will be crated. This .hex file will be transferred into the Microcontroller. 42 Transferring Hex File to PIC To transfer or download the .hex file to PIC, firstly open WinPIC800 software. Then plug the PIC Kit board into Serial Port/COM1 in the PC. The PIC must be connected to Programmer circuit to download .hex file. Select PIC 16F88 in pull down as shown by figure. Then click to check the serial port connection of the PIC board to the PC. 43 Then click icon “erase all” to delete all previous program existing in the PIC. Then click open file icon PIC. to select .hex file that will be downloaded to the Then click to transfer the hex file of your project to PIC. 44 After the hex file has been transferred, then unplug the PIC board from the serial port of the PC. And remove the IC to application circuit. Connect 9v supply to the terminal and press switch ON. We can see the LEDs are ON according to the programmed sequence. 45