Thiet Ke He Thong Nhung Truong Quang Vinh Esd Ch5 New (Cuuduongthancong - Com)
Thiet Ke He Thong Nhung Truong Quang Vinh Esd Ch5 New (Cuuduongthancong - Com)
om
system
1. Software diagram
1
.c
2. C Programming for PIC
3. Timer and interrupt
ng
co
1
an
th
ng
1. Software diagram
o
processes.
cu
1
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
1. Software diagram
• Draw a software diagram:
– Use a rectangle for a process
– Use a rounded rectangle for a terminator
– Use a diamond shape for a decision
– Use a parallelogram for data
– Use a rectangle with two vertical lines for predefine process
om
N
.c
Y
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK 3
an
th
ng
1. Software diagram
o
– Show
Sh ththe iinterface
t f with
ith users
cu
Examples
2
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
1. Software diagram
• Software block diagram ‐ Example
Program flowchart State machine diagram
om
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK 5
an
th
ng
1. Software diagram
o
3
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
Group discussion
• Discus about below software diagram:
START •Decision block must have
YES/NO branches
•A process block must have 1
Init LCD input and 1 output
Read
temperature T
om
Turn OFF
T>T_đặt?
heater
.c
Turn ON heater
Team work
o
4
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
om
– PICC18 (www.htsoft.com)
– MPLAB C18,
C18 C30 (www.microchip.com)
( i hi )
.c
– CCS C (www.microchipc.com/reviews/CCS_C/)
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK 9
an
th
ng
Outline
o
du
5
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
om
Program function: Outputs an 8-bit code
Simulation circuit: OUTBYTE.DSN
*******************************************************/
.c
#include "16F877A.h" // MCU select
ng
{
output_D(255); // Switch on outputs
}
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK 11
an
th
ng
6
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
om
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK 13
an
th
ng
Setup IO Ports
o
• Port modes
du
– #use fast_io(port): leaves the state of the port the same unless re‐
configured
u
– #use fixed_io(port_outputs=pin,
fixed io(port outputs=pin pin): permanently sets up the data
cu
7
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
om
•The program for processing the data usually contains repetitive loops
and conditional branching,
branching which depends on an input or calculated
.c
value.
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK 15
an
th
ng
Variables
o
• Variables:
du
– automatically
i ll assigned
i d to theh next available
il bl location
l i or llocations
i (many
(
cu
8
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
#include "16F877A.h"
void main()
om
{
int x; // Declare variable and type
.c
x=99; // Assign variable value
output_D(x); // Display the value in binary
}
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK 17
an
th
ng
Looping
o
9
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
Decision Making
• The simplest way to illustrate basic decision making is to change an output
depending on the state of an input.
• Figure 2.4 show test circuit with input switch
om
the input is high.
•The switch needs to be
closed before running to
see this effect.
.c
•The LED cannot be
switched off again until the
program is restarted.
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK 19
an
th
ng
#include "16F877A.h"
void main()
{
int x; // Declare test var.
output_D(0); // Clear all outputs
10
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
Loop Control
• The program can be simplified by combining the input function
with the condition statement as follows:
– if (input(PIN_C0)) output_high(PIN_D0);
• The conditional sequence can also be selected by a while
condition.
• In Program WHILOOP.C ( Listing 2.5 )
– the input is tested in the loop condition statement and the output flashed on
om
and off while the switch is open (input high).
– If the switch is closed, the flash loop is not executed and the LED is switched off.
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK 21
an
th
ng
*******************************************************/
cu
#include "16F877A.h"
#use delay (clock=1000000) // MCU clock = 1MHz
void main(){
while(1)
{
while(input(PIN_C0)); // Repeat while switch open
{ output_high(PIN_D0);
d l
delay_ms(300);
(300) // D
Delay
l 0
0.3s
3
output_low(PIN_D0);
delay_ms(500); // Delay 0.5s
}
output_low(PIN_D0); // Switch off LED
}
}
Bộ môn Kỹ Thuật Điện Tử - ĐHBK 22
11
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
FOR Loop
• The WHILE loop repeats until some external event or internally
modified value satisfies the test condition.
• In other cases,, we need a loopp to repeat
p a fixed number of
times.
• The FOR loop uses a loop control variable, which is set to an
initial value and modified for each iteration while a defined
condition is true.
om
• In the demo program FORLOOP.C ( Listing 2.6 ), the loop control
parameters are given within the parentheses that follow the for
.c
keyword.
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK 23
an
th
ng
FOR Loop
o
du
u
cu
12
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
SIREN Program
• A program combining some of these basic features is shown in
SIREN.C ( Listing 2.7 ).
• This program outputs to a sounder rather than an LED,LED
operating at a higher frequency.
• The output is generated when the switch is closed (input C0
low).
• The delay picks up the incrementing value of “ step” giving a
om
longer pulse each time the for loop is executed.
• This
hi causes a burst
b off 255 pulses
l off increasing
i i length
l h (reducing
( d i
.c
frequency), repeating while the input is on.
• Note that 255 is the maximum value allowed for “ step, ” as it is
ng
an 8‐bit variable. co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK 25
an
th
ng
*******************************************************//
#include "16F877A.h"
cu
13
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
om
{
int // Declare global variables
.c
while(1) // Start control loop
{
// Program statements
ng
}
} // End main block
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK 27
an
th
ng
Blank Program
o
as a general template.
• We should tryy to be consistent in the header comment
u
14
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
Compiler Directives
#include source files Include another source code or header file
#use functions(parameters) Include library functions
C Blocks
main(condition) {statements } Main program block
while(condition) {statements } Conditional loop
if(condition) {statements } Conditional sequence
for(condition) {statements } Preset loop
om
C Functions
delay_ms(nnn) Delay in milliseconds
.c
delay_us(nnn) Delay in microseconds
output_x(n) Output 8-bit code at Port X
output_high(PIN_nn) Set output bit high
ng
output_low(PIN_nn) Set output bit low
input(PIN_nn) Get input
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK 29
an
th
ng
● Variable types
u
● Floating
Fl ti point i t numbers
b
cu
● Characters
● Assignment operators
15
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
int1 1 bit 0 1
16 bits 0 65525
om
unsigned int16
.c
unsigned int32 32 bits 0 4294967295
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK 31
an
th
ng
8 bits 1 23 bits
cu
16
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
om
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK
an
th
ng
0010 " 2 B R b r
cu
0011 # 3 C S c s
0100 $ 4 D T d t
0101 % 5 E U e u
0110 & 6 F V f v
0111 ' 7 G W g w
1000 ( 8 H X h x
1001 ) 9 I Y i y
1010 * : J Z j z
1011 + ; K [ k {
1100 , < L \ l |
1101 - = M ] m }
1110 . > N ^ n ~
1111 / ? O _ o Del
17
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
Single operand
Increment ++ Add one result = num1++; 0000 0000 0000
to integer 0001
Arithmetic Operation
om
Float num1 - num2; - 0000 0011 0111
Divide / Integer
g or result = 0000 1100 0000
Float num1 / num2; / 0000 0011 0100
.c
Logical Operation
Logical AND & Integer result = 1001 0011 0001
Bitwise num1 & num2; & 0111 0001 0001
ng
Logical OR | Integer result = 1001 0011 1111
Bitwise num1 | num2; | 0111 0001 0011
18
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
om
Greater than or equal to >= if(a >= 4) b=b+1;
L
Less th
than or equall tto <
<= if( <
if(a <= 5) b
b=b+0;
b+0
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK
an
th
ng
● While loops
● Break, continue, goto
u
● If,
If else,
else switch
cu
19
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
While Loops
om
The program DOWHILE shown in Listing 2 2.9
9 includes the
.c
same block of statements contained within both types of loop.
The WHILE block is not executed because the loop control
variable has been set to 0 and is never modified. By contrast, ‘
ng
count ’ is incremented within the DO WHILE loop before being
co
tested, and the loop therefore is executed.
Bộ môn Kỹ Thuật Điện Tử - ĐHBK
an
th
ng
Conditio
Statement
cu
n True?
Block
Statement
Block Conditio
n True?
20
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
om
outbyte1++;
count--;
}
count=0; // This loop is
.c
do // executed
{ output_C(outbyte2);
outbyte2++;
ng
count--;
} while (count!=0);
while(1){};
}
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK
an
th
ng
21
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
label
Statement
Block
Continue
om
Goto
Break
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK
an
th
ng
// CONTINUE.C
du
main()
i ()
cu
{
int outbyte;
again: outbyte=0; // Goto destination
while(1)
{
output_C(outbyte); // Loop operation
delay_ms(10);
outbyte++;
if (!input(PIN_D0)) continue; // Restart loop
if (!input(PIN_D1)) break; // Terminate loop
delay_ms(100);
if (outbyte==100) goto again; // Unconditional jump
}
}
22
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
YES NO
Condition Condition
True? True?
NO
YES
If If Else
block block block
om
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK
an
th
ng
Test Variable
u
NO
NO
NO
NO
Default
Procedure
23
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
om
case 2: output_C(3); // Input = 0x02, output = 0x03
break; // Quit block
case 3: output_C(7); // Input = 0x03, output = 0x07
break; // Quit block
default:output_C(0); // If none of these, output = 0x00
.c
}
// If..else option....................................................
if (input(PIN_D0)) output_C(1); // Input RD0 high
ng
if (input(PIN_D1)) output_C(2); // Input RD1 high
if (input(PIN_D0) && input(PIN_D1)) output_C(7); // Both high
else output_C(0); // If none of these, output = 0x00
}
co
}
an
th
ng
Class Assignments
o
24
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
om
within which the rest of the program is constructed.
• When running on a PC, main() is called by the operating system, and control
.c
is returned to the OS when the C program is terminated. In the
microcontroller, main() is simply used to indicate the start of the main
ng
control sequence, and more care needs to be taken in terminating the
program. co
• Normally,
Bộ môn Kỹ Thuật Điện
the Tử - ĐHBKruns in a continuous loop, but if not, the final
program
an
th
ng
void fun1()
u
{
statements
cu
...
...
Main() }
{
statements
fun1()
statements
statements
....
.... void fun2(arg) void fun3
.... { {
.... statements statements
statements ... ...
fun2(arg) fun3 ...
statements ... }
} return(val)
}
25
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
Basic Functions
• A simple program using a function is shown in FUNC1.C, Listing 2.12 . The
main block is very short, consisting of the function call out() and a while
statement, which provides the wait state at the end of main().
• IIn thi
this case, the
th variables
i bl are d declared
l d before
b f th
the main
i bl
block.
k This
Thi makes
k
them global in scope; that is, they are recognized throughout the whole
program and within all function blocks. The function out() is also defined
before main() , so that, when it is called, the function name is recognized. The
function starts with the keyword void , which indicates that no value is
om
returned by the function. The significance of this is explained shortly.
• The function itself simply increments Port C from 0 to 255. It contains a for
l
loop to
t provide
id a delay,
d l so thatth t the
th output
t t countt iis visible.
i ibl Thi
This iis a simple
i l
.c
alternative to the built‐in delay functions seen in previous examples and is
used here to avoid the inclusion of such functions while we study user‐
defined functions. It simply counts up to a preset value to waste time. The
ng
delay time is controlled by this set value.
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK
an
th
ng
// FUNC1.C
du
int16 n;
cu
26
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
om
execution, and the locations used for them are freed up on completion of
function call. This can be confirmed by watching the values of C program
variables
i bl whenh theth program is i executed
t d iin simulation
i l ti moded — the
th llocall
.c
ones become undefined once the relevant function block is terminated.
• If only global variables are used and the functions do not return results to
ng
the calling block, they become procedures. Program FUNC3.C, Listing 2.14 ,
shows how local variables are used. co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK
an
th
ng
// FUNC2.C
du
#include "16F877A.H”
int8 outbyte=1; // Declare global variables
int16 n,count;
u
cu
27
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
#include "16F877A.H"
int8 outbyte=1;
outbyte 1; // Declare global variables
int16 count;
om
{ outbyte++;
for(n=1;n<t;n++);
}
return outbyte; // Return output when loop stops
.c
}
main()
{
ng
count=50000;
out(count); // Pass count value to function
output_C(outbyte); // Display returned value
while(1);
co
}
Bộ môn Kỹ Thuật Điện Tử - ĐHBK
an
th
ng
28
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
om
.c
ng
co
an
th
ng
Compiler directives are typically used at the top of the program to set
up compiler options, control project components, define constant
labels, and so on before the main program is created. They are
preceded by the hash symbol to distinguish them from other
types of statements and do not have a semicolon to end the line.
29
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
om
.c
ng
co
an
th
o ng
du
u
cu
30
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
om
.c
ng
co
an
th
o ng
du
u
cu
31
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
om
.c
ng
co
an
th
o ng
du
u
cu
32
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
om
.c
ng
co
an
th
ng
• Interrupt:
du
33
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
om
4 INT_TIMER1 Timer 1 overflow
5 INT TIMER2
INT_TIMER2 Timer 2 overflow
.c
6 INT_CCP1 Timer 1 capture or compare detect
7 INT_CCP2 Timer 2 capture or compare detect
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK 67
an
th
ng
34
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
C Interrupts
• CCS C Interrupt Functions
om
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK 69
an
th
ng
Interrupt example
o
du
C1 4MHz
U1
13 33
OSC1/CLKIN RB0/INT
14
OSC2/CLKOUT RB1
34 R1
15pF 1 35 10k
MCLR/Vpp/THV RB2
X1 36
u
RB3/PGM
2 37
C2 3
RA0/AN0 RB4
38
cu
RA1/AN1 RB5
4 39
RA2/AN2/VREF- RB6/PGC
5 40
RA3/AN3/VREF+ RB7/PGD
15pF 6
RA4/T0CKI
7 15
RA5/AN4/SS RC0/T1OSO/T1CKI
16
RC1/T1OSI/CCP2
8 17
RE0/AN5/RD RC2/CCP1
9 18
RE1/AN6/WR RC3/SCK/SCL
10 23
RE2/AN7/CS RC4/SDI/SDA
24
RC5/SDO
25
RC6/TX/CK
26
RC7/RX/DT U2 RP1
19 1 20 9
RD0/PSP0
20 2 19 8
RD1/PSP1
21 3 18 7
RD2/PSP2
22 4 17 6
RD3/PSP3
27 5 16 5
RD4/PSP4
28 6 15 4
RD5/PSP5
29 7 14 3
RD6/PSP6
30 8 13 2
RD7/PSP7
9 12
PIC16F877 10 11 1
220R
35
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
Interrupt example
#include " 16F877A.h "
#use delay(clock = 4000000)
#int_ext // Interrupt name
void isrext() { // Interrupt service routine
output_D(255); // ISR action
delay_ms(1000);
}
void main() {
int x;
om
enable_interrupts(int_ext); // Enable named interrupt
enable_interrupts(global); // Enable all interrupts
ext_int_edge(H_TO_L); // Interrupt signal polarity
.c
while(1) { // Foreground loop
output_D(x); x + + ;
ng
delay_ms(100);
}
}
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK 71
an
th
ng
Interrupt statements
o
• #int_xxx
du
36
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
Interrupt statements
• enable_interrupts(level);
– Enables the interrupt at the given level.
– Examples:
enable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER0);
enable_interrupts(INT_TIMER1);
• Disable_interrupts(level)
om
– Disable interrupt at the given level
• ext_int_edge(H_TO_L);
.c
– Enables the edge on which the edge interrupt should trigger.
ng
This can be either rising or falling edge.
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK 73
an
th
ng
Class Assignment
o
37
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
om
• The p
principal
p modes of operation
p
.c
– Counters for external events
– Timers using the internal clock.
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK 75
an
th
ng
Counter/Timer Operation
o
38
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
Counter/Timer Operation
• Timer0 is an 8‐bit register that can count pulses at RA4; for this
purpose, the input is called T0CKI (Timer0 clock input).
• Timer1 is a 16‐bit register that can count up to 0xFFFF (65,535)
(65 535)
connected to RC0 (T1CKI) .
• The count can be recorded at any chosen point in time;
alternatively, an interrupt can be generated on overflow to notify
the processor that the maximum count has been exceeded.
om
• If the register is preloaded with a suitable value, the interrupt
occurs after a known count.
count
.c
• Timer0 has a prescaler that divides by up to 128;
• Timer1 has one that divides by 2, 4, or 8;
ng
• Timer2 has a prescaler and postscaler that divide by up to 16.
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK 77
an
th
ng
Timer Functions
o
du
AL | RTCC_DIV_8);
cu
39
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
Timer Functions
• Set_timer_0(mode)
– RTCC_INTERNAL, RTCC_EXT_L_TO_H or RTCC_EXT_H_TO_L
– RTCC_DIV_2, RTCC_DIV_4, RTCC_DIV_8, RTCC_DIV_16, RTCC_DIV_32,
RTCC_DIV_64, RTCC_DIV_128, RTCC_DIV_256
• Set_timer_1(mode)
– T1_DISABLED, T1_INTERNAL, T1_EXTERNAL, T1_EXTERNAL_SYNC
– T1_CLK_OUT
om
– T1_DIV_BY_1, T1_DIV_BY_2, T1_DIV_BY_4, T1_DIV_BY_8
• Example:
–
.c
set_timer_0(RTCC_INTERNAL | RTCC_DIV_8)
– setup_timer_1 ( T1_DISABLED ); //disables timer1
– setup_timer_1 ( T1_INTERNAL | T1_DIV_BY_4 );
ng
– setup_timer_1 ( T1_INTERNAL | T1_DIV_BY_8 );
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK 79
an
th
ng
Timer Functions
o
T2 DIV BY 4 T2
T2_DIV_BY_4, T2_DIV_BY_16
DIV BY 16
cu
40
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
Timer Functions
• Create delay by timers
N = 2n – (T * Fclock) / (4*Prescaler)
N: the count number
n: bit number of timer (Timer 0 & 2: n=8, Timer1:
n = 16)
T: delay time
om
Fclock: frequency of crystal
.c
Prescaler: prescaler number
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK 81
an
th
o ng
du
u
cu
82
41
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
PWM mode
• In Pulse Width Modulation mode, a CCP module can
be used to generate a timed output signal.
• This provides an output pulse waveform with an
adjustable high (mark) period.
• CCS C functions:
Set_pwm1_duty(value);
om
Set_pwm2_duty(value);
duty cycle = value / [ 4 * (PR2 +1 ) ]
.c
PR2 is the count value of timer 2
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK 83
an
th
ng
{
cu
42
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
Compare Mode
• Generate a timed output in conjunction with Timer1.
• The 16‐bit CCPR register is preloaded with a set value, which is
continuously compared with the Timer1 count count. When the count
matches the CCPR value, the output pin toggles and a CCP
interrupt is generated. If this operation is repeated, an interrupt
and output change with a known period can be obtained.
om
.c
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK 85
an
th
ng
Capture mode
o
• The CCP pin is set to input and monitored for a change of state.
du
rate.
cu
• When the next active edge is detected at the input, the timer
register value is copied to the CCP register. The count therefore
corresponds to the period of the input signal. With a 1MHz
instruction clock, the count is in microseconds
43
CuuDuongThanCong.com https://fb.com/tailieudientucntt
10/24/2014
om
2) Write the program that
create a 2Hz pulse on
.c
PIN_B1, given that
FOSC = 4MHz and
dutycycle = 20%
ng
co
Bộ môn Kỹ Thuật Điện Tử - ĐHBK 87
an
th
ng
Class Assignment
o
Timer 0.
0
cu
44
CuuDuongThanCong.com https://fb.com/tailieudientucntt