
{{DS1302 Real Time Clock routines}}

CON
  RTC_clk       = 13
  RTC_rst       = 14
  RTC_io        = 15

  wr_year       = %1000_1100
  wr_month      = %1000_1000
  wr_date       = %1000_0110
  wr_hour       = %1000_0100
  wr_minute     = %1000_0010
  wr_second     = %1000_0000
  wr_charge     = %1001_0000
  wr_diode      = %1010_0101    'one diode, 2K ohm resistor
VAR

OBJ

PUB Start : okay

PUB Stop

PUB read(cmd_s)| rx_byte,temp

  dira[RTC_clk..RTC_rst]~~       'set RTC comm bits to output 
  outa[RTC_clk]~                'initialize RCT clock to low
  outa[RTC_rst]~                'initialize RCT RST to low
  dira[RTC_io]~~
  outa[RTC_rst]~~
  rx_byte:=0
  repeat 8                      'send 8 bits of command
    outa[RTC_io]~               'assume bit to send is low
    
    if cmd_s & %0000_0001                'check if bit to send is high
      outa[RTC_io]~~            'if it is high, set bit to send high
    
    outa[RTC_clk]~~
    outa[RTC_clk]~              'clock in bit
    cmd_s>>=1                   'shift cmd one bit for next send
  outa[RTC_io]~
  dira[RTC_io]~                 'make I/O input
  
  repeat 8                      'get 8 bits from RTC
    rx_byte>>=1                   'shift bits 1 right LSB send first
    
    if ina[RTC_io]                 'check in bit is high
      rx_byte := rx_byte | %1000_0000       'if high, set MSB to high which will rotate right
    outa[RTC_clk]~~
    outa[RTC_clk]~
   'outa[RTC_clk]~~             'else bit is left low
    'outa[RTC_clk]~              'clock in bit
    
  outa[RTC_rst]~
  temp:=rx_byte >> 4  
  temp:=temp*10
  result:=temp+(rx_byte & %00001111)

PUB clock_stop | cmd_s
   RTC_write(%1000_0000,128)

PUB clock_start
    RTC_write(%1000_0000,0)


 
PUB set_RTC(year,month,day,hour,minute,second)
   RTC_write(wr_year,year)
   RTC_write(wr_month,month)
   RTC_write(wr_date,day)
   RTC_write(wr_hour,hour)
   RTC_write(wr_minute,minute)
   RTC_write(wr_second,second)
   RTC_write(wr_charge,wr_diode)
    
 
PUB RTC_write(cmd_s, data)      'used to set one clock parameter
  data := bcd(data)             'convert DEC data into BCD data
  dira[RTC_clk..RTC_rst]~~       'set RTC comm bits to output 
  outa[RTC_clk]~                'initialize RCT clock to low
  outa[RTC_rst]~                'initialize RCT RST to low
  dira[RTC_io]~~                'make I/O output
  outa[RTC_rst]~~               'start to send data
 
  repeat 8                      'send 8 bits of command
    outa[RTC_io]~               'assume bit to send is low
    
    if cmd_s & %0000_0001       'check if bit to send is high
      outa[RTC_io]~~            'if it is high, set bit to send high
    
    outa[RTC_clk]~~
    outa[RTC_clk]~              'clock in bit
    cmd_s>>=1                   'shift cmd one bit for next send
  outa[RTC_io]~
    
  repeat 8                      'sending data bits to RTC
    outa[RTC_io]~               'assume bit to send is low
    if data & %0000_0001                'check if bit to send is high
      outa[RTC_io]~~            'if it is high, set bit to send high
    outa[RTC_clk]~~             'clock in bit
    outa[RTC_clk]~              
    data >>=1                   'prepare to send next bit
    
  outa[RTC_rst]~                'stop sending data
  

Pub bcd(num) | temp1             'converts DEC time byte to BCD for programming DS1302
  temp1:= num/10                 'get 100's digits
  num := num - temp1*10          'get 10's digits
  temp1 <<=4                     'shift 100's digits to upper nibble
  result:= temp1+num             'add back in 10's digits in lower nibble
    
  