Proton BASIC Compiler - Oscilloscope


  • PicŪ Basic


  • Oscilloscope-using 18F452

    I had some time on my hands & just for fun, I thought I'd do a (very basic) "scope" with Crownhill's development board.

    The screen updates with only the changes in the waveform so the display remains "flicker free" for anything reasonably periodic.

    The "scope" is good for < ~1Khz - did I say it was basic(!)

    The input signal is coupled directly into A0 through a 10uF cap.
    The onboard trim pot already present on the dev board is set to give a mid point reading of 128 from the A/D with no signal present. The signal swings around this.

    Altering the two commented 2500uS delays varies the timebase.

    The largest puzzle I have (still have) is writing a reliable (software) trigger routine. The +ve edge routine is fine with sine, triangle and any slowly changing waveforms which cross through zero, but square waves defeat it entirely as they pass through zero too rapidly.

    It's not a serious project, just some fun but I thought I'd share.
    Feel free to muck about with it. I'd be curious to see how fast it could be made to run & if the triggering routine can be improved!

    Chris
    (Cruster on the PDS Forum)

    Code:
     
     
    '****************************************************************
    '*  Name    : PIC Scope                                         *
    '*  Author  : Chris Rennie                                      *
    '*  Notice  : Copyright (c) 2006 Chris Rennie                   *
    '*          : All Rights Reserved                               *
    '*  Date    : 06/10/2006                                        *
    '*  Version : 1.0                                               *
    '*  Notes   : Uses two arrays to update display with only       *
    '*          : changes to the input waveform                     *
    '*          : Software driven triggering needs further          *
    '*          : "refinement"                                      *
    '****************************************************************
     
     
            Device = 18F452
            XTAL = 10
            Declare PLL_REQ = On    '40Mhz
     
       Include "PROTON18_G4.INT" 
     
    WARNINGS = OFF        
            OPTIMISE_BIT_TEST = On
            OPTIMISER_LEVEL = 6
     
           ADIN_RES 8 ' Set the resolution to 8
            ADIN_TAD FRC ' Choose the RC osc for ADC samples
           ADIN_STIME 80        ' Allow 80us for charge time
     
    ' Set up some Variables
            Dim Raw   As Byte 
           Dim temp          As Byte
            Dim x          As Byte       ' screen co-ords
            Dim myarray1[130] As Byte       ' A/D result storage array 1
            Dim myarray2[130] As Byte       ' A/D result storage array 2
            Dim i             As Byte       ' loop counter
            TRISA.0 = 1 ' Setup bit0 of PortA as an input 
            TRISC.1 = 0                     ' Setup bit1 of PortC as an output
            Symbol Trig = PORTC.1           ' Trigger Led for PortB.0
            Trig = 0
     
            DelayMS 100 ' Wait to stabilise
            Cls              ' Clear screen
     
            For i = 1 To 128                'initialise myarray1 with "0"
                myarray1[i] = 0
            Next i   
     
            For i = 1 To 128                'initialise myarray2 with "0"
                myarray2[i] = 0
            Next i     
     
            Print At 3,5, "PIC Scope"     ' Title... blah blah
            DelayMS 1000
            Print At 3,5, "         "
     
            Cls      
    Main:        
            GoSub Poztrigger:        'wait for +ve edge trigger
     
                Raw = Raw >> 2           'Raw = Raw/4 scale Raw for display
     
                For x = 1 To 128     ' Do A/D & fill myarray1
                myarray1[x] = Raw
                Raw = ADIn 0
                    Raw = Raw >> 2       'Raw = Raw/4 scale Raw for display
                    myarray1[x] = 64 - myarray1[x]
            '        DelayUS 2500      '"timebase" delay
                Next
     
                For x = 1 To 128     'draw updates only 
                    If myarray1[x] <> myarray2[x] Then         
                    UnPlot myarray2[x],x
                    Plot myarray1[x],x
                    EndIf
                Next  
     
            GoSub Poztrigger:       'wait for +ve edge trigger
     
                Raw = Raw >> 2          'Raw = Raw/4 scale Raw for display
     
                For x = 1 To 128     ' Do A/D & fill myarray2
                myarray2[x] = Raw
                Raw = ADIn 0
                    Raw = Raw >> 2       'Raw = Raw/4 scale Raw for display
                    myarray2[x] = 64 - myarray2[x]
              '      DelayUS 2500      '"timebase" delay
                Next
     
                For x = 1 To 128     'draw updates only 
                    If myarray1[x] <> myarray2[x] Then         
                    UnPlot myarray1[x],x
                    Plot myarray2[x],x
                    EndIf
                Next  
     
     
    GoTo Main:                  'repeat loop
     
     
     
    '//--------------------- SUBROUTINES
     
    Poztrigger:
            Raw = ADIn 0        'get reading
            Select Raw        
            Case 127 To 129 'near zero crossing ? (zero = 128)
                temp = Raw  'yes
                DelayUS 800     'wait a bit before next reading..
                Raw = ADIn 0    'get next reading
                    Select Raw  'compare with previous
                        Case >= temp + 4  'positive edge ?
                        GoTo exitloop:    'yes
                        Case temp-2 To temp+2         'zero(ish)
                        GoTo exitloop:  
                        Case Else                     'negative
                        Trig = 0
                        GoTo Poztrigger:  'start over
                    End Select            
            End Select           
            GoTo Poztrigger:
    exitloop:
    Trig = Trig ^ 1             ' Toggle trigger LED      
    Return              
     
    Include "font.inc"