|
EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
小弟学习PIC16F1947,自己根据画了一个原理图(见附件),以后会把所有要做的程序都发上来,程序是用状态机写的,推荐给大家。. G/ f4 h$ d3 V' o
" n# j: s6 Y% H# z
/*
8 h8 D3 q3 q- B; P( M*@Name: PWM_led
. A& A1 E" U/ `) `, Q*@version 1.0
e5 P0 ^6 {) G& I9 u( L( v* J*@Author xiaopan
% M. G$ M$ M. p*@Date 2014.8.2
! b4 |" ~. `! J X3 m+ B; Q*/- b6 m7 A0 c- d: C% T# l- ` k
( P# S% }6 J8 n% p1 O# t9 ^$ D" }0 ~* c#include<pic.h>6 ?( j; P7 q' q( b. x
#define uint unsigned int f5 G* ^, v" W% a
#define uchar unsigned char' ?- n7 ?8 I i: T
1 j( a/ v( t9 t" f, k
#define PWM1(x) {CCPR3L = x>>2;DC3B0 = 1;DC3B1 = 1;} // X X>>1
: ?' w/ i3 n0 `#define PWM2(x) {CCPR4L = x>>2;DC4B0 = 1;DC4B1 = 1;} // X X>>1 / ]% ^% q4 F1 b9 N( Y& n4 ?
- R! l$ x, o6 U$ m4 ~ i- y" \uint Time = 0;
* V, m/ k) ]) Fuint cnt_1ms = 0;4 c, m# E# v3 C& M) Y* ^ D
uchar State = 0;
6 p& B& j9 l6 P* u+ j
6 C# q' }1 E. a: X2 u2 i. Q: S% avoid GPIO_INIT();' K: M# X0 n5 U/ k
void TIMER2_INIT();
5 U! [* d* Q. \/ gvoid TIMER4_INIT();; O0 E& ]6 N j8 Y8 h! B
void PWM_INIT();
: p( l u5 e9 O% J G! e" Avoid Control_Task();9 ]+ [ t+ R- }$ p" r6 C# `: B
- y5 [6 Q. ?' D- C& M: |' m: Y5 m+ T/*
, b+ q# Z3 ?+ K e) W" v! j2 O; V2 u*@function 主函数( C# A8 w' ^, [) L
*/
. B" n9 K1 ?3 |) Evoid main()
: n1 P) `& B/ h1 f; x( \{* R* v) T0 K8 d1 {7 Z3 O4 r; _
GPIO_INIT();
! r# y+ w. x* @4 w$ x PWM_INIT();
2 X. C- J8 p; ?9 } TIMER2_INIT(); //PWM利用TIMER2
( H, i) O7 o+ `4 z- n TIMER4_INIT(); //定时1S出发改变一次占空比,调节PWM波形
5 d) u) I; V' s- ]7 a6 g$ _ while(1)# g% Y6 R3 ?+ p. ?, H* f) Q
{1 _2 I. P$ K: f* F# e l
Control_Task();
" O' X# ^4 S' A. F }
P, {: k8 s3 O9 P}
0 T! z9 i2 N1 x0 y# Z @/*
1 y& l L' E: }. c' ^*@Function IO口初始化 RG0 CCP3 RG3 CCP45 Z* g. W! i! K. r
*/( Q7 I! b" J' J+ M: K3 S) H
void GPIO_INIT()! }# \1 Z9 W, Z3 d$ Q3 l% `
{# y" H- t! ?: z) ]3 O( k: ?5 O
TRISG = 0X00; //设置RG口为输出3 P% T3 u5 J! B& d
ANSELG = 0; //设置RG口为数字量" Q: s% c3 _ h5 F8 h
LATG = 0; //设置RG口为低电平
' X0 j& m8 k! H" T 0 E4 C4 g1 F8 [1 p, M- [. u+ \' @
TRISF = 0;
: }9 a( c1 k7 m# {) T ANSELF = 0;: K( G4 ]% s" @- s, z" y, `$ V
LATF = 0;/ k* u+ e- L# k; J
}
6 J) X0 L3 K, {8 } h
/ n o1 B0 @8 y' H2 I/*
' t7 T1 E) Q% B. ?# [& a: R, t9 p*@定时器2初始化
3 U8 e0 o( X, H4 U8 q" s O; T*/" U2 Z- v- u0 ^- ^; k: e/ T
void TIMER2_INIT()
2 J+ [5 n2 N$ r3 I4 c, e& @{3 ^' V: |& z1 j' s
TMR2 = 0;
3 q- y. B0 b4 M. O# T8 V g PR2 = 255; //2554 k! T+ ?+ ~( ], C: m; A2 o" h4 G6 a/ u
( ^3 j- R1 k L8 P |1 d! ~ TMR2IF = 0;
- R: ~& f$ X0 o0 u0 m O. V. n, ~% ?# _; J9 u0 w) X" B
T2CKPS0 = 1; //1
2 A- U0 J1 ?0 M! ^, Q0 F$ W* G T2CKPS1 = 0; //00 @' Z- T5 h g
5 y9 _) J. j( Y( ~. U4 Q TMR2ON = 1; 9 {% q9 q4 g8 D; I( t/ ^4 z
}+ S% b, _0 `5 c. w1 o, l( @- @
* w$ ^+ k9 M% M; k2 K5 V$ h7 Q/*
5 `) {/ ?6 R, R2 t$ d*@定时器4初始化5 \0 R7 M1 s' f- `* k
*/
" F5 b* f0 n! evoid TIMER4_INIT()
D. N" V3 n1 F: ~/ k, l{0 }) S: ?2 P2 |: T. M
T4CON = 0x0e; //0000 1110: ]: J4 R4 I; r1 L
' m' o; M$ D( b2 C$ V" y
TMR4IE = 1; `6 [: m% n/ M) u" F% ?$ ^
TMR4IF = 0; ! r, _( d" a: z2 P/ }6 U' ?0 a
5 G2 m7 E! |8 F+ ?
TMR4 = 0X00;
, U8 m% j) f8 b# y/ z* `0 D7 k' `
2 f Q: v1 f0 }1 |. m8 b PEIE = 1;
- U) z# _4 a0 x, |3 S GIE = 1;' D# ~7 i: G) o7 }
}( d6 a r9 m) d- J4 o$ Z
8 p7 L+ R2 u$ c. f' K
/ T( k" D, ]. ^" q! E' p$ H1 B5 N/*; p" i- @9 s, s* D. d( R/ r- X& I! g
*@ PWM初始化
+ H& ~( ~: w4 g" w4 J*/* f# j& c2 Y3 k. a) ^( D. [
void PWM_INIT()3 Y8 W$ j0 O, N7 y( c- ~5 j% ~
{& D) z& }( n: W0 g% t K2 r7 |
CCP3CON = 0X0C;9 r' H+ ~) S; [. s; m
CCP4CON = 0X0C; E3 n( p+ v8 Y: `
6 N' c$ Y* f5 J5 h8 F, ]1 e5 V CCPTMRS0 &= 0X0F;
: f& D, O% W" _5 o) |3 c, z & A7 b, N' W/ B
TRISG = 0;
; o: ^3 L! H* L; M: q q}. H/ y( Z% B% J3 W; {
" o: ^5 k: y* \) h- q$ s
/*
, V @0 J. A& k. x2 B*@Function 主任务函数
; t8 b2 |3 Y) A+ t5 `0 R*/; ]. D' j$ r8 L$ x+ c/ X0 Z
void Control_Task()
! Z' S' u k$ q2 D% T{
. \" J+ j( J5 X static uint Time ;
: t4 H! K# `, d3 g6 W// static uchar State = 0;1 V, m- n! l! I( _9 Z: K; p
static uint Record_Time;5 O9 _2 q0 Y6 N; U, q
uint Delta_Time;
& }% K' }( e& B2 W1 X" i 9 j& ]2 P. }* Q- O" N1 Y' I
if(State == 0): ]. }) @ ^: @0 s' _$ T
{+ A7 l o# M- M% m8 d. W
Time = 1000;6 z$ x$ g1 P) ?, c! E6 ?7 o
State = 1;
/ K7 H6 W2 O) p0 x. {5 s2 `$ W- ?- I8 l* y
}
0 F6 u. S, ?; P# J if(State == 1)
) q- i1 a/ W+ h9 R" _" E {
. c* a+ }0 c& ~ PWM1(Time);$ H% S, Z$ l" \0 T: R, a! x) A% r
PWM2(Time);' l* {' d* p: W# {, e; y; n
5 ^) }) P' S. L3 ` Record_Time = cnt_1ms;
7 k8 o/ z3 J( e2 l7 {) D
0 _% y. y, v# s: W State = 2;
7 c9 i) r- R3 Z+ Y/ i* V }2 ] \* S' J+ E
8 m6 V$ z: j8 G9 f/ g& r8 X if(State == 2)' X' R( e* m0 h0 Q+ f2 o
{# }# a a- f' R0 L% F
if (cnt_1ms < Record_Time)" J4 Z2 d8 ]8 a* H/ W
{
. \: I: N8 g9 `: {. M/ w' o/ v4 L Delta_Time = 65536 - Record_Time + cnt_1ms; 5 E8 D( v; O$ n( l0 x1 U7 o
}+ {7 e: t+ `! y; d% J
else! A7 E' p& W" R3 c+ }. q& \
{
" g: F; U- q6 c+ g5 ?% \8 _3 _3 ] Delta_Time = cnt_1ms - Record_Time;
: N4 S9 h. {. l) E8 u }
) o9 h& a: c; z4 I. [, W9 ]2 o/ U+ V; o( |
if (Delta_Time >= 200) //300MS循环一次执行新的PWM_OUT
0 I# D+ H0 m! N' t3 a {
, y& s$ U! l4 U) o G Time = Time - 100;9 e9 V3 l8 r6 @; t1 J. g- w
State = 1;1 o* |9 P# M& W. u: t9 z& M
, X; s' H* S! d+ X# q( z4 X if(Time == 0)
4 `8 r& {& W7 e v {& }7 T2 h/ p! s- G' B
Time = 0;
. g2 r" p- Z7 v# m% @ State = 3;! j% A! t% E5 |9 m6 u
}) e+ E# V9 w7 f* J5 e( k& _; E; A
}0 D! V* L8 V& O. }
}. L% o: T$ y$ B! Y
1 S6 ~( x/ I% V if(State == 3)( O5 I5 u: J( F% z& e6 a
{. g/ S, N! ~9 P7 ]5 r
PWM1(Time);2 {# R- e5 A! Y1 E0 b9 S I, M
PWM2(Time);
7 c4 v5 }! H9 A
1 J3 _; Z* l9 K4 X$ s0 S- N1 \ Record_Time = cnt_1ms;2 V/ y8 A! T5 J: O* Q2 S0 c
Z( X3 J6 R+ D7 G
State = 4;9 E# y. d0 L6 L8 a
}& O. t, Y( R6 ?. ]! L
; }9 ]) C+ u% e% G. d7 ^5 M" _5 K
if(State == 4)4 Z' V7 M" ~; C/ x* o; s
{
Z3 J& O- o9 T4 i if (cnt_1ms < Record_Time)
& f0 o+ G# q9 a9 D6 { {, o9 W* O! G" y& ~
Delta_Time = 65536 - Record_Time + cnt_1ms; 5 U: M& @# w* A- o- X8 K
}
, a% t( l& W+ v else9 m- ~" o: w/ A3 K0 a) i f* _" x7 B7 c
{ M" g. A/ e( a2 u4 `
Delta_Time = cnt_1ms - Record_Time; ! @5 b& t/ F8 Y( t4 ~" [1 l
}
! n/ D. W) d0 I+ H) m3 ~( A8 y. e, O! r0 ~' t/ b
if (Delta_Time >= 200) //300MS循环一次执行新的PWM_OUT
2 ~7 ]& `. B: J4 `* P {5 g$ L$ z4 }1 Z1 C7 p5 k
Time = Time + 100;8 s9 l- T" V6 G4 y3 N
State = 3;, C l! n' ^) l% ?, e2 N
9 j: J: A. ^4 w/ a. ?
if(Time == 1000)
$ s) E# j& d" G! g {
, n; J, ?0 Z3 U, w& E$ D5 u State = 0;
# ~ |! U- F. s. } }
$ S$ \& w& A5 ? }- y8 ]* [7 M" s0 x" } u8 Q" A
}( t4 x+ w( ~7 b/ a$ m* U" ?
}, ]3 k. R4 q- r7 r$ w
: @* M4 ] `! R, k8 Z! W% {
void interrupt ISR(void)
' y; b7 y# t' y$ E' {; j{; Q( _2 N2 j* ?4 B- s9 X9 f) Y
if(TMR4IF == 1)) ? I; Z8 h/ ?, x/ k3 E1 a/ Q+ ]
{8 @4 Q$ H0 u3 G# K/ T3 o
TMR4IF = 0;. o& _5 o: H2 t4 T! z0 b$ C
cnt_1ms++;7 l0 C9 I* R) y: ?* _2 n
# v3 N* c5 Z8 l, F8 D2 o
// LATF0 = !LATF0;, W2 l/ [. [5 X$ ]2 |; P
} 0 }' O- @2 x7 {: ^( w
}
" z0 v% q' V) W, H g- ~/ d( `9 C) k |
|