;-------------------------------------------------------------------------------
;16F684_blink
;
;Led knipperlicht
;
; interne oscillator op 4MHz
; interne MCLR
;-------------------------------------------------------------------------------
include 16f684

pragma target clock    4_000_000       ;oscillator frequency
pragma target OSC      INTOSC_NOCLKOUT ;interne oscillator/osc pinnen beiden IO
pragma target WDT      disabled        ;no watchdog
pragma target MCLR     internal        ;reset internally

enable_digital_io()    ;alle pinnen digitale IO

;LED is via voorschakelweerstand aangesloten op pin A0
alias   led      is pin_A0
pin_A0_direction =  output
led = off
;-------------------------------------------------------------------------------
;instellingen voor Timer0
;-------------------------------------------------------------------------------
const byte Timer0Reload = 0x06
const byte TimerCounterReload = 250
var byte TimerCounter = TimerCounterReload

INTCON_T0IE     = high  ;interrupt!
OPTION_REG_T0CS = low   ;Timer0 timer mode
OPTION_REG_PSA  = low   ;prescaler op Timer0
;reset alle prescaler bits en zet daarna de prescaler op 011 (=/16)
OPTION_REG = (OPTION_REG & 0xF8) | 0x03
TMR0       = Timer0Reload
var bit TimePassed = low

;-------------------------------------------------------------------------------
;instellingen voor Timer2
;Prescaler= 16
;PostScaler = 16
;PR2 = 195
;Freq = 20.03 Hz
;Tijd = 0.04992000 seconden
;-------------------------------------------------------------------------------
T2CON        = 0x7B    ;pre- en postscaler op 16
T2CON_TMR2ON = high    ;bit 2 turn timer2 on;
PR2          = 195     ;herlaadwaarde
PIE1_TMR2IE  = high    ;Timer2 interrupt enable
INTCON_PEIE  = high    ;perifierie interrupt enable
PIR1_TMR2IF  = low     ;wis interrupt vlag

var bit LedBlinkTime = low
var bit LedTmp = low

;-------------------------------------------------------------------------------
;Enable interrupts
;-------------------------------------------------------------------------------
INTCON_GIE = high        ;enable interrupts


;-------------------------------------------------------------------------------
;INTERRUPT PROCEDURE
;-------------------------------------------------------------------------------
procedure Timer0Interrupt is
    pragma interrupt
    if(INTCON_T0IF)then
      ;T0IF is geset, 4 ms zijn verlopen
      TMR0 = Timer0Reload       ;herlaad Timer0
      INTCON_T0IF = low         ;reset Timer0 interrupt vlag
      ;laat dit 250 keer gebeuren voor 1 seconde
      TimerCounter = TimerCounter - 1
      if(TimerCounter == 0)then
        TimerCounter = TimerCounterReload       ;herlaad de teller
        TimePassed = high                       ;Meldt dat de tijd verstreken is
      end if
    end if
    if(PIR1_TMR2IF)then
      PIR1_TMR2IF = low
      LedBlinkTime = high
    end if
end procedure



;-------------------------------------------------------------------------------
forever loop
  if(TimePassed)then
    ;1 seconde is verstreken
    TimePassed = low        ;wis de vlag
    ledTmp = !LedTmp
    ;Zet de led uit wanneer die uit moet, aanzetten gebeurt elders
    if(!LedTmp)then Led = Off end if
  end if
  ;hier tijd voor andere zaken uit te voeren
  ;...
  ;Knipper de led tijdens de aan-periode
  if(LedTmp)then
    if(LedBlinkTime)then
      LedBlinkTime = low
      Led = !Led
    end if
  end if

end loop
;-------------------------------------------------------------------------------