include f648_20_roel

;===============================================================================
;Tabel van DCF77-bits
;===============================================================================
;
;bit/sec naam doel
;0 	     M 	  Minuutmarkering
;1-14 		    gereserveerd
;15 	   R 	  1 als reserve-zendantenne in gebruik is
;16 	   A1 	aankondiging omschakeling zomer/wintertijd
;17      Z1   0 tijdens wintertijd, 1 tijdens zomertijd
;18 	   Z2 	1 tijdens wintertijd, 0 tijdens zomertijd
;19 	   A2 	aankondiging schrikkelseconde
;20 	   S 	  Start van tijdsbericht, altijd 1
;21-24 		    eenheden minuten
;25-27 		    tientallen minuten
;28 	   P1  	controlesom van bit 21-27
;29-32 		    eenheden uren
;33-34 		    tientallen uren
;35 	   P2 	controlesom bit 29-34
;36-39 		    eenheden datum
;40-41 		    tientallen datum
;42-44 		    dag van de week
;45-48 		    eenheden maand
;49 		      tiental maand
;50-53 		    eenheden jaartal (00..99)
;54-57 		    tientallen jaartal (00..99)
;58 	   P3 	controlesom bit 36-57
;(59) 	 - 	  ontbrekende impuls; markeert einde van een minuut

; pariteitsbit wordt 0 bij een even aantal éénen
; pariteitsbit wordt 1 bij een oneven aantal éénen
; aantal éénen + pariteitsbit = even aantal éénen

;JUISTE pariteit
;00101001|1  => bittrein
;00110001|0  => P1
;
;10101001|0  => bittrein
;11001110|0  => P1
;
;Verkeerde pariteit
;10100101|1  => 5 éénen
;11000110|1  => P1
;
;P1 moet 0 zijn voor juiste pariteit

;===============================================================================
;Variabelen declareren
;===============================================================================

;pin-aliasen
var bit LED_ROOD is pin_a0
pin_a0_direction = output
var bit LED_GEEL is pin_a1
pin_a1_direction = output
var bit LED_GROE is pin_a3
pin_a3_direction = output
var bit DCF_IN is pin_a2
pin_a2_direction = input

;tabellen
var bit DCF_bit[60]

;woorden
var dword INTCNTR1
INTCNTR1 = 0
var word INTCNTR2
INTCNTR2 = 0

;byte's
var byte i
i = 0
var byte sec_disp
sec_disp = 0
var byte min_disp
min_disp = 0
var byte uur_disp
uur_disp = 0
var byte sec_dcf
sec_dcf = 0
var byte min_dcf
min_dcf = 0
var byte uur_dcf
uur_dcf = 0
var byte dag_dcf
dag_dcf = 0
var byte dag_week_dcf
dag_week_dcf = 0
var byte maand_dcf
maand_dcf = 0
var byte jaar_dcf
jaar_dcf = 0
var byte time_on
time_on = 0
var byte time_off
time_off = 0
var byte time_off_2
time_off_2 = 0
var byte sec_teller
sec_teller = 0
var byte VINKJE = 2
var byte KRUISJE = 3

;bit's
var bit bit_DCF
bit_DCF = off
var bit P1
P1 = off
var bit P2
P2 = off
var bit P3
P3 = off
var bit dcf_frame_error
dcf_frame_error = off
var bit START_BIT_GEVONDEN
START_BIT_GEVONDEN = off

;table's

const byte msg1[16] = {" ", 0 ," ","D","C","F","7","7","-","K","l","o","k"," ", 0 ," "} ;DCF77-klok
const byte msg2[16] = {" ","V","a","n"," ","H","o","y","l","a","n","d","t"," ","R","."} ;Van Hoylandt R.
const byte msg3[16] = {" "," ","V","e","r","s","i","o","n"," ","1",".","0","2"," "," "} ;Version 1.02
const byte msg4[16] = {"R","e","l","e","a","s","e"," ","0","6","/","1","1","/","0","7"} ;Release 03/10/07
const byte msg5[16] = {" ", 1 ," ","O","p","s","t","a","r","t","e","n"," "," ", 1 ," "} ;Opstarten
const byte msg6[16] = {" ","v","a","n"," ","a","n","t","e","n","n","e",".",".","."," "} ;Van antenne...
const byte msg7[16] = {"S","t","a","r","t","b","i","t"," ","z","o","e","k","e","n","!"} ;Startbit zoeken!
const byte msg8[16] = {"I","n","i","t","i","a","l","i","s","e","r","e","n",".",".","."} ;Initialiseren

