|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
PIC12F508单片机学习之一——定时器
- ? _! B: ~' M+ `8 n2 E) `( @! H5 f- Y
PIC12F508单片机是没有中断的,定时器只能是查询方式。
/ G. I3 [$ W2 |( v6 @
1 s) K0 I0 Y* E, G编译器用的XC8,编译环境IDE用的是MPLAB X IDE。
: T: q8 w3 b# D) N! `4 I4 Z3 q$ H6 q+ U5 Y) ~2 X
//***************************************************
- `! k& n Z! ^// __________________
9 Y/ d$ N6 @. g+ G// VDD-| 1 8 |-VSS0 [( S5 W: D1 s; m# ]3 A
// GP5-| 2 27 |-GP0/DAT; S' v8 c5 O( C& {$ g7 Y
// GP4-| 3 26 |-GP1/CLK5 t' \* g# K# f5 n/ W# Z
//GP3/RMCLR--| 4 25 |-GP2
( l6 Y. _3 \" ~- i7 X, E# w- I// |________________|% ?; O+ I% r/ ?8 Q& ]7 Q
// 12F508* m8 x! f- o4 `. u2 e& L; l4 O
//*************************************************** ^9 L U1 d1 J& C+ H; t7 d
8 v0 |4 |+ o/ `' C* K//定时器模式试用7 r; p# C7 \4 ?; K5 v
#include
' g- D* u. S; |/ P; K+ i8 L" T( s#include/ s& d' W, I& B2 ?
: `5 T$ N4 x2 X2 j1 L% i" R+ m// #pragma config statements should precede project file includes.% L. u6 ~; h; D9 t2 p2 m, k* s
// Use project enums instead of #define for ON and OFF.
9 X: ~+ t4 Y& o2 k
0 q+ V c& z* q5 \. G- M& n0 N& O// CONFIG8 h' Y7 D$ a# Q9 q% Y
#pragma config OSC = IntRC // Oscillator Selection bits (internal RC oscillator)
! T) n! d* T8 ?% X- }#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled)
$ n6 `6 C$ o. m% b* a#pragma config CP = OFF // Code Protection bit (Code protection off)1 O4 u- o/ K2 l3 H& ~- Z
#pragma config MCLRE = OFF // GP3/MCLR Pin Function Select bit (GP3/MCLR pin function is digital input, MCLR internally tied to VDD)
& m9 k( T* Z- k2 Q( P i \! y) z& e8 _/ V
#define uchar unsigned char8 N4 f# L0 A. }( F
#define uint unsigned int; }8 @( Z, \% p! N; i- [5 P
uchar count;
1 e4 {7 e$ `8 ?# l//uchar GP3_F;; H. G+ t6 z3 K! P- u2 C& v( ~
void Init()
. J1 D0 t1 ]8 {- ^/ x5 z+ Y; t{
! U4 G; M$ C1 ~* ]' I2 _ TRIS=~0x3F; //GP3输入,其它输出+ a3 H9 f- W& Z7 L1 n: V- Q- c- M
OPTION=0xC5;//分频器给定时器 64 4MHz x=257-tJ/4F; {( i3 `. t4 B
TMR0=0x63; //10ms x=257-tJ/4F& P+ p6 ^2 A, [ M; i/ |
}$ X6 D0 X4 B2 Z$ L
void main()
+ `6 T9 `5 w) }7 r{
6 ?& n* ]2 O5 ?8 cInit();
# K: @* S8 x6 l8 Z3 uwhile(1)2 X [) Q+ {0 v! e2 A
if(TMR0==0xFF)
8 D3 z# B7 g) y# c2 |- t {9 N# j( |: m) R" z
TMR0=0x63; //
1 K1 J% b) m7 [, Q8 U if(++count==50)//1s
$ J% ?* W2 s1 I' N' ?* u {- o7 L. F+ q5 A0 `1 \7 v" K: P
count=0;
! P3 `* m5 i! i) D4 x9 F/ B GP2=~GP2;//LED闪烁! n q9 R7 i5 F: N' L: V b) Y5 Y
}
$ p+ \, L/ D* m2 y$ Q }
7 r0 J' b2 B+ G: A( D' x# g: m }' Y; I# R4 j4 Y
} ( B5 @! z7 H9 ?
. S- ?7 G/ [7 }+ w
( s2 W8 r1 h% A8 v3 ^PIC12F508单片机学习之二——看门狗和休眠模式试用
; R- ^0 g* {7 K5 k. p# A- V1 K1 k2 y; b! o
PIC12F508单片机是没有中断的,复位情况只能是查询方式。+ F6 w* U9 z/ H g7 T5 ^
' z; u6 B0 G5 R9 u( K8 j编译器用的XC8,编译环境IDE用的是MPLAB X IDE。
8 l! y( n+ L+ w! I5 l$ n" J0 b8 i/ H
4 K8 d8 ~) q, C4 ]4 d下载器是PICKIT3.# c" M) ?9 j4 ~* X2 s" r
9 |* E! i7 D X
//***************************************************
) H. D w4 e5 s2 m4 y! V: ~7 S7 \9 g// __________________
, f; j- I0 V0 _ L9 m, @/ e: K8 @0 l8 A: m// VDD-| 1 8 |-VSS8 V* K4 k9 z& T) |) D
// GP5-| 2 27 |-GP0/DAT
$ |5 t. `; I2 U+ c* U// GP4-| 3 26 |-GP1/CLK; o, A4 Q& z2 S% V% |' ^
//GP3/RMCLR--| 4 25 |-GP26 a T0 l7 h8 Z+ ~( o" I) q+ a* U8 p
// |________________|
0 j$ s& C6 v4 W2 B! I// 12F508' D. ^2 {( v2 ]4 [3 S
//***************************************************5 x) ~/ a! N2 r5 k8 m) a% Q4 F3 {
// 看门狗和休眠模式试用
4 e% I: B+ ], y, ~' R" a2 s: l
; c1 o8 V2 g3 q5 O2 R5 f. p( a5 n9 t3 s& F9 e6 c: ~/ }* O& {
#include
3 j' L0 n2 z1 A/ ~$ _' G#include
1 \6 V6 Q* x, z x a( A' u* J2 ~( o* j2 d0 E( w* d! j1 t/ ^. F
// #pragma config statements should precede project file includes.7 S) M( G E/ E4 F; O" P, O
// Use project enums instead of #define for ON and OFF. u! p8 b# J4 Q; o6 \& v: _, F
- E& q5 C# b5 x7 ?5 F// CONFIG6 C! L9 R3 {! U9 M9 c
#pragma config OSC = IntRC // Oscillator Selection bits (internal RC oscillator): u5 h; x8 p9 W D6 i
#pragma config WDT = ON // Watchdog Timer Enable bit (WDT Enable )% b9 j1 p6 w* D% n# z8 b! E8 D
#pragma config CP = OFF // Code Protection bit (Code protection off)
+ d! ]" p2 k; T- E! z#pragma config MCLRE = OFF // GP3/MCLR Pin Function Select bit (GP3/MCLR pin function is digital input, MCLR internally tied to VDD)# F7 ] i( [$ M7 w
& z, K$ `( @0 ? ], N9 r
#define uchar unsigned char
6 m/ {# p4 v1 s2 D+ { k% M#define uint unsigned int& ^5 K! k. C- `, S9 k
uchar count;
- N3 Y( Z2 O' R$ z; l! k//uchar GP3_F;
! e3 @( m# P5 b* mvoid Init()" O/ ?8 R) @3 T" D1 q$ K% j
{/ s p+ \! o/ a' ]: \7 O8 Q% F
TRIS=~0x3F; //GP3输入,其它输出8 x; W3 Y1 [0 R- X
OPTION=0xCE; //定时器分配看门狗 时间是18Ms*64=1.152S0 x3 s `6 y6 d- B1 s v. Z" W
}
) ]+ d6 _2 P0 c1 R3 r- Kvoid main()' u2 W; p+ |- Y H: A
{ 6 q3 q K) I2 ^5 o/ O
Init();
) w/ l0 ~* _; ~) M( d4 G' h# `1 C
& t! J$ M9 E5 R: a8 Q1 `, o" w2 N - D3 H7 N; ?+ R0 B& B
while(1)
0 _5 K \, c, l5 m5 s9 J0 l- \! Y {& Y, G5 [) u# B0 h, p( a
( C0 N9 ?( s& M if(nTO==0) //看门狗引起的复位
X/ k& h- g! `: v7 L {
1 Q/ M/ q+ ~, R- K: {* s7 t9 M GP2=~GP2;
: Z2 ~+ A, T2 O- C }
6 t0 B ?* E; L- J' I/ C SLEEP();
7 n4 w3 U" l- {4 m }
( a* {7 y5 h% |6 P}. R' B0 i. J3 y9 Y5 l$ P0 { T4 i
0 v- w" V0 `: p. q0 y
) D9 |2 k4 p- n7 X& D8 O& _' i
PIC12F508单片机学习之三——按键唤醒) l5 G# ^! T( m
. Z; u$ e0 |* YPIC12F508单片机是没有中断的,按键中断只能是查询方式。' ^' Z' V7 e0 p9 a: v6 B
5 x4 u, O H, v7 F4 b% V1 G9 W
编译器用的XC8,编译环境IDE用的是MPLAB X IDE。
/ x6 }2 B1 B- w% a# f; j9 q$ G- U' X6 b& i9 S- P$ O
下载器是PICKIT3.
" _) x6 S# p& Y8 M
4 y7 E2 i2 m2 Z//***************************************************: R& q) `# k: U4 [: _; N
// __________________, ?. D2 y9 f* O. ~+ N( I
// VDD-| 1 8 |-VSS
0 ^/ `) S) _* N2 J5 f, r7 e// GP5-| 2 27 |-GP0/DAT
) ?, e: ?: h @- k// GP4-| 3 26 |-GP1/CLK% v$ K) g* m& _8 @. Z
//GP3/RMCLR--| 4 25 |-GP2
0 r" m- \! K, i; n// |________________|
+ q% ]: s" ?4 {4 @8 K// 12F508
2 c9 {3 c1 A" D//***************************************************
8 V4 u$ }+ ?- h+ w5 b7 u8 D# r9 M8 R
//看门狗 休眠唤醒
: {0 }; e, N# x//按键唤醒
, ^6 s o3 j& ^1 O
5 B: x: c8 C- I#include( z& ^8 r- B6 N* K0 T) p! s
#include
]1 o# Q5 }* |. g1 J7 W. z. [
: ]; v' \2 g( Q! ^+ D& P' O// #pragma config statements should precede project file includes.
3 F6 l% \; r, z1 D+ @// Use project enums instead of #define for ON and OFF.
2 G' |. E4 L5 \ g
6 p V& N: F+ Z! y) Q/ C: o( k& {! K// CONFIG& P: d2 _7 X f/ y6 X
#pragma config OSC = IntRC // Oscillator Selection bits (internal RC oscillator)
, d$ D3 ~2 c5 V, V' b' o6 i#pragma config WDT = ON // Watchdog Timer Enable bit (WDT Enable ) p3 W3 y! g7 k! B
#pragma config CP = OFF // Code Protection bit (Code protection off)
5 p1 h( ] e! e8 c/ X( \#pragma config MCLRE = OFF // GP3/MCLR Pin Function Select bit (GP3/MCLR pin function is digital input, MCLR internally tied to VDD)
$ c* j7 l' B7 G- ]8 b" _$ ]/ f& J r n/ c, D) @4 |3 B
#define uchar unsigned char" N% Y: X c( K, g+ `$ }
#define uint unsigned int! C( {0 @( e; Z, a* B
#define LED1 GP5: b0 Z+ O4 E7 s( I3 c3 W F
#define LED2 GP4 m' h6 s" l$ d* d
#define KEY GP3
- |* Z# W7 t( L0 u6 s5 Gbit KEY_F;0 a- f2 [; i2 W
uchar count;. T. H3 ~) G5 D4 X" g: |
//uchar GP3_F;, j5 F: y$ a/ d S
void Init()
! ~ g9 i% A* m; j% l3 o- l/ N, f{8 \- i/ z$ ]: S$ }
TRIS=~0x3F; //GP3输入,其它输出
- f9 L) |, [( m, k$ @! O ? // OPTION=0x07; //这个寄存器上电复位均为1
6 P' r U) R2 P8 ` // OPTION=nGPWU|nGPPU|PSA|PS1|PS2; //引脚中断唤醒禁止 弱上拉禁止定时器分配看门狗时间是18Ms*64=1.152S0 a6 z8 O" ?7 l3 E4 p- B5 }
OPTION=0x1E; // 引脚中断唤醒使能 弱上拉使能 定时器分配看门狗时间是18Ms*64=1.152S% e5 R" ?, w6 F) z
// TMR0=0x63; //10ms6 A$ N+ S C8 a, d
}; e! Q( J/ E- q1 q
void main()
' F* G- x c" z( X& l, }) c{ / p Y u1 _& C; @
Init(); ' g y* \6 W' u% ^( R4 t6 R% k
while(1)
2 q4 c B& J$ d5 ^{
- _, z. F0 t+ \: x. U7 ]# j if((GPWUF==1)&&(KEY==0)) //引脚引起的中断唤醒1 r3 _6 ^( l! K1 K* c& O$ Y
{
# D+ S: x% E+ [$ b LED2=~LED2;+ A5 c4 ?0 f. f6 |# Z, \; o
}
8 B- Q( |$ Y6 T, B) a: i! Q e if(nTO==0) //看门狗引起的复位
& a4 s; B0 Q2 q {. J, { {' b4 B ~% S! E
LED1=~LED1;4 r- `+ M! a6 P5 u
}
6 k5 r* Q; Q3 Q' O4 T' V, G KEY_F=KEY; //读出休眠前的按键状态。. b2 U+ ^/ U. L4 O
SLEEP();
& L6 |2 }! }# X0 g! d( }2 f } |
|