The document discusses microcontrollers and the PIC16F877 microcontroller in particular. It provides the following key points:
- A microcontroller is a single-chip computer containing a processor, memory, and input/output peripherals. Microcontrollers can store and run user-written programs.
- The main parts of a microcontroller include a CPU, RAM, ROM, I/O lines, timers, and analog-to-digital and digital-to-analog converters.
- The PIC16F877 is chosen for its low cost, reliability, ease of use, and ability to perform a wide range of tasks using C language software.
2. What is microcontroller? ..cont
•A microcontroller is a relatively low cost single chip microcomputer
•A single-chip microcomputer indicates that the complete
microcomputer system lies within the confines of the integrated
circuit chip.
•Microcontrollers are capable of storing and running the program
that was written, compiled and downloaded onto it.
3. What is microcontroller? ..cont
main parts of a microcontroller generally consist of
the:
Central Processing Unit (CPU), Random Access
Memory (RAM), Read Only Memory (ROM),
input/output lines (I/O lines), serial and parallel
ports, timers and other peripherals such as an
analog to digital (A/D) converter and a digital to
analog (D/A) converter.
3
4. ?Why use a Microcontroller
The microcontroller's ability to store and run unique programs
makes it fairly flexible. For example, one can program a
microcontroller to perform functions based on predetermined
situations (I/O-line logic) and selections.
The microcontroller's capability to carry out mathematical
and logic functions allows it to imitate complicated logic and
electronic circuits.
4
5. ? Why use a PIC Microcontroller
This type of microcontroller is chosen because it is low
cost, reliable, easy to use and capable of performing a
wide range of tasks.
The required tasks by the
proposed system are carried
out via software using
C language .
5
6. Electrical consideration
To deploy a PIC 16F877 microcontroller
within the framework of a project, it is
important to pay particular attention to the
following components:
1. Power supply.
2. reset button .
3. oscillator.
6
7. Power supply
The PIC microprocessor offers a wide operating voltage range
varying from 2V DC to 6V DC, depending, naturally, on the
particular device used .
A simple 5V DC power supply will be used .
A 5V DC supply is easy to construct due to the availability of
monolithic voltage regulators such as the 7805 positive 5V
regulator, which provides good regulation as well as automatic
thermal shutdown and short circuit overload protection.
7
8. Power supply
The hardware configuration of the
5V DC power supply is shown in
.Figure
The circuit consists of a 7805 IC
voltage regulator and some ripple. reducing smoothing capacitors
8
9. Circuit Layout and Construction
1) The oscillator crystal and capacitors should be located in close
proximity to the IC with short copper tracks or connecting leads.
2) A small decoupling capacitor (≈ 0.1µF) should be placed across the
power supply and as close as possible to the PIC MCU.
9
10. :Developing a Project Using PIC MCU
Developing a PIC-based project takes only six easy
steps:
1. Type in the program
2. Compile the program into a binary file
3. Simulate the program and debug it
4. Load the binary program into the PIC’s memory
5. Wire up the circuit
6. Switch on and test.
10
11. :Using C-Compiler for PIC MCU
We look at two programs
as shown in The figure
First is PIC C Compiler
Second is proutes.
We will talk about both
11
12. Why we choose C-programming language
• C-language is the best option to program microcontrollers.
This is because C-language is user friendly, efficient and
requires less code to perform the associated tasks
12
13. Pic component
The pin diagram of the PIC 16F877
chip, which is utilized in the present
work, is shown in Figure.
As shown there is 5 I/O ports
(A,B,C,D,E).
PORT A and PORT E may be used as
Analog to Digital input
Pin 13 &14 are crystal input
Pin RC6 & RC7 are serial interface
13
14. Port A
What is a port :is a set of pins
every pin construct a one bit.
RA0 RA1 RA2 RA3 RA4 RA5
PORT A may be used as Analog to Digital input and used
with LCD device
14
17. PORT C
RC0 RC1 RC2 RC3 RC4 RC5 RC6 RC7
PORT C contain of 8 bits usually use as digital i/o ports and
we use it as a parallel port’s
17
18. PORT D
RD0 RD0 RD0 RD0 RD0 RD0 RD0 RD0
PORT D contain of 8 bits usually use as digital i/o ports and
we use it as a parallel port’s
18
19. Include device
Include libraries
Set fuses
Set delay use
function
Define your
global registers
#include <16f877a.h>
#include "lcd_kbd1.c"
#fuses xt,NOWDT
#use delay(clock=4000000)
int value;
void main() {
while(1)
{
}
}//MAIN
main
Your code
19
End main
20. Program in c
1. #include <16f877a.h>
2. #include "lcd_kbd1.c"
3. #fuses xt,NOWDT
4. #use delay(clock=4000000)
5. int value;
6. void main() {
7.
8. while(true)
9.
{
10.
11. }
12.}//MAIN
20
21. Data types
Int1 or short
Defines a 1 bit number
Int8 or int
Defines an 8 bit number
Long or int16
Defines a 16 bit number
int32
Char
Defines a 32 bit number
Defines a 32 bit floating point
number
Defines a 8 bit character
void
Indicates no specific type
float
21
22. operators
arithmetic
Bitwise
Addition (+)
Logic and (&)
Subtraction (-)
Logic or (|)
Division (/)
multiplication (*)
Increment (++)
decrement (--)
not (~)
logic
Logic and (&&)
Logic or (||)
One’s
complement (!)
Other are known by default such as
equality and inequality,….etc.
Note: see example on slide number 32
22
23. Interface divided into two kinds:
1.parallel interface
2. serial interface
Parallel Interface
Port interface
Decide port as input or output
Output a value on the port
Pin interface
Decide pin as
input or output
Output high
Read a value from the port
Output low
23
25. ?How to compile code
If your code is well
done and no
errors this
message will
appear.
A list of files will be
created in the
same location of c
file, you need hex
file.
25
26. Protues simulation software
This product combines mixed mode circuit simulation,
micro-processor models and interactive component
models to allow the simulation of a complete microcontroller based design.
As shown before double click on the icon (see).
26
27. Protues simulation software
Press P to select
component from
list as shown in
Figure
27
Prepared by : Eng. Sa’ed M. Hayajneh & Eng. Hadeel Qasaimeh
30. Start programming
.Now we will review a general points in C language
Then we will talk about special commands in CCS C
.Compiler
30
31. parallel Ports commands and function’s
set_tris_b (value);
You can use that function with port B and A
and C and d for example:
1.
set_tris_c (0x00); //port c is output
2.
set_tris_b (0xff);//port b is input
3.set_tris_d (0x0f);//D0-D3 are input and D4-D7 are
output
31
32. How that command is work
output <- 0
That command will initialize your port to
prepare it to be input or output .
For example if we use set_tris_b (0x00);
RB7
output <- 0
RB6
output <- 0
RB5
output <- 0
RB4
that mean port B is prepared to be output and
(0x00) mean ox with hex 00 [0000,0000] output <- 0
and 0 is short cut to output
RB3
output <- 0
RB2
output <- 0
RB1
output <- 0
RB0
32
33. Another example
For example if we use set_tris_b (0xf0);
input <- 1
RB7
input <- 1
RB6
input <- 1
RB5
input <- 1
RB4
output <- 0
RB3
output <- 0
RB2
output <- 0
RB1
output <- 0
RB0
33
34. Parallel port commands ..cont
To output a value direct to a port:
output_a (value);
• value is a 8 bit int.
• To input a value direct from a port.
New= input_b();
• new is a 8 bit predefined int.
34
35. :Let consider the following example
#include <16f877A.h>
#fuses xt,NOWDT
#use delay(clock=4000000)
int8 value;
void main()
{
set_tris_d(0x00);
set_tris_b(0xff);
output_d(0x00);
while(TRUE)
{
value = input_b();
output_d(value);
delay_ms(1000);
}
}
35
38. Pin commands
A Pin is one bit of a port
For example, port D contain 8 pins.
D0
D1
D2
D3
D4
D5
D6
D7
Port D
• Note that not necessary that all ports contains
8 pin
Prepared by : Eng. Sa’ed M. Hayajneh & Eng. Hadeel Qasaimeh
38
39. Pin commands ..cont
You have the choice to output high or low value on a
pin (one bit).
To output high or low value direct to a port:
output_high (PIN_A0);
output_low (PIN_A0);
• To input a value direct from a port.
value = input (pin)
• Value is TRUE if pin is high ,and FALSE if pin low
39
41. .Delay commands
You may need to delay your code some seconds less or more.
Your code already know the clock that you will work on from the
PRE-PROCESSOR :
#use delay (clock=speed)
We talk about it before.
This line Tells the compiler the speed of the processor and enables
the use of the built-in functions: delay_ms(), delay_us() and
delay_cycles (count) .
41
42. Delay commands ..cont
#use delay (clock=speed)
speed is a constant 1-100000000 (1 Hz to 100 MHz).
•delay_ms (time) .
•time - a variable 0-255 or a constant 0-65535.
•This function will create code to perform a delay of the
specified length. Time is specified in milliseconds.
42
43. Delay commands ..cont
•delay_us (time) .
•time - a variable 0-255 or a constant 0-65535.
•Creates code to perform a delay of the specified length.
Time is specified in microseconds
•delay_cycles (count) ;
count - a constant 1-255
•Creates code to perform a delay of the specified number of
instruction clocks (1-255). An instruction clock is equal to four
oscillator clocks.
43
44. :Let consider the following example
We want to
output pins(D0D3)high and
pins(D4-D7)
Low for 1second
Then do the
inverse
44
45. Let consider the following example:
#include <16f877A.h>
#fuses xt,NOWDT
#use delay(clock=4000000)
void main()
{
set_tris_d(0x00);
output_d(0x00);
while(TRUE)
{
output_d(0x0f);
delay_ms(1000);
output_d(0xf0);
delay_ms(1000);
}
}
45
46. Deal with LCD library
Most of microcontroller devices are using 'smart LCD'
displays to output visual information.
The LCD requires 3 "control" lines from the
microcontroller: Enable (E), Read/Write (R/W) ,
Register select (RS) .in addition to data lines.
CCS C LCD library give them automatically.
46
47. Deal with LCD library
:LCD interface with PIC 16f877a is shown in the following
47
48. Deal with LCD library
There is a lot of LCD libraries ,,we will introduce one
of them called “lcd_kbd1.c”
This c file must be included in the following path to
get ready:
My computer/c/program file/picc/drivers/lcd_kbd1.c
48
49. Deal with LCD library
This library contain the following function:
1) LCD_Init ( );.
2) LCD_PutCmd ( CLEAR_DISP );
3) LCD_SetPosition ( LINE_16_1);
4) printf(LCD_PutChar,“AILA Company" );
First line this will power lcd on, this function defined
on the begging of the code once.
Second clear display.
Third set cursor on line one or two
Fourth display data or sentence you want
49
50. .Print "hello, world "on LCD
Traditionally the first code is to print the following sentence
“hello ,world”.
end
start
no
Power up LCD
Set curser
yes
Print character
Another
sentence?
Wait short time
50
51. .Print a sentence on LCD
Print “just “
On line one and
Print ”university”
On line two and
wait for 2
seconds
Redo always
51
53. .Print different values on LCD
As I mention before c deal with different types of data
(int ,char, string , float ,…etc).
If you want to print some value you must tell lcd what
kind of data this value, using the formats:
Character
Strings
Unsigned int
Signed int
Long unsigned int
(Hex int (lower case
(Hex int (upper case
(Hex long int (lower case
(Hex long int (upper case
Float
c
u
d
Lu
x
X
Lx
LX
F
53
54. .Print different values on LCD
Format Example:
Int value =0x02;
LCD_SetPosition ( LINE_16_1);
printf(LCD_PutChar,“value is:" );
LCD_SetPosition ( LINE_16_2);
printf(LCD_PutChar,“%u”,value );
Similarly :
%4X
0002
54
55. .Read a port and display the result
Read port B
Print the value
named by ”bvalue”
wait for 2sec
Always repeat this
operation
55
56. .Read a port and display the result
#include <16f877A.h>
#fuses xt,NOWDT
#use delay(clock=4000000)
#include "lcd_kbd1.c"
int bvalue;
void main()
while(TRUE)
{
bvalue = input_b();
LCD_SetPosition ( LINE_16_1);
printf(LCD_PutChar,“bvalue is:");
LCD_SetPosition (LINE_16_2);
printf(LCD_PutChar,"%u",bvalue);
{
}
LCD_Init ( );
LCD_PutCmd ( CLEAR_DISP );
set_tris_b(0xff);
}
56
57. !!Wait a minute
?How to print on a specific pixel
Each line in LCD (16X2) divided into 16
pixel each line, this found on
:datasheet. example
LCD_SetPosition ( 0x80);
printf(LCD_PutChar,"v1");
57
58. Deal with keypad
The keypad is actually a collection of push-buttons, organized into a
matrix. It looks like this:
58
59. Deal with keypad
If you have a different layout keypad, you can change
the definition of the buttons (key table) in the source
code.
You can use a 3x4 keypad or a 4x4 keypad. The
advantage of the bigger keypad is that you can use the
extra letters in codes. In case of using a 3x4 keypad,
simply leave the COL3 input unconnected, no other
modification is required. If you don't have a keypad,
you can even wire one from push-buttons.
59
60. Deal with keypad
In order for the microcontroller to scan the keypad, it outputs a nibble to
force one (only one) of the columns low and then reads the rows to see if any
buttons in that column have been pressed.
The columns are pulled up .
Consequently, as long as no buttons are pressed, the microcontroller sees a
logic high on each of the pins attached to the keypad rows.
The nibble driven onto the columns always contains only a single 0. The only
way the microcontroller can find a 0 on any row pin is for the keypad button
to be pressed that connects the column set to 0 to a row. The controller
knows which column is at a 0-level and which row reads 0, allowing it to
determine which key is pressed. For the keypad, the pins from left to right
are: R1, R2, R3, R4, C1, C2, C3, C4.
60
61. Deal with keypad
Use the previous
subroutine to
print the key
pressed on LCD.
Always repeat
this operation
61
63. Deal with keypad
.Try to output the value of key pressed parallel
Think how to output character on parallel port
.See how to convert character into integer
63
64. Analog to digital conversion
As we mention before (see) PIC have 8 pin as
analog/digital input ,every one called channel.
Follow the following steps:
1. Setup ADC mode.
2. Choose channel.
3. Wait short time.
4. read
64
65. Analog to digital conversion
1.
example.
Float value;
setup_adc_ports( ALL_ANALOG );
set_adc_channel( 0 );
Delay_ms(100);
value = read_adc();
65
66. Analog to digital conversion
Read both channel
periodically.
Let channel one is
light and second is
temperature.
Display the value
on LCD
66
68. Use internal eeprom of chip
The data EEPROM and Flash program memory is
readable and writable during normal operation
In PIC16f877a it 256 byte size.
To read a value use the following command:
value = read_eeprom (address) ;
• address is an (8 bit or 16 bit depending on the part)
int . value must predefined as 8 bit int.
• The
68
69. Use internal eeprom of chip
To write a value (int) use the following command:
write_eeprom (address,value) ;
• address is an (8 bit or 16 bit depending on the
part) int .
• The value must predefined as 8 bit int.
69
70. !!!Wait a minute
What if you want to write or read other type of
data on your chip internal eeprom?
What if you need to save a huge size of data?
Its depend upon your cleverness
70
71. Take the advantage of using
interrupts
When interrupt occur the compiler will generate code
to jump to the function when the interrupt is detected.
It will generate code to save and restore the machine
state, and will clear the interrupt flag.
Its useful to detect event in huge projects.
As an example :fire accidents .
71
72. Take the advantage of using
interrupts
PIC microcontroller deal with different kinds of
interrupts ,depending on the device you use.
As an example,16f877 deal with 15 kind of interrupt.
72
73. Take the advantage of using
interrupts
To deal with interrupts just follow the following
steps:
1. Enable global interrupts
enable_interrupts(GLOBAL);
2. Enable the specific interrupt.
enable_interrupts(INT_TIMER0); //timer0 intterupt
73
74. Take the advantage of using
interrupts
:Finally write interrupt subroutine ; for example.3
#int_rda
serial_isr()
{
//your own code
}
74
75. Take the advantage of using
interrupts
I will introduce three kinds of interrupts
1. RB change interrupt , which detect any change
on B4-B7 (Port B ).
2. External interrupt, which detect positive
/negative edge on RB0.
3. RDA interrupt ,which detect and sense
availability of data on RS232 port (this will
explained later).
75
78. External interrupt
In this type there is extra command:
ext_int_edge (source, edge) ;
• source is a constant 0,1 or 2 for the PIC18XXX and 0
otherwise source is optional and defaults to 0 edge is a
constant H_TO_L or L_TO_H representing "high to low"
and "low to high" .
•Determines when the external interrupt is acted upon. The
edge may be L_TO_H or H_TO_L to specify the rising or
falling edge.
78
81. RDA RS232 interrupt
This section will explained later ,we must introduce
RS232 library first.
81
82. RS232 library
To communicate through RS232 Port, we have to
define the port as follows:
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
•This called hardware rs232
•baud=is boud rate i.e.data transfer rate per second and we
choose to be=9600
•Xmit: is transmitter pin, its standard as pin C6
•rcv: is recevier pin, its standard as pin C7
82
83. RS232 library
To write data to RS232 Port:
printf (Aila );
Aila is data to write.
It's advised to set the type of data i.e.
printf ("%s",Aila);
where "%s" tell the pic that Aila is a string (array of
characters).
83
84. RS232 library
To read data to RS232 Port:
value = getc();
value=getch();
value=getchar();
You may use any one of them.
Value is one character.
84
85. RS232 library
To read data (string) from RS232 Port:
value = gets();
• Or do loop technique as the following:
char RX [100];
RX[i++]=getch();
•100 is the buffer ,every device have different size of buffer
•I is int
85
86. RS232 library
Other strongly related topic is to use RDA interrupt:
enable_interrupts(GLOBAL);
enable_interrupts(INT_RDA);
•And do read operation when interrupt occur instead of wait
data all time ,and check the flag periodically.
#int_rda
void serial_isr() {
RX[i++]=getch();
flag=1;
}
86
87. Built two codes first send the following “ya raaab” every 5 seconds
The second receive it and print on LCD
87
97. Use string library
If your code needs to process and analyze some
string, then you must know and deal with "string.h"
library .
Follow the following procedure:
1. Include the library:
#include <string.h>
97
98. Use string library
Now you can use function directly ,here is some of. 2
:them
Strcat(S1,S2), it will past string S1 and string S2 and
put the result on S1
strcmp (s1, s2), it compare string S1 with string S2
and return True or false, for ex:
if(!strcmp(S2, S5));
• this line check if S2 and S5 are equal.
98
99. Use string library
strcpy(S1,"*");,this copy * to S1.
strcspn (s1, s2),its Count of initial chars in s1not in s2
strcpy(S1,"*");
strcpy(S3,"#");
strcpy(RX,"12345*Aila#");
rank1=strcspn(RX, S1);
rank2=strcspn(RX, S3);
rank1 is integer and equal to 5
rank2 is integer and equal to10
99
100. Use string library
?. How to analyze strings
Try to get string analyze it and print on
.LCD
This will left for you
100