Download presentation
1
Simple Microcontroller Programming with PIC16F88.
ECE 3102/ ECE 4102 International Islamic University Malaysia
2
ABOUT MICROCONTROLLER
3
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. Microcontroller: mini size 4cm x 2 cm or smaller. Computer: big size Microcontroller also consumes less energy.
4
Some Appliances Using Microcontroller
AC Rice Cooker Washing machine Auto gate Modern car Electric chair
5
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. ADC PWM Low Power High Speed Cheap Small Layout Timer/ Counter function Digital I/O RAM ROM Serial I/O
6
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. After the Microcontroller is programmed, then it is ready to use in the application. Blank Microcontroller Programmer/writer Application unit Computer
7
Preparation to Microcontroller Programming
Software: Compiler MikroC can be downloaded freely from: Data transformer to Microcontroller: Winpic800. Download free from: Hardware: Programmer Application circuit) Programmer Example of application circuit
8
Programmer Circuit This Programmer uses external 5V power supply.
5volt power supply Resistor: 100 W or 56W This Programmer uses external 5V power supply.
9
Simple Programmer This Programmer does not use external power supply but sources the power directly from serial port. More convenient.
10
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 Fixed Frequency Oscillator The Oscillator seems to have same function as heart in our body. It is to trigger constant pulses to execute the program in Microcontroller.
11
Important Notice in Microcontroller Operation
Microcontroller PIN location must be correct. Check properly the Socket direction by looking at PIN mark or number. Switch Off the power supply when assembling and dismantling the Microcontroller to and from the Socket. 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. 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.
12
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.
13
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.
14
Digital Value of Port I/O Microcontroller
15
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.
16
Microcontroller Kit Circuit
Application circuit Programmer Circuit
17
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.
18
Kit Connection to PC This is used when downloading the program from PC to PIC.
19
Kit Connection to PC Serial extension cable can also be used.
20
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.
21
Basic Command in C
22
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
23
Programming Structure
For each program, it must begin with command: void main() Each command line ends with semicolon: ;
24
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
25
Operator (contd.) Relational Operator Logic 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
26
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.
27
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.
28
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=0b ; PORTB = 1
29
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: Per PORT PORTB= 0b ;
30
Per PORT Command Per PORT command can be in decimal, binary or hexadecimal form. Bilangan Operator Example Value Binary 0b PORTB=0b ; Value of PORT B: B0=1, B1=1, B2=1, B3=1, B4=1, B5=1, B6=0, B7=0 Hexa-decimal 0x PORTB=0x0F ; 0F is equivalent to , that means PORT B value: B0=1, B1=1, B2=1, B3=1,B4=0,B5=0, B6=0, B7=0 Decimal PORTB =12 ; Decimal 12 is equal to in binary, thus PORT B: B0=0,B1=0, B2=1, B3=1, B4=0,B5=0, B6=0, B7=0
31
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 0b PORTB as input TRISB = 0b ; Only PIN B1 as output TRISB.F1 = 0; Only PIN B3 as input TRISB.F3 = 1;
32
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)
33
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=0b ;
34
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;
35
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-Green-Yellow). 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.
36
First Project: Algorithm
For this project, the algorithm is as follows: Define PORT that will be used as output. Define initial value of PORT. Start the program. Set output HIGH (1) for PIN on PORT B0-B5 Delay for 1s (1000ms). Set output HIGH for PORT B0, B2, B4 only, the rest are LOW. Set output HIGH for PORT B1, B3 and B5, the others are LOW. Repeat to number 3.
37
First Project: Programming
Programming is done using MicroC software. Click ProjectNew, then specify the name of the project and folder location.
38
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 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.
39
/* 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 = 0b ; //output PORTB in binary Delay_ms(1000); PORTB = 0b ; //output PORTB in binary PORTB = 0b ; // output PORTB in binary } while(1) ;
40
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 = 0b ; TRISB = 0b ; Set initial value PORTB=0. Set PORTB as Output. Example: TRISB = 0x00 is also to set PORT B as output and TRISB = 0b is to set PORT B as input. Partly input and output can also be set, for example: TRISA= 0b 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).
41
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 = 0b ; 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.
42
Program Compiling After the program is wriiten in the editor, then save the file. Then click Compile Icon 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.
43
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.
44
Then click icon “erase all” to delete all previous program existing in the PIC.
Then click open file icon to select .hex file that will be downloaded to the PIC. Then click to transfer the hex file of your project to PIC.
45
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.