enics
member
คะแนน 0
ออฟไลน์
กระทู้: 7
|
|
« เมื่อ: ธันวาคม 20, 2010, 03:23:54 pm » |
|
ช่วยห่นอยครับ ผมมีโค้ดนาฬิกา rtc ds1307 แสดงผล 7-segment แบบ 6หลักครับ แต่รบกวนแก้โค้ด ให้ใช้กับ 7-segment แบบ 4 หลักให้หน่อยคราบ
โค้ด ตามนี้เลยครับ
'**************************************************************** '* Name : UNTITLED.BAS * '* Author : [select VIEW...EDITOR OPTIONS] * '* Notice : Copyright (c) 2010 [select VIEW...EDITOR OPTIONS] * '* : All Rights Reserved * '* Date : 20/12/2010 * '* Version : 1.0 * '* Notes : * '* : * '**************************************************************** @ DEVICE PIC16F887, HS_OSC, WDT_OFF , MCLR_ON, LVP_OFF, PROTECT_OFF @ DEVICE2 BOR21V, WRT_OFF '@ DEVICE PIC16F877A,HS_OSC,LVP_OFF,BOD_OFF,WDT_OFF Segments Var PORTB Digits Var PORTA
i2 var byte I Var Byte J var byte k var byte p var byte n Var Byte q var byte x var byte z1 var byte z2 var byte SEC VAR BYTE avce var byte MINT VAR BYTE HOUR VAR BYTE ticks var byte sw1 var portc.4 sw2 var portc.5 sw3 var portc.6 sw4 var portc.7 L1 var portd.7 L2 var portd.6 L3 var portd.5 L4 var portd.4 SDA VAR PORTc.3 ' DS1307 SDA pin #5 SCL VAR PORTd.0 ' DS1307 SCL pin #6 SQW VAR PORTc.2 ' Ds1307 SQW Pin Dt1 var byte Dt2 var byte Dt3 var byte Dt4 var byte Dt5 var byte Dt6 var byte Dt7 var byte Dt8 var byte Dt1_ var byte Dt2_ var byte Dt3_ var byte Dt4_ var byte Dt5_ var byte Dt6_ var byte Dt7_ var byte Dt8_ var byte ct var byte
TRISA = %000000 TRISB = %00000000 ' Set segment all pins to output TRISC = %11111111 TRISD = %11110000 TRISE = %000 ' Set digit all pins to output z1=1 avce=0 hour = 0 ' Set initial time mint = 0 ' Set initial time sec = 0 ' Set initial time ticks = 0 sw1=0 sw2=0 sw3=0 sw4=0 k=0 p=0 q=0 x=0 high l1 high l2 high l3 high l4
I2CREAD SDA,SCL,$D0,$00,[Dt1,Dt2,Dt3,Dt4,Dt5,Dt6,Dt7,Dt8] if dt1.7=1 then GOSUB Write_1307 ' Write time & date on entry Read_1307: ' Read time Secs,Mins,Hours,Day,Date,Month,Year,Control
start: if SQW=1 and x=0 THEN GOSUB readdata if sqw=0 and x=1 then x=0 gosub display if sw1=0 then k=k+1 if k>=100 then k=0:goto setting
gosub lamptime
goto start
display: for i=0 to 5 IF i=0 THEN n=hour DIG 1 IF i=1 THEN n=hour dig 0 IF i=2 THEN n=mint DIG 1 IF i=3 THEN n=mint DIG 0 IF i=4 THEN n=sec DIG 1 IF i=5 THEN n=sec DIG 0 gosub display1 pauseUS 1000 next i return 'COMMON CATHODE display1: digits=$00 Lookup n, [$3F, $06, $5B, $4F, $66, $6D, $7D, $07, $7F, $6F], Segments i2=i if i2=4 then high porte.0 if i2<>4 then low porte.0 digits = dcd i2 return Write_1307: ' Set time & date to 15:30:00 Tuesday 6th of July 2004 I2CWRITE SDA,SCL,$D0,$00,[$00,$00,$00,$02,$06,$07,$04,$90] ' Write to DS1307 RETURN ' Sec Min Hr Day D M Y Control
setting: I2CWRITE SDA,SCL,$D0,$00,[$80,$00,$00,$02,$06,$07,$04,$90] ' Sec Min Hr Day D M Y Control showtime: sec=$00 gosub display if sw2=0 then k=k+1 if k>=40 then mint=mint+1:k=0 if mint>59 then mint=0 if sw3=0 then p=p+1 if p>=40 then hour=hour+1:p=0 if mint>59 then mint=0 if hour>23 then hour=0 if sw4=0 then q=q+1 if q>=100 then q=0:goto settime goto showtime
settime: dt2=mint dig 1 dt2=dt2 << 4 dt2=dt2+mint dig 0
dt3=hour dig 1 dt3=dt3 << 4 dt3=dt3+hour dig 0 dt1=$0
I2CWRITE SDA,SCL,$D0,$00,[Dt1,Dt2,Dt3,Dt4,Dt5,Dt6,Dt7,Dt8] goto start
READDATA: x=x+1
I2CREAD SDA,SCL,$D0,$00,[Dt1,Dt2,Dt3,Dt4,Dt5,Dt6,Dt7,Dt8]
Dt1_=((Dt1>>4)&$0F)*10+(Dt1&$0F) 'dt1_=(dt1* 16)*10+(dt1//16)
Dt2_=((Dt2>>4)&$0F)*10+(Dt2&$0F)
Dt3_=((Dt3>>4)&$0F)*10+(Dt3&$0F)
sec=dt1_
mint=dt2_
hour=dt3_
return
lamptime: if hour=07 and mint=30 then high L1:high L2:high L3 'on lamp 07:30 if hour=12 and mint=05then low L1 'off lamp 12:05 if hour=12 and mint=55 then high L1 'on lamp 12:55 if hour=15 and mint=05 then low L1:low L2:low L3 'off lamp 15:05 return
|