|
EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
小弟学习PIC16F1947,自己根据画了一个原理图(见附件),以后会把所有要做的程序都发上来,程序是用状态机写的,推荐给大家。( g6 m7 o; ~' h' B Q
+ @6 p. H% |5 H3 ?5 N% \
/*$ @7 q$ C' d" p& z
*@Name: PWM_led9 s9 `, y2 m* D. _ A+ X
*@version 1.0. w7 z+ [ S/ i) p0 Z. _
*@Author xiaopan7 Y6 e, h: I, Z7 o6 C
*@Date 2014.8.26 u4 R' \: V4 S2 E6 h
*/
2 I" }- h- \" ]6 ] m2 [1 H' L7 ^0 p1 g% M, v/ r: m3 {; E& ]
#include<pic.h>. U$ g2 `: a: ~4 e& L* W) L; B
#define uint unsigned int ' G( A2 _1 |, G, c4 U) d3 |0 x
#define uchar unsigned char
2 L) u! m+ d: ?: a0 l! t6 `+ _- [0 H7 f8 v
#define PWM1(x) {CCPR3L = x>>2;DC3B0 = 1;DC3B1 = 1;} // X X>>1
' i( g2 s2 l% O# @: u; R#define PWM2(x) {CCPR4L = x>>2;DC4B0 = 1;DC4B1 = 1;} // X X>>1 - b: E$ M+ s1 P
8 J5 f P/ I: p/ ~, w. tuint Time = 0;
2 a5 O( e/ m' {0 suint cnt_1ms = 0;3 `4 I( @: y. [/ O3 o; C" y/ C
uchar State = 0;
$ w5 x! { J8 ?- X' @1 m( R. _2 [" V4 X" [* C. j. g
void GPIO_INIT();
" _# Z1 J0 Y5 z* ?7 c. ]void TIMER2_INIT(); k$ {1 s) l# e3 i0 Q+ c
void TIMER4_INIT();9 }+ F O& W2 @9 s9 p( \1 i9 y( K+ O
void PWM_INIT();9 n) U3 {- Z2 g8 o: F+ F& E
void Control_Task();2 f A+ @8 n+ n4 C/ P, h
, q6 Y7 T8 ^- E i. o H/*& t; M0 V& H6 B8 @( O2 m
*@function 主函数
( d" n0 R, u! m' Y*/
* f, X2 D) F1 F- U- mvoid main()
# Q- I5 C$ c8 P/ P6 c{' ~/ E! x8 v4 v. f0 d" @# F3 n' }
GPIO_INIT();
# X0 H/ ?/ x0 a1 E8 C PWM_INIT();
6 ]6 X( B! F! j- v. n w2 { TIMER2_INIT(); //PWM利用TIMER2
- N* R1 h& E" L2 `; K& N5 F3 S TIMER4_INIT(); //定时1S出发改变一次占空比,调节PWM波形, c' x$ X6 G4 W' l, ~, b" b
while(1)! R& f5 D5 c4 h0 j/ P
{- y( L* q, j0 \$ C% h
Control_Task();( O6 A3 b$ l0 g$ Z0 |/ [5 m
}
9 t% p+ g0 i. u U y}
$ `1 U4 G! j; c/*
" [( h* M% W) D/ H! K*@Function IO口初始化 RG0 CCP3 RG3 CCP44 \- \9 e! w8 w/ u# F; o7 S% [
*/
) J& ]" v6 q8 V$ @! {void GPIO_INIT()
0 U2 `6 q0 N6 x2 y$ \7 R{
% J7 w* e. b# Q* O8 A" d, ]: C TRISG = 0X00; //设置RG口为输出
3 v2 G2 n6 K+ | {% i ANSELG = 0; //设置RG口为数字量7 k. b) P) H- p2 f0 f0 l
LATG = 0; //设置RG口为低电平
% l3 p q i. e9 B6 `( A
) Q0 \/ x# U6 d- B+ N7 q! e, p TRISF = 0;
% K/ p! T0 @2 H9 T' s& U) h7 E ANSELF = 0;0 u4 X$ ~" t" U- ?. S% B: o
LATF = 0;
7 b- v O6 m1 ?; ?3 q}0 Q( S o1 k* A2 R( K5 w/ r Y
0 B6 o; R) L9 e) H" g( D/*9 v' B! t0 r7 ~+ s) h4 B
*@定时器2初始化
6 J( B1 y2 t, q9 [5 v( Q" U*/. F# i( Z, e |8 O( G/ y) v2 Z
void TIMER2_INIT()! n) i; o2 K/ v8 }0 z @
{
8 R8 b( U! u/ C/ G, ? TMR2 = 0;
/ `) J2 d6 x$ S- j% u- \ PR2 = 255; //255
' Z4 w) F$ v( L b6 D . w, X- K4 {$ \) f2 s/ Q0 y
TMR2IF = 0;' M- O8 n* l: o6 }4 H. v0 t
% N; g- C3 m) B5 u0 ? V- Z T2CKPS0 = 1; //1: O( \2 T/ s) ]! q% Y4 Q
T2CKPS1 = 0; //0
6 S; ?" a$ U. Y7 u6 `+ K% |
) p u; k# N. B. _ TMR2ON = 1;
3 ^# }3 n: {4 g/ W}% P: D% T# {) j# l' f' Y+ U
3 o; Q( Q) C. _( @
/*
1 Y+ j+ |7 F/ T% R*@定时器4初始化9 W6 j+ O ^( h6 X; a/ C H
*/- ]1 N; [% |' Z2 j4 B1 Z
void TIMER4_INIT()7 j: F' I: K3 y: Y
{5 S# Y( I1 z9 H$ Y
T4CON = 0x0e; //0000 1110
. C- H$ x; y* p; |
3 Z& m: h6 Z+ g+ k/ Y) H9 B( i% H TMR4IE = 1;
% O! I' e/ ~0 W& Z TMR4IF = 0;
! m- ]* X# k3 B6 B0 e+ ]9 C+ f2 G+ H$ s5 H2 |0 o
TMR4 = 0X00;
! ]# z7 Z' b& Y
$ l# s0 B, D9 A5 |5 ?* C PEIE = 1;
$ N4 }8 A7 t# W' M2 e- K GIE = 1;; t- I' w* Z8 h3 B- K
}
, f. G% B" J [9 k, x( h
1 \" W. T( _2 a4 F G8 M: D% I& Z, Y9 l4 M- C$ c; ^7 K% f9 z
/*4 `7 ?5 m7 n- Y% K
*@ PWM初始化1 q: {1 }4 t" x
*/
0 p; A9 u+ V* e7 x3 L. tvoid PWM_INIT()
% ?, F! m# J- U- o& L{! J& x2 l* g9 t+ A: [) e: L
CCP3CON = 0X0C;
1 y2 p& E# n- _6 b CCP4CON = 0X0C;
* t6 A6 [/ U& r
- h% F7 p9 u. g7 y- f& E CCPTMRS0 &= 0X0F;; d/ @6 l/ d: M, N0 }( G; m3 B
' K9 K9 i" T! l0 q
TRISG = 0;
7 j! k7 u: |8 o8 |}
% w7 @% ]: q2 x) V& d" Z0 g2 Z6 y0 z
% [. K) T# \# |/ D: [0 f0 o" h$ u/*
9 ?% G, y: i2 o! a4 a*@Function 主任务函数: G; G5 @" k) v/ Z% e; R% ^2 x
*/+ @, f' u/ @" ^' i$ r/ ]/ b, `
void Control_Task()
2 \+ n1 R8 X: T p0 n+ u9 P{
1 x- q* z9 W! f9 K$ Q; O% x static uint Time ;# [8 B& ^# S/ ^0 {" D% E( C
// static uchar State = 0;
' t Y% L# `4 f static uint Record_Time;
6 n: B$ K/ a) b2 ]8 n uint Delta_Time;
; Y ?- k0 N% l# b5 g1 H 9 K* L! }$ \" U0 Y
if(State == 0)
# ]2 E2 p/ l8 v/ i5 t {
) s" q% q' q/ R& \6 Y% w2 A6 B Time = 1000;
( e* B2 [0 i; R6 j State = 1;
1 h, A7 a/ A7 g1 d' @ {8 z+ n- T- h2 a4 k* m
}
( d/ R5 e( u* [- W9 H if(State == 1)5 K/ c" u) N1 v; @! `1 s! s. M% w
{0 a6 P& Z, c9 {; W& O1 a" y; y
PWM1(Time);
$ i5 l% N: g, A+ ]" E9 L PWM2(Time);
; S2 }2 _0 [6 p+ ^1 w$ m - C( x" a$ r' D9 L9 k3 s
Record_Time = cnt_1ms;
9 M0 j) a/ d# {) ~* l& D8 ] ; Y, ~8 e- H0 S% e) b
State = 2;0 p$ r# E2 A* n3 w- J
}
# U* \! x, G. ?% w) o0 i ( o+ y3 P4 Q' h& y5 k! l& J
if(State == 2)
" F9 W- g( u' X+ a( U( y3 w9 x {% B8 B" M# ^4 ^$ Y( [" T( l
if (cnt_1ms < Record_Time)1 |! H4 M- g( c/ P9 c: A# q6 ^3 d3 K
{- I8 u7 D; [. R' ^ c
Delta_Time = 65536 - Record_Time + cnt_1ms;
4 B4 |- w, a, S* l }6 e9 m s, a$ U7 [
else& L$ E& N; S" m4 h& G' ?8 Z$ |
{* [8 U: q; S+ J, C
Delta_Time = cnt_1ms - Record_Time; 1 ?) E: }1 J- \
}
+ Q7 i" n, {" {5 w0 a2 W; R: J; t; w3 _( z( ^7 `
if (Delta_Time >= 200) //300MS循环一次执行新的PWM_OUT
5 W9 Q/ e! g9 R' P2 \* h! V {/ K9 l( Q. v7 _) p, T* K
Time = Time - 100;3 S. M! C( }$ g. P
State = 1;- w9 _+ r! ~+ ~$ x" A
+ o7 H1 Q. d$ v+ A/ M2 ^# A if(Time == 0)
n" \# g7 l1 L' i/ @" S3 h {
5 G; s& C6 h! z9 L \+ y Time = 0;
8 {# I4 ^" f; z1 Y/ O+ A# M3 J9 E State = 3;- K z9 a T% j$ @4 L3 b' Y
}% X6 ~$ f X! O1 P
}
1 E3 C0 P: u9 E" Z }
* B7 P& Q8 y+ i
, Q; b2 }0 P; \/ I# l- t" u if(State == 3)
/ Q1 z$ M/ k; W- e$ w {& ?# }$ h6 Y' v
PWM1(Time);
& U, O6 L/ V4 P3 L PWM2(Time);
+ S( N6 `% e% w7 q6 ~3 M & n( f( j1 s& y% J* P8 q
Record_Time = cnt_1ms;
) S* J3 D- s1 \% A8 P; m T
6 ]* V7 r5 F A) J1 Q2 z State = 4;
5 W, u5 N1 [9 L w5 {& X( H }2 Y! a9 c, L v8 s
1 U5 h4 T- ~+ b8 i6 ^+ I
if(State == 4), C3 d- K( c$ [$ M& { x- E! k
{
1 i: }0 V: z+ t7 O! B8 P. P- D if (cnt_1ms < Record_Time)
1 V7 |( _+ q3 h% p4 } {2 z" @" I0 c& q7 Q9 C" K
Delta_Time = 65536 - Record_Time + cnt_1ms;
5 n8 H2 \0 ~3 V+ E/ t }5 _) e0 D- S) L7 v+ U* f& a
else- \# l6 U" w( R9 e' X# K
{" d2 U+ f/ B' s0 K
Delta_Time = cnt_1ms - Record_Time;
/ P- Z# |$ m, W+ s+ I4 |% O2 [ }
$ N2 Z& t& K" l' G3 A( o
$ e- z+ c5 l; C+ J. B: a" C9 c if (Delta_Time >= 200) //300MS循环一次执行新的PWM_OUT+ T) N6 ^9 N: l. W9 M; N8 z
{* R6 ~2 {2 P, n# u4 B, Y l
Time = Time + 100;
) Y$ z/ T/ t4 R' | State = 3;. I8 N9 ?2 g$ c6 ]5 I/ S
2 J0 R* w* p% r5 n" |8 E$ S
if(Time == 1000)5 S/ a2 d+ K' Y
{& @) W% O0 L1 r( d* l# @' p( j
State = 0;
* t8 S7 V7 p) c. i$ |) u% \ }4 [3 l$ I b3 C& I
}1 b( D" P0 _) y2 ~
}
7 X9 e+ W$ X7 J2 @8 n}6 }. s. X" y, z. h7 o3 t. i2 j1 M
' c4 o- `8 {* v, f
void interrupt ISR(void)" k; T9 Y) z I1 y+ E+ z
{# Y. d. ^6 H! |7 k3 m
if(TMR4IF == 1)& _$ n: l' u3 A: k- A
{
- x$ }; t, j p TMR4IF = 0;/ ]9 K( D( X: B4 _
cnt_1ms++;
" P, x; A, @. w ' Y4 v! ^9 G" G4 _) m2 N
// LATF0 = !LATF0;
& J ^. c8 Q+ h* f }
5 r* {2 G/ ^+ s$ ]# l+ ^5 x}4 M7 |5 O. Q5 M. n" L9 Y3 A4 {
|
|