;===============================================================================
;Display intstellingen
;===============================================================================

hd44780_clear

hd44780_define(0,14,21,21,23,17,17,14,0)    -- klokje
hd44780_define(1,0,4,21,14,4,4,4,0)    -- antenne
hd44780_define(2,1,2,2,4,20,12,4,0)    -- vinkje
hd44780_define(3,0,6,9,17,17,18,12,0)    -- kruisje

;===============================================================================
;TMR0 intstellingen
;===============================================================================

GIE = on    ;global interrupts enabled
T0IE = on   ;TMR0 interrupts enabled
T0CS = off  ;Internal instruction cycle clock (CLKOUT)

PSA = off   ;Prescaler is assigned to the Timer0 module

PS2 = off   ;000 => prescaler = 1:2
PS1 = off   ;9765,625 interrupts/sec
PS0 = off   ;=> 78125 interrupts/ 8 Seconds

T0IF = off  ;TMR0 flag bit

;===============================================================================
;Start-teksten
;===============================================================================

;msg1 & msg2
hd44780_line1
i = 0
for 16 loop
  hd44780 = msg1[i]
  i = i + 1
end loop
hd44780_line2
i = 0
for 16 loop
  hd44780 = msg2[i]
  i = i + 1
end loop
delay_100ms( 10 )

;msg2 & msg3
hd44780_line1
i = 0
for 16 loop
  hd44780 = msg2[i]
  i = i + 1
end loop
hd44780_line2
i = 0
for 16 loop
  hd44780 = msg3[i]
  i = i + 1
end loop
delay_100ms( 10 )

;msg3 & msg4
hd44780_line1
i = 0
for 16 loop
  hd44780 = msg3[i]
  i = i + 1
end loop
hd44780_line2
i = 0
for 16 loop
  hd44780 = msg4[i]
  i = i + 1
end loop
delay_100ms( 10 )

;msg4 & msg5
hd44780_line1
i = 0
for 16 loop
  hd44780 = msg4[i]
  i = i + 1
end loop
hd44780_line2
i = 0
for 16 loop
  hd44780 = msg5[i]
  i = i + 1
end loop
delay_100ms( 10 )

;msg5 & msg6
hd44780_line1
i = 0
for 16 loop
  hd44780 = msg5[i]
  i = i + 1
end loop
hd44780_line2
i = 0
for 16 loop
  hd44780 = msg6[i]
  i = i + 1
end loop

;===============================================================================
;Antenne laten opstarten
;===============================================================================

while DCF_IN == on loop end loop
;antenne is opgestart !

;===============================================================================
;Startbit zoeken
;===============================================================================

hd44780_clear
hd44780_line1
i = 0
for 16 loop
  hd44780 = msg7[i]
  i = i + 1
end loop

while DCF_IN == off loop end loop
while DCF_IN == on loop end loop
;dcf_in wordt laag !

START_BIT_GEVONDEN = off
i = 60
hd44780_position(69)
hd44780 = "6" hd44780 = "0" hd44780 = "/" hd44780 = "6" hd44780 = "0"  ;60/60

while START_BIT_GEVONDEN == off loop

  time_off = 0

  while DCF_IN == off loop
    delay_1ms(10)
    time_off = time_off + 1
  end loop

  i = i - 1
  hd44780_position(69)
  format_byte_dec(hd44780,i,2,0)
  hd44780 = "/" hd44780 = "6" hd44780 = "0"

  while DCF_IN == on loop end loop

  if time_off >= 100 then
    T0IE = off  ;TMR0 disabled
    T0IE = on   ;TMR0 enabled
    START_BIT_GEVONDEN = on
    sec_teller = 0
  end if

end loop

;===============================================================================
;Interrupt
;===============================================================================

procedure isr is ; Interrupt Service Routine
  pragma interrupt

;  INTCNTR1 = INTCNTR1 + 1
;  INTCNTR2 = INTCNTR2 + 1
;
;  if INTCNTR1 == 78125 then ;elke 8 sec (100 % juist)
;    INTCNTR2 = 0
;    INTCNTR1 = 0
;  end if
;
;  If INTCNTR2 == 9765 then  ;elke 0,999936 sec (64 µs vertraagt)
;    INTCNTR2 = 0
;    sec_disp = sec_disp + 1
;    if sec_disp == 60 then
;      sec_disp = 0
;      min_disp = min_disp + 1
;      if min_disp == 60 then
;        min_disp = 0
;        uur_disp = uur_disp + 1
;        if uur_disp == 24 then
;          uur_disp = 0
;        end if
;      end if
;    end if
;  end if

   LED_ROOD = DCF_IN
   LED_GROE = ! dcf_frame_error
   T0IF = off     ;TMR0 flag bit

