{

                                ********************************************

                       HM55B Compass Module and SPD-015-G-2 pressure sensor demo

                                ********************************************

                       made from code by Beau Schwabe (Parallax) and Dave Scanlan

                                ********************************************

                       with changes made by Jacob Ancker        

 

         ┌────┬┬────┐

  P5 ──│1  6│── +5V       P3 = Enable

           ├┴──┴┤                 P4 = Clock

  P5 ──│2 │ /\ │ 5│── P3        P5 = Data

           /  \│ 

 VSS ──│3 └────┘ 4│── P4

         └──────────┘

                               

}

CON

  _CLKMODE = XTAL1 + PLL16X

  _XINFREQ = 5_000_000

    _stack = ($3000 + $3000 + 100) >> 2                   'accommodate display memory and stack

 

   x_tiles = 16

   y_tiles = 12

 

    Enable = 3

     Clock = 4

      Data = 5

 

paramcount = 14

 

display_base = $5000

bitmap_base = $2000

 

  OUT = %1

  IN = %0

  HIGH = 1

  LOW = 0

 

  ADC_CS = 0

  ADC_Data = 1

  ADC_CLK = 2

 

VAR

    long  tv_status     '0/1/2 = off/visible/invisible           read-only

    long  tv_enable     '0/? = off/on                            write-only

    long  tv_pins       '%ppmmm = pins                           write-only

    long  tv_mode       '%ccinp = chroma,interlace,ntsc/pal,swap write-only

    long  tv_screen     'pointer to screen (words)               write-only

    long  tv_colors     'pointer to colors (longs)               write-only              

    long  tv_hc         'horizontal cells                        write-only

    long  tv_vc         'vertical cells                          write-only

    long  tv_hx         'horizontal cell expansion               write-only

    long  tv_vx         'vertical cell expansion                 write-only

    long  tv_ho         'horizontal offset                       write-only

    long  tv_vo         'vertical offset                         write-only

    long  tv_broadcast  'broadcast frequency (Hz)                write-only

    long  tv_auralcog   'aural fm cog                            write-only

 

    word  screen[x_tiles * y_tiles]

    long  colors[64]

    Byte  ADC_Result

    long  thetatoth

    word  thetatotl

    word  thetamean

    word  thetameanl

    word  thetameanh

    word  j

    word  delta

 

OBJ

    tv    :     "tv"

    gr    :     "graphics_mini"

    HM55B :     "HM55B Compass Module Asm"

    BS2:        "BS2_Shiftin"

   

PUB DEMO | i,dx,dy,theta,x,y

 

    'start tv

    longmove(@tv_status, @tvparams, paramcount)

    tv_screen := @screen

    tv_colors := @colors

    tv.start(@tv_status)

 

    'init colors

    repeat i from 0 to 63

      colors[i] := $00001010 * (5+4) & $F + $2B060C02

 

    'init tile screen

    repeat dx from 0 to tv_hc - 1

      repeat dy from 0 to tv_vc - 1

        screen[dy * tv_hc + dx] := display_base >> 6 + dy + dx * tv_vc + ((dy & $3F) << 10)

 

    'start and setup graphics

    gr.start

    gr.setup(16, 12, 128, 96, bitmap_base)

 

    'start and setup Compass

    HM55B.start(Enable,Clock,Data)

    dira[16]~~

    repeat

      gr.clear                                            'Clear graphics screen

 

      gr.colorwidth(2, 0)                                 'Set Color and Width

      gr.textmode(1,1,7,%0100)                            'Set text mode

      gr.text(0,82,string(" Sealion    Kurs          Djup"))       'Display Header Text

 

                                                        'Draw compass

      gr.arc(0, 0, 60, 60, 0, 23, 360, 0)               '     Great circle

 

 

 

      thetatoth := 0

      thetatotl := 0

      thetameanh := 0

      thetameanl := 0

      j := 0

      repeat i from 0 to 20                             '     Make mean value and

        theta := HM55B.theta                            '     compensate for zero passage

        IF theta < 4096 AND theta > 0

          j := j + 1

          !Outa[16]

          thetatotl := thetatotl + theta                '     l means low

          theta := 0                                    '     h means high

        thetatoth := theta + thetatoth

        waitcnt(2_000_000 + cnt)

      thetameanh := thetatoth/(21-j)

      thetameanl := thetatotl/j

      IF (thetameanh-thetameanl)>7500 AND j>0

        thetamean := (thetatoth+thetatotl)/21 + 8192*j/21

      ELSE

        thetamean := (thetatoth+thetatotl)/21

 

 

      repeat i from 0 to 32                             '     Draw ticks around Great circle

        gr.arc(0, 0, 60, 60, i*256-thetamean, 0, 1, 0)

        gr.arc(0, 0, 55, 55, i*256-thetamean, 0, 1, 1)

 

      gr.textmode(1,1,7,%0000)

           

