Rami Al-Salman
Training course in PIC
Email:rami@informatik.uni-bremen.de
What is microcontroller? ..cont

•A microcontroller is a relatively low cost single chip microcomputer
•A single-chip micr...
What is microcontroller? ..cont
main parts of a microcontroller generally consist of
the:
 Central Processing Unit (CPU),...
?Why use a Microcontroller
 The microcontroller's ability to store and run unique programs
makes it fairly flexible. For ...
? Why use a PIC Microcontroller
 This type of microcontroller is chosen because it is low
cost, reliable, easy to use and...
Electrical consideration
 To deploy a PIC 16F877 microcontroller
within the framework of a project, it is
important to pa...
Power supply
 The PIC microprocessor offers a wide operating voltage range
varying from 2V DC to 6V DC, depending, natura...
Power supply


The hardware configuration of the
5V DC power supply is shown in
.Figure
The circuit consists of a 7805 IC...
Circuit Layout and Construction

 1) The oscillator crystal and capacitors should be located in close
proximity to the IC...
:Developing a Project Using PIC MCU

 Developing a PIC-based project takes only six easy
steps:
1. Type in the program
2....
: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...
Why we choose C-programming language

• C-language is the best option to program microcontrollers.
This is because C-langu...
Pic component
 The pin diagram of the PIC 16F877
chip, which is utilized in the present
work, is shown in Figure.
 As sh...
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 A...
Port E
RE0/RD

RE1/WR

RE2

PORT E may be used as Analog to Digital input and used
with LCD device

15
PORT B
RB0

RB1

RB2

RB3

RB4

RB5

RB6

RB7

PORT B contain of 8 bits usually use as digital i/o ports and
we use as a p...
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 paral...
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 paral...
Include device
Include libraries
Set fuses
Set delay use
function
Define your
global registers

#include <16f877a.h>
#incl...
Program in c
1. #include <16f877a.h>
2. #include "lcd_kbd1.c"
3. #fuses xt,NOWDT
4. #use delay(clock=4000000)
5. int value...
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 ...
operators

arithmetic

Bitwise

Addition (+)

Logic and (&)

Subtraction (-)

Logic or (|)

Division (/)
multiplication (*...
Interface divided into two kinds:
1.parallel interface
2. serial interface

Parallel Interface

Port interface

Decide por...
?How to compile code

This
button to
compile a
code

24
?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...
Protues simulation software
 This product combines mixed mode circuit simulation,
micro-processor models and interactive ...
Protues simulation software

Press P to select
component from
list as shown in
Figure

27
Prepared by : Eng. Sa’ed M. Haya...
Protues simulation software

Locate
component ,wire
them.
Click left click
then right click on
microcontroller
component

...
Protues simulation software

This box will
appear set
them and then
press ok.
Click on run
button.

29
Start programming
.Now we will review a general points in C language



Then we will talk about special commands in CCS C...
parallel Ports commands and function’s

 set_tris_b (value);
 You can use that function with port B and A
and C and d fo...
How that command is work
output <- 0



That command will initialize your port to
prepare it to be input or output .

For...
Another example
For example if we use set_tris_b (0xf0);

input <- 1

RB7

input <- 1

RB6

input <- 1

RB5

input <- 1

R...
Parallel port commands ..cont
To output a value direct to a port:
output_a (value);

• value is a 8 bit int.
• To input a ...
:Let consider the following example
#include <16f877A.h>
#fuses xt,NOWDT
#use delay(clock=4000000)
int8 value;
void main()...
Parallel command using logic operator

36
#include <16f877A.h>
#fuses xt,NOWDT
#use delay(clock=4000000)
int8 value,value1;
void main()
{
set_tris_d(0x00);//set as ...
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

• ...
Pin commands ..cont

 You have the choice to output high or low value on a
pin (one bit).
 To output high or low value d...
:Let consider the following example
#include <16f877A.h>
#fuses xt,NOWDT
#use delay(clock=4000000)
int8 value;
void main()...
.Delay commands
 You may need to delay your code some seconds less or more.
 Your code already know the clock that you w...
Delay commands ..cont
#use delay (clock=speed)

 speed is a constant 1-100000000 (1 Hz to 100 MHz).

•delay_ms (time) .
•...
Delay commands ..cont
•delay_us (time) .
•time - a variable 0-255 or a constant 0-65535.
•Creates code to perform a delay ...
:Let consider the following example

We want to
output pins(D0D3)high and
pins(D4-D7)
Low for 1second
Then do the
inverse
...
 Let consider the following example:
#include <16f877A.h>
#fuses xt,NOWDT
#use delay(clock=4000000)
void main()
{
set_tri...
Deal with LCD library

 Most of microcontroller devices are using 'smart LCD'
displays to output visual information.

 T...
Deal with LCD library
:LCD interface with PIC 16f877a is shown in the following

47
Deal with LCD library

 There is a lot of LCD libraries ,,we will introduce one
of them called “lcd_kbd1.c”
 This c file...
Deal with LCD library

 This library contain the following function:

1) LCD_Init ( );.
2) LCD_PutCmd ( CLEAR_DISP );
3) ...
.Print "hello, world "on LCD
 Traditionally the first code is to print the following sentence
