|
EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
小弟学习PIC16F1947,自己根据画了一个原理图(见附件),以后会把所有要做的程序都发上来,程序是用状态机写的,推荐给大家。: W$ h9 t# i, J* d1 L+ r( ?) d
8 v# f) n; x8 W( z ~7 o
/*8 j1 K7 ] w" t9 J7 A; s! H
*@Name: PWM_led
' o& J& q! B! c*@version 1.0
, l' B# @, j3 g*@Author xiaopan3 d" q. n/ H1 w9 S0 I( Z3 F# R
*@Date 2014.8.2' J% B8 }$ D+ R
*/7 X8 A2 u6 B) u1 d* ^3 g5 p! X
2 f/ N6 f$ z1 R" S#include<pic.h>. @, W& V- a! _8 k2 w
#define uint unsigned int 9 R, g. D" X8 [( A7 Y% i
#define uchar unsigned char3 R7 _* A( I6 X9 ]2 N' n5 N
, J& {1 M9 M, n- E
#define PWM1(x) {CCPR3L = x>>2;DC3B0 = 1;DC3B1 = 1;} // X X>>1
3 F) F- T% m0 p" @& H$ [* B0 W2 ^#define PWM2(x) {CCPR4L = x>>2;DC4B0 = 1;DC4B1 = 1;} // X X>>1 ( h" ]# b6 `/ L3 E
( i8 t2 k5 U0 V* i9 [% Yuint Time = 0;4 p# Z0 z. Y; Y0 i+ f1 b. e
uint cnt_1ms = 0;
p8 C. I9 P1 n2 ^4 u0 }' ?uchar State = 0;
! X- H% i+ E1 Y
, N6 h& \& \3 \9 V0 \5 r3 P3 bvoid GPIO_INIT();* ?- X/ G" v) N8 ]
void TIMER2_INIT();
5 V: D1 E8 k4 c pvoid TIMER4_INIT();2 U7 I0 Z9 m0 P+ T
void PWM_INIT();
' S9 n; c. p1 p: W& {: G9 evoid Control_Task();$ q5 u6 _4 x- I7 j# I& N5 p
! R. s! X' K! J- L5 E- A5 ^' g/*) C( t( U" |' H+ p6 m! ]
*@function 主函数$ _" u: M _. p
*/* s; a1 |% g( _ v- F
void main()
6 d! h8 K0 I0 H{
4 D4 R9 u8 Q1 B7 N D9 I GPIO_INIT();
/ p' E5 `$ x G' e PWM_INIT();
( L$ Q, f* a/ M TIMER2_INIT(); //PWM利用TIMER2
0 V1 `( c7 a; W# H TIMER4_INIT(); //定时1S出发改变一次占空比,调节PWM波形
$ S( z1 J# i' ^+ ~8 p while(1)
! D2 t( R+ f8 Y2 a, c$ _ {
4 w D" ]8 ^7 t+ | l' s. | Control_Task();
0 Y1 _+ f' R8 ? }
: y, k& m4 t- { I6 q1 w}
% M4 Q, ~$ o' I% D/*
U2 {/ j9 `4 F, D- Y8 T' k5 n0 n*@Function IO口初始化 RG0 CCP3 RG3 CCP4
) Y' m4 a; ?# Z# b+ S9 y. V*/8 C9 @; z [' M: G* [
void GPIO_INIT()
- \5 i3 j: i) f* P( `{, M, r# O8 a/ _ n; L1 H$ j5 a% _
TRISG = 0X00; //设置RG口为输出3 c- L) d$ \/ d8 R$ X5 N! K
ANSELG = 0; //设置RG口为数字量
% @0 V( I/ ` _5 |* V6 {) g6 N LATG = 0; //设置RG口为低电平1 |# ?# ^# A2 f4 q' \% J* b
, G/ T+ F; D* O' @
TRISF = 0;9 x9 `- Q- d! V/ p0 Q/ D
ANSELF = 0;2 s; L; L. ?5 T V
LATF = 0;
2 P/ B$ @+ R9 W# X}
7 U. t7 \& n0 S
. ^, v$ U' l% H9 K/ m/*+ v2 Y7 s" c1 a( i/ ]
*@定时器2初始化
1 _: n) Z2 I7 M, x* N: t*/
( V$ A; U* L% _2 k& T0 `5 k Tvoid TIMER2_INIT(). q6 t4 @1 d3 F
{5 V. t& Y5 {8 H6 l2 @
TMR2 = 0;
: U* P. a: p' x PR2 = 255; //2557 G3 J7 y0 `/ f/ b1 i
9 ^0 m' e v; D7 F- m- _( v TMR2IF = 0;
/ n' J2 d+ y2 I
1 L1 N* O- @' ` T2CKPS0 = 1; //1
" `. ]) ?5 W! U H T2CKPS1 = 0; //0
1 ?$ s% P; N7 i
6 |3 ^* M, L0 a& h& W: f TMR2ON = 1; 5 _+ r: ~; G- a7 U3 V! f) o% _
}( s3 `( d8 U: m+ n
1 F, E9 n9 X$ t6 h) M3 Y
/*, C1 f, u4 Z! T& B; \
*@定时器4初始化
s9 F: Z# y/ o1 u9 s, u4 p* L `* j8 D' l*/
4 t8 n6 F" _1 Z" V% E" Q) G0 Wvoid TIMER4_INIT()3 ?$ w* ^! M ^, m
{
5 J9 i+ t3 O$ @; n7 h7 |! T- o$ c# c" j T4CON = 0x0e; //0000 1110
9 \2 j) s3 B y$ Y+ C
" J6 b& z- B8 S" w. U TMR4IE = 1;( `; m3 [7 g9 s3 ~
TMR4IF = 0;
2 ]. }* e) C" T- E1 k% S1 |. L
1 e: Q+ z# ?+ K1 h2 s1 d TMR4 = 0X00;
8 J- H$ l( D$ X' U& ~
- N: F. c* Q$ J6 e O PEIE = 1;- Z: p) W, i4 G* v% I
GIE = 1;
I, P6 w4 f. N0 o}2 O, R" Q$ X3 w
- x* }2 D& l% H" _3 t
5 e( }- h( s. X7 [6 |/ ]! m1 f
/*
# b# c- `% A! y# p*@ PWM初始化
2 e" F3 ]7 q7 }# |*/
6 [/ U, {7 b* P! H; Ovoid PWM_INIT(); T0 x* L2 p) A/ x1 f& u
{0 P/ E9 J9 m7 i
CCP3CON = 0X0C;
6 `0 V# y" K% m CCP4CON = 0X0C;
+ O) q4 e8 o* W. j# y! C9 K / i# y2 i' I5 ?, @/ z9 z* g! p
CCPTMRS0 &= 0X0F;- e; m9 V( D1 i* e! A+ l
) u9 V; I) @3 W) d3 i+ G$ E
TRISG = 0;
7 X% k- o; U( Y' h4 d} g9 M5 n7 c, [' e
?8 n( \) y! h( e9 L2 ?; M/*' b2 }3 T8 L) y: v6 p, t
*@Function 主任务函数$ X3 F) {- b% x; P$ A( ]: d8 A- N& v
*/
: B1 |% W4 p/ I1 A! r+ Rvoid Control_Task()
3 V8 D* z. n0 D0 U6 \2 x{
& } A* V$ Y: D1 g) X static uint Time ;
& o8 B4 y4 I0 U3 P; A: Z" {! t9 p// static uchar State = 0;
" t i b4 i v# }% V/ e static uint Record_Time;
& n; d1 k, I4 m/ T2 k" v uint Delta_Time;
* s) R+ h& a, c9 y G% e
8 B, _: e) Z* U1 L1 t if(State == 0)' D$ V: X _+ {& u5 B
{ h) n2 @$ _8 Z0 p7 d: ^
Time = 1000;0 g& j: O' l$ ?( b6 g
State = 1;
: Y& u7 V g- o! |4 ~! `" K
* D! l* r2 P2 M9 |/ P! C }% Z7 h1 A& ]: I
if(State == 1)
% @& `( u. j; G8 l {
0 r4 c' g6 j# o0 ]( Z PWM1(Time);; M+ C+ K: v* J
PWM2(Time);
0 ~$ ^1 \ G# k6 L/ X
. Q( A# {' s: K6 @& c# _) v, l: q Record_Time = cnt_1ms;
$ n$ `- I4 z7 X9 i0 P! I% |
& Q3 A3 O! U1 V6 |$ L0 ] State = 2;# r/ M; L- T7 G, j
}
( i" z: U7 i* s- h. X3 f: @2 K Y* }1 Z ( {' b: S5 A2 d4 |* ^
if(State == 2)$ w4 ~: {" F' ?/ R1 l1 ^* z
{) | |0 J j2 O4 {
if (cnt_1ms < Record_Time)
: d% G* h J9 w$ Z% d) N% ^7 o1 l {2 l6 C7 ^0 @" c! ]7 s
Delta_Time = 65536 - Record_Time + cnt_1ms; + V J. d. ~* `8 U D3 r
}7 ~3 Q+ n9 c& S. _0 P" \) k
else9 K# o" S/ v" M3 d9 |0 s% c
{" E8 c' |: h. n% ?4 J! `
Delta_Time = cnt_1ms - Record_Time; 4 m; b# B. k' B
}3 h9 u' U4 O b; S G) n
- Z6 b6 `! l) R3 n if (Delta_Time >= 200) //300MS循环一次执行新的PWM_OUT9 d' [1 D0 }& U- w
{* g: i* M# s4 w/ K$ x2 z
Time = Time - 100;0 z& n4 }6 Q1 ]. Z
State = 1;# w" |9 o+ T v3 L( M9 a' A
# F2 N: g' j* L0 U2 G if(Time == 0)+ T- U7 a; I, z5 v9 T
{5 u9 C( Z2 h/ h7 E% N; c
Time = 0;
) l! i9 b% R5 [2 D% e; b State = 3;
! Q* J/ v, H- q2 o3 C4 n4 b$ f! ~1 q }
$ A" G, b; d! n6 R: m+ P" Q }
6 |: l6 n# |& |2 Y v }
% m( R @7 Z8 [
# Z* J' F1 {: Y# h/ B& n if(State == 3)
9 [5 T9 P. h2 B- V! R$ } {' ]. a: g2 t% N% I/ D, h0 ?
PWM1(Time);& D8 f9 ~1 e/ N4 O6 b& t# X+ ^
PWM2(Time);' `+ Z9 f, Q7 I
* t; u: a: [* N/ p
Record_Time = cnt_1ms;- |6 g* K# c/ }( b3 x
# g% W/ F% ^, t: [' L. R5 o# _ State = 4;
% u& w/ U1 r/ w5 l1 H, l }
. ^' j: ^2 T' K 5 m, L; G4 K. D5 k
if(State == 4) I6 Y* S& l* n! h& X7 ~ R
{
! O1 R+ C8 f1 R% @; } if (cnt_1ms < Record_Time)
1 M3 b% I+ h; H" | F) R {1 T% u; c/ R0 B5 i$ y2 h# J
Delta_Time = 65536 - Record_Time + cnt_1ms; # E7 j4 \1 v& |
}/ ^2 F# ^3 j' V
else+ o8 w4 F, W/ H' Q7 @
{* ]( }1 K: t, Y
Delta_Time = cnt_1ms - Record_Time;
! i& ?" d6 r) C) f; S }
4 G0 L: L* [( K; O5 }1 b% V$ C! a! `: \4 g3 l# ^
if (Delta_Time >= 200) //300MS循环一次执行新的PWM_OUT
. M5 g- A; \/ n) B- Q X7 ] {
9 u" `8 _0 v5 E Time = Time + 100;7 D/ n6 X" @4 ^, m4 h
State = 3;8 T6 x' f3 B3 \) S# s: D2 [0 }; C3 g( h
- B* o9 N1 j$ v if(Time == 1000)& w8 Y/ F7 \) P0 p
{( r2 `0 e9 n: c
State = 0;
# A6 X) z9 r% ` }
8 b: ~. T! Z% h, u# b }
7 b! j0 W- E! {8 S }
- ]2 n" u2 u% ?. ?( X6 R}
4 \" [ j6 o! y: Q2 A' b8 j5 E6 C3 u8 v. y/ m/ K: g
void interrupt ISR(void)
4 y0 [" I6 D0 W; I! X{
. |+ x4 x3 s) q( V0 K3 P5 L0 ] if(TMR4IF == 1)
2 {* r2 t" M6 J. F: P: a& @ {
* t; G4 R% k- u" Z% b0 f; X3 ?! f TMR4IF = 0;; F4 y0 [5 _8 o S }/ s0 t7 M9 N
cnt_1ms++;
* Y& }( \. e0 Q
1 d& A+ }( C6 _& }: N# D( @* a // LATF0 = !LATF0;
" c3 }4 o( [5 |' X, { }
8 A4 Y" d3 B# ]" k" D# _}5 J* a% L& W- {2 {" E3 Y3 T9 G
|
|