Practical Training On Arduino
Practical Training On Arduino
/ *
Fadi ng
Thi s exampl e shows how t o f ade an LED usi ng t he
anal ogWr i t e( ) f unct i on.
The ci r cui t :
* LED at t ached f r omdi gi t al pi n 9 t o gr ound.
*/
Arduino Code Examples - 3
ME407 Microcontrollers&Arduino
Seminar
32
i nt l edPi n = 9; / / LED connect ed t o di gi t al pi n 9
voi d set up( ) {
/ / not hi ng happens i n set up
}
voi d l oop( ) {
/ / f ade i n f r ommi n t o max i n i ncr ement s of 5 poi nt s:
f or ( i nt f adeVal ue = 0 ; f adeVal ue <= 255; f adeVal ue +=5) {
/ / set s t he val ue ( r ange f r om0 t o 255) :
anal ogWr i t e( l edPi n, f adeVal ue) ;
/ / wai t f or 30 mi l l i seconds t o see t he di mmi ng ef f ect
del ay( 30) ;
}
/ / f ade out f r ommax t o mi n i n i ncr ement s of 5 poi nt s:
f or ( i nt f adeVal ue = 255 ; f adeVal ue >= 0; f adeVal ue - =5) {
/ / set s t he val ue ( r ange f r om0 t o 255) :
anal ogWr i t e( l edPi n, f adeVal ue) ;
/ / wai t f or 30 mi l l i seconds t o see t he di mmi ng ef f ect
del ay( 30) ;
}
}
Arduino Code Examples - 3
ME407 Microcontrollers&Arduino
Seminar
33
Pulse Width Modulation, or PWM, is a technique for getting analog results
with digital means. Digital control is used to create a square wave, a signal
switched between on and off. This on-off pattern can simulate voltages in
between full on (5 Volts) and off (0 Volts) by changing the portion of the time
the signal spends on versus the time that the signal spends off.
Arduino Code Examples - 3
ME407 Microcontrollers&Arduino
Seminar
34
Arduino's PWM frequency at about 500Hz,
the green lines would measure
2 milliseconds each.
A call to analogWrite() is on a scale of 0 -
255, such that analogWrite(255) requests
a 100% duty cycle (always on), and
analogWrite(127) is a 50% duty cycle (on
half the time) for example.
Arduino Code Examples - 4
ME407 Microcontrollers&Arduino
Seminar
35
Stepper motors, due to their unique design, can be controlled to a high degree
of accuracy without any feedback mechanisms. The shaft of a stepper, mounted
with a series of magnets, is controlled by a series of electromagnetic coils that
are charged positively and negatively in a specific sequence, precisely moving it
forward or backward in small "steps".
Arduino Code Examples - 4
ME407 Microcontrollers&Arduino
Seminar
36
Unipolar Stepper Circuit
Arduino Code Examples - 4
ME407 Microcontrollers&Arduino
Seminar
37
Code
/ *
* Mot or Knob
*
* A st epper mot or f ol l ows t he t ur ns of a pot ent i omet er
* ( or ot her sensor ) on anal og i nput 0.
*/
#i ncl ude <St epper . h>
/ / change t hi s t o t he number of st eps on your mot or
#def i ne STEPS 100
/ / cr eat e an i nst ance of t he st epper cl ass, speci f yi ng
/ / t he number of st eps of t he mot or and t he pi ns i t ' s
/ / at t ached t o
St epper st epper ( STEPS, 8, 9, 10, 11) ;
/ / t he pr evi ous r eadi ng f r omt he anal og i nput
i nt pr evi ous = 0;
=
where the timer index X is 0 or 1 while
n e {1, 2, 4, ...} is the prescaler.
ME407 Microcontrollers&Arduino
Seminar
80
Timer Interrupts (Contd)
One can precisely set the desired interrupt frequency (f
d
)
[Hz] by selecting the initial value (m
0
) of timerX:
)
`
=
+
d
X
f n
m
6
) 1 ( 8
0
10 5
int 2
Hence, at the beginning of ISR, the counter value is set
to m
0
using set _t i mer X( m
0
) . As an illustration, let
us calculate the initial counter value for timer0 (with
RTCC_DI V_128 option: n = 128) if f
d
= 750 Hz:
204 52 256
750 128
10 5
int 2
6
) 0 1 ( 8
0
= =
)
`
=
+
m
ME407 Microcontrollers&Arduino
Seminar
81
Example 6a Timer0 Interrupt
As an example, we shall revisit Example 5.
Instead of connecting an external clock to the
timer0, we shall utilize PICmicros internal clock
@ 5 Mhz to generate timer0 interrupt.
ISR is to count up each-time it is invoked.
As in the previous case, we would like to send
count values to the serial port monitor (PC) over
the serial port whenever the pushbutton is
pressed.
ME407 Microcontrollers&Arduino
Seminar
82
Example 6a - C program
/ /
/ / Pr epr ocessor di r ect i ves ar e same as Exampl e 5a
/ /
l ong count = 0; / * Gl obal var i abl e */
#I NT_RTCC / * I SR f or t i mer 0- over f l ow i nt er r upt */
voi d i sr ( ) {count ++; }
voi d mai n( ) {
set _t r i s_b( 255) ; / * Al l pi ns of Por t B ar e i nput s */
set up_count er s( RTCC_I NTERNAL, RTCC_DI V_256) ; / * Set up RTCC */
set _t i mer 0( 0) ; / * I . C. = 0 */
enabl e_i nt er r upt s( I NT_RTCC) ; / * Enabl e i nt er r upt s */
enabl e_i nt er r upt s( GLOBAL) ;
whi l e( TRUE) {
i f ( ! SW) { / * When SWi s depr essed. . . */
pr i nt f ( " Count er = %l u\ n\ r " , count ) ; / * . . . send i t out ! */
del ay_ms( 200) ; }; / * Wai t 200 ms t o debounce */
}
}
ME407 Microcontrollers&Arduino
Seminar
83
External Interrupt
PIC16F877A allows the use of a single external
interrupt (INT_EXT):
External (interrupt) source must be connected to
RB0/INT pin.
Before enabling the external interrupt, the type
of clock edge triggering intrerupt should be
specified:
ext _i nt _edge( L_TO_H) denotes the positive edge
of the clock triggers the interrupt.
ext _i nt _edge( H_TO_L) denotes the negative
edge of the clock triggers the interrupt.
ME407 Microcontrollers&Arduino
Seminar
84
Example 6b External Interrupt
In this example, the external clock is to be coupled to RB0/INT pin. We will
write a C program for the PICmicro such that it will count the pulses of
external clock and will send out counter values to the serial port monitor (PC)
whenever the pushbutton is depressed.
+
RB0/INT
1
CD40106B
2
20 kO
100 F
V
dd
(14) = 5V
V
ss
(7) = GND
RB1
10 kO
V
dd
= 5V
Button
ME407 Microcontrollers&Arduino
Seminar
85
Example 6b External Interrupt
/ /
/ / Pr epr ocessor di r ect i ves ar e same as Exampl e 5a
/ /
l ong count = 0; / * Gl obal var i abl e */
#I NT_EXT / * I SR f or ext er nal B0 i nt er r upt */
voi d i sr ( ) {count ++; }
voi d mai n( ) {
set _t r i s_b( 255) ; / * Al l pi ns of Por t B ar e i nput s */
ext _i nt _edge( L_TO_H) ; / * I nt er r upt on ( +) edge */
enabl e_i nt er r upt s( I NT_EXT) ; / * Enabl e i nt er r upt s */
enabl e_i nt er r upt s( GLOBAL) ;
whi l e( TRUE) {
i f ( ! SW) { / * When SWi s depr essed. . . */
pr i nt f ( " Count er = %l u\ n\ r " , count ) ; / * . . . send i t out ! */
del ay_ms( 200) ; }; / * Wai t 200 ms t o debounce */
}
}
ME407 Microcontrollers&Arduino
Seminar
86
Pin-change Interrupt
In 16F877A, pin change interrupt (INT_RB) is
used to detect the logic state changes at pin
RB7-RB4.
If there is any change in these four inputs,
PICmicro generates an interrupt.
This interrupt is intended for keyboard wake-up
Microchip does not recommend the utilization of this interrupt
for any other applications.
Any read or write to Port B (say RB3-RB0) clears the
mismatch condition.
ME407 Microcontrollers&Arduino
Seminar
87
Example 6c Pin Change Interrupt
In this exercise, we would like to develop a C program for PICmicro so that the
microcontroller measures the input frequency (Hz) of the external clock at RB7
and transmits that information through the serial port. Note that for testing
purposes, a LED coupled to RB1 pin gets toggled whenever a pin-change
interrupt is generated.
RB1
470 O
+
RB7
1
CD40106B
2
20 kO
100 F
V
dd
(14) = 5V
V
ss
(7) = GND
ME407 Microcontrollers&Arduino
Seminar
88
Example 6c RB Change Interrupt
#i ncl ude <16F877A. h>
#f uses HS, NOWDT, NOPROTECT, NOLVP
#use del ay( cl ock=20000000)
#use r s232( baud=19200, xmi t =PI N_C6, r cv=PI N_C7)
#or g 0x1F00, 0x1FFF {}
#use f ast _i o( B)
#opt 9
#bi t LED = 0x06. 1 / * RB1 dr i ves a LED */
i nt count = 0; / * Gl obal var i abl es */
shor t Q = 0;
#I NT_RB / * RB Change I SR */
voi d i sr ( ) {
count ++; / * Count changes */
Q = ! Q; / * Toggl e LED */
LED = Q; / * must R/ Wt o Por t B bef or e cl ear i ng RBI F */
cl ear _i nt er r upt ( I NT_RB) ; / * Cl ear RBI F */
}
ME407 Microcontrollers&Arduino
Seminar
89
Example 6c (Contd)
voi d mai n( ) {
i nt f r eq = 0, f r eq_ol d;
set _t r i s_b( 253) ; / * Onl y RB1 i s out put */
por t _b_pul l ups( TRUE) ; / * I nt er nal pul l - ups act i vat ed */
enabl e_i nt er r upt s( I NT_RB) ; / * Enabl e i nt er r upt s */
enabl e_i nt er r upt s( GLOBAL) ;
whi l e( TRUE) {
f r eq_ol d = f r eq; / * Cal cul at e f r equency */
f r eq = count ; count = 0;
i f ( f r eq! =f r eq_ol d) pr i nt f ( " Fr equency = %d [ Hz] \ r " , f r eq) ;
del ay_ms( 1000) ; / * Sampl i ng per i od i s 1s */
}
}
ME407 Microcontrollers&Arduino
Seminar
90
EEPROM Commands
PIC 16F877A incorporate 256-byte non-volatile
memory.
The user can access EEPROM through the
following built-in functions of CCS C:
r ead_eepr om( addr ess) reads a byte from the
EEPROM at the specified address (8-bit).
The address ranges between 0 and 255 for PIC 16F877A.
wr i t e_eepr om( addr ess, val ue) writes a byte
(value) to EEPROM at the specified address.
This function takes several milliseconds to execute.
ME407 Microcontrollers&Arduino
Seminar
91
Watch Dog Timer (WDT)
The WDT function is to monitor the computer system whether work
normally.
Otherwise, it will have some measures to fix up the system.
The main reason of the WDT is that software sometimes got lost:
Enter infinite loops
Unexpected (conditional) branching puts the routine to go crazy.
The WDT unique intention is to reset the machine if certain
programmable timer expires. Therefore properly operating"
software should "reset" WDT before it does!
For instance, if you program the WDT to 1 second, you should reset the
WDT timer before that happens.
If a routine gets lost, the WDT will reset your machine in one
second, so not much is wasted.
ME407 Microcontrollers&Arduino
Seminar