“hello ,world”.
end
start
n...
.Print a sentence on LCD
Print “just “
On line one and
Print ”university”
On line two and
wait for 2
seconds
Redo always

...
.Print "hello, world "on LCD
#include <16f877A.h>
#fuses xt,NOWDT
#use delay(clock=4000000)
#include "lcd_kbd1.c"
void mai...
.Print different values on LCD
 As I mention before c deal with different types of data
(int ,char, string , float ,…etc)...
.Print different values on LCD
 Format Example:
Int value =0x02;
LCD_SetPosition ( LINE_16_1);
printf(LCD_PutChar,“value ...
.Read a port and display the result
Read port B
Print the value
named by ”bvalue”
wait for 2sec
Always repeat this
operati...
.Read a port and display the result
#include <16f877A.h>
#fuses xt,NOWDT
#use delay(clock=4000000)
#include "lcd_kbd1.c"
i...
!!Wait a minute
?How to print on a specific pixel
Each line in LCD (16X2) divided into 16
pixel each line, this found on
:...
Deal with keypad
 The keypad is actually a collection of push-buttons, organized into a
matrix. It looks like this:

58
Deal with keypad
 If you have a different layout keypad, you can change
the definition of the buttons (key table) in the ...
Deal with keypad


In order for the microcontroller to scan the keypad, it outputs a nibble to
force one (only one) of th...
Deal with keypad

Use the previous
subroutine to
print the key
pressed on LCD.
Always repeat
this operation

61
Deal with keypad
#include<16f877a.h>
#include "lcd_kbd2.c"
#fuses NOWDT, XT, NOPROTECT
#use delay (clock=4000000)
void mai...
Deal with keypad



.Try to output the value of key pressed parallel
Think how to output character on parallel port
.See...
Analog to digital conversion
 As we mention before (see) PIC have 8 pin as
analog/digital input ,every one called channel...
Analog to digital conversion
1.

example.

Float value;
setup_adc_ports( ALL_ANALOG );
set_adc_channel( 0 );
Delay_ms(100)...
Analog to digital conversion

Read both channel
periodically.
Let channel one is
light and second is
temperature.
Display ...
Analog to digital conversion
#include <16f877a.h>
#fuses xt,NOWDT
#use delay(clock=4000000)
#include "lcd_d.c"
float a,b;
...
Use internal eeprom of chip
 The data EEPROM and Flash program memory is
readable and writable during normal operation
 ...
Use internal eeprom of chip
 To write a value (int) use the following command:

write_eeprom (address,value) ;

• address...
!!!Wait a minute
 What if you want to write or read other type of
data on your chip internal eeprom?
 What if you need t...
Take the advantage of using
interrupts

 When interrupt occur the compiler will generate code
to jump to the function whe...
Take the advantage of using
interrupts

 PIC microcontroller deal with different kinds of
interrupts ,depending on the de...
Take the advantage of using
interrupts

 To deal with interrupts just follow the following
steps:
1. Enable global interr...
Take the advantage of using
interrupts
:Finally write interrupt subroutine ; for example.3

#int_rda
serial_isr()
{
//your...
Take the advantage of using
interrupts
 I will introduce three kinds of interrupts
1. RB change interrupt , which detect ...
RB change interrupt

Now try to
read value
each time its
changed then
output onto
port D

76
RB change interrupt
#include<16f877A.h>
#fuses XT, NOWDT
#use delay (clock = 4000000)
int x, y, data;
#INT_RB
void RB_isr(...
External interrupt
 In this type there is extra command:

ext_int_edge (source, edge) ;
• source is a constant 0,1 or 2 f...
External interrupt

Now try to read value each time its changed and then display on LCD
79
External interrupt
#include <16f877a.h>
#fuses xt,NOWDT,NOLVP
#use delay(clock=4000000)
#int_ext
void ext_isr() {
output_h...
RDA RS232 interrupt

This section will explained later ,we must introduce
RS232 library first.

81
RS232 library
 To communicate through RS232 Port, we have to
define the port as follows:
#use rs232(baud=9600, xmit=PIN_C...
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...
RS232 library
 To read data to RS232 Port:
value = getc();
value=getch();
value=getchar();
You may use any one of them.
V...
RS232 library
 To read data (string) from RS232 Port:
value = gets();
• Or do loop technique as the following:
char RX [1...
RS232 library
 Other strongly related topic is to use RDA interrupt:
enable_interrupts(GLOBAL);
enable_interrupts(INT_RDA...
Built two codes first send the following “ya raaab” every 5 seconds
The second receive it and print on LCD

87
Serial interrupt
//TX code
#include<16f877A.h>
#fuses XT, NOWDT
#use delay (clock = 4000000)

#use rs232(baud=9600, xmit=P...
Serial interrupt
//RX code
#include<16f877A.h>
#fuses XT, NOWDT
#use delay (clock = 4000000)
#use rs232(baud=9600, xmit=PI...
Read analog input from A0,do adc, and out the result serial.
90
Serial interrupt
#include <16f877a.h>
#fuses xt,NOWDT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, r...
while(TRUE)
{
set_adc_channel(0);
delay_us(100);
value1 = read_adc();
LCD_SetPosition ( LINE_16_1);
printf(LCD_PutChar,"va...
Serial interrupt
How to send and receive data with
? different data type
How to send and receive data with
? different bau...
RS232 and RDA interrupt
Output the key pressed serial and print it on LCD

94
Serial interrupt
#include<16f877.h>
#fuses NOWDT, XT, NOPROTECT
#use delay (clock=4000000)
#use rs232(baud=9600, xmit=PIN_...
.
if (key==1)
printf(LCD_PutChar,"1");
if (key==2)
printf(LCD_PutChar,"2");
if (key==3)
printf(LCD_PutChar,"3");
if(key==4...
Use string library
 If your code needs to process and analyze some
string, then you must know and deal with "string.h"
li...
Use string library
Now you can use function directly ,here is some of. 2
:them
 Strcat(S1,S2), it will past string S1 and...
Use string library
 strcpy(S1,"*");,this copy * to S1.
 strcspn (s1, s2),its Count of initial chars in s1not in s2
strcp...
Use string library
?. How to analyze strings
Try to get string analyze it and print on
.LCD
This will left for you

100
Upcoming SlideShare
Loading in …5
×

Picmico

828 views

Published on

Published in: Education, Technology
  • Be the first to comment

Picmico

  1. 1. Rami Al-Salman Training course in PIC Email:rami@informatik.uni-bremen.de
  2. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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
  15. 15. Port E RE0/RD RE1/WR RE2 PORT E may be used as Analog to Digital input and used with LCD device 15
  16. 16. PORT B RB0 RB1 RB2 RB3 RB4 RB5 RB6 RB7 PORT B contain of 8 bits usually use as digital i/o ports and we use as a parallel port’s 16
  17. 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. 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. 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. 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. 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. 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. 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
  24. 24. ?How to compile code This button to compile a code 24
  25. 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. 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. 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
  28. 28. Protues simulation software Locate component ,wire them. Click left click then right click on microcontroller component 28
  29. 29. Protues simulation software This box will appear set them and then press ok. Click on run button. 29
  30. 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. 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. 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. 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. 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. 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 
  36. 36. Parallel command using logic operator 36
  37. 37. #include <16f877A.h> #fuses xt,NOWDT #use delay(clock=4000000) int8 value,value1; void main() { set_tris_d(0x00);//set as output set_tris_b(0xff);//set as input output_d(0x00);//initially value1= ~((value)|(0x0f));//nor output_d(value1); delay_ms(1000); value1= ~((value) &(0x0f));//nand output_d(value1); delay_ms(1000); while(TRUE) { value =input_b(); value1= ((value) |(0x00));//or output_d(value1); delay_ms(1000); value1= ((value) &(0x0f));//& output_d(value1); delay_ms(1000); } } 37
  38. 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. 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
  40. 40. :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); while(TRUE) { if(input(PIN_B0)) { output_high(PIN_D0); } else if(!input(PIN_B0)) { output_low(PIN_D0); } delay_ms(1000); } 40 } 
  41. 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. 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. 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. 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. 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. 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. 47. Deal with LCD library :LCD interface with PIC 16f877a is shown in the following 47
  48. 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. 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. 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. 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
  52. 52. .Print "hello, world "on LCD #include <16f877A.h> #fuses xt,NOWDT #use delay(clock=4000000) #include "lcd_kbd1.c" void main() printf(LCD_PutChar," just"); LCD_SetPosition (LINE_16_2); printf(LCD_PutChar," university"); delay_ms(1000); } } { LCD_Init ( ); LCD_PutCmd ( CLEAR_DISP ); while(TRUE) { LCD_SetPosition ( LINE_16_1); 52
  53. 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. 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. 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. 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. 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. 58. Deal with keypad  The keypad is actually a collection of push-buttons, organized into a matrix. It looks like this: 58
  59. 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. 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. 61. Deal with keypad Use the previous subroutine to print the key pressed on LCD. Always repeat this operation 61
  62. 62. Deal with keypad #include<16f877a.h> #include "lcd_kbd2.c" #fuses NOWDT, XT, NOPROTECT #use delay (clock=4000000) void main() { int NOKEY = 0xff, key; LCD_Init(); LCD_PutCmd ( CLEAR_DISP ); LCD_SetPosition ( LINE_16_1); printf(LCD_PutChar,"welcome"); delay_ms(1000); LCD_PutCmd ( CLEAR_DISP ); LCD_SetPosition ( LINE_16_1); While (TRUE){ key = NOKEY; while (key==NOKEY) key = GetKey(); if (key==1) printf(LCD_PutChar,"1"); if (key==2) printf(LCD_PutChar,"2"); if (key==3) printf(LCD_PutChar,"3"); if(key==4) printf(LCD_PutChar,"4"); if(key==5) printf(LCD_PutChar,"5"); if(key==6) printf(LCD_PutChar,"6"); if(key==7) printf(LCD_PutChar,"7"); if(key==8) printf(LCD_PutChar,"8"); if(key==9) printf(LCD_PutChar,"9"); if(key==10) printf(LCD_PutChar,"0"); if(key==11) printf(LCD_PutChar,"*"); }} 62
  63. 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. 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. 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. 66. Analog to digital conversion Read both channel periodically. Let channel one is light and second is temperature. Display the value on LCD 66
  67. 67. Analog to digital conversion #include <16f877a.h> #fuses xt,NOWDT #use delay(clock=4000000) #include "lcd_d.c" float a,b; void main() { LCD_Init ( ); LCD_PutCmd ( CLEAR_DISP ); setup_adc_ports( RA0_RA1_RA3_ANALOG ); set_adc_channel(1); delay_us(100); b = read_adc(); LCD_SetPosition ( LINE_16_1); printf(LCD_PutChar,"Temp:%f",a); LCD_SetPosition (LINE_16_2); printf(LCD_PutChar,"light:%f",b); delay_ms(1000); } } setup_adc( ADC_CLOCK_INTERNAL ); while(TRUE) { set_adc_channel(0); delay_us(100); a = read_adc(); 67
  68. 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. 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. 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. 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. 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. 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. 74. Take the advantage of using interrupts :Finally write interrupt subroutine ; for example.3 #int_rda serial_isr() { //your own code } 74
  75. 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
  76. 76. RB change interrupt Now try to read value each time its changed then output onto port D 76
  77. 77. RB change interrupt #include<16f877A.h> #fuses XT, NOWDT #use delay (clock = 4000000) int x, y, data; #INT_RB void RB_isr() { delay_ms(200); x = input_b(); } void main() { set_tris_d(0x00); set_tris_b(0xff); enable_interrupts(INT_RB); enable_interrupts(GLOBAL); While(true) { output_d(x); delay_ms(1000); } } 77
  78. 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
  79. 79. External interrupt Now try to read value each time its changed and then display on LCD 79
  80. 80. External interrupt #include <16f877a.h> #fuses xt,NOWDT,NOLVP #use delay(clock=4000000) #int_ext void ext_isr() { output_high(PIN_D0); delay_ms(150); output_low(PIN_D0); void main() { enable_interrupts(GLOBAL); enable_interrupts(INT_EXT); ext_int_edge( H_TO_L ); start: } goto start; } 80
  81. 81. RDA RS232 interrupt This section will explained later ,we must introduce RS232 library first. 81
  82. 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. 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. 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. 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. 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. 87. Built two codes first send the following “ya raaab” every 5 seconds The second receive it and print on LCD 87
  88. 88. Serial interrupt //TX code #include<16f877A.h> #fuses XT, NOWDT #use delay (clock = 4000000) #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) void main() { While (TRUE) { Delay_ms(5000); Printf (“ya raaab”); } } 88
  89. 89. Serial interrupt //RX code #include<16f877A.h> #fuses XT, NOWDT #use delay (clock = 4000000) #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) #include "lcd_d.c" LCD_PutCmd ( CLEAR_DISP ); enable_interrupts(GLOBAL); enable_interrupts(INT_RDA); while(TRUE) { LCD_SetPosition ( LINE_16_1); printf(LCD_PutChar,"RX:"); LCD_SetPosition ( LINE_16_2); printf(LCD_PutChar,"%s",RX); int i; int1 flag; static char RX[25]; #int_rda void serial_isr() { RX[i++]=getch(); flag=1; } } } void main() { LCD_Init ( ); 89
  90. 90. Read analog input from A0,do adc, and out the result serial. 90
  91. 91. Serial interrupt #include <16f877a.h> #fuses xt,NOWDT,NOLVP #use delay(clock=4000000) #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) #include "lcd_d.c" float value1; int1 flag; void main() { enable_interrupts(GLOBAL); enable_interrupts(INT_RDA); setup_adc_ports( RA0_RA1_RA3_ANALOG ); setup_adc( ADC_CLOCK_INTERNAL ); LCD_Init ( ); LCD_PutCmd ( CLEAR_DISP ); 91
  92. 92. while(TRUE) { set_adc_channel(0); delay_us(100); value1 = read_adc(); LCD_SetPosition ( LINE_16_1); printf(LCD_PutChar,"value1"); LCD_SetPosition (LINE_16_2); printf(LCD_PutChar,"%2.3g",value1); delay_ms(1000); printf("%2.3g",value1); delay_ms(300); }//while }//MAIN 92
  93. 93. Serial interrupt How to send and receive data with ? different data type How to send and receive data with ? different baud rate 93
  94. 94. RS232 and RDA interrupt Output the key pressed serial and print it on LCD 94
  95. 95. Serial interrupt #include<16f877.h> #fuses NOWDT, XT, NOPROTECT #use delay (clock=4000000) #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) #include "lcd_kbd2.c" #byte PORT_B =6 int main() { int NOKEY = 0xff, key; LCD_Init(); //initialize LCD LCD_PutCmd ( CLEAR_DISP ); LCD_SetPosition ( LINE_20_1); START: //Start of the Program key = NOKEY; //get an input key while (key==NOKEY) key = GetKey(); 95
  96. 96. . if (key==1) printf(LCD_PutChar,"1"); if (key==2) printf(LCD_PutChar,"2"); if (key==3) printf(LCD_PutChar,"3"); if(key==4) printf(LCD_PutChar,"4"); if(key==5) printf(LCD_PutChar,"5"); if(key==6) printf(LCD_PutChar,"6"); if(key==7) printf(LCD_PutChar,"7"); if(key==8) printf(LCD_PutChar,"8"); if(key==9) printf(LCD_PutChar,"9"); if(key==10) printf(LCD_PutChar,"0"); if(key==11) printf(LCD_PutChar,"*"); if(key==12) { printf(LCD_PutChar,"#"); } delay_ms(200); printf("%u",key); goto START; } 96
  97. 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. 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. 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. 100. Use string library ?. How to analyze strings Try to get string analyze it and print on .LCD This will left for you 100

×