|
EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
5 u# m* d$ i% l* j
//基于pic16f676宝贵的IO,本程序使用2片74HC595
( m' d+ n$ E; }//驱动8位数码管的小时钟。可做成实物,特附PROTEUS仿真图。
$ ` c+ c$ J- x. t! o7 c//小弟初学,水平有限,大家多多指点,不吝赐教。
# `( o6 M' m, p9 M# _( l3 |//QQ交流:2713446915 Z0 C* x) k$ I
#include<pic.h>//PIC16F676-SOP14: [4 j8 r) ]! G0 x1 o
#define uint unsigned int
2 L; i5 |5 R' O2 o' v! X- z, d }#define uchar unsigned char
( B4 P, w$ L$ Y$ w% D#define SH_CP RC0//595端口
2 b+ j! d3 w6 a#define DS RC15 k. t7 e# N8 s2 V. g L' Q
#define ST_CP RC21 I2 d9 c9 g; S/ U \! F# c
#define key0 RC3//按键端口
5 @* |5 L7 x; ?9 s5 l#define key1 RC4# w: G: C& [6 L$ L$ c# u0 g+ h
#define key2 RC5" |, Q2 \+ ^& u# |/ B( v
#define _XTAL_FREQ 16000000UL//16MHZ时钟,指令周期0.25微秒
, ~& ~7 q' @# bconst uchar table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};//0-9共阳数码管代码
& h+ _7 f- z# u& Xuchar sec=0;
" r, z- P! Z7 H* z9 E6 Guchar min=0,hour=0;
, f% I! n- Y: D5 T- m# @uchar count=0;
4 d. W/ V# `9 G% _% u- T9 H! C__CONFIG(0X0002);//外接16MHZ晶振
) M c3 n% P) avoid delayms(uint z) //1ms延时函数
k2 r4 S0 }) j1 s. a {3 g: B- `9 f- Q
uint x,y;2 i. {1 J6 B$ @; t; t# U* w8 p
for(x=z;x>0;x--)2 d' X; \7 O6 c7 N' R$ Q( ~
for(y=100;y>0;y--);* } b S; ]& {& B
}( [/ C# c$ I7 j) U& T4 z7 ? Z% T- J
void delayus(uint z)
9 ^2 [# j$ L0 |. s3 {# e{& e- N+ I+ \( G
uint x,y;
F9 ^( I. ~0 k" K9 z for(x=z;x>0;x--); Z2 I5 T+ @. s% l$ b* D: k& `: {
for(y=10;y>0;y--);
2 Y2 p. _2 u; H T3 L}( V; e& M: a" E' x
void serial_input_595(uint dat)- j7 v$ }9 ]: Y! l! s8 q
{
! m t; E: _3 |: [1 V) x$ H2 \7 I. G for(uchar i=0;i<8;i++)
* P+ t I2 t9 Y2 t) W {
$ W0 b2 t Z3 b$ t if(dat&0x80) DS=1;else DS=0;, J9 p1 ]2 L( G; v4 B
dat<<=1;. _2 o( I0 r' j+ O/ @
SH_CP=0;delayus(3);
8 y6 ?2 c( M+ ^) e% m/ t/ u; M8 e SH_CP=1;delayus(3); Q9 M- r; |1 u1 a1 K/ T# O: ]
SH_CP=0;delayus(3);
7 Q& l+ _/ E/ T$ Q7 \ }1 }" I- d/ ~ }! r
}
d6 ?* f( Z9 m! o: e! tvoid parallel_output_595()
) s3 S3 U+ L8 Z- ~7 ]2 @8 Y0 j{
5 C: @4 L7 _* h0 L6 m/ D ST_CP=0;delayus(2);
+ o. N0 r. A6 i- ?9 K; Z5 Q ST_CP=1;delayus(2);" k0 V% L: w' r1 [; W/ v) D, K3 s
ST_CP=0;delayus(2);
( \. ^& F9 c+ l2 K% E}
/ f ]2 s/ H4 E5 evoid distime()//显示时间$ o* m m2 k7 z$ M: x# F( N4 d% t1 v% D
{4 Y. R1 J$ k. C- s$ F" G
serial_input_595(0x01);& P# i* ^6 j3 Z5 H1 G" F
serial_input_595(table[hour/10]);
& I ?4 y+ _4 o parallel_output_595();
$ |8 ]3 S: c; m( K delayms(1);
5 } B6 M8 t, ]+ e! } serial_input_595(0x02);
, t; t6 \7 y0 F0 d- b5 e serial_input_595(table[hour%10]);& p$ t+ r( U* b. R0 I4 [
parallel_output_595();3 s9 h5 \5 L' B( y" X9 v: ^4 ~3 ~# D
delayms(1);. Y9 Y0 n8 K# K4 b7 m+ S
serial_input_595(0x04);% _! G- P+ b @. Y( z. V
serial_input_595(table[10]);) `! S1 n- Q7 r; B" t- E' N9 J8 A/ b0 i
parallel_output_595();8 w. a( z% o5 I7 p) `
delayms(1);
! N* P5 \' y: v* j! j3 w1 a! m serial_input_595(0x08);
5 H+ }# }- V: k7 z* w2 b2 m, J serial_input_595(table[min/10]);1 [3 K$ a, k6 W/ z3 s; D$ n0 N9 u
parallel_output_595();+ a9 z2 L9 K7 t t6 Y
delayms(1);
|. f) S1 q3 X. w4 i4 R3 O serial_input_595(0x10);
5 a5 w/ C1 L: U7 @# u' F2 N1 S serial_input_595(table[min%10]);
! ~6 M7 L3 G0 F( u6 V3 E parallel_output_595(); % L7 M& Q$ v% D. u! V/ P; p9 u
delayms(1);
' V( m5 P/ ~, b$ F% c f9 i" p serial_input_595(0x20);
: E# Y* f$ @' w; X serial_input_595(table[10]);# O5 l* `4 Z' m9 ]& X9 ]- p
parallel_output_595();
. ]8 u) J+ W/ g4 t# r9 m t delayms(1); 7 g C7 t* W* a6 G4 A% b
serial_input_595(0x40);
, R* M* z2 k6 _- j8 D- G serial_input_595(table[sec/10]);: u# l5 N& @- M- s' h8 H
parallel_output_595();
~1 F+ |$ d0 k) z* N delayms(1);
5 R. k4 r5 @9 o serial_input_595(0x80);
% G+ Q. Z" X0 b& p1 e" @ serial_input_595(table[sec%10]); g- L6 @5 q3 ?
parallel_output_595();
+ Z2 ^# W1 c& Y delayms(1);4 I# F& k" J# ^2 S4 N6 w+ Y
}, ], X' `) E3 B) j+ `
void keyscan()//简单按键处理,实现调时。+ p) J C" P' l8 n" l) d, N7 {, W, V- D
{0 g% X, I6 V+ x
if(key0==0). P% T3 S2 `+ _1 [1 j( D3 u9 ^
{
% Q1 V q4 r4 X3 ]" W delayms(10);
0 f9 H7 e$ q0 a: f" J& v if(key0==0)9 q$ a) X7 A7 L+ Z* X
{
0 T/ q- N: Z( y: H sec++;2 v2 J2 y% P; I, {
if(sec==60)$ d0 c# `2 i7 j
sec=0;
& `, g* a! M- I+ f, s* e distime();
+ d8 H# e* ]4 @; N B$ \7 ]5 b
* O! P" a7 K" _ }
?8 O4 p* C% M/ C) w% @% B while(key0==0);
4 C: W" w1 L" r+ [1 l5 J8 q. l, ? }) r+ p( ~ i+ Y( O( I- E" N0 c1 ^7 l
- q! o0 e+ Y0 k9 G if(key1==0)+ V6 R: ?" e5 F7 N4 o6 Z
{
- t8 f/ m, R' U9 o" m delayms(10);5 [! l3 I; [. I% X, T+ P' _9 f6 P
if(key1==0)
# O+ ^" w- s7 `0 v$ r! H& } {& E( T7 A( o( c% I8 j+ S% v
min++;: R1 g u. L4 } b
if(min==60)# t* P5 n8 l8 |7 i5 M) m
min=0;1 B1 a) r% N l4 e
distime();
D& j J4 T3 g" }) z$ R }
4 s j/ L' M. [% _6 _0 q while(key1==0);
% {: ]! j2 N# ? k6 ` }
) R! I1 U$ `+ ?4 e% y# ]) ^7 u: ? if(key2==0)
3 k5 e9 w& M6 [& T {
( g/ ] C; d4 W& n7 G delayms(10);
4 |3 c* d: a9 q. K if(key2==0)8 [# g, i& c! d+ [" P
{
0 @8 R" B3 `8 }2 y3 j* x hour++;
2 H3 z' c; T/ N5 a/ ]# h if(hour==24) d' B% t/ N: Q8 {
hour=0;
4 d/ C8 [+ @* l$ `0 y distime();
0 n. J& L7 l0 F1 A) H }$ d3 y( t$ p- N6 g% r
while(key2==0);# [* q3 w; Y! A2 D; [4 I# q m
}
3 h4 H9 B6 ]- x. t x& Q" q}
+ e, h+ \ T @) ~0 B/ `- z0 @void main()4 _# U% ~3 U& Z: B" G
{
) X6 j- a# ^5 {+ ~# x: i0 e TRISC0=TRISC1=TRISC2=0X00;//595端口设置成写- ]7 R2 d# q1 A1 `" d3 M* ]$ o
TRISC3=TRISC4=TRISC5=0XFF;//按键端口设置为读( A) f+ u( x1 F/ `' @0 ~' ?
PORTC=0XFF;: ] r: `9 G9 ]& P
ANSEL=0X00;0 F6 `" Z0 t2 a$ i
T1CKPS1=T1CKPS0=1;//16位定时器1设置为8分频,//总计时为65536*8*0.25=131ms1 R$ H; l, Y( x; G
TMR1H=(uint)(65536-_XTAL_FREQ/4*8*0.01)>>8;//定时10ms,注意不能超过总计时时间
- W1 X5 P( E& M4 h TMR1L=(uint)(65536-_XTAL_FREQ/4*8*0.01)&0XFF;& [/ }" i# k5 X4 E4 K( U
//TMR1H=(65536-5000)/256;也可采用这种方法装初值,跟51单片机一样 L/ \! r+ O+ l! L& m
// TMR1L=(65536-5000)%256;5000*8*0.25=10ms,效果一样。+ }$ G- j' Z E0 u; S. j& o; Q
TMR1CS=0;- [& j+ V0 R" E
TMR1IF=0;
5 M' i9 n, Y3 T- D TMR1IE=1;
- P/ c0 f) X/ G% @ TMR1ON=1;/ v# a% j5 b/ i$ }' d2 D; V' R4 T
GIE=1;
4 Y0 F6 r4 V6 R. c$ S. m PEIE=1;
$ R' w X6 t! `+ h# U while(1)9 h8 G& v0 R m+ Y8 a3 W
{
8 G! L8 { x4 M( j- C' \ distime();
" R& w, F* O3 W keyscan();
. Y( Y3 I9 j6 `+ }6 v- S }
9 U3 ^# M9 t% H- d d}( E- Q) o- W1 {# @" ]
void interrupt TMR_CONTROLL(). {! M/ D2 G* E! j4 D9 I3 q" A* v
{5 w: f2 y7 `+ ~& g$ G% j
if(TMR1IF==1)0 M1 J* x. I+ B9 ]- y
{" f# y: D; U9 c. M* k
//TMR0=6;! @" M0 q- ~7 v F
count++;
( H9 f0 J9 L J/ R ~$ X1 E( ~& H8 y if(count==100)//16M crystal3 c' }# O" q8 a
{
) @6 y0 j4 G. B3 d count=0;7 X# F% \, w* ?; _
sec++; / v1 M2 l8 H6 P; a
if(sec==60)# F0 c8 n" V. I0 m; N0 @* N/ Y7 |
{; N1 s" M* k5 n) \, z6 p/ _1 Y
sec=0;6 t1 P! p: t" t- T8 U7 [
min++;/ [" w- f5 X+ K! W$ |! T/ L
if(min==60)4 @ L/ K: C' J8 l4 k9 U7 K; T7 n
{
) w) b: I) q0 U/ ] min=0;$ H1 T& F. n& K- y4 |
hour++;$ `0 i( [& d0 Q$ N
if(hour==24)
' G3 N& g9 r* c/ X2 o1 g, F0 N {
0 o9 _/ ^, a+ g5 q* M- ` hour=0;
5 x4 `5 m \/ F
' L* z- @2 k: D/ ?9 ^+ V* a/ R }
& c) I9 s9 d' y. `- Z }
, w( r3 L( F% k }
0 M/ I" f B& T8 c
/ @% L( ?: C8 E% z }
, I9 y% d; `) w4 Z+ ` //TMR1H=(65536-5000)/256;7 O# h4 V q$ D. \0 `, u2 {! @' d
// TMR1L=(65536-5000)%256;
9 b x% q& a! L1 ? e1 a4 l TMR1H=(uint)(65536-_XTAL_FREQ/4/8*0.01)>>8;
$ ]; R" x. l- U1 w( h TMR1L=(uint)(65536-_XTAL_FREQ/4/8*0.01)&0XFF;7 M) z7 Q4 ?8 S
TMR1IF=0;$ K5 A7 G! }$ C/ p7 s. ]9 _
}( ~, T, S" L2 G" G" o* A+ k" P/ K1 ?$ L
} |
|