|
EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
PIC12F508单片机学习之一——定时器
2 p. q; @# W7 y# }) c& [9 @8 ^" ]) H% k3 `! d
PIC12F508单片机是没有中断的,定时器只能是查询方式。
, p# ^. n/ S4 ]3 d
5 f, T+ j- {' `$ O+ y编译器用的XC8,编译环境IDE用的是MPLAB X IDE。7 i+ q4 {0 I4 `8 O9 T4 n% d
8 S4 q6 U1 W, a//***************************************************7 @7 Z8 Y9 k9 g7 ?7 u, i a
// __________________
; g6 q- D3 o% Y: R0 Z& c5 @// VDD-| 1 8 |-VSS
2 I$ P& o: B6 y8 w* z% }// GP5-| 2 27 |-GP0/DAT
4 _9 m8 k3 _ X5 \// GP4-| 3 26 |-GP1/CLK7 ^" h) X! c8 J6 f0 ?
//GP3/RMCLR--| 4 25 |-GP2
: J& f- x4 ?# G A// |________________|
, {0 u* K$ C6 w% R0 `- ^// 12F508
, K. }% Y4 y8 ~, P$ ~//***************************************************
3 |, B5 y2 v$ {# q: g7 a
$ l: G, b, E4 a: p3 v//定时器模式试用
: J4 | g$ o2 Q1 _% z4 q2 T; m#include0 s1 ?8 m- A9 x1 h& i' a
#include6 @+ P) }( }. o" b" s) D( S+ @
. h( D \( L' r
// #pragma config statements should precede project file includes.& ]3 L( h2 b ^: T/ _6 Y
// Use project enums instead of #define for ON and OFF.
& ~; g, m3 l5 G* I% I, V+ b
6 a& Y% I; K1 n; ?* J0 A/ |// CONFIG
3 u9 L$ T R$ h* T; e3 o# l#pragma config OSC = IntRC // Oscillator Selection bits (internal RC oscillator)
$ g5 o; O! B- D0 a( ^9 q#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled)' R7 v# ], y: w/ d( Z5 c
#pragma config CP = OFF // Code Protection bit (Code protection off)7 e1 {! `( m8 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 b3 O7 b; _9 |9 N L8 e- x
$ c9 Y! O1 o! I: V4 Z& g4 \- s
#define uchar unsigned char( C5 E1 Y# Z% B. @: F1 ?
#define uint unsigned int
- G2 e- ^0 B5 O* }6 v N7 ouchar count;
4 p, _( B- H$ M: P( Z//uchar GP3_F;
, r! X+ m8 U0 z% g( u, Wvoid Init()2 \( a) n- \4 P/ d& |
{
; O! s& `% j) B4 @% D* C. Y TRIS=~0x3F; //GP3输入,其它输出5 e1 W- M( D) t) `2 Y, }/ b
OPTION=0xC5;//分频器给定时器 64 4MHz x=257-tJ/4F
$ p9 D- C7 o6 C+ \# L TMR0=0x63; //10ms x=257-tJ/4F
7 u( B9 Z0 g. r1 P1 {' z, ]. _}
/ i" A2 n3 V$ Kvoid main()
/ m+ u' s/ i& \+ X. _: L{
7 B2 d7 K8 F# g( J( K6 QInit(); % {2 P' B9 B `- w6 ^; _% W
while(1)6 ~/ X s1 r E) y+ S0 F
if(TMR0==0xFF)
+ D0 T( t/ R, R P {4 Z: A9 S8 W- \; p \" s% w
TMR0=0x63; //
) [( w( t; b1 ^, n L if(++count==50)//1s
6 c$ e7 I: U3 ]6 g, @/ j {
, g$ H" \, g1 c) ?9 N count=0;. H7 D* P* M& |' l. H9 Z% T
GP2=~GP2;//LED闪烁
/ F& @1 C& M; Q+ }; Z. N }% ~ P. j" F: k8 N
}
* j8 B. E7 i) p6 r- g; f" @2 I }9 F9 h) ^2 ?* Q$ D2 h
}
: ?4 S) b4 K0 X1 z3 D; h2 _
8 X- v. w; p# w+ O2 ?: U. G, ^
% h% U. v+ H \% UPIC12F508单片机学习之二——看门狗和休眠模式试用0 a9 y- P, R3 `4 r& w+ K
( a. C) |% b- N: m* GPIC12F508单片机是没有中断的,复位情况只能是查询方式。: l5 g. X/ Q( R- l: B: |
2 l% x0 f8 q+ W6 Y6 ?- E5 E编译器用的XC8,编译环境IDE用的是MPLAB X IDE。1 f$ Q% x0 r' u9 u) }; a- B
$ ]1 u. N" C O2 |& A下载器是PICKIT3.8 a+ c. r5 f! o8 J2 f% t
4 K; z) [1 |4 O: d
//***************************************************
1 S+ c: m) g: D3 e- \/ p+ p// __________________# a1 q; x6 f/ L( K( ]1 U5 ^2 z% M
// VDD-| 1 8 |-VSS
4 J" a. { t; |7 n( h( m// GP5-| 2 27 |-GP0/DAT
) [8 i$ g( ?4 `6 {, i, ~// GP4-| 3 26 |-GP1/CLK
2 F2 {9 x: k3 U* G//GP3/RMCLR--| 4 25 |-GP2
; h" U' u2 D* V$ {" O% x4 |// |________________|6 b$ n- W" N, U. i% e6 T. w
// 12F5083 J: U* t* I: D4 X1 {
//***************************************************) L) n) G. y7 n2 E* K5 \2 R
// 看门狗和休眠模式试用
5 D1 v7 l2 X {4 M9 e9 D5 Y4 [7 u0 Y# Q! `& b
6 H7 }" r, z; z% n/ B
#include9 z8 @9 c: @- n
#include
- U- U* [2 ?# w5 G4 x% x
. W2 z3 d' [4 W4 B$ y/ i: J// #pragma config statements should precede project file includes.# C# M' ` y, m; ^4 L9 @& \: x3 T8 g6 L
// Use project enums instead of #define for ON and OFF.
; o8 F8 e% ~ ]6 P* s% O1 T& d( q; g) ~3 D* ?! e& {
// CONFIG0 D7 L1 o Z% j
#pragma config OSC = IntRC // Oscillator Selection bits (internal RC oscillator)& O* V! }2 }9 n' N
#pragma config WDT = ON // Watchdog Timer Enable bit (WDT Enable )
# Y! p- _2 S, E+ J4 f3 o: h#pragma config CP = OFF // Code Protection bit (Code protection off)
7 M0 ]$ M, v4 e( E' }# }#pragma config MCLRE = OFF // GP3/MCLR Pin Function Select bit (GP3/MCLR pin function is digital input, MCLR internally tied to VDD). B% X, l& l G( h! \( _
: B3 y2 I$ N+ N! ~) m* U$ r( l
#define uchar unsigned char9 A" K% I/ e- j6 E
#define uint unsigned int
6 r/ d. u& ?# R/ {+ A! W- ~uchar count;
# M8 P N8 `, h+ y) L* y' W8 c2 S//uchar GP3_F;! t6 `8 m- B$ N# Z {
void Init()
# y( u- ]2 l, h( K* [; i{ g! C: c6 W4 H& b
TRIS=~0x3F; //GP3输入,其它输出
3 K, x9 s. f" s ] OPTION=0xCE; //定时器分配看门狗 时间是18Ms*64=1.152S
; K" @% {) h# V2 H1 ~8 [ j k}
# w. C9 K& u5 ^7 _4 v/ c8 `void main()
4 P5 s: G9 h0 H5 V# E# o, U2 q, [3 n; N{
* z8 f1 D' |' d) DInit();* P. E5 b) z. Q1 {( e
/ W. g8 _; Y+ X9 b# g4 T
1 ?. }- m& M$ \7 Qwhile(1)6 X6 m/ A5 f( i+ r H. c
{
& E6 J! I( B, P" T+ l- p) S7 A: {- u) m2 C: W+ A2 `9 F3 S* ^4 g
if(nTO==0) //看门狗引起的复位
% [ h. n( H; @6 q* ^+ H {3 k/ e- I; O* y
GP2=~GP2;
" h+ Y, F# L$ S$ ^0 p( ~& k } 5 A2 w* W- C% b; y8 T# ~) r
SLEEP();8 i# ^+ T3 C6 Z
}
7 J# @& _" C- V: h2 `- d( M}
( b8 y) [ d# N# P1 p5 M+ B) T
: F6 |4 e; p9 b
PIC12F508单片机学习之三——按键唤醒
: p4 e: ]% F. v: ~$ f
% y, n& o1 c4 j6 P7 Y" BPIC12F508单片机是没有中断的,按键中断只能是查询方式。: l4 P% m6 |: d
. E$ j# O: E3 ^6 Z) l
编译器用的XC8,编译环境IDE用的是MPLAB X IDE。
$ k( U V* y( h5 O( H6 h' k, b0 `4 c, ^& Z
下载器是PICKIT3., n' K' s+ W4 a( x6 j
- n& J# S( \% L1 q//***************************************************3 v* V* E3 L. h; h8 B
// __________________. E: ?+ R- i9 s+ R _
// VDD-| 1 8 |-VSS
: ]4 ~: T! Q$ x. H& b ~// GP5-| 2 27 |-GP0/DAT
- ^, U) {( h+ B// GP4-| 3 26 |-GP1/CLK
( q; ?- i+ V/ L, D3 ^* Y//GP3/RMCLR--| 4 25 |-GP2
I" R8 v+ B0 Y+ k// |________________|
# k& R3 r% l; B( `! ^7 p// 12F508
0 X$ {* r6 f" Z$ O; I. Q- C//***************************************************# S) k0 F E' r3 e
, o6 Q, e! s. j M! \//看门狗 休眠唤醒
6 x: M9 x n* a* Y9 q7 z//按键唤醒
( X4 Z0 F( W* V5 N* f% c' G$ Q9 E& I$ L5 s
#include; e* O. H( {% T- i; s
#include
' K, l6 W$ _9 |' W5 ^# l8 x6 S5 u
/ c- }3 V- P: v1 O+ b// #pragma config statements should precede project file includes." ^6 l* N! I. j9 G
// Use project enums instead of #define for ON and OFF.4 ]. j$ [/ `9 \/ f% D2 G
/ D0 P( J3 \9 P9 m, l// CONFIG. X1 A0 S# Z! g9 {% j' J
#pragma config OSC = IntRC // Oscillator Selection bits (internal RC oscillator)
( V, f3 H: o0 ?6 ]0 v#pragma config WDT = ON // Watchdog Timer Enable bit (WDT Enable )
- O, G# a2 M; L" `. S, C% a#pragma config CP = OFF // Code Protection bit (Code protection off)4 ]% @" T' D9 i
#pragma config MCLRE = OFF // GP3/MCLR Pin Function Select bit (GP3/MCLR pin function is digital input, MCLR internally tied to VDD)+ Y' }- v; E% k; d) n
; T3 h- Z! e: Y& `. A8 R& t( c4 V#define uchar unsigned char
/ j( w2 ~- T, } S9 A- L4 r#define uint unsigned int
2 l' G4 d6 n- j( C" s3 D- H! T#define LED1 GP5
' n5 R) {7 [# c# I* w4 q#define LED2 GP4
- Z- f' c3 |& i4 x' o) L#define KEY GP31 ]" `5 f3 f" h1 l) w( i+ h
bit KEY_F;
) l# g. | e$ Luchar count;
: Z7 Q4 a6 g6 e: k; \: R- I8 z//uchar GP3_F;* \! r: y" ^0 B0 C( _/ `
void Init()
3 C- T- p8 J! z3 m5 x0 w! Z9 }) x{& M: S+ V% M3 o2 ?$ f" _, k
TRIS=~0x3F; //GP3输入,其它输出
9 Q" h; z2 d- c5 d2 e8 b& l // OPTION=0x07; //这个寄存器上电复位均为1- w) C. b; V- y4 f% j- q: Q& {4 V
// OPTION=nGPWU|nGPPU|PSA|PS1|PS2; //引脚中断唤醒禁止 弱上拉禁止定时器分配看门狗时间是18Ms*64=1.152S
+ }3 r2 U w7 y3 ~ OPTION=0x1E; // 引脚中断唤醒使能 弱上拉使能 定时器分配看门狗时间是18Ms*64=1.152S
+ L) _& x) r3 R // TMR0=0x63; //10ms
2 G- H9 |8 }2 H9 r3 S+ P}* w/ P# N; S$ g: \
void main()# w: n" T3 [' N% G0 ?4 f+ B% U
{
! b+ D" O4 v+ h, E8 aInit(); 5 J/ p2 n1 Q4 }" D" Y, f
while(1)
6 M# a: X4 d6 r8 @( l5 Y" L5 t1 t{ g) T5 A7 I; l& `1 m: s6 b
if((GPWUF==1)&&(KEY==0)) //引脚引起的中断唤醒- V9 m2 m, A0 F4 D1 G" w
{! A) j! T0 X g( F$ [
LED2=~LED2;
6 {0 x k, S) S" c+ O4 B$ |( U2 Z1 F( H }
: p, f4 g+ a1 |" Q: I# u. v if(nTO==0) //看门狗引起的复位
! l9 U% {' s/ w- F {6 Q) I! Y3 a: Z
LED1=~LED1;9 k: L; H$ O& I& L& h5 F ?- A, t
}" ^/ h* R8 f5 z3 B
KEY_F=KEY; //读出休眠前的按键状态。
- g6 g( f8 }2 j9 i SLEEP();
# F4 L) b# h3 A$ j: P } |
|