program test_ronald3

dim received_byte as byte
dim received_byte2 as byte[50]
'dim received_byte3 as byte[90]
dim getal as byte
dim i as word
dim counter as float

dim aantalhoeken as integer
dim tijd_per_hoek as float

dim geweest_eersteif as boolean
dim op_sensor as boolean

dim bit1 as integer
dim bit2 as integer
dim bit3 as integer
dim bit4 as integer
dim bit5 as integer
dim bit6 as integer
dim bit7 as integer
dim bit8 as integer


dim j as float








sub procedure interrupt
   counter = counter+1          ' Increment value of counter on every interrupt
   TMR0   =  10
   INTCON = $20          ' Set T0IE, claer T0IF

 end sub














main:


  OPTION_REG = $87       ' Assign prescaler to TMR0
  counter = 0            ' Initialize counter
  TMR0 = 10
  INTCON = $A0           ' Enable TMRO interrupt




aantalhoeken = 100




Usart_Init(600)                 ' Initialize USART module

TRISB=%00000001
TRISC=%11000000
TRISD=%00000000
TRISA=%000000
TRISE=%00000000




PORTA = %000000
PORTB = %0000000
PORTC=%00000000
PORTD = %00000000



counter = 0
tijd_per_hoek = 100
i=3







PORTD=%11111111

Vdelay_ms(3000)

PORTD=%00000000

while true


  'counter = counter+1
  
  

      if op_sensor = false then
         if PORTB.0 = 1 then


           tijd_per_hoek  = counter / aantalhoeken
           counter = 0
           op_sensor= true


         end if
      end if

       if PORTB.0 = 0 then
            op_sensor = false
       end if
  
  
  
  
  

      if Usart_Data_Ready = 1 then
      i=3
      while i<303
         

         
          while    Usart_Data_Ready = 1



                  if i<255       then

                  received_byte = Usart_Read  ' Read received data
                  Eeprom_Write(i,received_byte)

                  end if
                  
                  if i>254  then


                        received_byte2[i-255] = Usart_Read  ' Read received data


                  end if

              i=i+1

           wend

       wend

       end if




       
       
       
       'PORTD=%00000100
       
        'if (counter mod tijd_per_hoek = 0) then

            j = ceil(counter/tijd_per_hoek)



            if j < 85 then

            getal=   Eeprom_Read((j*3)+1)

            end if

            


            if j > 84 then

            getal=   received_byte2[(j*3)+1-255]
            
            end if







            if getal >   127  then

                     bit8 = 1
                     getal= getal-128
            else   bit8=0
            end  if


            if getal > 63      then
                     bit7=1
                     getal =  getal - 64
            else bit7=0
            end if

            if getal > 31     then
                      bit6=1
                      getal =  getal - 32
            else bit6=0
            end if

            if getal > 15     then
                       bit5=1
                       getal =  getal - 16
            else bit5=0
            end if

            if getal > 7       then
                       bit4=1
                       getal =  getal - 8
            else bit4=0
            end if

            if getal > 3        then
                       bit3=1
                       getal =  getal - 4
            else bit3=0
            end if

            if getal > 1         then
                        bit2=1
                        getal =  getal - 2
            else bit2=0
            end if

            if getal > 0        then
                        bit1=1
            else bit1=0
            end if


            PORTA = bit5 + (bit6 * 2) + (bit7 * 4) + (bit8 * 8)
            PORTB = 0    + (bit2 * 2) + (bit3 * 4) + (bit4 * 8)  + (bit1 * 16)


             if op_sensor = false then
             if PORTB.0 = 1 then


           tijd_per_hoek  = counter / aantalhoeken
           counter = 0
           op_sensor= true


           end if
           end if

       if PORTB.0 = 0 then
            op_sensor = false
       end if
       
       counter = counter+1




              if j < 85 then

            getal=   Eeprom_Read((j*3)+2)

            end if




            if j > 84 then

            getal=   received_byte2[(j*3)+2-255]

            end if




            if getal >   127      then

                     bit8 = 1
                     getal= getal-128
            else   bit8=0
            end  if


            if getal > 63       then
                     bit7=1
                     getal =  getal - 64
            else bit7=0
            end if

            if getal > 31       then
                      bit6=1
                      getal =  getal - 32
            else bit6=0
            end if

            if getal > 15        then
                       bit5=1
                       getal =  getal - 16
            else bit5=0
            end if

            if getal > 7         then
                       bit4=1
                       getal =  getal - 8
            else bit4=0
            end if

            if getal > 3         then
                       bit3=1
                       getal =  getal - 4
            else bit3=0
            end if

            if getal > 1         then
                        bit2=1
                        getal =  getal - 2
            else bit2=0
            end if

            if getal > 0         then
                        bit1=1
            else bit1=0
            end if




            PORTE = bit8+ (bit7 * 2)
            PORTC = bit1 + (bit2 * 2) + (bit3 * 4) + (bit4 * 8) + (bit5 * 16) + (bit6 * 32)



              if j < 85 then

            getal=   Eeprom_Read((j*3))

            end if

            if j > 84 then

            getal=   received_byte2[(j*3)-255]

            end if
            
            
            PORTD = getal
            
            
            
            







        'end if
wend

end.