end procedure

;===============================================================================
;Main Loop
;===============================================================================

hd44780_clear

forever loop
    LED_GEEL = ! LED_GEEL

    time_on = 0

    while DCF_IN == on loop
      delay_1ms(1)
      time_on = time_on + 1
    end loop

    if time_on <= 120 then
      i = "0"
      bit_dcf = off
    else
      i = "1"
      bit_dcf = on
    end if
;================================BIT 20=========================================
    if sec_teller == 20 & bit_dcf == off then   ;bit 20 moet 1 zijn
      dcf_frame_error = on
    elsif sec_teller == 20 & bit_dcf == on then
      dcf_frame_error = off
      min_dcf = 0
      uur_dcf = 0
      dag_dcf = 0
      dag_week_dcf = 0
      maand_dcf = 0
      jaar_dcf = 0
      P1 = off
      P2 = off
      P3 = off
    end if
;===============================MINUTEN=========================================
    if sec_teller == 21 & bit_dcf == on then   ;waarde 1 bij de minuten
      min_dcf = min_dcf + 1
      ;P1 = P1 + 1
    elsif sec_teller == 22 & bit_dcf == on then   ;waarde 2 bij de minuten
      min_dcf = min_dcf + 2
      ;P1 = P1 + 1
    elsif sec_teller == 23 & bit_dcf == on then   ;waarde 4 bij de minuten
      min_dcf = min_dcf + 4
      ;P1 = P1 + 1
    elsif sec_teller == 24 & bit_dcf == on then   ;waarde 8 bij de minuten
      min_dcf = min_dcf + 8
      ;P1 = P1 + 1
    elsif sec_teller == 25 & bit_dcf == on then   ;waarde 10 bij de minuten
      min_dcf = min_dcf + 10
      ;P1 = P1 + 1
    elsif sec_teller == 26 & bit_dcf == on then   ;waarde 20 bij de minuten
      min_dcf = min_dcf + 20
      ;P1 = P1 + 1
    elsif sec_teller == 27 & bit_dcf == on then   ;waarde 40 bij de minuten
      min_dcf = min_dcf + 40
      ;P1 = P1 + 1
    elsif sec_teller == 28 then                   ;pariteit bij de minuten
      ;P1 = P1 + bit_dcf
      ;
      ;if P1 == on then
      ;  dcf_frame_error = on
      ;end if
      ;
;      if min_dcf == 0 then
;        min_dcf = 59
;      else
;        min_dcf = min_dcf - 1
;      end if
    end if
;=================================UREN==========================================
    if sec_teller == 29 & bit_dcf == on then   ;waarde 1 bij de uren
      uur_dcf = uur_dcf + 1
      ;P2 = P2 + 1
    elsif sec_teller == 30 & bit_dcf == on then   ;waarde 2 bij de uren
      uur_dcf = uur_dcf + 2
      ;P2 = P2 + 1
    elsif sec_teller == 31 & bit_dcf == on then   ;waarde 4 bij de uren
      uur_dcf = uur_dcf + 4
      ;P2 = P2 + 1
    elsif sec_teller == 32 & bit_dcf == on then   ;waarde 8 bij de uren
      uur_dcf = uur_dcf + 8
      ;P2 = P2 + 1
    elsif sec_teller == 33 & bit_dcf == on then   ;waarde 10 bij de uren
      uur_dcf = uur_dcf + 10
      ;P2 = P2 + 1
    elsif sec_teller == 34 & bit_dcf == on then   ;waarde 20 bij de uren
      uur_dcf = uur_dcf + 20
      ;P2 = P2 + 1
    elsif sec_teller == 35 then   ;pariteit bij de uren
      ;P2 = P2 + bit_dcf

      ;if P2 == on then
      ;  dcf_frame_error = on
      ;end if
    end if

