|
EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
小弟学习PIC16F1947,自己根据画了一个原理图(见附件),以后会把所有要做的程序都发上来,程序是用状态机写的,推荐给大家。
/ A ]! j& h: H+ D( I$ t% Y' G8 I/ D% p0 `/ l9 a- o# m
/*
: o/ ]- o) T( G) S*@Name: PWM_led
}& j9 F+ d7 L7 ~# z' r*@version 1.0
. _& a' [8 c9 l8 Z*@Author xiaopan
6 u- I( \& _. N, N" O*@Date 2014.8.2
1 k8 C9 i0 A# F*/
- M4 z( f, \% D; f' U& f P6 d( ?. a- s2 L3 j Z( b
#include<pic.h>9 m3 w* o8 c1 |* f# o Y
#define uint unsigned int ' u7 C2 I2 s; D5 m/ i
#define uchar unsigned char
$ a6 k/ ?9 `# e- D- T# [9 c( p0 [9 e# t+ d
#define PWM1(x) {CCPR3L = x>>2;DC3B0 = 1;DC3B1 = 1;} // X X>>1 : x5 v4 T w8 X3 K- L+ x7 p
#define PWM2(x) {CCPR4L = x>>2;DC4B0 = 1;DC4B1 = 1;} // X X>>1 # x5 a0 T9 g% i, G# b4 {+ v
5 G% O2 }, g% L# {
uint Time = 0;2 U& O* Z! S0 g7 _' G# a2 Y; H- o
uint cnt_1ms = 0;
. u- k0 C& w! @0 j4 a* _4 ^uchar State = 0;+ P5 n# n% k2 b% r* x( V
9 q3 n# E) Z# h) n
void GPIO_INIT();1 J7 F& k" R. _5 D3 C4 ]2 p
void TIMER2_INIT();
1 P3 ^: Z6 Y; p' U# \ F* i8 v" Cvoid TIMER4_INIT();
0 q9 D9 V. [2 f8 ^* s9 Zvoid PWM_INIT();; C7 x8 S3 D4 a9 h n
void Control_Task();: \+ B; }+ u5 ?4 ]) n
5 K' I- I K- D, t9 z M/*! m, I; J2 {3 x* L# ?* m4 @7 R. O( y
*@function 主函数
8 J0 m& p9 O4 k3 B' C6 C- w*/
0 B9 o% ~ C1 h/ u8 H" g1 a9 p7 svoid main()
2 z1 Q+ M$ r! E6 L$ c; H{7 Q! Q. M6 T& v1 [" U
GPIO_INIT();1 B: V- X4 Y1 a) ?
PWM_INIT();
: O/ O$ E- n- W L+ c" \ TIMER2_INIT(); //PWM利用TIMER2
$ `* D/ F) ^" f2 Y6 { TIMER4_INIT(); //定时1S出发改变一次占空比,调节PWM波形$ i: f% w7 B! o' T* q" @2 `" m
while(1)
4 i+ @" ^- p) @( ?! x1 f; R1 O {& o q0 z% w2 k
Control_Task();
3 {) ^* O! l9 @) U }" `3 u! E+ z7 T( i" F- B% O ~
}
" g: h9 O2 W9 F4 j/*
; C! T2 D6 n- S2 i" ^+ a! O2 {*@Function IO口初始化 RG0 CCP3 RG3 CCP4- b3 W5 S' Z; N* N1 ?2 l7 P
*/! R, j" B; P+ U3 H6 Z6 p2 u2 ~7 |
void GPIO_INIT()$ _. N% O% p* N |- d, y2 K# o" x+ m
{* l: ^- G2 z/ N2 H( A
TRISG = 0X00; //设置RG口为输出
( \0 a0 C' ~, j! U0 a7 b ANSELG = 0; //设置RG口为数字量
$ J9 p: D6 v4 ~; g J LATG = 0; //设置RG口为低电平
4 {( z2 K! I# ~/ l$ v9 X
3 {7 I' N& q; {( v; I6 U TRISF = 0;
) u. H$ m: h2 J4 F6 I ANSELF = 0;4 T- S }! N% P- a
LATF = 0;
# y& B2 m' r$ h0 g' W}
9 p8 Y9 \, }% Z3 C- D
7 l7 O; I. U# F/*
, A1 _9 @4 z1 m*@定时器2初始化
; ^7 W) ~& L4 l' [+ @. h r*/
7 ~0 F2 S, r, q- M2 C8 p) vvoid TIMER2_INIT()
, ?1 q( C7 ?* Y* M& p ]{
, A- E6 n. R; f- d' N TMR2 = 0;
3 I3 x' k2 h( n7 p% K: r" t0 k PR2 = 255; //255
% d8 X& p5 ]7 [. L; r 7 D: [* q1 T3 b1 U# p5 l* v3 e; A, y
TMR2IF = 0;
9 m6 H0 ^8 ^6 _4 Z0 F/ B2 \" ]3 q # |! b1 v3 Y5 l; c8 n
T2CKPS0 = 1; //1
% P6 {4 N6 v* [& ?7 } T2CKPS1 = 0; //0
S. p+ e5 k1 m) u. m b * Y* S7 O: L% q# I. ]4 E/ |
TMR2ON = 1; ) s7 b7 I( j4 }* }
}; f" U8 O# h4 C8 p$ S5 X' ^
- m/ N {2 v8 i7 |
/*8 d; c' T6 G7 o4 [7 L
*@定时器4初始化
- P; K6 O* u. a1 |8 y*/
, K2 H) t. e/ xvoid TIMER4_INIT()
8 [4 c3 k6 c: q& W# K6 D{$ O! W6 V2 x0 d+ J+ w8 C
T4CON = 0x0e; //0000 1110
! U- E; f2 a2 ^( w2 [
6 G4 s5 t& T- Q- i' s% o1 Q( n2 a TMR4IE = 1;. X! p$ n8 ?# r7 Z$ C
TMR4IF = 0; 5 o4 m: L P/ Z- i5 F
/ O9 ? p- F7 @) Z- ]8 G; d3 l. O TMR4 = 0X00;9 h( h. p, r/ G2 Z W4 x
7 b2 n- \" r- e. K/ K
PEIE = 1;; h/ E1 n5 S2 w; b) O9 z$ v$ z
GIE = 1;
1 l9 Y" a; @; D- ~; Z+ f}9 h% F; s9 g8 j$ ?- M0 Y T
' X& w7 f) c: o$ b0 `% I
- Q) m5 K: p4 @/*
5 p' D% Y% T- u% K*@ PWM初始化
8 _3 \: W! {2 t+ D2 h*/
1 e8 R- k; ~6 d% I m: W0 N# hvoid PWM_INIT()+ [2 t h6 k. @3 w3 i$ d
{4 P9 Z6 Q& ^2 Z, R( n" q0 P
CCP3CON = 0X0C; S/ y, p/ E }. T0 {3 L- G- _
CCP4CON = 0X0C;
+ A: Y9 ]4 O% w' u# _. K y0 b
' B+ M* [: }, p' N CCPTMRS0 &= 0X0F;' p5 _1 f* r7 H: ~5 A8 A: w
7 @* t, [8 o- l! G
TRISG = 0;
+ {. o G: \+ C a* ?, t; Z}7 y2 V7 o. U- H# v3 A1 u) H
6 u0 H! ~6 N# z4 V$ g1 [2 {/*
: d& }4 [: i( W3 ?*@Function 主任务函数
! \5 a$ v1 u& r% j( B0 `*/
K! \6 w+ R) X- q1 c0 Pvoid Control_Task()
9 e9 o( p* v: }8 H; p/ p! |{/ s* U1 l; Z: Z' \: b' A2 H
static uint Time ;3 u% t0 ~) f6 Q& B0 N6 N, H
// static uchar State = 0;
3 O( ~2 ~. J+ a/ C5 T \8 v5 F static uint Record_Time;, S9 z r0 t% }: [+ G9 R1 x7 [
uint Delta_Time;
, T }. D0 q4 q J' J* V3 A, ] % f }$ p; S; I: [( J5 L
if(State == 0)
+ w: w: t: c g6 k8 j {
* e, m2 y( i, ?9 f$ R, W Time = 1000;
3 [1 n. \# W/ {5 R, C' G/ h State = 1;. |( W% _3 M7 p+ m8 _& Q& `. V
1 i& q5 b: E& N" ~! M- v3 j }' s' M( C' a2 C5 h
if(State == 1)
1 R" V; x* t- z! V( ~& b. W# t) E {
" O4 R: [$ m$ \5 a PWM1(Time);6 k7 Q8 S% l' ]0 N, Q o& j, \
PWM2(Time);
. r; o/ }" T9 M+ B, w 2 @+ v" L3 z2 Q' G8 C1 \0 i" ?
Record_Time = cnt_1ms;
( L. x! j; a* r3 X1 K( t- v " ^% g2 \& E$ F. _6 G9 H
State = 2;
j9 @6 O' H, x; F9 t }$ ?8 h6 [0 k) ^( J7 B1 E
* Y' p- o4 c) ]$ R if(State == 2)- o5 i% U3 {% G* M6 \( K0 k6 F: q
{
$ s" ~& J! c' w2 ^3 o- z6 }, o if (cnt_1ms < Record_Time)$ T6 z' [ T3 r2 B: g+ J
{* N6 s+ ~+ n# P h; y0 y
Delta_Time = 65536 - Record_Time + cnt_1ms; 5 l0 @# T1 \% Q9 Q, w. G# p- U
}
* b v+ y. ]6 D$ \ else% Z* ^8 V7 |! S
{0 k7 c& h$ U1 Q, ~% T% r0 S t
Delta_Time = cnt_1ms - Record_Time;
0 i P& i0 P" W0 e1 F8 _' h# T }6 e* P3 o; N) {: o3 Y+ S
$ B, X% ~% W1 G7 A4 ?; G4 i8 Q
if (Delta_Time >= 200) //300MS循环一次执行新的PWM_OUT: z {/ j3 r$ F! h; ~; F3 b& D
{) E9 Y9 I/ K2 k
Time = Time - 100; V: r( _9 H* l. H1 f
State = 1;0 `3 |" ~: b3 f* y& e! `
5 Q: s+ J+ b7 T0 X V if(Time == 0)
/ Z9 I4 i7 d+ O3 ]6 [ R: J$ G {% `0 A, [+ t8 P8 B5 x& x. P6 ^
Time = 0;: s- |! G; a/ F
State = 3;
, u/ y) E! \" I7 O0 K0 h' X; { }1 M% _! g$ H; E U# t0 A8 D7 ^* P; @4 G
}
8 w2 c+ n! I' O z ]* D }
% x/ y9 A7 h" I: j; _% c7 d; J
* K: A# z% V+ p6 v8 w7 Z if(State == 3)
; `- q% B M% w& K1 l2 Z* M \' g {
6 L9 e! o9 F% L PWM1(Time);
( q: s( ]% Z; ^ w5 r PWM2(Time);
- v a- I1 ]6 k6 F. l; m - F2 N, ^0 m4 _3 C
Record_Time = cnt_1ms;% }( W$ k* m6 }% R$ r
$ V% L) Q& e4 C3 \ State = 4;
0 I! P2 k' U' @1 `1 n }0 T$ }: `5 q6 {. Y7 c0 n1 ~( d
7 d9 B( g* X/ L0 l+ }
if(State == 4)
) z6 e* m' J5 Z, I7 s8 t* y# V; Z {
: g0 i4 u, r+ k- o5 Y' v& s+ B4 h if (cnt_1ms < Record_Time)1 W2 ?: d" `* H& C: h6 [. o5 Z; Y
{
0 B0 t1 i6 O4 r/ Q% b' r Delta_Time = 65536 - Record_Time + cnt_1ms;
6 A M; k/ z0 k+ e }- f }" T& \+ q. X6 V" S2 e, i
else3 G3 i5 e7 [; O" l# H1 e% N
{
5 Y6 u8 M: _, M9 \7 v Delta_Time = cnt_1ms - Record_Time;
4 Q; Z/ A* q. }& b" t: c }0 f. q/ E( X0 |
E' M( V2 Y( x2 e$ L3 n. D if (Delta_Time >= 200) //300MS循环一次执行新的PWM_OUT
9 v4 a G: a0 |# {$ u {
& ?$ R& r. k4 \" h Time = Time + 100;
* C$ v6 J- I+ {# \6 ~# E$ r State = 3;
) N, v8 \2 P' u0 D3 W* j. `* Z& O; f
" [5 }. E. }: M8 j' z if(Time == 1000)8 ? P& v& B% B
{
( y! M6 N3 Q5 {+ ~ R3 L5 T: @ State = 0;( K9 e7 R$ n N, G% u* D; c
}
% T _8 S4 s: E' y+ a } `, ]1 ^4 Z D7 Y: J: @9 N% X
}* s, j3 l' h+ C" v m
}
8 x, @3 t/ L8 D9 E/ A
% \, ? q0 h) p1 h2 Y }void interrupt ISR(void)
+ I4 _; l5 `8 n( w; @{
* T" }! \3 [: a5 v3 @6 ?& h if(TMR4IF == 1)' E( }$ @/ }$ ^& t- N
{/ S3 i0 c! l) b) a
TMR4IF = 0;
: _; b9 W$ u. P0 c cnt_1ms++;
% I# ]6 K" @8 x2 A( C& b$ n, P
6 c; B1 C9 l- @! Z+ D // LATF0 = !LATF0;, x6 ?5 F0 a/ I. o! e/ b: I2 E
}
5 H7 j. d7 u5 n$ H}
! b1 q" \6 D% P) V0 v |
|