|
EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
小弟学习PIC16F1947,自己根据画了一个原理图(见附件),以后会把所有要做的程序都发上来,程序是用状态机写的,推荐给大家。: h% g7 G B4 Y5 l- e% q& p
( G) T7 ~& m) }( R1 i
/*: I0 p _* G) h7 G9 d$ @8 q
*@Name: PWM_led
0 p6 o9 A. {5 j. g* n*@version 1.0
, j. ~( t* L) X* @- O# E; y1 c8 u# l*@Author xiaopan, ~2 r! B0 i. k3 N
*@Date 2014.8.2
, b9 G4 q) ~8 d q! i4 k: `$ \) ]*// l. o2 s. a4 j' z0 Z5 c
; \7 a# J& d; _#include<pic.h>
8 k# j, l6 X2 X5 f7 p/ A#define uint unsigned int
5 y Z% z+ r. z3 A$ u4 K& W& l#define uchar unsigned char1 L g0 C4 O4 g* ~9 Q
) o# y0 x' j3 C/ j
#define PWM1(x) {CCPR3L = x>>2;DC3B0 = 1;DC3B1 = 1;} // X X>>1
; [& Q/ G# f7 I- B$ } l) t#define PWM2(x) {CCPR4L = x>>2;DC4B0 = 1;DC4B1 = 1;} // X X>>1
( r1 c& D' e& @. |6 Q( }0 ^0 y! X/ p [/ [3 }0 z6 m/ d9 q
uint Time = 0;! d1 Q( W3 V2 n
uint cnt_1ms = 0;! G1 B9 |" M; ?* k# ` M! H
uchar State = 0;
! Y6 @3 `) O( ~. c" Y: U3 n6 i1 k7 N! n+ ]& C, B
void GPIO_INIT();5 ^. u4 |0 _8 F, {5 y: T) I! D* l
void TIMER2_INIT();- m( L. U6 T& }9 g" l% J
void TIMER4_INIT();
/ v$ Q8 `/ {9 O) }- e9 L0 m; S0 B/ J* rvoid PWM_INIT();
! f2 C# g. J* N7 Gvoid Control_Task();
& N: y, R6 A9 s* K1 ]
4 [" p# ^. W- S$ T- \8 v9 e/*
- Y& ?8 t2 Z7 Y*@function 主函数: ]7 Q' }% d) B
*/
/ Q0 F% y& G5 C7 z4 v$ ?( Gvoid main()
; J6 i5 u2 s! v- X% r2 O: }3 L4 Z# e{
6 Y/ B6 q& Y! V+ D8 y+ F t# T GPIO_INIT();- E- {$ A+ s; n8 F+ P h' H
PWM_INIT();
9 }% t8 V- a7 @ TIMER2_INIT(); //PWM利用TIMER2" }5 m* j) P+ c! I+ K
TIMER4_INIT(); //定时1S出发改变一次占空比,调节PWM波形
& L. Z) C# q, W2 G while(1)
9 h8 v. I' R. ` {. z" t4 t k( T) D) d" m9 N
Control_Task();9 ^- u- v" g1 x+ r
}
' P0 W0 u6 s( g0 p) Y}4 R9 r1 N }5 X5 g `. P
/*" T+ X2 [. E0 ]2 C$ I
*@Function IO口初始化 RG0 CCP3 RG3 CCP4
- I! U4 M4 P$ k, R, t1 Z*/) f; e4 ]( @+ y# `3 ?
void GPIO_INIT()
: x7 ~$ d7 }. O# ]4 \# s{
- n4 _4 ^- V$ H. M+ _- T8 ^3 N TRISG = 0X00; //设置RG口为输出0 {. w9 K2 f' u2 R4 I
ANSELG = 0; //设置RG口为数字量
# d% x/ A1 u+ G' x: V: y) w LATG = 0; //设置RG口为低电平/ {# N% u' o9 g" S. C
% p |, F* t+ X! u, V8 T% p+ a% S TRISF = 0;( A- Q& G' y8 E3 X
ANSELF = 0;. E1 h! t2 c; u
LATF = 0;& J. Z7 Q6 p% X
}
+ H) B% V/ R! ?( P! N% o- i- p- m, Q+ @, Q
/*" R8 \* K0 W, ~. z, n- Z1 s
*@定时器2初始化. B. e1 `" A" q& L! E9 p X9 n. X5 H
*/) F8 M' A8 c7 j& U
void TIMER2_INIT()2 L7 `7 ]4 d- I
{
# `! X$ O) ?3 h2 f' N TMR2 = 0;
0 u3 C6 u j T PR2 = 255; //255
2 d" A2 \% D/ G- w5 m- k $ H) h1 J. I# x4 m! u
TMR2IF = 0;
6 o7 }% S+ u" X9 W) V
" O( J& Q1 A; U" N1 E3 G R T2CKPS0 = 1; //1
7 G/ w; ~ x& m" Q6 D T2CKPS1 = 0; //0
! K5 d3 J( T8 y+ F: ` ! d. H/ \5 h4 G8 ^0 f8 [9 `
TMR2ON = 1; 5 x1 \0 J- ~# A6 Q# x' m& `
}
3 ?$ {, \) ~+ W( S
1 ^' E; n7 S: w! s2 {2 W3 h5 r/*8 O" T! J/ Z) I, g$ V# i
*@定时器4初始化7 U6 p9 b& A0 |) L: I$ k0 |, G
*/4 Z a U5 \- ]" n
void TIMER4_INIT()3 n5 ]. s7 K4 F+ k9 l, H
{
3 c; j" Q. Z- |* d8 q! v/ a T4CON = 0x0e; //0000 1110
* [* e" w$ L' N% h
; _5 S; l$ ?$ K! Y0 I1 U TMR4IE = 1;
, K( I' d b1 ?: X! s" S$ w TMR4IF = 0; 3 a9 h1 M7 H% ~* U/ H: K h3 s8 D
* ]4 |% W- m2 j) v
TMR4 = 0X00;6 L0 Z& y+ V& `& P Z* ?
) ^2 m# F# Z: s4 h
PEIE = 1;
) `0 p7 Z/ e+ K, ] GIE = 1;# ^& u9 c3 `4 C$ O. b3 O
}& _( F3 v+ B) ]- l$ u# H" z5 a
# r2 e2 i6 I" s1 Z' P8 v* C4 V6 ~" K5 Y
/*
7 l' u" O$ W9 r4 I0 a*@ PWM初始化4 f- R4 h: q* v
*/
' d+ W% w( F0 Y2 B5 x8 _void PWM_INIT()1 r. d) N0 S: h1 G f$ e. i0 Z
{) R( E2 C# o. q
CCP3CON = 0X0C;( g8 Q! b3 Q I3 m% }
CCP4CON = 0X0C;" P+ C. X( x+ C6 f; S# g# n, T
1 p6 U! E! A1 i3 r( w3 W% r H
CCPTMRS0 &= 0X0F;
( g X7 R' k) w0 |( t 2 w3 q! _( N9 `5 I: D- ~
TRISG = 0;( h) W! ^( q9 X1 ^
}5 h3 N, F( k+ O' K, B/ w% P& s
; \- _6 O1 |1 j0 B3 i/*
- f5 d$ h, h, K*@Function 主任务函数: G$ t# p% s+ C# \+ n7 x7 ^, u% F
*/. G* u0 ?- [7 ]/ q3 n
void Control_Task()' q v' X+ O" ^& b
{2 V8 Y, t0 ]6 S% l& G
static uint Time ;7 N3 H' V0 B$ T; k1 J f" d
// static uchar State = 0;) p8 u$ n/ w9 U* z# g5 }; R! u
static uint Record_Time;
' d9 O' d: y9 [& }3 F uint Delta_Time;
5 f3 h# B# d/ H- M) g' r1 S+ e; y # A6 D0 o# k( p! x' g$ v
if(State == 0)
3 A5 o4 z8 X ?) f( m {3 D7 K/ T) s0 |/ x
Time = 1000;" q: ~( }( e* u
State = 1;- E* [8 t6 W5 l2 o" w4 |# e5 U
. n) x. y' \' R3 d; S7 G
}/ T" r) U5 Y6 D7 n
if(State == 1)
6 F8 }8 L% c) |( H* R# I+ I: ^: O {2 X4 P/ q. v6 `3 ^$ C2 A+ _
PWM1(Time);" ?& t% c" M2 J! k3 \" j1 m
PWM2(Time);
- l& }, v+ ~3 r7 U) j
( p' w) [% J; e: P Record_Time = cnt_1ms;
4 o3 G6 A! r: { 6 M, f& k$ m5 a$ Y9 Q" D/ Q
State = 2;& G4 `* D8 k: h/ S- ^$ `9 [0 E
} w4 M8 D' ]$ j; f
0 I6 ~, P2 y& r; S# `9 e" V2 [; N
if(State == 2)
6 X1 \( Z1 k; L% D' M {# ^. A3 L+ F) @9 F
if (cnt_1ms < Record_Time)
7 ~6 w. p8 K7 @; ~ {
: R* }* \" E% K; ` Delta_Time = 65536 - Record_Time + cnt_1ms;
1 P1 H8 p8 L0 B% {6 U }2 a& O1 _2 U" M0 R% i
else0 D% o5 x4 e- ~0 S ]* o8 Q, M
{: b% H) e/ N; J" j
Delta_Time = cnt_1ms - Record_Time;
/ [1 y, r, B% q5 Y; J- k }; A, N4 Z' V# z4 A
( }$ ^, |% R. x s if (Delta_Time >= 200) //300MS循环一次执行新的PWM_OUT& h, w( X2 ^+ O8 N
{
# L- T! b0 ^8 x Time = Time - 100;
7 r r% s+ z5 |. V- E& y4 E State = 1;1 o3 j- g8 e( W. S( T/ E7 W4 h
% S# h; K; q- P7 l& O3 H/ o
if(Time == 0)& Y$ \. L' M* u3 x
{
4 `% P2 D, X1 J! t; C0 ? Time = 0;0 y3 d" S+ ]6 K/ D( j! j; f7 S
State = 3;
3 N s& l4 x6 Q% k$ }7 l }$ `5 ?, g) l. @- f: c v
}; e8 Y& p% J* B# _9 Y7 e5 S
}
$ ]% W2 V; B! |+ f, R7 q O
" h9 I x7 `( X: \, l+ i; ? if(State == 3)
) c" c7 q; N9 g( O3 [ {
- n: i. l) O8 j8 T& x2 G! ] PWM1(Time);
# O# g( C2 d; | PWM2(Time);0 i% V2 i- X; H' p
! H! R* k2 A; `% c Record_Time = cnt_1ms; n; L" c) n% \
( n4 A2 e$ \. v0 O+ b6 J
State = 4;
& O1 Q8 J4 K5 z }0 V2 |4 P1 [/ T8 D0 [4 B
6 D9 ]2 e, {4 [, B; \ if(State == 4)& X7 Y# o% ?% G
{. s; _$ ?5 h1 y1 h; f+ l
if (cnt_1ms < Record_Time)3 ?6 D6 i; s9 |' v& r5 \" D# k
{. ?: }6 |3 K& H) |$ [
Delta_Time = 65536 - Record_Time + cnt_1ms; 6 ^5 {* P6 J: x. c+ T0 g
}+ n E* a! T3 P1 x
else
1 R% u$ ]( G% p! Z/ } {
6 U9 g! H7 t+ O' T j; \$ k# B/ K9 ` Delta_Time = cnt_1ms - Record_Time;
* G$ b, Q) V5 Z, B }
+ ?6 O' Z% ^! o& W' k: l6 }& M2 p p* w6 S8 d+ h6 B
if (Delta_Time >= 200) //300MS循环一次执行新的PWM_OUT
}; C7 q+ _# n& b {
0 Z! d( C2 C1 P( _& C6 _& E Time = Time + 100;/ z) y: p/ v: G
State = 3;
7 y0 {) X, ^2 {1 H4 C& X1 B9 ~7 S1 F 2 c- B7 Y. @0 A I" |/ t
if(Time == 1000)
" D8 _% f2 Z( f0 r- L {1 f% D% w/ \% h9 [9 {
State = 0;* [3 h9 ]) Z7 b, a# e# m0 m" G
}
2 v. d" u' D* h* h* n }9 h2 q# D: N/ i! F" B! D( O( y1 Q
}# q1 U8 y5 H p/ q0 V; C
}
. A9 |, I: @. X/ i$ }( b) B4 Q
: o" H! y( } o& r4 n; ^void interrupt ISR(void)
; E' m. l% L, [0 E{
: V9 O' m( |. S: U& a f4 p if(TMR4IF == 1)
8 N" }1 M5 Z. N. S {" G5 |9 f' {' j$ [- d
TMR4IF = 0;0 h. z( J7 K& `
cnt_1ms++;
5 @. F9 E7 L" _# W) y! j
" C2 F4 A3 D5 C% F8 x. @7 ^ // LATF0 = !LATF0;
- F( D6 z% ]4 O2 C( A# s }
( _( b. ?! S3 P3 i! l, f}( d' O( f# k. ]( ?- M2 \) @' c
|
|