;===================================DAGEN=======================================

    if sec_teller == 36 & bit_dcf == on then   ;waarde 1 bij dagen
      dag_dcf = dag_dcf + 1
    elsif sec_teller == 37 & bit_dcf == on then   ;waarde 2 bij dagen
      dag_dcf = dag_dcf + 2
    elsif sec_teller == 38 & bit_dcf == on then   ;waarde 4 bij dagen
      dag_dcf = dag_dcf + 4
    elsif sec_teller == 39 & bit_dcf == on then   ;waarde 8 bij dagen
      dag_dcf = dag_dcf + 8
    elsif sec_teller == 40 & bit_dcf == on then   ;waarde 10 bij dagen
      dag_dcf = dag_dcf + 10
    elsif sec_teller == 41 & bit_dcf == on then   ;waarde 20 bij dagen
      dag_dcf = dag_dcf + 20
    elsif sec_teller == 42 & bit_dcf == on then   ;waarde 1 bij dag van de week   ;als men (bijvoorbeeld) deze regels                                        <=====================
      dag_week_dcf = dag_week_dcf + 1                                             ;weggelaat lukt het wel
    elsif sec_teller == 43 & bit_dcf == on then   ;waarde 2 bij dag van de week   ;omdat hij dan maar 2024 bytes gebruikt                                    <=====================
      dag_week_dcf = dag_week_dcf + 2                                             ;met deze regels erbij gebruikt hij 2214 en werkt
    elsif sec_teller == 44 & bit_dcf == on then   ;waarde 4 bij dag van de week   ;hij niet meer                                                             <=====================
      dag_week_dcf = dag_week_dcf + 4                                             ;

    end if

;==================================MAANDEN======================================
    if sec_teller == 45 & bit_dcf == on then   ;waarde 1 bij maand
      maand_dcf = maand_dcf + 1
    elsif sec_teller == 46 & bit_dcf == on then   ;waarde 2 bij maand
      maand_dcf = maand_dcf + 2
    elsif sec_teller == 47 & bit_dcf == on then   ;waarde 4 bij maand
      maand_dcf = maand_dcf + 4
    elsif sec_teller == 48 & bit_dcf == on then   ;waarde 8 bij maand
      maand_dcf = maand_dcf + 8
    elsif sec_teller == 49 & bit_dcf == on then   ;waarde 10 bij maand
      maand_dcf = maand_dcf + 10
    end if
;===================================JAREN=======================================
    if sec_teller == 50 & bit_dcf == on then   ;waarde 1 bij jaar
      jaar_dcf = jaar_dcf + 1
    elsif sec_teller == 51 & bit_dcf == on then   ;waarde 2 bij jaar
      jaar_dcf = jaar_dcf + 2
    elsif sec_teller == 52 & bit_dcf == on then   ;waarde 4 bij jaar
      jaar_dcf = jaar_dcf + 4
    elsif sec_teller == 53 & bit_dcf == on then   ;waarde 8 bij jaar
      jaar_dcf = jaar_dcf + 8
    elsif sec_teller == 54 & bit_dcf == on then   ;waarde 10 bij jaar
      jaar_dcf = jaar_dcf + 10
    elsif sec_teller == 55 & bit_dcf == on then   ;waarde 20 bij jaar
      jaar_dcf = jaar_dcf + 20
    elsif sec_teller == 56 & bit_dcf == on then   ;waarde 40 bij jaar
      jaar_dcf = jaar_dcf + 40
    elsif sec_teller == 57 & bit_dcf == on then   ;waarde 80 bij jaar
      jaar_dcf = jaar_dcf + 80
               ;:    elsif sec_teller == 58 & bit_dcf == on then   ;pariteit bij de datum

    end if

;
;;
;;      hd44780_position(4)
;;  format_byte_dec(hd44780,uur_disp,2,0)
;;  hd44780 = ":"
;;  format_byte_dec(hd44780,min_disp,2,0)
;;  hd44780 = ":"
;;  format_byte_dec(hd44780,sec_disp,2,0)
;;  hd44780 = " "
;
    hd44780_line1
    format_byte_dec(hd44780,uur_dcf,2,0)
    hd44780 = ":"
    format_byte_dec(hd44780,min_dcf,2,0)
    hd44780 = ":"
    format_byte_dec(hd44780,sec_teller,2,0)
;
;
    hd44780_line2
    format_byte_dec(hd44780,dag_dcf,2,0)
    hd44780 = "/"
    format_byte_dec(hd44780,maand_dcf,2,0)
    hd44780 = "/"
    format_byte_dec(hd44780,jaar_dcf,2,0)
    hd44780 = " "
    format_byte_dec(hd44780,dag_week_dcf,1,0)
;
    sec_teller = sec_teller + 1
    if sec_teller == 59 then
      sec_teller = 0
    end if

    time_off_2 = 0

    while DCF_IN == off loop
      delay_1ms(10)
;      time_off = time_off + 1
    end loop

;    if time_off >= 100 then
;      T0IE = off  ;TMR0 disabled
;      T0IE = on   ;TMR0 enabled
;      sec_teller = 0
;    end if

end loop