|
EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
小弟学习PIC16F1947,自己根据画了一个原理图(见附件),以后会把所有要做的程序都发上来,程序是用状态机写的,推荐给大家。) f: A$ Q) a$ u/ k) }' D% S# z, n
0 y" }+ {2 q! ?$ Y9 v$ u( C' q0 F
/*0 L& s6 k9 P% b+ e
*@Name: PWM_led
+ _! R* p, I9 W; q8 q6 w*@version 1.0( @% O* L' P$ p
*@Author xiaopan
. q2 ?0 l! C0 i" ^ N3 z: u9 m% Y4 _*@Date 2014.8.2
& A; d5 ~! l; \*/
+ X) | [$ z& G" h' e! j: k
( Y6 o1 k& a" I#include<pic.h> q# _, j# w" A# O
#define uint unsigned int / `% h/ r6 a' E: H- j2 D, r
#define uchar unsigned char+ y4 v$ S! _( t) p6 P' Z. [5 U
/ t' K2 k" g% q#define PWM1(x) {CCPR3L = x>>2;DC3B0 = 1;DC3B1 = 1;} // X X>>1 ' {3 g* l& k! E e# v
#define PWM2(x) {CCPR4L = x>>2;DC4B0 = 1;DC4B1 = 1;} // X X>>1 ! P* f8 V) [" [& R# N: u0 X o
$ r8 [& R; r1 Z
uint Time = 0;
; c; \' D" Y2 Z1 C8 Tuint cnt_1ms = 0;) G# \. J0 o8 x0 J* C
uchar State = 0;$ z0 [2 r7 I% H* U4 N7 I" B
' [5 F1 ~, \. l- B$ {
void GPIO_INIT();. K. P3 g _4 Q( \: g2 `; x7 o
void TIMER2_INIT();, F* e( O# ~) ]" j1 E7 U( {
void TIMER4_INIT();, |( S0 l0 |6 j+ h2 c
void PWM_INIT();: r3 A0 c( m2 Q7 C
void Control_Task();
0 p8 h' y1 i( y5 T; y/ M* a, O3 a
5 T* ?, `6 D2 A8 d- p: W/*
, s+ m& W% [- q/ K* s0 I*@function 主函数
$ H. @/ _" r" Y5 _7 J6 j( P*/
, q: H8 P9 g/ M8 E( n9 q: svoid main()4 h/ P- m4 J2 z7 L: [! e4 ^3 h
{; E- E8 z, c5 c8 x, ~& }& n( z
GPIO_INIT();: X$ x1 s: W6 e* P# F; N
PWM_INIT();
6 s' }4 ?# P; q( [ TIMER2_INIT(); //PWM利用TIMER2
- O$ d M8 J) @5 H* a TIMER4_INIT(); //定时1S出发改变一次占空比,调节PWM波形
' v! q2 `: v7 i' t8 V( ]9 g while(1)
, N" H3 Q: ~. p {
; o0 z( p$ T( |9 S6 P0 I+ @+ I3 H, L Control_Task();
9 j. E1 j( E5 s }
6 q. T+ `3 M1 m+ h7 |( b3 f}
+ [5 s Y/ l Z$ `7 H. I* I/*% a, K2 j, ?, M* x( ]% y
*@Function IO口初始化 RG0 CCP3 RG3 CCP4; o g+ W- d8 K" }4 z; ~( W/ i7 _
*/; O& ]2 x8 S9 P) H
void GPIO_INIT()
+ j: s# U9 P" p{8 `; m4 G `/ } E' X
TRISG = 0X00; //设置RG口为输出8 h- y" k; z8 @/ x
ANSELG = 0; //设置RG口为数字量
T, p+ }( _, r9 ~& w- K LATG = 0; //设置RG口为低电平
3 ]( V( O5 ^: t, |( |% |( Z ^
_- B2 p h. v- e, W2 ] TRISF = 0;( }6 D. r5 Y+ L( G' U) @+ y7 Z5 C, |
ANSELF = 0;
7 q) a/ q+ O) K LATF = 0;, v' i% B n6 ^8 |
}( @3 y' b; }, S4 R" O+ G, {& z( t( @
4 R4 M$ n7 _; A3 ? Q" v
/*
, ?3 r& t) w# y3 ]' [: r8 [*@定时器2初始化$ @8 t+ m* ^/ r0 P$ p3 u$ d
*/7 C9 Z0 v @$ t5 {5 X, ^0 v
void TIMER2_INIT()/ j% M! A1 }5 o$ @: k$ p% X
{
3 u3 k: `$ U8 o4 k+ ?4 h TMR2 = 0;6 U+ u1 I& ?4 G
PR2 = 255; //255
$ p4 ^4 V+ ~0 J8 T+ h9 } ' v! R( h* Y0 O7 d- a8 d, a" J
TMR2IF = 0;
6 n" G C- x) ?
( t+ H a& y) K/ F5 R, H T2CKPS0 = 1; //1
; d* D/ R! O# W! d6 ~' f* H T2CKPS1 = 0; //0- F! [( e8 m- |6 b9 X# u0 O3 X
6 L- z, @4 x6 M K# v2 G# d' p" x
TMR2ON = 1;
) _- X- [; L$ G' B" X! a' H" Y! ^}7 H9 b+ ^# Q- i! E. w6 t: Y1 E
# w; N/ `" D) B- {
/*8 K* z! I/ D3 y
*@定时器4初始化, n/ {4 V* ^7 v7 K* e
*/
+ x4 R! h! T" ~void TIMER4_INIT()
$ c: k& u% f5 ~2 `* D) M, U{
, \) k W. n4 ` a0 L# b T4CON = 0x0e; //0000 1110
" s9 c( r; u% \9 \1 C; a8 u; G# h! v. r
TMR4IE = 1;# l1 c' s; M$ f r1 U. V8 @ v
TMR4IF = 0; ) j1 E6 d# O: e0 ^. Z
( F3 L, d+ H# a1 M, ~
TMR4 = 0X00;7 ?; f. l( M l) b! }
$ p2 r6 a1 h% Y2 s! [
PEIE = 1;2 ?: l! d/ V6 m3 s+ t" ^
GIE = 1;
$ C! U. w! a4 x& f}
8 ~+ k4 h P+ W! ?; N5 A- ^1 r {9 d6 A/ |2 a
8 W3 c) D* W9 s1 Y
/*$ c, ]! y) a2 s
*@ PWM初始化- r8 e3 c. J4 c/ Q
*/
& s+ F+ \4 j0 Z7 }- R+ n c. pvoid PWM_INIT() a* R) b) X' q$ P: F* X
{
+ ~1 G" X: E/ _! D! l# f! { CCP3CON = 0X0C;
+ d5 T7 S: p3 o/ D( Y" O CCP4CON = 0X0C;9 E" H% v9 h- a& m
6 V! Q$ [0 n& X; M& D CCPTMRS0 &= 0X0F;
2 b& x# m% m: h8 E
8 f2 P o; r6 g2 G! p8 m TRISG = 0;, J# t9 o0 V- n- I; e
}" }1 W+ s6 u3 v3 ^
# {$ n) {5 _8 }6 P4 o# F& R1 v: Z$ l/*
) t7 B% F8 |. c* ~*@Function 主任务函数
6 l% V% c- b: M) S# w7 {2 n' O*/. K$ c) I/ S) [
void Control_Task()
. T; K' P) O& g _" r* z{9 o$ Z+ [9 Z6 a( ~ h( d# _
static uint Time ;
4 X; K9 F5 }6 _6 R% s// static uchar State = 0;' T; i* n) f9 {# c8 r! F* a
static uint Record_Time;
! X% |( j/ |% V, D6 s uint Delta_Time;& N( A& j8 G. ^
* C, t+ z( X; [+ v9 ^ if(State == 0)
6 }+ u" u6 A& B) | w6 m6 b {, I/ j( U* _( J& o# k
Time = 1000;" n: [) `7 V: X. c6 y o* p& q
State = 1;
6 S5 r6 U3 c( p" v$ s7 D8 N7 d4 J' s5 k( e
}5 F6 L; ~# c& R4 T( M; C
if(State == 1)" `! l& k( ?" n8 Y* R1 x" T
{
. a& l: p3 q' e: ?( }+ A$ y PWM1(Time);
& `" g' F) }8 H" u I6 g PWM2(Time);
7 Q4 Q% ]5 k, j" n6 a& a
& [# O! V' z1 H2 C+ d, U/ k Record_Time = cnt_1ms;
5 v- ~6 \" x A) X& [5 P! i
% X# x& z/ ]* s. G* W State = 2;0 ^' l3 G1 C B4 S
}2 G( J" s) K+ r
* P) q" p5 h/ m! B! b1 q" E
if(State == 2) j9 X# q+ z3 e# s3 Q3 ~9 E" Y
{
6 A4 C+ U2 p! X. _6 J4 \1 r$ { if (cnt_1ms < Record_Time)
% k; @- U' M1 f, \, e4 g& C d1 Q {
# W5 g- p1 u- W, d Delta_Time = 65536 - Record_Time + cnt_1ms;
6 q- K7 ^5 G: l) q7 a p% R }
! J7 ^9 |- O- I D( g- t else0 S9 L3 S- k& G+ v. ]% r
{& U$ @/ j2 ~5 v- {/ D
Delta_Time = cnt_1ms - Record_Time; 1 p& g! D d2 X. F
}
# \& r* z- Q0 B; b& V, l5 }
i' D/ ^+ l1 J3 H4 D& {5 m! i if (Delta_Time >= 200) //300MS循环一次执行新的PWM_OUT
, z9 p) t1 E D" ~ {% f, f$ Q* V" @7 k2 M
Time = Time - 100;5 |$ w, l* s' N T1 b
State = 1;- C" q, {& U( N% Q& f
6 T. Q8 Z% k) K0 Z4 a) p
if(Time == 0)
0 {- H5 v4 p6 N1 o! x( V {9 a* l. d2 M& V' V+ d) T
Time = 0;8 z5 @4 {# ]9 ?; x# D
State = 3;3 [" I: [' x4 j4 Z( S3 H8 O
}
# @4 r: H4 p4 K9 P }
$ H0 R$ o' a4 {2 e }& K4 a1 G6 f& ~7 G& z
3 v% N& l7 a( q3 j* H7 b! Y- w `4 Q; ?
if(State == 3)" S; Z" A- O# A" K9 }
{) r/ c, t% ]- h* m. r4 |' O
PWM1(Time); k+ U. a% Z9 u% K3 L. E9 {
PWM2(Time);1 Q' _% M( @7 `& D2 q" d
! I, @! c- g8 X, b( M$ p' P, t Record_Time = cnt_1ms;
+ |2 k) B9 p8 @: S- _ 3 y+ T$ Q# \( e# f
State = 4;" E8 y3 R @2 e2 r4 G+ R4 ^
}
9 @2 o* f c+ Y* k! E' W* I# b L8 u6 W
" B Z- D/ {6 m, W) { if(State == 4)
3 k, t8 o+ c' k/ y {
# y4 J$ ^9 f) B; I: e6 O if (cnt_1ms < Record_Time); f0 {- ]! B5 C' ^+ C( Q- ^
{! Z6 V+ S- K+ Z" s* Y
Delta_Time = 65536 - Record_Time + cnt_1ms;
! i) E5 [+ S1 c/ n }
$ g2 |0 B( E; b9 S" m else
+ c8 j0 H( v0 h2 q( a: ~ {8 e$ _# V* @" S6 Q# s: U0 d
Delta_Time = cnt_1ms - Record_Time; : N3 a- P) ~3 M( g& `3 P# U5 t
}( u% j! p3 c: A0 f9 ~1 [
* P9 j F' n6 [1 K( n7 m if (Delta_Time >= 200) //300MS循环一次执行新的PWM_OUT$ | i# x, f: D( P' P
{" T2 g9 u# ^3 ?6 d; e; g& F
Time = Time + 100;
$ f, h) i, P6 b" O7 e State = 3;
$ R- B9 K2 Y, q7 f% s9 g p2 Y i * E4 M2 @* h: N% o- o
if(Time == 1000)
- n& c4 g- N z4 V! |' B {7 {" N# q- g' x0 y4 a
State = 0;
& V' {; {4 h Q5 |2 Y( w }
' A6 U+ d& R$ k" r, V }
$ m7 j; _( I* A, D" w }
, k6 o* f1 G4 @5 x( J6 Q* p}
1 v9 A1 Z' A ^( s0 }. u' Q9 |
void interrupt ISR(void)* }+ s; P5 ^/ O- E# Z( ^& N9 r( \
{, z, l9 s- _- B# B+ }' h
if(TMR4IF == 1), b: i& }5 J! E9 U
{ x( H9 g9 Q' R `; W- ~
TMR4IF = 0;+ B2 T) R8 O9 R/ Z+ A' k
cnt_1ms++;
, O+ a% k; c4 x& E( n# M
8 Q2 f( j8 M2 c+ z2 V // LATF0 = !LATF0;
0 f0 y6 G0 Z! s7 S }
( \; W8 }' w+ h3 C G, C& K/ Q}" w" H- k8 b( X2 {. F; D4 |0 V6 J
|
|