{{      gr.text(-2, 60, string("0"))                     '     Associate Degree ticks

      gr.text(42, 42, string("45"))

      gr.text(64, -8, string("90"))

      gr.text(45, -55, string("135"))

      gr.text(-9, -76, string("180"))

      gr.text(-65, -55, string("225"))

      gr.text(-83, -8, string("270")) 

      gr.text(-62, 42, string("315"))                         }}

 

      gr.textarc(-3, -8, 73, 72, 2048-thetamean, string("0"))

      gr.textarc(-8, -8, 73, 72, 3072-thetamean, string("315"))

      gr.textarc(-8, -8, 73, 72, 4096-thetamean, string("270"))

      gr.textarc(-10, -8, 73, 72, 5120-thetamean, string("225"))

      gr.textarc(-10, -8, 73, 72, 6144-thetamean, string("180"))

      gr.textarc(-10, -8, 73, 72, 7168-thetamean, string("135"))

      gr.textarc(-10, -8, 73, 72, 8192-thetamean, string("90"))

      gr.textarc(-10, -8, 73, 72, 9216-thetamean, string("45"))                 

 

{{      gr.text(-2, 38, string("N"))                      'Spell the NEWS

      gr.text(45, -8, string("E"))

      gr.text(-50, -8, string("W"))

      gr.text(-2, -55, string("S"))                            }}

 

      gr.textarc(-2, -8, 45, 45, 2048-thetamean, string("N"))

      gr.textarc(-2, -8, 45, 45, 4096-thetamean, string("W"))

      gr.textarc(-2, -8, 45, 45, 6144-thetamean, string("S"))

      gr.textarc(-2, -8, 45, 45, 8192-thetamean, string("E"))

           

      gr.arc(0, 0, 50, 50, 2048, 0, 1, 0)        'Draw needle

      gr.arc(0, 0, 50, 50, 6144, 0, 1, 1)

      gr.arc(0, 0, 5, 5, 0, 0, 1, 0)               '     Draw needle cross

      gr.arc(0, 0, 5, 5, 4096, 0, 1, 1)

      gr.arc(0, 0, 40, 40, 2048-150, 0, 1, 0)    '     Draw needle arrow

      gr.arc(0, 0, 50, 50, 2048, 0, 1, 1)     

      gr.arc(0, 0, 40, 40, 2048+150, 0, 1, 1)

 

      A2D_Conversion

      gr.text(100, 60, string("4 m"))

      gr.text(100, 25, string("3 m"))

      gr.text(100, -10, string("2 m"))

      gr.text(100, -45, string("1 m"))

      gr.text(100, -80, string("0 m"))

      gr.arc(90, -80, 0, 0, 0, 0, 1, 0)                    'Draw staple

      gr.arc(90, -80, ADC_Result * 1000/255, ADC_Result * 1000/255, 2048, 0, 1, 1)              

 

     

      gr.copy(display_base)                               'copy bitmap to display

      waitcnt(2_000_000 + cnt)

 

PRI A2D_Conversion

    Dira[ADC_CS] := OUT

    Dira[ADC_Data] := IN

    Dira[ADC_CLK] := OUT

    Outa[ADC_CS] := LOW

    ADC_Result := BS2.Shiftin(ADC_DATA, ADC_CLK,BS2#MSBPOST,9)

    Outa[ADC_CS] := HIGH                               

 

DAT

 

Zero                    word    $30

DP                      word    $2E

Hyphen                  word    $2D

 

tvparams                long    0               'status

                        long    1               'enable

                        long    %001_0101       'pins

                        long    %0001           'mode  PAL

                        long    0               'screen

                        long    0               'colors

                        long    x_tiles         'hc

                        long    y_tiles         'vc

                        long    10              'hx

                        long    1               'vx

                        long    0               'ho

                        long    0               'vo

                        long    0               'broadcast

                        long    0               'auralcog