|
EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
小弟学习PIC16F1947,自己根据画了一个原理图(见附件),以后会把所有要做的程序都发上来,程序是用状态机写的,推荐给大家。 x! K5 O/ w7 ~- F
' A# S7 G5 U; o' x/ f
/*
4 r$ n8 ?* `) v*@Name: PWM_led
: S u4 F& B% @) G+ }* @, ] ?*@version 1.0. A7 [% \. J8 X( S! d
*@Author xiaopan8 `0 g" k2 t; E3 ~- V1 p% R, b
*@Date 2014.8.2) O5 `( w% }- U7 J
*/* f0 Q& D. c2 C, [& P. g
; d* S' U% ]+ B8 C6 p* C) C#include<pic.h>! |5 ~. S& B F2 C
#define uint unsigned int
' W+ ~6 C) L1 m* u( p3 q4 s& K#define uchar unsigned char
% J3 l+ K2 d/ v) z
9 z f! Q1 l. }+ ^9 A( `#define PWM1(x) {CCPR3L = x>>2;DC3B0 = 1;DC3B1 = 1;} // X X>>1
) x- X4 k8 I9 V* I" X#define PWM2(x) {CCPR4L = x>>2;DC4B0 = 1;DC4B1 = 1;} // X X>>1 9 v+ y, V( z- V* E i$ \. X
$ f0 b' z* D! g; fuint Time = 0;
6 M8 c" M9 f' t* C- H8 ]& \6 y% U: ~( A2 Xuint cnt_1ms = 0;% L( L, i& }1 N' n4 V/ @/ @
uchar State = 0;
, A) w$ f( f: I9 q. z9 k
G2 M+ I" p p8 |4 V9 F ]) cvoid GPIO_INIT();! G3 w s0 `) I1 M8 U
void TIMER2_INIT();
9 m* t7 a: t4 T+ i# zvoid TIMER4_INIT();
# v) _4 l6 l6 ^8 _void PWM_INIT();3 ]; Z l0 B( F- f
void Control_Task();' s; S( s1 A. m1 I6 v
& o2 C* q! U* U! v0 x# A6 E R$ e/*
5 ^+ h: v# i5 q; Q2 d*@function 主函数
7 G9 w& U6 g4 S* `/ w& i*/9 ?' _1 _4 j5 q/ Q1 ]; {
void main()
2 Z' d' ^- G& w G& L9 n3 O' X{
" C! R/ @0 q# u# B1 e) W9 z GPIO_INIT();
0 g+ u0 S( C1 u/ O" P PWM_INIT();
& S8 g! M' |2 x& h/ K0 d! N TIMER2_INIT(); //PWM利用TIMER23 B: r- h4 y: I! S
TIMER4_INIT(); //定时1S出发改变一次占空比,调节PWM波形
L( V& t D6 Z7 u5 _" N I while(1)0 [- }, M- O5 G5 T5 G5 \. ]5 n6 Z
{7 Y* M8 e' y# u
Control_Task();
2 e7 c. j Z2 S* z' O) a" ?4 _; _ }# i. Z: c6 D5 {$ m5 ~
}
$ O: `( y* T& m+ h& u/*/ {1 z0 C3 l# \. e% a, S, _8 I7 H
*@Function IO口初始化 RG0 CCP3 RG3 CCP4' Q# k! U5 E% g+ k( w9 X( D7 d) }
*/
2 ]( f( Y8 d' xvoid GPIO_INIT()
$ k' U1 V# m+ a{
! E' m1 s* B* I1 [ TRISG = 0X00; //设置RG口为输出- ?' S+ {. R: }+ O
ANSELG = 0; //设置RG口为数字量4 {8 m- v. Y, p$ J y, K t; m0 s
LATG = 0; //设置RG口为低电平6 d& t7 k) [ [8 I
" k0 \3 p1 _, p% N
TRISF = 0;
; i; Z/ Y3 q, V ANSELF = 0;
& a) r) z8 v# _, k1 C X LATF = 0;
8 E, W; D& b' o) r; {3 ?7 e- z/ N}
( L2 W# S7 h* l7 u% E# x$ T
$ A- e3 N8 W9 t/*- i* Y7 o" k1 H% e. S9 Z
*@定时器2初始化1 B& o+ Y8 C6 D( w" P
*/
1 A6 j7 S' J: U; x4 ?: qvoid TIMER2_INIT(), r4 w9 P- f7 \% E' P4 |
{
" q$ P) |; k; L0 b TMR2 = 0;" ?1 Q: C6 A# ~$ F. \5 t
PR2 = 255; //255; I$ Q+ \6 d5 r3 s i' [
' G6 { |1 _; ?/ h* Z TMR2IF = 0;
, [: C/ F8 S# H/ n
7 y0 R* C4 E/ O% x T2CKPS0 = 1; //1
; U0 ]% l8 |" q/ @6 Z9 c T2CKPS1 = 0; //02 j- I4 F6 B# d+ b$ v( p9 ]8 W
' x: m/ }6 C7 z+ d4 y TMR2ON = 1;
. @% ]) ^/ n" k}
& @1 e4 W' \4 ^% F# E2 a
( Z4 p' u* p! W' ^/*
7 _2 @: t; ?6 n6 _*@定时器4初始化, M, E" K: ~; ]- ?
*/6 E9 r8 c5 G/ p, E* X n9 N
void TIMER4_INIT() B- |" k6 @9 G* p, q
{7 E4 x" G; q5 k/ l
T4CON = 0x0e; //0000 1110
( C: }- m( [; y& [0 R+ e" D
# |# R+ e5 j* T TMR4IE = 1; F! H- i1 s; _; ?: }
TMR4IF = 0; / t( G" z2 G2 d2 E( X W" g
& j5 }5 _- l: K/ s P
TMR4 = 0X00;
' {4 e8 Q7 B$ C6 {; b: F 4 e2 d- e- \- m* p' ]# |. O
PEIE = 1;4 z( r) [( n! o% e( f& V- |
GIE = 1;
+ Q: j5 f* P4 o3 k}0 Q% R$ k+ [; |9 {% n
`6 R. Q6 y4 H! R8 R* I$ G J- o- @2 S6 {$ z H, m
/*
# N9 Z. i2 x2 ?*@ PWM初始化
|) G- F/ ^& a3 f5 K; t8 n*/3 U `7 X2 p) Z. F: C
void PWM_INIT()
- D# F- m( x& `( V9 C{
, M1 Y) D8 y; O3 S CCP3CON = 0X0C;& {3 C: u" e( p/ _
CCP4CON = 0X0C;
4 p& r" R. z# K" X+ ? . v& R9 {9 Y' Z2 Z! ^. H v5 Y$ g
CCPTMRS0 &= 0X0F;9 G5 ~& ^5 M' z2 Y5 C
' ]4 Y2 t% y1 m+ ^; F TRISG = 0;& K+ l( f5 L0 O
}' _' ~6 J& f( v$ r' O+ V
9 W C- Y! w ^7 |
/*
" |. G2 U* m) A! W*@Function 主任务函数" Q: c6 _; R9 L( A I( s- M
*/) F3 a* T9 I- F- T# t2 ^2 g% [
void Control_Task(); |* s) F3 b5 e( a
{$ l- f x. J7 l) `! {3 o
static uint Time ;- T' N5 l+ Q; v& j! f+ W& b- E
// static uchar State = 0;; L* u+ t* n% Q
static uint Record_Time;
2 y, i8 w( ?/ ~# u uint Delta_Time;
. ^( ~" f% i5 h+ e9 z& s, X # R3 T Z9 [& E- s- w
if(State == 0)
( z7 O" i3 c4 Z0 p. Q8 o {1 ]- @ H v+ A j# d! n% N0 c
Time = 1000;
* _1 D- M1 {% T4 }3 t State = 1;) J A; V5 R* j3 D3 _# A
. X3 J. p# h- ?+ G C8 Q
}
. o9 M$ N: N& C& R8 U if(State == 1)
: d0 i" V: o, n" f8 c' | {0 s2 p; q2 E0 X: F2 v/ N
PWM1(Time);# A' t0 M M* Q0 A9 }$ a6 y
PWM2(Time);3 `. a# R: O; H I/ c/ v. ~0 o
9 x' D& L5 M! K1 ?3 {
Record_Time = cnt_1ms;. f- l8 O" C( ` P+ b
8 J) ^1 e: {- A. t
State = 2;- F4 n. u* }) t) s: Z- X' y
}
; u- ?+ d5 K1 P4 O " n, t6 S) D: Z _& w
if(State == 2), X7 H1 J2 G/ N
{
1 ^" E9 m' c& I if (cnt_1ms < Record_Time)# n& Q K: y+ J& u& e
{, I! f5 T+ Q; f4 M) [: n
Delta_Time = 65536 - Record_Time + cnt_1ms; . U: q3 w$ b- k$ K& |
}' a5 d3 ^" X# q
else7 I6 f2 D) y1 {' C
{
T. v$ I/ ?- @. W/ k* s- N6 f- Q Delta_Time = cnt_1ms - Record_Time;
: F6 n1 p6 i( { }! \0 G! }# O4 @, C* b# c$ i
6 u. ]) W' {, h& }1 X5 W0 Q if (Delta_Time >= 200) //300MS循环一次执行新的PWM_OUT
# t+ N8 \4 |* ]6 p- B4 O {
9 M8 a5 X( j7 d; Q( g W Time = Time - 100;; U4 x; l% {% |/ L
State = 1;
4 {3 |' i/ j5 y) K0 g9 [; o# Q' m & R+ {: @( [0 I
if(Time == 0)
/ W& i. @' ] J8 S$ g& A! A& [2 T" _ {
' Y4 A* x q8 n( P% O6 K Time = 0;0 m! ^3 R) \% I& ~* y' I
State = 3;% D; t y8 J3 C5 J0 } U
}
' a8 l/ [+ [& T5 L# M1 u" R }
5 F" X4 o! l Y/ b! h8 E }+ X4 M0 k* i4 J6 }0 u" x1 R
8 `( Q! M( D4 A. Z! r
if(State == 3)( m& X2 V8 P1 W3 y9 u. l. O
{
$ V- q8 Q, ~) E# K0 r9 e, e PWM1(Time);
6 Y. m9 j8 @" u& R8 n2 y PWM2(Time);
5 \( ]) T; P: z( i4 j + t5 E% j3 b) i9 H' n( J
Record_Time = cnt_1ms;
5 j4 S5 }) L# q9 }6 n: e ) F; a; T- J ^2 {' ]7 g; m2 e, B W# t
State = 4;5 p' b( g, `5 A5 C/ s& U& w$ A
}
^$ H1 L% t3 o) j# D7 n* ?
( L# @) W, v9 }; i6 j if(State == 4)
, r& I2 w8 d7 S% N, x {- z* R. J1 N$ r$ S7 D4 T0 I% t
if (cnt_1ms < Record_Time)' v# D3 t$ t" n* b
{
$ K. X1 `' n, `+ ^+ H. } Delta_Time = 65536 - Record_Time + cnt_1ms;
& g' |9 o1 ~: |3 e4 Y$ r }6 ?; D2 L+ Z8 P, f
else
* ?. i: d. G Q% C D- [ {9 Y" Q' _# f; @' s# Y8 T' `
Delta_Time = cnt_1ms - Record_Time;
5 a8 q3 Q& q% }1 M1 A }
3 l% C; a$ y. B. I& ~$ j' N" g' w; c$ I$ k4 w8 {6 u8 Q
if (Delta_Time >= 200) //300MS循环一次执行新的PWM_OUT
, n5 c- e3 w% P1 \ [ {' m, C( d7 Y% I7 L
Time = Time + 100;
5 |$ O! s9 B, `+ b3 [) r State = 3;3 T; q0 K" }) c4 D0 g: H N
. k1 U' h9 ^4 r6 o& T; J$ V! q" v
if(Time == 1000)
& f1 H* e: M: r) K# B. `4 G {
; k. c8 u& G7 R: N6 E4 G; o State = 0;
2 H/ n7 y+ `5 J }
5 |) q* b9 ~( ~, u j8 l5 T2 ^ }
% P7 O) @" ^; S8 i6 p9 D }" w9 k3 z) [6 \- b1 c
}! R& [# k+ V6 u! J- y
' k( c- a! t3 J8 w! z' n6 T
void interrupt ISR(void)8 y* ?! T! \2 `% M" B6 Y8 o* ?( R: Q
{
5 ?$ @- Q# [8 w9 Q1 D if(TMR4IF == 1)
h% f: h8 ?2 i4 S8 T: z {
. V& I& l( z; Q$ ?3 r7 K4 M TMR4IF = 0;8 R( @( u' ^ A# k7 _" A* i' B
cnt_1ms++;
: s7 L+ K0 o( ?% ? % Z% w/ L! V: q; e# s. C& S
// LATF0 = !LATF0;
' M% Z+ k/ G* { }
& S/ K9 ~5 ? L" _}( }9 D' K* R/ a% D: P
|
|