|
EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
8 V# c9 I3 W% ?) K/ G8 @9 l8 G//基于pic16f676宝贵的IO,本程序使用2片74HC5959 b) y E, c7 @ U; l& f! }( Y" g6 k; k
//驱动8位数码管的小时钟。可做成实物,特附PROTEUS仿真图。
" y: H8 G6 M/ t5 d0 ?) X) o9 u//小弟初学,水平有限,大家多多指点,不吝赐教。5 W" r# m( L1 A; C2 `) R, W
//QQ交流:271344691
2 ]+ W' X) ~( e4 G2 ~#include<pic.h>//PIC16F676-SOP14
7 l; _( ]) Z. a! K& a; J#define uint unsigned int
( w2 m* W; q; ^7 L* ~9 M#define uchar unsigned char
( f6 w& T) [" d S, ~4 A! e7 V#define SH_CP RC0//595端口, R0 H. p2 \8 \' I
#define DS RC1
" z( E% P. D: ]/ M#define ST_CP RC2
& n! t( @, M/ T$ y1 E#define key0 RC3//按键端口
6 A. N$ J# i" R3 V6 D#define key1 RC4
0 S7 H' @7 ]$ f% O% L4 w#define key2 RC5
$ [. b) Y- F; @+ F; x9 W1 ]#define _XTAL_FREQ 16000000UL//16MHZ时钟,指令周期0.25微秒& u, ^, X7 t) P& n1 M+ X
const uchar table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};//0-9共阳数码管代码
/ J0 {! ^5 P' P; tuchar sec=0;& U; {/ S( g6 p' Y {1 B7 w: c4 \' @4 E
uchar min=0,hour=0;
7 D. @- v) _5 u j9 Suchar count=0;
4 D- w* i8 r& H/ d__CONFIG(0X0002);//外接16MHZ晶振
$ ]% X/ `+ ^+ f+ N" }0 U5 h' @void delayms(uint z) //1ms延时函数
# D) q6 D3 h* ~' [ {, r, X5 Q+ |& p
uint x,y;
7 ^, [4 x( E+ v u$ d for(x=z;x>0;x--)( y3 J1 ?9 i9 O5 O; J" r
for(y=100;y>0;y--);# o0 b: s( C$ M& R4 q, Z# f7 F
}
' p/ {& E; W+ O1 bvoid delayus(uint z)* F) a5 P) u9 b
{) M" R3 ?% i: x0 b& A% A! p
uint x,y;! o2 o. | o! C Y4 T4 Y
for(x=z;x>0;x--)
) v% Q1 Y: \: X7 `/ a; ] for(y=10;y>0;y--);; B9 |8 Q9 ^" M; B! P7 S
}
4 Y, U) E9 I. p: O' N2 }, {6 R+ H, h; \void serial_input_595(uint dat) z% ? Q3 S. u# E
{8 l/ i6 l" Q, D
for(uchar i=0;i<8;i++)$ e* m7 R) L2 C& `* d2 e7 D
{" c$ S; W( }8 F5 e, v' Y
if(dat&0x80) DS=1;else DS=0;
4 `$ v2 U9 T; L0 r/ L dat<<=1;
# ]% p! t2 g3 B3 S2 R SH_CP=0;delayus(3);
3 U. J+ Y7 f+ l2 E SH_CP=1;delayus(3);
0 F+ @/ j) ?" x SH_CP=0;delayus(3);& R8 p/ J6 P0 A! p) Q0 _( Q. Y k
}
- i; `- c* h' A* k$ p7 b, }- A8 M2 q}
% D8 U9 P% o2 j8 Q0 H" o( g) a$ P1 Xvoid parallel_output_595()4 L5 a. r- p" n
{
& C) T. x4 Z5 `4 U: k ST_CP=0;delayus(2);& \% \+ R& u4 O7 i
ST_CP=1;delayus(2);
# E% h, t. D+ E. G* L% _7 J2 o) o# z ST_CP=0;delayus(2);
( b* \# R% |4 F- w}
) |3 t: L9 L7 u$ {2 U6 e) mvoid distime()//显示时间
1 _: j* H$ `/ ?{
5 ?/ g; J8 H; i) V: L6 G& m9 ? serial_input_595(0x01);* R& U1 v/ @: f. y
serial_input_595(table[hour/10]);
. j: ?% o+ e* y/ {' X parallel_output_595();
5 G) ^: n& l, @0 m delayms(1); V% h" d9 \% h. ^! r
serial_input_595(0x02);
1 P$ P7 `2 a1 f+ Q+ h {! { serial_input_595(table[hour%10]);0 W9 a7 v9 O& s( C$ m
parallel_output_595();& Q. q9 s7 ~3 F
delayms(1);
. X; G# v8 E5 t. u7 _/ O serial_input_595(0x04);: M' w& W, d* E# n
serial_input_595(table[10]);: u& v! _/ q) B2 S4 v+ D# a
parallel_output_595();
: h: ]1 \- q/ Z2 l* A, b delayms(1);% a% \ ^7 ~; c `
serial_input_595(0x08);% ~0 x1 m* _% n3 z% m# s
serial_input_595(table[min/10]);/ l- S( I0 ]8 O$ D
parallel_output_595();
, `. x. a }6 ~' z delayms(1);
- a' x4 t/ z* o* m9 U serial_input_595(0x10);
% a0 l) Q1 o# X9 I% E, t serial_input_595(table[min%10]);( B1 s- f2 M, S
parallel_output_595(); + O" D4 F7 |1 d' s4 N& J# H
delayms(1); * s6 h9 m, B+ v" o
serial_input_595(0x20);
8 e8 m7 ^8 n; g# @$ q serial_input_595(table[10]);" G# }) u& X |* t- z
parallel_output_595();6 s, H" z* `3 [0 U
delayms(1);
+ {7 @+ o; o2 @/ t/ F serial_input_595(0x40);
" w- C( m8 R+ d9 E% M" j serial_input_595(table[sec/10]);; G* s% I' b0 \/ [# A6 c7 j( p
parallel_output_595();5 [* _( {& A* z0 L
delayms(1);
9 T; i# r: ^8 g8 v. h. i8 i$ d serial_input_595(0x80);! h6 M/ \6 _8 U% @
serial_input_595(table[sec%10]);4 A, s( u4 I7 c7 g' F. v2 M4 m( {6 O
parallel_output_595();+ Q& O2 d1 r1 e9 q0 l
delayms(1);. B& r( k+ `6 X, m1 n/ K
}
. [( ?3 B1 j+ } A3 qvoid keyscan()//简单按键处理,实现调时。
1 c6 C1 C( v! Q4 ]{
! ~" P. _6 A, V: [ if(key0==0)3 F8 \, r$ }' t, ]! {2 y
{1 f* p1 l6 z; S7 x7 ]% d
delayms(10);1 r) l% [: I+ E- ?: t) ?
if(key0==0)
% O4 t0 W4 l3 ?5 \* }, M6 P$ o/ O {
* |2 g3 i9 c v, J sec++;6 K1 z- P C9 d% a; A( V
if(sec==60)
9 G; N0 `8 D G! y$ d8 b4 _ sec=0;& `( H9 f" A- @1 U( J* l2 K
distime();
' d# d2 V+ z6 W2 r
' n5 P& ?( R3 x, S/ ?" ^5 [ }
+ M" ]) X$ z1 y while(key0==0);
0 y P$ V% N; b, e }1 G3 u0 z8 q' x- p
& {6 b2 w; w5 a, _7 G% O1 ?5 V" R if(key1==0)' M4 H" y; k6 _+ C9 p. b
{- ^% k4 z" r. F6 m
delayms(10);; ^1 M* k1 } k Q2 T. Q
if(key1==0)
7 j/ l. j5 G. R {% P- s( Y! V2 I" M& a) E
min++;
; Q5 | F) z- ~- Y if(min==60)+ A& ?" ~2 j ~1 S& j
min=0;5 `1 G5 H. `' c0 w! r5 ?
distime(); E6 G4 K6 u8 l* z( G* {$ H
}0 V; Y- Y. g) B# b. m
while(key1==0);
0 K' C( e B* s) p; V0 j1 ` }# p9 P2 o/ t" l# z5 ~1 x" f
if(key2==0)+ I* r" B. d) F1 y
{
9 r* R4 l! v) Y, v( ]: v6 I delayms(10);
" H' E& X& i& W! S if(key2==0)
$ t$ i1 x _( T& i! J4 I& N7 J {! D* W6 h2 t2 n" i6 G
hour++;8 h( J" M+ J9 s0 }7 ^8 c" p8 d
if(hour==24), V8 G4 b( p- A& o+ h
hour=0;
. E* ?, d. o/ K& u* r' Y5 B distime();
9 \7 ?" r5 X: t6 u- I8 s; g% O }
: X& T# H7 ~9 h) f! \) {' x9 L while(key2==0);4 D' }+ Y/ S u! _6 \ G
}
( h) X* O2 N9 J6 A}! q4 x0 I8 @ i
void main()
% K6 q0 d/ K& a1 p% F6 N5 e{+ k9 Z; F, ~! W$ ~2 q/ ^) v; t2 @# G8 }
TRISC0=TRISC1=TRISC2=0X00;//595端口设置成写9 p+ @6 \3 ]+ t' j2 _: F
TRISC3=TRISC4=TRISC5=0XFF;//按键端口设置为读) O0 N2 B _' R) o( r
PORTC=0XFF;
$ l+ M+ J. B; C* ^9 b6 e: [/ \8 v ANSEL=0X00;5 }- y. [* U8 R( ]4 @, R) T7 n
T1CKPS1=T1CKPS0=1;//16位定时器1设置为8分频,//总计时为65536*8*0.25=131ms! C- p: k0 z7 v5 p: M
TMR1H=(uint)(65536-_XTAL_FREQ/4*8*0.01)>>8;//定时10ms,注意不能超过总计时时间" u. W9 Y/ Y Q0 }% O# W
TMR1L=(uint)(65536-_XTAL_FREQ/4*8*0.01)&0XFF;
4 ?5 M8 T" A2 g' x //TMR1H=(65536-5000)/256;也可采用这种方法装初值,跟51单片机一样# o% M' q1 j6 n8 E8 k* U$ p
// TMR1L=(65536-5000)%256;5000*8*0.25=10ms,效果一样。: k* H4 f8 E& _, ^, }; I3 s: Q
TMR1CS=0;
; N( l1 i* Q8 H7 q+ l TMR1IF=0;
J* C) G/ |$ S& m x TMR1IE=1;3 Z. K1 k2 }3 z" d a; [! I
TMR1ON=1;9 B. S, _/ K$ ^0 j8 D& H1 Y, s
GIE=1; 3 C8 Q, q G( m0 U4 X& L# s
PEIE=1;7 ~/ p* V6 ?: \& T" N
while(1)+ u+ E+ Y! T8 M" O! [/ Q4 m
{) Y; K9 V7 P" X
distime();$ z {8 f4 B8 B; Y
keyscan();
4 v' m) ]/ h, U' ^; g }
, C) ?/ r# M$ a+ E}
' c6 t5 a- J: ]5 p% k/ lvoid interrupt TMR_CONTROLL()
6 J2 F$ S9 I7 z k' y+ C{
& }0 g# M6 g: q9 Tif(TMR1IF==1)
: S9 k: Z! C8 H4 y$ B" T0 C" p# i { y9 l, ]" d5 q$ ]+ P
//TMR0=6;+ g: m! _4 x! p6 v$ ~( p4 J' h
count++; o4 f0 u6 C( M6 \6 e
if(count==100)//16M crystal6 j2 B9 R7 F( O
{" t$ |; x! C2 P9 ^" T
count=0; K! W3 E0 R O% a& M3 k6 w& t5 _
sec++;
2 A4 l ~8 } n4 M8 k if(sec==60)6 I6 P# K0 A2 C
{
7 V* D, Y, U6 H+ {. f" b sec=0;
7 q9 b, M) [" `1 h1 w# D min++;& y) p9 J& }8 _1 b: [9 X
if(min==60). F) a4 ~6 Y# k+ O. e" ~4 c
{8 l8 l2 ]2 H8 t. S' K$ e ]
min=0;
/ @. I! G N3 i9 `; v5 y2 l: M hour++;
8 f4 [* I# E- a if(hour==24), W! V7 h) s, e5 k5 ^. U* [
{
1 M6 A- i( o9 B0 A" T hour=0;
, U2 A2 K Q1 W6 X, v( O% w3 F Y
2 a! b4 N; D. c8 R( q } . `% m; o& |6 o N
}
6 E( e5 \" p. I' H' C, k! d }1 c. D* k3 j+ i* i& T
1 g+ s- p& }1 B* a6 r
}0 E4 b* O% U9 }3 a( E( G
//TMR1H=(65536-5000)/256; x/ i' v* z* |& i1 G
// TMR1L=(65536-5000)%256;
, w: F# g" {. ?$ L( z( J$ B8 L TMR1H=(uint)(65536-_XTAL_FREQ/4/8*0.01)>>8;
: o. b8 T' \& z Q- R TMR1L=(uint)(65536-_XTAL_FREQ/4/8*0.01)&0XFF; t& R9 y f* `6 g8 u1 s: |
TMR1IF=0;' t9 J+ c) Z5 j7 {2 L; |& a
}9 s [, T6 t& I$ [5 v
} |
|