|
EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
: u7 V" b% {- `- [* u% x//基于pic16f676宝贵的IO,本程序使用2片74HC595
' C" c! ?! Y7 a6 A0 ^//驱动8位数码管的小时钟。可做成实物,特附PROTEUS仿真图。. M h! P ?! W0 E% D9 a
//小弟初学,水平有限,大家多多指点,不吝赐教。
9 n7 r* k; ?# e3 o2 T: k! L//QQ交流:271344691 Q6 S& Y* X! x9 _
#include<pic.h>//PIC16F676-SOP144 j$ G% G/ k/ S: ~- U) T* g( P8 _
#define uint unsigned int
: K# ]' d K# ]) b& m% ^! y# |#define uchar unsigned char! w R" r# f% x4 M
#define SH_CP RC0//595端口4 X2 ~; l G" w& N# [/ M
#define DS RC1
0 t* m1 j0 K; F7 ?9 v#define ST_CP RC2
. P6 b9 ^; h3 |% e#define key0 RC3//按键端口9 q' _" a! n( D C( R
#define key1 RC4
, X8 Y8 U0 [2 B: [1 O6 @#define key2 RC5 W" p9 Z) T3 c+ K6 y8 W
#define _XTAL_FREQ 16000000UL//16MHZ时钟,指令周期0.25微秒" Z* H9 p+ q3 r) b5 y
const uchar table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};//0-9共阳数码管代码 + \* a9 X: i; N* p, ?& ]
uchar sec=0;* g; q! g/ v) `1 S
uchar min=0,hour=0;
, ~& ~# r6 [1 guchar count=0; ( L- O* U0 H1 v+ j
__CONFIG(0X0002);//外接16MHZ晶振! f% S# Q$ X* F: G. Y; W" S5 r
void delayms(uint z) //1ms延时函数9 b# G# {1 o. x' h& E- l# S
{ r3 \$ C }0 {" L
uint x,y;
7 \, j7 ]' P! n, s# U$ Z; t for(x=z;x>0;x--)
" g! r o. k- [& o for(y=100;y>0;y--);
) {! n# {( |' y' u/ r* q, Z}* v1 R& t0 q+ }2 `2 `" J' ^. s/ ?
void delayus(uint z)
3 [, v! N N% W6 {) V, w$ P{
6 p4 ?( B5 q9 h- j uint x,y;
3 x* B V0 U% ~4 ]. S for(x=z;x>0;x--)( C6 M, k4 M0 Y1 h K
for(y=10;y>0;y--); k5 B4 r* T8 f# T5 R5 @) ^
} N( H2 L7 [ H: U* D
void serial_input_595(uint dat)
# c. t" S$ M) @ T4 I' a{
! c3 o, U( ?! d, o: |8 d7 H1 ] for(uchar i=0;i<8;i++)
; ^6 x- P, L8 _* j3 I1 D {
" g7 C8 Q i; Y) T( ~/ ^" S& U if(dat&0x80) DS=1;else DS=0;6 ?% E. y5 X- C3 K7 P* W
dat<<=1;
) O( b9 u" I) n6 k; \9 }+ L: y- q SH_CP=0;delayus(3);
7 k: x2 S, E+ z7 |; H' c3 v6 w SH_CP=1;delayus(3);
1 E/ [; S) W+ H" n% |( _5 D8 q; E ^ SH_CP=0;delayus(3);
/ t: e0 @5 {, A5 t: d$ P7 V7 R9 s }% j W* w& @$ p1 S0 ?
} ) z; f' R) }: v0 E- z
void parallel_output_595()* Z4 a' G8 z2 r. @
{1 x6 O7 i' j3 I& ~# J/ f7 n
ST_CP=0;delayus(2);5 E& Y: E" E% u. q
ST_CP=1;delayus(2);
# W" W( E: d0 M6 N& b( b0 W5 e2 n2 D! b ST_CP=0;delayus(2);
: b, {. z6 b% J9 r; m}
0 y8 o# E: V4 ^void distime()//显示时间
- r: Z; m8 F! p {9 ~0 a& y{
1 u9 h I# V$ K- k, n serial_input_595(0x01);
0 u9 {2 ^+ C8 z) S R- j7 J serial_input_595(table[hour/10]);) Q3 R( X& d- ^: D( T- k+ [
parallel_output_595();3 z7 F3 [4 g7 w2 B
delayms(1);
0 O, _3 U3 f' d! F6 K4 w serial_input_595(0x02);) N+ O* Z1 U! r! ^) D
serial_input_595(table[hour%10]);1 g) o1 c2 h5 ~0 [
parallel_output_595();
$ D9 c. e& d7 l delayms(1);
) y( o1 D$ R, L' w# [ serial_input_595(0x04);
' U! `7 u" a; X! i; Q serial_input_595(table[10]);( {2 q% v1 a$ c( y w
parallel_output_595();' E* D% f$ ]5 J b: p7 `/ P) h
delayms(1);; d3 J" {: Y6 b& J
serial_input_595(0x08);" E4 y5 \3 I8 R6 [4 r
serial_input_595(table[min/10]);. D8 V& ^+ ?- }' t7 h8 P
parallel_output_595();+ Z& T5 ~9 B- v. V. b
delayms(1);5 l. e0 K% G. z( F( M3 t) W; k6 }
serial_input_595(0x10);2 U: X9 J1 Z ?5 S
serial_input_595(table[min%10]);+ r) ]& [+ ^3 Q8 I. i, Z/ S0 d
parallel_output_595(); + V M, J* U( L2 J
delayms(1); % n1 H f; Y9 v) x' T! a2 V
serial_input_595(0x20);) F# W2 ~; A" H. Z. V
serial_input_595(table[10]);
1 n( j& j; r7 G# ]5 Q parallel_output_595();' k8 d: N5 W7 _9 E' S( d8 Y" t/ u
delayms(1); % ?, r3 X1 x$ ?# |2 p3 k8 E+ d# L
serial_input_595(0x40);2 ?9 R- h! d) _2 J( s; E, p
serial_input_595(table[sec/10]);
) [$ G8 C" f. ^ parallel_output_595();
G/ \( k, b9 \+ q" g4 o( @ delayms(1);( f. }& J! {" u0 X' E) w
serial_input_595(0x80);
& s) S9 I }& Y$ P) b0 d serial_input_595(table[sec%10]);
3 [. o9 G4 ~7 s b parallel_output_595();
1 E; X+ h. T% @" x( |( r$ g6 [ delayms(1);( ]! a# d( T: ]: b4 L1 g v' P
}) d2 Z1 d& @8 k l% K( l# r: x7 T- Q
void keyscan()//简单按键处理,实现调时。8 g+ m3 _5 c w. B' K
{/ t {, {, u0 A
if(key0==0)
9 L6 P. z4 [- @) l9 g$ ?7 L/ I3 G {
4 |- z0 ~7 `% b8 }& c delayms(10);
6 V9 ]. p. }9 M/ G8 m' W$ m if(key0==0)) K# r, {/ a8 k, i; @; ~
{
1 q! H6 a- c O; Y' c) r" u sec++;
1 i3 S7 w$ n B* y. \* i* c if(sec==60) o$ h: R: u2 I9 U- T Z# H
sec=0;
! ?3 Z( r9 Y+ |* K distime();; }( G+ g' S. g* c) l+ P* C6 k
) N+ B; c) X# L+ N1 t2 e. v
}0 U) [" j S: n* z) ]0 I. O* x$ _
while(key0==0);
. x2 l2 E8 z z }
/ z# l' l+ r" D+ h4 f % I" ]" t; L3 n3 m; l( n" g4 U! D
if(key1==0); t3 P) d; D7 S, _; E' t# Q0 f6 l
{
# p; B" B& z3 d3 }1 L delayms(10);
- o2 M8 d$ m2 O1 _+ m; D z( ~ if(key1==0)6 o9 q" o; L+ }" \% F% y
{. {$ R3 P4 F( {( k
min++;( d3 y6 I: X/ V6 ]
if(min==60). x, R7 G) a) a$ w, k9 \& b
min=0;# f8 w& Q" P. z" z) `. [# t2 u9 r
distime();% d: m. q( }7 I$ c7 D3 `
}- A& u+ |4 ~6 ?$ ]1 N, s
while(key1==0);& y. r x0 {9 l) R4 x' U
}" _, a* {9 E2 N( t) {
if(key2==0)7 ]6 @! q9 d e) s
{0 k9 c) _7 K. {' y8 Q5 G+ r1 k. |% O6 u) N
delayms(10);+ }3 [1 u5 G* U5 }* Y. H; [
if(key2==0)! Z' {$ }: ]- w9 ]2 S8 e' w3 B
{
6 O1 Q+ \9 q, W1 u hour++;
1 s3 G5 I5 B# G, ~% C/ m if(hour==24); ~# {* ?7 m+ t3 u1 E
hour=0;
* y) t+ r5 D o distime();
# ^9 ]2 o2 c% b- ]3 i0 b }2 T/ g' x& Q% y
while(key2==0);0 \5 G; S& a3 N
}
`- V, Q6 a+ W3 @! V0 E}
4 g$ X# S% v1 N! r- Evoid main()
8 ^7 I2 B) d. r& K) m{$ C; Q; T4 V) B; Z
TRISC0=TRISC1=TRISC2=0X00;//595端口设置成写6 J4 @; ~5 i8 [9 h% E" z4 F9 x
TRISC3=TRISC4=TRISC5=0XFF;//按键端口设置为读: U; Z8 H7 J! P1 S: ?
PORTC=0XFF;
T* K8 O, }( N ANSEL=0X00;% ^! m* Y3 F- H5 Z: Y& O
T1CKPS1=T1CKPS0=1;//16位定时器1设置为8分频,//总计时为65536*8*0.25=131ms# `+ J7 p$ g) [0 l
TMR1H=(uint)(65536-_XTAL_FREQ/4*8*0.01)>>8;//定时10ms,注意不能超过总计时时间8 ~; _ @+ I7 X B& X+ W
TMR1L=(uint)(65536-_XTAL_FREQ/4*8*0.01)&0XFF;
' m$ Q: g$ n) d //TMR1H=(65536-5000)/256;也可采用这种方法装初值,跟51单片机一样
; N4 L* H3 ~; H- `3 G // TMR1L=(65536-5000)%256;5000*8*0.25=10ms,效果一样。5 D! T% u" G4 @/ W) D1 ^
TMR1CS=0;1 p0 U4 O: u3 P' Z7 [- ]2 ?6 p
TMR1IF=0;9 F& v* p, d$ M- n; ^5 _
TMR1IE=1;% I( H5 d# K# N5 O
TMR1ON=1;3 x) g/ y) O3 p5 ]7 _, o) b: c
GIE=1; z) d; c# B* B) `. ^& K
PEIE=1;4 N3 ~) M' Y& X# W5 s6 _8 u7 x
while(1)* m/ \: c1 t2 W* Y( U) e; H
{5 J1 g, Q' k7 l: f8 u3 ~
distime();
, k9 |% E4 H: s* z% p# Q keyscan();/ M! C1 p4 i, {
}5 S- h& m+ q8 G" E1 s
}3 S" j+ S6 |# F( d7 C
void interrupt TMR_CONTROLL()
' b n8 k* D8 [: G: H; d{
7 Y2 H/ n: \% C) C/ t7 W& Kif(TMR1IF==1)
. g" W) p( M4 U9 f) D {
/ ?5 ^+ d7 e2 j* _$ O1 i. X* {; i //TMR0=6;
& R; p. f ]9 N2 a count++; ) v2 S0 \( D) L8 f/ h
if(count==100)//16M crystal
0 G; x l' X0 \4 @0 `" s8 \ {
3 S( X- Y; T! y0 Y: g, S count=0;& U% C" m2 O4 i( q4 W& r+ V9 j) M4 }
sec++; 0 L" R( V" Z8 {6 V1 P! ~0 x9 f
if(sec==60)
& H5 i; v/ |/ O3 I3 j {( e2 B: n2 H; s8 S g
sec=0;
$ g' j0 t( \" a& W7 c1 u! ~: p min++;; J; C0 `! D# y5 f
if(min==60)
" U5 D" \! a7 Y# a+ l' [7 E {7 W' U' a9 G( K' {" m
min=0;
6 U9 ]( a$ q1 B2 p2 e* y hour++; n- X" J* ?3 D5 m r3 }, X, c* V
if(hour==24). `% k' _3 I! u" @- t h% ^' C6 Y. Z+ P
{/ E( k1 B7 e- D/ e
hour=0;5 B! I# p3 R- E3 d) ?* U5 G
2 t {3 t( t0 u3 n! B }
( T" c0 ~9 Y" b }$ s8 m3 J2 d" z: s$ N4 J
}
! _: N" d h/ }: A# {
" y3 M& h1 Y4 W0 M }
) s. `2 i6 \5 ]7 B3 a3 r1 I3 H //TMR1H=(65536-5000)/256;$ G' ? r6 ?% a* y: I* S
// TMR1L=(65536-5000)%256;
( e" t9 ^1 J9 A9 J2 E. |$ {3 p TMR1H=(uint)(65536-_XTAL_FREQ/4/8*0.01)>>8;
y3 _5 R/ }. Y/ N& g6 E2 M1 Z/ s) J TMR1L=(uint)(65536-_XTAL_FREQ/4/8*0.01)&0XFF;6 [5 D) O' g; U E- ?! W" l2 }
TMR1IF=0;
, e& n* H) g2 k1 w% |/ P }/ r7 L m' s1 b# j, W
} |
|