EDA365电子工程师网
标题:
利用PIC16F1947的PWM模块做了两个呼吸灯
[打印本页]
作者:
Dedy01
时间:
2016-7-2 14:38
标题:
利用PIC16F1947的PWM模块做了两个呼吸灯
小弟学习PIC16F1947,自己根据画了一个原理图(见附件),以后会把所有要做的程序都发上来,程序是用状态机写的,推荐给大家。
, K% D( ]0 W' v; |# K H
5 ^+ T: ~ f$ z+ i) D# z
/*
! P# f; n: S. V M. m
*@Name: PWM_led
- A( o, g) u! R) q+ B# N" B+ }
*@version 1.0
6 P8 p- W: F' i1 j7 N' O4 T
*@Author xiaopan
* q5 [. h2 T4 E x7 \/ T* ~1 I
*@Date 2014.8.2
2 C+ t* ~! z! a& C& B
*/
1 q4 O4 C7 Y1 Z" l4 `4 y
5 H9 ]% C, R0 D/ Y- j' P
#include<pic.h>
. U% f# x, o* U9 z7 ]. V
#define uint unsigned int
4 f, T/ z# O2 {* A" r- S! s5 O! E; A
#define uchar unsigned char
( N3 ?0 P$ a' ?
1 C$ I$ T9 a6 H. b
#define PWM1(x) {CCPR3L = x>>2;DC3B0 = 1;DC3B1 = 1;} // X X>>1
3 e$ o& C: x4 p! T3 y' q; s- u, x; ^
#define PWM2(x) {CCPR4L = x>>2;DC4B0 = 1;DC4B1 = 1;} // X X>>1
( W* t3 B. v5 P* t# A
0 |( C5 ]; Q- P' [
uint Time = 0;
8 m. Q1 O8 ^! }% k
uint cnt_1ms = 0;
2 Z: u1 x5 f( O9 O; m
uchar State = 0;
$ X. g; R! g, q1 a; n8 @; y1 M' I
0 C) w' Z, C+ F4 o6 O. C
void GPIO_INIT();
+ C) E7 ?+ `7 p: |# K, b
void TIMER2_INIT();
% O- I2 L( p( ^# t6 i# p4 T
void TIMER4_INIT();
+ e3 u, z9 X2 _; i- ^
void PWM_INIT();
- g: y, y3 H9 l
void Control_Task();
* @& [2 b4 o/ h1 o4 W
' x k# r! Y1 v: h( z, O
/*
8 u% M9 N) X2 ?' f" Y: |
*@function 主函数
# t. O% N: T" p. t6 V% ^, [( u
*/
: R4 M; q3 Q9 O, R
void main()
+ [' [" u, z# i% n8 h d! a
{
0 o* ?+ l C' Y' N% J" g4 d/ O& z
GPIO_INIT();
( ~3 F1 ^# \) O& C- W: W! u
PWM_INIT();
5 b$ B1 R: K; B( Y' j3 S7 o2 ?
TIMER2_INIT(); //PWM利用TIMER2
) [5 X, \1 ]+ p, B* M
TIMER4_INIT(); //定时1S出发改变一次占空比,调节PWM波形
( `$ S- E H! }% F& A4 a0 z. }$ g M
while(1)
* e4 b* d B1 R: ~
{
9 J: c2 r0 ]0 g. p2 ^3 Q4 b# C
Control_Task();
5 Y; c( G6 I! P
}
( S+ D' ~" O9 e$ s6 ~: _( h* \4 h
}
0 { z0 T/ ^& W% ?
/*
3 n. h9 c" l; K( g6 V
*@Function IO口初始化 RG0 CCP3 RG3 CCP4
. x% D( S* y3 `& B& c! W. z
*/
4 I6 }. L; X2 i; V
void GPIO_INIT()
( j* Q$ `) S0 I- n1 u
{
7 ?/ R: t: `# G
TRISG = 0X00; //设置RG口为输出
" [7 q" J' k5 I$ I
ANSELG = 0; //设置RG口为数字量
: r) O: G- `8 j: n7 X
LATG = 0; //设置RG口为低电平
5 l% p$ D2 G4 R" ]9 `1 s
, \, O# t0 f/ t2 J+ S% y. [
TRISF = 0;
% V' V: m7 f7 V9 h
ANSELF = 0;
; @9 r' s& Z% f# o
LATF = 0;
! E# m6 |0 x1 G; B1 ]
}
9 b+ ]! R& ^+ [/ K- e) q$ ] O
% g3 \# ] o# U# c5 P& X
/*
2 a% @. a- C9 {' d; q: S
*@定时器2初始化
8 H$ c% \% c4 [: v
*/
' j1 R7 h) w$ o
void TIMER2_INIT()
$ T6 P" s- t5 W. M, Z8 g2 y
{
$ s+ X! G( M& R% e4 t: k- W- H+ m
TMR2 = 0;
( y- i8 U4 {0 j$ @1 |
PR2 = 255; //255
h' y3 ?5 {( [! a
) C5 ]; a( F3 v, S/ i* x, h* e
TMR2IF = 0;
4 S- f8 s6 l) Y# [& |8 {
% U' b/ V5 E6 h* b
T2CKPS0 = 1; //1
. t- \* X3 n0 N& [5 e2 E
T2CKPS1 = 0; //0
, A# j2 c' M$ s/ O- f
. D0 H. G) q7 J
TMR2ON = 1;
1 k4 ~7 r( E! A J% l
}
% t5 G3 f5 J( k2 x
! E1 u# y# g0 d& t* B0 t
/*
. r9 z! J; M$ {8 p$ X% r5 d" m# T
*@定时器4初始化
; |' j5 Y S" Z
*/
3 u; ^( Y- \1 a! F5 t' g. [8 B
void TIMER4_INIT()
( b: @- O5 l, D
{
) X7 _) x- }- E& S; }
T4CON = 0x0e; //0000 1110
% N- c) ?: }! z! P
" I- S' E5 x9 O7 p5 }
TMR4IE = 1;
) z# j+ v% Z9 g* s
TMR4IF = 0;
+ }& { ]( G! o% q; T! z. I% A
4 L+ r: b4 b; |5 S; p" h
TMR4 = 0X00;
1 I) E+ M+ M* M# c! i( l" f8 a
, C, M0 {' X9 ^! L6 T! ~
PEIE = 1;
: \, D0 v( F2 o7 s6 M& X0 @- `
GIE = 1;
" R# `- ~6 B9 p/ x9 @. v5 ^- u
}
' Q5 P- c1 Q6 P- ~5 ?7 m2 f4 \
7 x: j9 D0 [$ h' `* S1 W
0 @( t* m$ D( A- @* }8 X1 [4 K; q
/*
5 Y2 l. K& _" W2 A7 w" x0 F- Y
*@ PWM初始化
+ D( K5 }" v% u4 _# n0 H5 O* K
*/
" U; h+ t$ t' R* `/ H; n+ G j
void PWM_INIT()
2 i s. y, n" z1 K9 P6 U0 p
{
& ]! b) I' i- g9 q$ e9 o0 D5 S
CCP3CON = 0X0C;
3 e3 I; q0 d# W$ k' F- A e
CCP4CON = 0X0C;
2 G2 E, t, @! h4 ^
8 t2 ~1 a) J9 F' ]# X8 E
CCPTMRS0 &= 0X0F;
0 g0 S5 a" N% f, z4 |+ \1 Z0 y
# Y8 E7 T8 S; p5 l V7 F
TRISG = 0;
' \8 q# G- s/ {& k2 A/ ?
}
4 }2 Z; n |8 g9 V0 R$ o3 U* W
' D) h, W8 A4 ~: |9 J, b
/*
2 U) ]+ U E1 P! H8 `# f
*@Function 主任务函数
7 E& |3 c/ t& h7 P! p3 b3 B, t
*/
/ e. {7 A U4 U5 p) b/ [
void Control_Task()
* K, S: @. O7 r5 {
{
; h @( W! M$ q
static uint Time ;
9 A1 ]/ h! @9 h' Y. V3 v& Q: {
// static uchar State = 0;
6 w; W* \; p& R* m/ D
static uint Record_Time;
5 N; C0 J' P- O6 Q4 f. b6 g/ t
uint Delta_Time;
C( Q$ k1 |# H1 h; v1 x; ^/ Y
% M J3 R2 v% }8 o; N5 M
if(State == 0)
) \! x6 W4 o+ G+ n: N7 @3 M
{
, g( c1 y3 j) q2 y7 T t* R p$ [6 m( m
Time = 1000;
7 c# y3 h$ c4 W
State = 1;
( P2 b/ F: U/ Z) _5 u4 {
8 |0 j3 R0 U% p0 S) N# O! \
}
: X. N$ n. }2 a# \+ r
if(State == 1)
$ }# [4 @, n A
{
: `+ K M1 R1 D" M
PWM1(Time);
6 _% M8 u- w" c) x
PWM2(Time);
* K4 y- v! |( R' d2 C) A
+ d. o- D+ r, V1 @7 Y0 {! T
Record_Time = cnt_1ms;
& l0 G3 m l5 a2 I G
8 P# L) U& E8 L" l. j" K
State = 2;
4 M. `& |. y- c: E/ K
}
1 c' C, W" }* D7 s" B- }7 t
3 ^. z2 S: A1 o5 M0 k& E
if(State == 2)
) w( j- p1 p. ?- v* x3 x$ u2 p% h
{
; a1 p0 M9 V1 j, t, @7 s
if (cnt_1ms < Record_Time)
- h' ^ _3 H. z2 z$ d4 H; N
{
- C3 o3 E; h9 r, L+ g4 ^. x
Delta_Time = 65536 - Record_Time + cnt_1ms;
" y# j! W( s. ^0 W' q8 F
}
% e9 m4 Z' x9 K
else
0 X! [" e7 p+ K
{
# R/ ?8 g! X% y N( ^/ g0 N
Delta_Time = cnt_1ms - Record_Time;
4 j: t4 O. ^9 O6 d8 Q9 ^1 W4 R1 q
}
! P# Q6 E8 Z+ d9 M, q! d$ y
9 j9 W! @( f, {' e
if (Delta_Time >= 200) //300MS循环一次执行新的PWM_OUT
6 R3 R3 n, R' P; x
{
l4 Q! f2 x% b+ X7 J8 s1 l
Time = Time - 100;
) f0 k- _# H9 r# ]) T1 Q* o* O4 w: l
State = 1;
C) ]' t) i! f: w, y
# b) Q( D6 }4 B+ H# P9 s) t2 u! ?
if(Time == 0)
* Z6 o D0 h( ?6 s7 a
{
1 F2 B( \ ?& h2 b; O* O( T
Time = 0;
* P+ D& t8 Q, i
State = 3;
; W. y3 I g5 Y6 a
}
7 y9 M, F: ~& a+ e2 z
}
2 L' X+ M9 @! f! h7 q
}
, R3 Z4 F) c7 V' F# @, h
4 I: Z, Q- m- a, X4 ] }
if(State == 3)
* m0 a- u' M; Q
{
+ }, Y& v. P2 M2 C3 F
PWM1(Time);
! Z/ f2 h- G- m/ t$ K7 _
PWM2(Time);
5 o# o7 Y+ A* N
7 u$ q# {% H; f; }( _2 w
Record_Time = cnt_1ms;
6 F8 P( o: M3 W
. F$ d! o0 p* v" q, Y. q
State = 4;
2 Q4 l: T) r4 w$ Z/ ]* R5 }
}
, C0 ]. ~% v( |0 P5 g. p5 _+ i
; N" U' u& f A/ t0 O W# o
if(State == 4)
# Z/ ]( F- s: n: h2 {
{
/ O) g. _: @, x4 s+ n/ F' X0 c; k
if (cnt_1ms < Record_Time)
) i( z0 i, G, r7 B4 O
{
4 t! P& z v3 h1 b7 Z5 V
Delta_Time = 65536 - Record_Time + cnt_1ms;
L7 d! l; Z# J3 F% U, A4 ?1 \
}
0 q5 x* q- m- B" W8 ]8 Y( \
else
4 j* m9 F& U9 `* G: |2 D
{
, ~4 }4 C: V$ ?' N4 M
Delta_Time = cnt_1ms - Record_Time;
8 K. `" q2 h* Z; P
}
& C9 b9 c( ~& F# i8 p& [# ]8 `8 L' X
_1 l" M* z* K% w- K
if (Delta_Time >= 200) //300MS循环一次执行新的PWM_OUT
$ k' r; q# N2 z5 k
{
' I- Z! o9 w1 O. k' ?
Time = Time + 100;
! Z, B8 a; ^: G7 H
State = 3;
% r) O6 c9 q: w& A0 J) H
9 n9 A, c2 Q1 h: e2 ?* {
if(Time == 1000)
; n4 v8 o- y2 Z% D9 A4 H
{
0 F, o5 e* W4 {# @' _: w2 a2 P
State = 0;
. D% q b |: p4 C
}
4 u* W5 ]) S/ ]9 d- T
}
E+ r& G3 G7 X" z; r
}
$ ^9 @: T+ i; r
}
. {, t9 E0 z; ~4 s# Q1 c
$ L2 ?6 e P* N. b" M2 P
void interrupt ISR(void)
2 ^5 P7 w/ p0 x* H m) c# J
{
2 J6 j$ V& z5 K2 w1 x
if(TMR4IF == 1)
9 l1 e4 X5 j; ^5 D
{
2 M& B1 _" M, B1 R% o1 I, E0 O
TMR4IF = 0;
1 V4 l- X+ S9 u
cnt_1ms++;
, I7 s3 Y. \* S j8 F# n& |
' a, M' k4 I( Z
// LATF0 = !LATF0;
' z: H* \ w$ \4 x6 S
}
) y# n9 ^, g" Q. D
}
5 ^/ Y& u. h$ I% H! F4 E
PIC16F1947原理图.pdf
2016-7-2 14:37 上传
点击文件名下载附件
下载积分: 威望 -5
533.08 KB, 下载次数: 0, 下载积分: 威望 -5
作者:
Wenghezhong
时间:
2016-7-4 14:27
谢谢O(∩_∩)O哈哈~谢谢O(∩_∩)O哈哈
. S6 P/ k$ ?% j& {
作者:
yhg-lee
时间:
2016-7-4 14:30
楼主辛苦了!
& |8 c6 J' y6 H2 b
作者:
xuzwg
时间:
2016-7-4 14:42
路过捧场,谢谢分享!
/ T. A6 n0 Z, v$ \
欢迎光临 EDA365电子工程师网 (http://bbs.elecnest.cn/)
Powered by Discuz! X3.2