Enjoy !
; SCROLLING THERMOMETER
; FINAL VERSION
; By NICULESCU DAN
; SEPTEMBER, 13, 2011
;
; .oooO
; ( ) Oooo.
; \ ( ( )
; \_) ) /
; (_/
;
@ DEVICE pic16F628A, intOSC_osc_noclkout, WDT_OFF, PWRT_OFF, BOD_OFF, MCLR_off
define osc 4
INTCON = 0
TRISA = 000000 ' CATODES TO PORTA
TRISB = 000000 ' ANODES TO PORTB
CMCON = 7
DQ var PortB.7
'***************************************************************************************
eeprom 0, [100001,111010,110110,101110,100001] '0 Note : 1 = led OFF ; 0 = led ON
eeprom 5, [111111,111101,000000,111111,111111] '1
eeprom 10,[111101,111110,101110,110110,111001] '2
eeprom 15,[111101,111110,110110,110110,101001] '3
eeprom 20,[100111,101011,101101,000000,101111] '4
eeprom 25,[111000,111010,111010,111010,100110] '5
eeprom 30,[100001,110110,110110,110110,101101] '6
eeprom 35,[111110,001110,110110,111010,111100] '7
eeprom 40,[001001,110110,110110,110110,001001] '8
eeprom 45,[111001,110110,110110,110110,001001] '9
counter var byte
scan var byte
scroll var byte
leddata var byte[36] 'Column Data for display 36 columns
temperature var Word 'reading from sensor
Sign var BIT
tempA var byte 'Stores First digit (High)
tempB var byte 'stores Second digit (Mid)
tempC var byte 'stores Third digit (low)
n var byte
DS18B20_12bit CON 111111
START:
CLEAR
' Init Sensor
OWOUT DQ, 1, [$CC, $4E, 0, 0, DS18B20_12bit]
OWOut DQ, 1, [$CC, $48]
OWOut DQ, 1, [$CC, $B8]
OWOut DQ, 1, [$CC, $BE]
Pause 50
OWIn DQ, 2, [temperature.byte0, temperature.byte1]
Pause 50
OWOut DQ, 1, [$CC, $44]
OWOut DQ, 1, [$CC, $BE]
OWIn DQ, 2, [temperature.byte0, temperature.byte1]
Sign = temperature.15
temperature= ABS(temperature)
temperature=((temperature >> 4)*100) + ((temperature & $F) * 100 >> 4)
if sign then temperature= -temperature
tempA = temperature DIG 3
tempB = temperature DIG 2
tempC = temperature DIG 1
if tempA = 0 then
FOR counter = 0 TO 4
leddata [counter+8] = 111111
next
else
for counter = 0 to 4
READ tempA*5+counter,leddata [counter+8] 'stores 1st digit in leddata locations 8,9,10,11,12
NEXT
endif
FOR counter = 0 TO 4
READ tempB*5+counter,leddata [counter+14] 'stores 2nd digit in leddata locations 14,15,16,17,18
NEXT
FOR counter = 0 TO 4
READ tempC*5+counter,leddata [counter+23] 'stores 3rd digit in leddata locations 23,24,25,26,27
NEXT
;==========================================================================================================
'leddata locations 0-7 are blank so that data scrolls onto display
'leddata locations 8-12 1st digit (leading digit)
'leddata location 13 blank column between digits
'leddata locations 14-18 2nd digit
'leddata location 19 blank column between digits
'leddata locations 20 & 21 Decimal Point
'leddata locations 23 - 27 3rd digit (last digit)
'leddata locations 28 blank column between digits
'leddata locations 29 - 34 Degrees C symbol
'leddata location 35 is a blank column at end to clear display as it scrolls
;==========================================================================================================
for n=0 to 7
leddata [n] = 111111
next n
leddata [13] = 111111
leddata [19] = 111111
leddata [20] = 111111 ' DECIMAL POINT
leddata [21] = 111111
leddata [22] = 111111
leddata [28] = 111111
leddata [29] = 111100 ' degrees c
leddata [30] = 111100
leddata [31] = 100011
leddata [32] = 111101
leddata [33] = 111101
leddata [34] = 111101
leddata [35] = 111111
LOOPing:
FOR scroll = 0 TO 35
FOR scan = 0 TO 10
PORTA=111111
PAUSE 1
PORTb = 1
FOR counter = 0 TO 7
PORTa = leddata [counter]
PAUSE 1
PORTb = PORTb * 2
NEXT
NEXT
FOR counter = 0 TO 34
leddata [counter] = leddata [counter+1]
NEXT
NEXT
GOTO START
END
Update : Based on the same ideea, I write the code for :
- scrolling-thermo-clock, with PIC16F877A, DS18B20, DS1307 (Thanks Mrs.Melanie for great example of using RTC - see "Calendar-clock MN1307 !) and 7x7 LED matrix (remember - DIY matrix). The time can be set, with three buttons ! ( see video how it works here http://www.youtube.com/watch?v=Zkv4EbElOCg )
;****************************************************************
; Scroll-clock-thermometer-with-16F877A
; with setting procedure
; for my DIY matrix (7x7 LED's)
; by NICULESCU DAN - october, 29, 2011
;
; .oooO
; ( ) Oooo.
; \ ( ( )
; \_) ) /
; (_/
;
'****************************************************************
@ DEVICE pic16F877A, XT_OSC, WDT_ON, LVP_OFF, PWRT_ON, BOD_ON
DEFINE OSC 4
INCLUDE "ALLDIGITAL.pbp"
OPTION_REG.7 = 0
ADCON1 = 7
PORTA = 000001
TRISA = 000001
PORTB = 000000 ' anodes of led's
TRISB = 000000
PORTC = 100000
TRISC = 100000
PORTD = 000000 ' catodes of led's
TRISD = 000000
DecButton var PORTC.6 ' Press to Decrement Button
SetButton var PORTC.7 ' Press to Set/memorise Button
IncButton var PORTC.5 ' Press to Increment Button
DQ var PortA.0 ' port for DS18B20
SDA Var PORTC.3 ' DS1307 SDA pin #5
SCL Var PORTC.2 ' DS1307 SCL pin #6
'***************************************************************************************
eeprom 0, [000011,110101,101101,011101,000011] '0
eeprom 5, [111111,111011,000001,111111,111111] '1
eeprom 10,[111011,111101,011101,101101,110011] '2
eeprom 15,[111101,111101,110101,101001,011101] '3
eeprom 20,[001111,010111,011011,000001,011111] '4
eeprom 25,[110001,110101,110101,110101,001101] '5
eeprom 30,[000111,101011,101101,101101,011111] '6
eeprom 35,[111001,111101,011101,101101,110001] '7
eeprom 40,[010011,101101,101101,101101,010011] '8
eeprom 45,[110011,101101,101101,101101,000011] '9
eeprom 50,[110011,101101,010011,111111,001111] 'ok
eeprom 55,[101111,101111,101111,101111,101111] 'minus
counter var byte
scan var byte
scroll var byte
leddata var byte[75] 'Column Data for display 75 columns
temperature var Word 'reading from sensor
tempA var byte 'Stores First digit (High)
tempB var byte 'stores Second digit (Mid)
tempC var byte 'stores Third digit (low)
n var byte
TemperC Var Word
Sign Var Word
Float Var Word
Dummy Var Byte
semnul Var byte
DB Var BYTE[8] ' Data byte array
RTCSec Var DB[0] ' alias individual bytes in array
RTCDay Var DB[3]
RTCDate Var DB[4]
RTCMonth Var DB[5]
RTCYear Var DB[6]
RTCMin var byte ' Minutes
RTCHour var byte ' Hours
RTCCtrl var byte ' Control
SetSec var byte ' Seconds
SetMin var byte ' Minutes
SetDecMin Var byte
SetHour var byte ' Hours
SetDecHour var byte
TimeOut var word ' Variable for SetUp Menu Time-Out
CounterA var byte ' General purpose Variable
CounterB var byte ' General purpose Variable
CounterC var byte ' General purpose Variable
CounterD var byte ' General purpose Variable
ore var word
minute var word
oraa var byte
orab var byte
minutea var byte
minuteb var byte
okey var BYTE
ButtonRepeat con 200 ' Timeperiod in mS for Edit Buttons auto-repeat
Debounce con 250
DS18B20_12bit CON 111111
DisplayLoop:
Clear
If SetButton=0 then
Gosub SetButtonRelease
goto Setup
endif
' Init DS18B20 Sensor
OWOUT DQ, 1, [$CC, $4E, 0, 0, DS18B20_12bit]
OWOut DQ, 1, [$CC, $48]
OWOut DQ, 1, [$CC, $B8]
OWOut DQ, 1, [$CC, $BE]
Pause 50
OWIn DQ, 2, [temperature.byte0, temperature.byte1]
Pause 50
OWOut DQ, 1, [$CC, $44]
OWOut DQ, 1, [$CC, $BE]
OWIn DQ, 2, [temperature.byte0, temperature.byte1]
If Temperature.15 then
Temperature= ~Temperature +1
sign="-"
else
sign="+"
Endif
Dummy = 625 * Temperature
TempC = DIV32 10
TemperC = (Temperature & $7FF) >> 4
Float = ((Temperature.Lowbyte & $0F ) * 25 )>>2
Temperature = TemperC*100 + Float
tempA = temperature DIG 3
tempB = temperature DIG 2
tempC = temperature DIG 1
semnul = sign
if semnul = "+" then
For counter = 0 to 4
leddata [counter] = 111111
next
else
semnul = 55
for counter = 0 to 4
READ semnul+counter,leddata [counter]
next
endif
if tempA = 0 then
FOR counter = 0 TO 4
leddata [counter+8] = 111111
next
else
for counter = 0 to 4
READ tempA*5+counter,leddata [counter+8] 'stores 1st digit in leddata locations 8,9,10,11,12
NEXT
endif
FOR counter = 0 TO 4
READ tempB*5+counter,leddata [counter+14] 'stores 2nd digit in leddata locations 14,15,16,17,18
NEXT
FOR counter = 0 TO 4
READ tempC*5+counter,leddata [counter+23] 'stores 3rd digit in leddata locations 23,24,25,26,27
NEXT
gosub read_1307
oraa = ore dig 1
orab = ore dig 0
minutea = minute dig 1
minuteb = minute dig 0
if oraa = 0 then
FOR counter = 0 TO 4
leddata [counter+41] = 111111
next
else
FOR counter = 0 TO 4
READ oraa*5+counter,leddata [counter+41] 'stores tens hours digit in leddata locations
NEXT
endif
FOR counter = 0 TO 4
READ orab*5+counter,leddata [counter+47] 'stores unit hours digit in leddata locations
NEXT
FOR counter = 0 TO 4
READ minutea*5+counter,leddata [counter+57] 'stores tens minutes digit in leddata locations
NEXT
FOR counter = 0 TO 4
READ minuteb*5+counter,leddata [counter+63] 'stores unit minutes digit in leddata locations
NEXT
for n=5 to 7
leddata [n] = 111111
next n
leddata [13] = 111111
leddata [19] = 111111
leddata [20] = 111111 ' DECIMAL POINT
leddata [21] = 111111
leddata [22] = 111111
leddata [28] = 111111
leddata [29] = 111000 ' degrees c
leddata [30] = 111000
leddata [31] = 100011
leddata [32] = 011101
leddata [33] = 011101
leddata [34] = 111111
leddata [35] = 111111
leddata [36] = 111111
leddata [37] = 111111
leddata [38] = 111111
leddata [39] = 111111
leddata [40] = 111111
leddata [46] = 111111
leddata [52] = 111111
leddata [53] = 111111
leddata [54] = 010111
leddata [55] = 111111
leddata [56] = 111111
leddata [62] = 111111
leddata [68] = 111111
leddata [69] = 111111
leddata [70] = 111111
leddata [71] = 111111
leddata [72] = 111111
leddata [73] = 111111
leddata [74] = 111111
leddata [75] = 111111
'
' Display RTC
' -----------
LOOPing:
FOR scroll = 0 TO 70
FOR scan = 0 TO 10 ' decrease for faster speed of scrolling
PORTd=111111
PAUSE 1
PORTb = 1
FOR counter = 0 TO 7
PORTd = leddata [counter]
PAUSE 1
PORTb = PORTb * 2
NEXT
NEXT
FOR counter = 0 TO 70
leddata [counter] = leddata [counter+1]
NEXT
NEXT
'
Pause 250 ' Repeat about 4 times/sec ; DO NOT INCREASE !!!
Goto DisplayLoop
'
' Clock Settings
' ===========================
'
' Initial Default Settings
' ------------------------
'
SetupPreset:
RTCSec =$00 ' Seconds preset to 00
RTCMin =$00 ' Minutes preset to 15
RTCHour=$13 ' Hours preset to 13'00
RTCCtrl=$10 ' Control preset to output 1 second 'Tick' on SQWpin
'
' Convert the Current BCD Data to Something easier to Edit
' --------------------------------------------------------
Setup:
I2CREAD SDA,SCL,$D1,$00,[STR DB\8] ' Read 8 bytes from DS1307
ore = (db[2] & $F )+((db[2]>>4)*10) ' Hex to Dec hour
minute = (db[1] & $F )+((db[1]>>4)*10) ' Hex to Dec minutes
setdechour= ore dig 1
sethour=ore dig 0
setdecmin = minute dig 1
setmin=minute dig 0
CounterC=0 ' Set to Start of Setup (four steps from 0 to 4)
TimeOut=0 ' Zero the Time-Out counter
' ------------------
SetupEntryLoop:
oraa = setDechour
orab = sethour
minutea = setDecmin
minuteb = setmin
gosub staticx
'
' Decrement Button Pressed
' ------------------------
If DecButton=0 then
'
' Decrement Tens Hours
' ---------------
If CounterC=0 then
If SetDecHour=0 then
SetDecHour=2
else
SetDecHour=SetDecHour-1
endif
endif
'
' Decrement Units Hours
' ---------------
If CounterC=1 then
IF setdechour=2 then
IF SetHour=0 THEN
SetHour= 3
else
SetHour=SetHour-1
Endif
else
IF sethour=0 then
SetHour=9
else
SetHour=SetHour-1
Endif
Endif
Endif
'
' Decrement Tens Minutes
' -----------------
If CounterC=2 then
If SetDecMin=0 then
SetDecMin=5
else
SetDecMin=SetDecMin-1
endif
endif
'
' Decrement Units Minutes
' -----------------
If CounterC=3 then
If SetMin=0 then
SetMin=9
else
SetMin=SetMin-1
endif
endif
Pause ButtonRepeat
TimeOut=0
Goto SetupEntryLoop
endif
'
' Increment Button Pressed
' ------------------------
' This is just like Decrement but opposite...
If IncButton=0 then
'
'
' Increment Tens Hours
' ---------------
If CounterC=0 then
If SetDecHour=2 then
SetDecHour=0
else
SetDecHour=SetDecHour+1
Endif
Endif
'
' Increment Units Hours
' ---------------
If CounterC=1 then
IF SetDecHour = 2 then
IF SetHour => 3 then
SetHour=0
else
SetHour=SetHour+1
Endif
else
IF SetHour=9 then
SetHour=0
else
SetHour=SetHour+1
Endif
Endif
Endif
'
' Increment Tens Minutes
' -----------------
If CounterC=2 then
If SetDecMin=5 then
SetDecMin=0
else
SetDecMin=SetDecMin+1
Endif
Endif
'
' Increment Units Minutes
' -----------------
If CounterC=3 then
If SetMin=9 then
SetMin=0
else
SetMin=SetMin+1
Endif
Endif
Pause ButtonRepeat
TimeOut=0
Goto SetupEntryLoop
endif
'
' Set Button Pressed
' ------------------
If SetButton=0 then
CounterC=CounterC+1
' Increment Menu Item
TimeOut=0
If CounterC>4 then
' Save Data if all edit items exhaused
'
' Save Hours to BCD DS1307's Format
' ---------------------------------------
CounterA= (SetDecHour *10)+ SetHour
Gosub ConvertBCD
RTCHour=CounterB
'
' Save Minutes to BCD DS1307's Format
' ---------------------------------------
CounterA= (SetDecMin *10) + SetMin ; Yupppy ! I found it !
Gosub ConvertBCD
RTCMin=CounterB
'
' Do the Business
' ---------------
I2CWrite SDA,SCL,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCDay,RTCMonth,RTCYear,RTCCtrl]
Pause 1000
Gosub SetButtonRelease
Goto DisplayLoop
endif
Gosub SetButtonRelease
Goto SetupEntryLoop ' Loop for Next Menu Item
endif
'
' Menu TimeOut Counter
' --------------------
Pause 1 ' Kill 1mS
TimeOut=TimeOut+1
If TimeOut>2000 then goto DisplayLoop
' User paused too long
' Return User to Time & Date Display
' This will however bounce the User to the original
' Setup Menu start point if the DS1307 has not
' been initialised
Goto SetupEntryLoop ' Loop for Button Press
'
read_1307: ' Read time Secs,Mins,Hours,Day,Date,Month,Year,Control
I2CREAD SDA,SCL,$D1,$00,[STR DB\8] ' Read 8 bytes from DS1307
ore = (db[2] & $F )+((db[2]>>4)*10) ' Hex to Dec hour
minute = (db[1] & $F )+((db[1]>>4)*10) ' Hex to Dec minutes
RETURN
'
' Subroutine Converts back to BCD
' -------------------------------
' CounterA is the entry variable
' CounterB holds the converted BCD result on exit
ConvertBCD:
CounterB=CounterA DIG 1
CounterB=CounterB<<4
CounterB=CounterB+CounterA DIG 0
Return
SetButtonRelease:
While SetButton=0:Wend
Pause 250 ' Small pause to kill any Key-Bounce
Return
staticx: ' not perfect, but readable digits !!!
if counterc=0 then
FOR counter = 0 TO 4
READ oraa*5+counter,leddata [counter]
NEXT
PORTd = 255
PAUSE 1
PORTb = 1
FOR counter = 0 TO 4
PORTd = leddata [counter]
PORTb = PORTb * 2
leddata [counter] = leddata [counter+1]
NEXT
endif
if counterc=1 then
FOR counter = 0 TO 4
READ orab*5+counter,leddata [counter]
NEXT
PORTd = 255
PAUSE 1
PORTb = 1
FOR counter = 0 TO 4
PORTd = leddata [counter]
PORTb = PORTb * 2
leddata [counter] = leddata [counter+1]
NEXT
endif
if counterc=2 then
FOR counter = 0 TO 4
READ minutea*5+counter,leddata [counter]
NEXT
PORTd = 255
PAUSE 1
PORTb = 1
FOR counter = 0 TO 4
PORTd = leddata [counter]
PORTb = PORTb * 2
leddata [counter] = leddata [counter+1]
NEXT
endif
if counterc=3 then
FOR counter = 0 TO 4
READ minuteB*5+counter,leddata [counter]
NEXT
PORTd = 255
PAUSE 1
PORTb = 1
FOR counter = 0 TO 4
PORTd = leddata [counter]
PORTb = PORTb * 2
leddata [counter] = leddata [counter+1]
NEXT
endif
if counterc=4 then
OKEY = 50
FOR counter = 0 TO 4
READ OKEY+COUNTER,leddata [counter]
NEXT
PORTd = 255
PAUSE 1
PORTb = 1
FOR counter = 0 TO 4
PORTd = leddata [counter]
PORTb = PORTb * 2
leddata [counter] = leddata [counter+1]
NEXT
endif
portb=0
portd=0
pause 1
return
End ; of story
Original topic can be found here : http://www.picbasic.co.uk/forum/showthread.php?t=15431
Thanks to all moderators and members of PBP forum, for support and sharing knowledges ! Best regards !
Dan
LE : I dont know why some characters in code appears "strange" ... For example most of "%" are missing...So, for good codes, check the forum, to the link.


Menu

Re: PIC16F1783: Erase algorithm incorrect
Hi,
HenrikOlsson Yesterday, 17:55No experience with the U2 here but according to the U2 supported device list (https://melabs.com/includes/compatibility/meprog.pics.htm) the 16F1783 is supported.
I don't know enough about...