|
EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
PIC12F508单片机学习之一——定时器
( V5 R: ^5 [( d6 L: D1 N( K
) [+ g9 R9 l ~$ `& @, LPIC12F508单片机是没有中断的,定时器只能是查询方式。
3 `" g7 J4 M+ B* p9 E
7 H; K/ B2 P3 q- G" g编译器用的XC8,编译环境IDE用的是MPLAB X IDE。% ?) e# x. p- x% b5 b
6 c: V! Q% k. Z l) U% ]0 K+ `//***************************************************7 V& B: L h% C" g9 [
// __________________
: Q V( p& C/ [ o// VDD-| 1 8 |-VSS
" C- _, y( B% t! M+ K( I$ c- A// GP5-| 2 27 |-GP0/DAT" \5 \% ]' i# C) r
// GP4-| 3 26 |-GP1/CLK
. U; P/ F5 j$ o//GP3/RMCLR--| 4 25 |-GP2
0 i+ N5 ?* O# ^$ B4 I// |________________|/ m7 |$ R2 I. s
// 12F508
& V+ d6 D5 x0 N//***************************************************
3 {7 B* U* B9 J) B: y- o; y1 M4 v: ~$ s: N; o
//定时器模式试用
' d% u2 h+ s" S7 \#include
1 L4 q+ a" A. e( q- x5 i) K#include
9 E! }: U, v+ v. h- N( _& P
6 ~: \9 J0 N. ~9 P% G// #pragma config statements should precede project file includes.
3 `$ s. |! M; M1 i# f2 k s+ e// Use project enums instead of #define for ON and OFF.
* \( t6 [# P! O5 Z
- X# ~. }9 f4 A, S1 x# y// CONFIG9 z$ }1 x* V* y3 \
#pragma config OSC = IntRC // Oscillator Selection bits (internal RC oscillator)# J W: b( v( `, Q" r# q" V* h" ~
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled)( V0 `# f+ K: y! K& E
#pragma config CP = OFF // Code Protection bit (Code protection off)
# e" ^5 @# R! Y7 N1 W2 t! {2 n#pragma config MCLRE = OFF // GP3/MCLR Pin Function Select bit (GP3/MCLR pin function is digital input, MCLR internally tied to VDD)
7 V5 X1 b6 I' h- @! Q9 M
. d( W$ b5 s( _! d d( U& k#define uchar unsigned char9 f/ L# j4 Y" ?) t) B2 D# ^
#define uint unsigned int
3 h% p5 h' f; t5 Muchar count;
4 Q4 i3 {2 R- N1 h3 g& i, n//uchar GP3_F;
! P. k8 n3 k/ Z( fvoid Init()% h* T7 }+ M" a/ }
{& a& {. m* A6 r+ o- w8 M+ G9 R
TRIS=~0x3F; //GP3输入,其它输出- [. Y% u) W5 { G8 q+ @% w
OPTION=0xC5;//分频器给定时器 64 4MHz x=257-tJ/4F! L) h7 k# e# C, s0 d9 w5 D* \
TMR0=0x63; //10ms x=257-tJ/4F
/ u; f$ U0 N6 t}: x( @, C" w1 l- Z1 t
void main()
6 [ ]; x3 \- Z% Z# Z& _{
$ l1 S, A5 \1 O* K- N, N: ~Init(); ( z; T3 r7 p m. E0 ^
while(1)
2 t( J! m9 {6 X* \ if(TMR0==0xFF)
) w1 h/ g8 U' j# b1 N {
8 S" z% q8 c0 n5 I0 k4 |# S TMR0=0x63; //- Z( b2 @- v7 \1 @9 e
if(++count==50)//1s
1 P# m! `- k8 Y. g. k {3 V" U# T3 g( x4 I R# @; r( O) X& u
count=0;
& B' n+ v* q8 S GP2=~GP2;//LED闪烁1 [$ Z+ M: T8 k6 o J7 ?4 S
}+ W, x% z5 M' S- Q
}
2 J/ s/ d' ^7 d( N% _ }
) H! l6 k6 i; ]' E1 n6 q& B/ g5 F}
, {% m2 n, p1 u1 }( n8 | n( Z* |1 t
# g ~, s- E8 O! r0 `7 JPIC12F508单片机学习之二——看门狗和休眠模式试用
" l# j. F% [; x o0 u4 m
, U1 j' S: [2 L' `PIC12F508单片机是没有中断的,复位情况只能是查询方式。
+ _2 d, `2 K n: P" u: o' N I. C9 V R G y: O
编译器用的XC8,编译环境IDE用的是MPLAB X IDE。! w# ?9 ?! [6 A/ i! C1 c" t9 ]- j
( a5 R% L, e" G0 \9 F下载器是PICKIT3.; s2 I$ Q8 S+ G- V* _! g/ M
3 S$ z" f8 O% x! x//***************************************************- T! K# ]0 x4 V! G3 J0 C
// __________________' Z# e* O$ Q, b3 ^) i1 S% d: i9 n
// VDD-| 1 8 |-VSS
, E3 A p$ I3 C9 ~7 @; A// GP5-| 2 27 |-GP0/DAT0 F R/ z4 ?4 {# h0 [) a" }
// GP4-| 3 26 |-GP1/CLK
, b! Z0 ?& N' p0 T0 l//GP3/RMCLR--| 4 25 |-GP2" k' L. ~' H0 {4 z* u
// |________________|) R# G7 o. C8 |: a
// 12F508+ Z1 G; G4 U* l' |# _. [5 B
//*************************************************** ~. f0 ?1 e0 G h- R6 W! Q0 b' r
// 看门狗和休眠模式试用0 ^2 K5 z0 d [) b3 X: g/ o. ?
; G1 B& c2 X' d0 N* ^
' D/ s: v. C) R2 `* ?! r#include" f5 y5 {& i$ h8 W
#include5 n2 X. Z+ A' u; i) W4 Z2 g8 B
5 I: i' p& e3 [4 h) ]// #pragma config statements should precede project file includes.) I- b; j5 ?1 {/ z/ j
// Use project enums instead of #define for ON and OFF.: J6 N d' `* p, C4 ?8 z
3 K9 }# V: n. [9 C+ U, i// CONFIG! |7 Z* Z4 M7 Y, l4 j9 r( M& a
#pragma config OSC = IntRC // Oscillator Selection bits (internal RC oscillator)
7 N* R/ a1 k5 n' m! k- ~#pragma config WDT = ON // Watchdog Timer Enable bit (WDT Enable )
/ Y$ u3 }; h( F* z& A9 T#pragma config CP = OFF // Code Protection bit (Code protection off)
: _$ n+ Q3 ^$ o& z#pragma config MCLRE = OFF // GP3/MCLR Pin Function Select bit (GP3/MCLR pin function is digital input, MCLR internally tied to VDD)9 O Y) U# d# f1 z+ _0 ?1 d' f
4 E" Y: J+ B1 j8 s! F#define uchar unsigned char& A V( y- N1 u4 |/ V$ \$ I
#define uint unsigned int3 P7 G9 k* L7 n/ s% K. P( [
uchar count;
' {4 ]: x1 `! G* {//uchar GP3_F;! e, d8 J1 A% U8 `
void Init(); a/ V+ Y5 ~0 ~
{% r* D8 b. e3 g$ ~& f/ `
TRIS=~0x3F; //GP3输入,其它输出& o# l) v( F& C' W% L
OPTION=0xCE; //定时器分配看门狗 时间是18Ms*64=1.152S
$ ?4 l6 T+ }: @7 u" D6 H0 r' y}
3 g9 q7 G. i& ~: _' | l; x9 ^! `# Svoid main()3 ?4 {1 O0 y: r& O
{ / ~2 O# i, a1 X8 A' z
Init();
: i2 ?0 r, F+ {$ o6 l" P( I
7 N1 t. H8 C; G/ \( s1 ^
* A7 i. O: a0 l: [while(1)
) u3 }" [9 `3 x1 W {( i, P& R6 L- }& I$ V
; i8 A" i6 z! x" p: T if(nTO==0) //看门狗引起的复位
* I1 R/ Y) I/ I, | {
' a% {# X8 C( g4 m: }0 [! d GP2=~GP2;+ c; q H9 d+ A" q7 Z
}
" w" w6 @4 | g- e( L" F4 t SLEEP();
. O7 i0 N5 E8 Q5 D }
. I( V. o4 G6 ^9 s& v; R}
6 P0 F! m5 |- c1 E3 _
" k7 [& F, z# ~6 F y+ f1 D1 e
: v2 ?5 [% I) c& jPIC12F508单片机学习之三——按键唤醒 G/ k: F \! \. g* g* x
3 P% A- ~2 H4 l# Z, m5 g/ t8 uPIC12F508单片机是没有中断的,按键中断只能是查询方式。
& n* B7 T2 y& Z3 V' e
( s" Y' _3 c2 f9 N编译器用的XC8,编译环境IDE用的是MPLAB X IDE。3 g7 ?/ m& m. v3 m- W# f, |
; R3 k5 S7 c! j, _下载器是PICKIT3." D1 H% ~) n! z
6 A" j% g0 {1 S
//***************************************************
/ d5 M9 o0 {2 C# c% L1 T// __________________
5 H) j3 R; _$ K2 `0 P% E- D6 X& _) [; |// VDD-| 1 8 |-VSS
2 U# @& i, b1 [- m// GP5-| 2 27 |-GP0/DAT
4 W$ R* ~- g' V5 _% p# S& ?- N$ L// GP4-| 3 26 |-GP1/CLK8 W& Q6 n+ u1 ~$ q" X% e/ f
//GP3/RMCLR--| 4 25 |-GP2
0 t8 ~4 f( k7 i! h- g- K/ R7 w; g// |________________|/ J- t# M' E# j" J* g3 ^# X) k
// 12F508# A9 a0 e, K. n8 Z% ]5 e8 x
//***************************************************
k6 d! I. `; E+ U* I
* s2 G- \' W( d0 |//看门狗 休眠唤醒
! U3 Y& O- K6 Y/ A//按键唤醒7 F! \$ `' k: ~; u1 s3 K# i* ^
8 W3 ]! R0 k" I% W0 @+ r* k% L ^#include
, M6 f! u3 T2 z3 T#include
, D2 A! d4 I# }& X! f" W% h8 N" V" A" Y" m" s" X
// #pragma config statements should precede project file includes.* z7 O0 @2 `4 k
// Use project enums instead of #define for ON and OFF.
4 U1 `$ V5 K1 j. P+ r& r
" {8 k* Z0 f6 _9 q3 U// CONFIG
. U5 J' ~: i( u9 c' s, t! q#pragma config OSC = IntRC // Oscillator Selection bits (internal RC oscillator)
/ r3 x, T+ u4 T7 P y( d1 ^8 [#pragma config WDT = ON // Watchdog Timer Enable bit (WDT Enable )
/ G+ v$ E% o; ?# H8 Q0 a3 W0 r+ L#pragma config CP = OFF // Code Protection bit (Code protection off)
" M* u* q* d) O+ e: J#pragma config MCLRE = OFF // GP3/MCLR Pin Function Select bit (GP3/MCLR pin function is digital input, MCLR internally tied to VDD)0 u# g1 f5 x% O3 v
. ^1 V- x6 @: B0 b l/ N) \% i#define uchar unsigned char! O4 W9 ~8 e/ i
#define uint unsigned int
; [# c/ y) F/ z#define LED1 GP57 M7 ~7 b: r. z, d; @
#define LED2 GP4( X5 ~* D8 C0 ]2 l' Q" l" u
#define KEY GP3
, T& A+ d& L6 a8 U k$ {bit KEY_F;' f' `8 w) X/ Y& m9 ^9 r& ^
uchar count;
# U% n0 W2 V0 `3 G, |5 w! A//uchar GP3_F;
a3 U& l. Z& J+ z) F+ v# } mvoid Init()
h( D2 q' ~2 M2 C7 q{
3 p! K9 g! c( F, u' R! | ] TRIS=~0x3F; //GP3输入,其它输出
# S3 \4 _" z: n // OPTION=0x07; //这个寄存器上电复位均为1# M" H" v3 e/ G+ M+ p5 k
// OPTION=nGPWU|nGPPU|PSA|PS1|PS2; //引脚中断唤醒禁止 弱上拉禁止定时器分配看门狗时间是18Ms*64=1.152S2 j: { H1 C2 ]4 s( R$ z9 Y
OPTION=0x1E; // 引脚中断唤醒使能 弱上拉使能 定时器分配看门狗时间是18Ms*64=1.152S5 B3 j8 J6 O8 i( L
// TMR0=0x63; //10ms
! N( j9 A. N* b- X& E}
: M. b w8 ^0 p) ]9 R d, @void main(); m) F" P: |5 @- ^
{ 5 }) z" F' Y1 q
Init(); * G a1 {0 m2 z/ T. @/ s
while(1)
0 H8 \" @5 Q3 h! m6 x{3 s0 f( j. S2 W. h0 m
if((GPWUF==1)&&(KEY==0)) //引脚引起的中断唤醒* |) r# g5 p: l# ?) [; ~# |7 T
{
7 a( t, i$ ~3 ?, X LED2=~LED2;$ z2 _$ R( z# |" `0 q0 L
}) P+ J6 ]& w6 H
if(nTO==0) //看门狗引起的复位/ y8 ~, F0 Q- ~9 B
{* N; x" Y2 K3 E
LED1=~LED1;
# ^) y+ u8 X" i6 c6 u }
t. P1 @ t& ~9 m# a2 C" A0 { KEY_F=KEY; //读出休眠前的按键状态。
4 H( ~) e) Q; d5 x; c5 ~; O; d SLEEP();4 _/ x- Z2 R1 B/ m7 K
} |
|