|
EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
小弟学习PIC16F1947,自己根据画了一个原理图(见附件),以后会把所有要做的程序都发上来,程序是用状态机写的,推荐给大家,程序 如下所示:
2 c' m9 u$ ^5 s9 ^4 Q3 c; f
) k& C' F! C9 G4 M3 A/*0 j3 G8 K8 k: B' ^! W
*@Name: PWM_led
5 a4 D/ ?* L. Y. E- l8 g8 J/ f*@version 1.0
8 q% x( ^/ a3 [3 B' |% V*@Author xiaopan/ U9 M1 i. _: R$ \" ^' P9 D$ B
*@Date 2014.8.2
9 R/ F* c$ n0 G2 r6 o*/8 J9 E3 ?' u# {8 x* Y
| }3 @. S. W% [; @
#include<pic.h>9 s: H4 p- g4 r) e; N
#define uint unsigned int
7 C) @$ D7 l/ H8 A5 R#define uchar unsigned char
4 J( J5 @' o3 Y8 f7 W! V6 J% w* Q- @4 b# _4 q9 s, i+ V
#define PWM1(x) {CCPR3L = x>>2;DC3B0 = 1;DC3B1 = 1;} // X X>>1
4 y; T1 p z8 w3 Q! |, b- @; k#define PWM2(x) {CCPR4L = x>>2;DC4B0 = 1;DC4B1 = 1;} // X X>>1
. J2 f$ B8 p+ G, d7 v6 L& Y+ F; D) |; K4 f0 R, N" G0 d: G
uint Time = 0;
6 r% P4 m2 ]( _uint cnt_1ms = 0;
* |! c+ v6 [6 V3 n7 Fuchar State = 0;
8 \& {( p2 Q% \: v9 ^' {3 ~1 ]
5 R0 y. J# K: f/ Gvoid GPIO_INIT();. R: p9 e* t4 }2 S H# Y
void TIMER2_INIT();
% ]8 Q- K4 O2 P, P2 o9 F) kvoid TIMER4_INIT();
* e( [# t6 Z/ Q; B" k% J2 Gvoid PWM_INIT();9 _2 j, m4 z. [$ J0 Q; j1 }- y
void Control_Task();7 _- K! M" o7 t1 S# ?/ T, Q
5 P6 {7 \1 [. k0 O" l9 p
/*' L& h- W! ]& {. ^1 z( g
*@function 主函数) _2 ?8 i5 E$ I, ]8 G9 C* B
*/
( A& T- M5 b. Q/ ~( |void main()6 [3 c% M: D8 d' M. |8 U# B
{
/ A7 [+ V, u, `. I6 g( M7 V GPIO_INIT();
( {4 }" k7 w: m8 r+ r6 b1 N) h2 T PWM_INIT();* {0 P( \- S7 k9 b2 F
TIMER2_INIT(); //PWM利用TIMER2# x- {) a8 C1 ?
TIMER4_INIT(); //定时1S出发改变一次占空比,调节PWM波形
/ ^% {1 S# g& U" _7 r% }( L while(1)
7 s8 ^3 f, U- o: E/ h {! J/ p: H1 P' B. A) y2 t1 y1 `9 j. W2 l
Control_Task();5 g# q0 J4 c# J
}
3 y& {5 b8 p/ z1 E0 o/ _. M}
+ o+ F( O: E1 F# O( C6 E* E, d/*' y T% Y: C% O/ ~5 V7 k; p" q4 F& c
*@Function IO口初始化 RG0 CCP3 RG3 CCP4& J0 T2 ?# ^! B* E: A( a5 P
*/
. p% z- _& k& p/ c/ Ovoid GPIO_INIT()0 G& i( n" T: }. z7 M
{% \8 U3 u, a1 S5 x' g/ D7 t
TRISG = 0X00; //设置RG口为输出, X6 ~, |4 N8 M
ANSELG = 0; //设置RG口为数字量
- z2 k1 m: ]1 g- f( `3 z LATG = 0; //设置RG口为低电平. M0 i% G& E! N
% u8 J$ D3 Y1 O( [5 x
TRISF = 0;
/ Z- X( P/ s* _' K) w" F% n4 ^ ANSELF = 0;& e/ n7 P' f1 q4 r3 x7 d+ y3 U2 M
LATF = 0;
/ ~& A# e; U0 q4 X R! r I}
; G7 G7 o6 e: |; O7 U+ q" k7 M& G- ]( ~6 ^
/*2 o1 m# N- ` q: c
*@定时器2初始化
4 A( U6 E' R$ H8 s7 F*/0 k' l% ^3 W( o0 g/ j2 O% R6 p
void TIMER2_INIT()
G j" E" ~, o4 A{
* J' v( W$ x2 D- [# _" n TMR2 = 0;9 F9 k2 z4 Z2 y" ^; q
PR2 = 255; //2550 \5 J6 c4 f0 V# r. N4 F5 G
# l1 s/ j% h" C( X9 i( L TMR2IF = 0;
+ u( c, m" K0 R* \7 r 0 B; w4 m- J: S+ U
T2CKPS0 = 1; //13 G# w q. Y/ ~
T2CKPS1 = 0; //0: d3 w" }" A0 m
: Y0 g( z1 m5 D% Y+ l. }, F2 F TMR2ON = 1; / N6 _: B. p5 e+ M4 y
}4 w; h" C% G* S3 s7 e0 }
- J+ N4 X S$ o, J- J
/*; A9 R/ e6 ~4 i: [* o& p* M
*@定时器4初始化
* Z" x2 _+ Z8 O. B4 e; ^: l6 c*/9 \$ C3 h( S3 C8 ~* V5 `
void TIMER4_INIT()- A+ i( p+ x s g
{- Y; X& {3 w/ V2 w# @1 @: s3 e( S! [
T4CON = 0x0e; //0000 1110. x3 i5 k' i6 M. T( S6 F
/ r9 }$ R7 H6 _: g# B
TMR4IE = 1;' |9 @4 I. }4 z8 k" J! f; ]
TMR4IF = 0;
3 x( `. b" q* ^/ {2 E/ i' a' b) R
# |7 @* {3 u) {0 g" V" F1 G$ Y TMR4 = 0X00;; Y. f7 ~5 M+ L) ]' B$ p* C2 q
/ j- {# Q% ]% ^( V( W* ^& Y
PEIE = 1; W( G% K! i0 }7 C* E! Z0 f b4 ]
GIE = 1;7 O: W, N: o' f# |+ U
}
% G. W7 r6 o1 Q( @8 x% a$ y7 {% _2 f$ H; c* d6 `. h h9 N0 x
& x% ], f% v. q9 Z0 r) r* o% _/*
' Z1 }' n9 g. Y*@ PWM初始化: t% K1 G% O( ?( u3 E
*/
' T2 n) M' ~, V& i: v! a" [void PWM_INIT()
8 P5 u7 G" b$ g* N; A: Y{9 j! V( |( j$ ~/ E6 v, M8 D
CCP3CON = 0X0C;
/ k1 V, e0 C l, ~$ [8 @2 q' `# B* K CCP4CON = 0X0C;
! g6 y# Q1 r* t% R% D" ~- l; C9 G) v
j/ l$ b/ Z* o* Q" D9 v' i CCPTMRS0 &= 0X0F;2 w( N, Q$ `# N* T' P
4 w/ {& x. P |. `. j
TRISG = 0;- r+ N1 D4 D0 N! D! E
}) [8 w. f+ ~4 R
/ q# w# ?5 X% A& m/ z
/*
4 m! _- [' K" E# v, c" V Q3 y*@Function 主任务函数
7 N+ f6 {* c- V8 m7 m$ J, c# F*/
+ M$ G. p2 Y. o3 I# _void Control_Task()
( R5 Z$ u9 b1 d/ a{
% d# c; v" P! O2 ?5 } static uint Time ;
' W. a" H7 e; g. a; z" s// static uchar State = 0;
# Q5 |0 ~2 v4 m- g static uint Record_Time;
/ D3 |6 R0 ?6 N, ]6 U3 f uint Delta_Time;% p5 m, A( R* I* |4 h/ W
0 H* F- @6 |2 h% E* N* w7 C; n0 @ if(State == 0)
+ H+ n9 k/ n: g% t/ I2 U2 B {
5 v% s4 C! f! v" e9 C8 U' d0 K Time = 1000;
5 |, x! }7 m* Z) B1 p! n State = 1;/ ]3 T5 F6 C* P' c' g$ d0 K
$ W6 n* ~+ W, E/ r1 W: A& H
}
7 s! C+ U" S9 d- v- F) A if(State == 1)
6 J/ n5 \2 _. I( Z) W5 ` {0 |3 q- O# i5 N; q
PWM1(Time);
* V/ a7 i3 N& J( M; c5 b; Q PWM2(Time);
6 Z4 ~4 G$ ]# ^4 L
. z- l. q5 r( }1 {3 I# \' u! a+ W, E6 _ Record_Time = cnt_1ms;+ U" U# u% g3 j) d2 o) {
" r9 Z( ^- [. c State = 2;
2 F; G4 o2 v- u: K; B, w }% [( d4 e' u/ a& ^
8 C9 }( _, x s5 |/ J4 { if(State == 2)$ }7 g0 Y- b4 E9 H8 _
{# R+ D9 n( N# T2 B$ Q* N$ H7 P9 w$ l
if (cnt_1ms < Record_Time)* R2 z+ x2 R! P0 w0 w0 }6 S
{/ @* _& ]6 J2 u. W7 F
Delta_Time = 65536 - Record_Time + cnt_1ms;
! W0 `5 \: y! ]+ n# C }
7 \: M) S* }/ |) I4 z else
, C2 u, z8 ~9 D+ H! }, o {" B! u' G: v8 J2 S. F+ z( v) ^
Delta_Time = cnt_1ms - Record_Time; 0 i, w% u0 e" v/ m2 d
}
4 c' \6 p6 @: z; A
! f) U# I h8 X3 f- L if (Delta_Time >= 200) //300MS循环一次执行新的PWM_OUT
& Q" N% s U) Z9 V7 ~" ? {$ O# g9 J& e* k2 _% O
Time = Time - 100;
' v" M. R- i. b8 ]- A5 S State = 1;
6 q7 ^, l+ x- ]6 g
/ h* r6 Y, \$ M4 r3 W# b0 Z) O1 `4 B- x if(Time == 0)& `$ _% k2 G' G% k- i
{' K, p/ |% @4 V; `( D1 t
Time = 0;# d7 {# b* d" K# {% |
State = 3;$ k- E: p4 n# C
}
8 s$ X* C0 n& k- U- v8 w0 M% g }7 y& M8 u$ Y9 r- P- P" Z3 e
}
# d7 E% b# {5 |' J; g & [7 u# A% c# n1 x7 V
if(State == 3)
1 h# U9 v: M5 i {' c1 o8 h9 B" a c4 \2 |
PWM1(Time);! o. a* M+ l; k* a
PWM2(Time);
4 h% c4 P" V8 C# o" f. I+ ?
& X$ x7 D( `9 d# k0 W0 t Record_Time = cnt_1ms;
- d" N6 M' z6 j4 E
! n* r$ v& V5 }" k3 B# L/ d4 e State = 4;
& k, h3 o* @$ R/ A1 }2 M) o }: g) r `# u8 ~* `- K$ }7 @/ y( d
3 m2 ]/ F8 x" R i if(State == 4)3 R, v. ^ Q. q P
{8 a$ y2 V' Y* u
if (cnt_1ms < Record_Time)- l: r5 j# J. H1 v8 c% ]' K, }/ ]
{
" K! u7 R: f' R: h- c Delta_Time = 65536 - Record_Time + cnt_1ms; 6 l; _' e1 a! H% I
}
# n, L$ W9 `7 p% g else9 A9 M) g* ?# k# H2 M* V5 k
{
5 ]. P) x& i4 B- X Delta_Time = cnt_1ms - Record_Time; * u/ @9 |- K u
}
' s8 [4 |& z( F4 M3 e# s& g, ?0 c( ^1 R) b; r
if (Delta_Time >= 200) //300MS循环一次执行新的PWM_OUT7 O5 y8 E! g' x8 r% T
{
/ G9 O" ]5 R R Time = Time + 100;: n3 [* {3 j% z, }/ q6 w+ z7 C% e; \
State = 3;, p$ t# j2 H+ g7 i+ x) F
6 y/ }" L* L* t& l7 V5 a L if(Time == 1000)3 N; F* M/ Q6 i, D1 ~
{
; a; S' V3 r' C* D5 @* N( a, c$ W State = 0;
4 q0 k- Z; L" c. V( N }
. q$ g: k5 e% i% r5 [2 d }
O) l) m/ I0 w5 ~. v0 T* V }
, M6 i8 W; {) z! [}
# }7 ~' S* S; m& f
8 z) \/ T2 T9 Yvoid interrupt ISR(void)9 P# Q5 E$ e. M: b
{
, [6 G3 ?& z3 c2 j: W if(TMR4IF == 1)* u/ b- r7 s; a
{
) T3 J" X- \6 a2 g2 F TMR4IF = 0;
0 ]" G/ Y3 y1 x: u4 E/ o5 J6 c; s9 I cnt_1ms++;+ `3 y: Q1 Y( d3 I8 S4 Y7 ?
$ `: ] A5 x: O7 K! c8 H/ M3 } // LATF0 = !LATF0;
5 o) {8 T7 \: A+ L7 p3 R } - j; S! F1 P$ G4 }( D' f o3 }
} |
|