Initialising the ADC

Discuss the Firewing language

Initialising the ADC

Postby Timbo » Fri May 15, 2015 9:24 am

Hello

This seems to have worked before. I post a question then answer it myself.

I'm doing some Adc reads and my VSM is showing no result and also saying the read time is to short. So I look through the ADC module and it has all the code to hand the freqs and TAD etc correctly.

But running the VSM I see that the code is never run.

The PWM module is in the list of code files but for some reason the ADC module is not.

This is on the firewing18 board.

Obviously there is code being generated to do the read but that is in line and does not seem to be in the adc module.

How do I force the ADC set up code to be run?

Thanks
Timbo
 
Posts: 93
Joined: Fri May 03, 2013 7:51 pm

Re: Initialising the ADC

Postby Coccoliso » Fri May 15, 2015 11:54 am

Hello,
can you post source code?
User avatar
Coccoliso
 
Posts: 177
Joined: Sat Sep 27, 2014 10:02 am

Re: Initialising the ADC

Postby Timbo » Fri May 15, 2015 12:26 pm

Hi,

Sorry did not get round to isolating the appropriate section.

This is work in progress

Code: Select all
'*****************************************************************************
'*  Name    : UNTITLED.1                                                     *
'*  Author  : [select VIEW...EDITOR OPTIONS]                                 *
'*  Notice  : Copyright (c) 2015 [select VIEW...EDITOR OPTIONS]              *
'*          : All Rights Reserved                                            *
'*  Date    : 10/05/2015                                                     *
'*  Version : 1.0                                                            *
'*  Notes   :                                                                *
'*          :                                                                *
'*****************************************************************************

' Imports section...

imports Tick

// A/D port defs
Dim WLevel as A0
DIm WCPress as A1
Dim WLLow as A2
Dim WLHigh as A3
Dim BSen as A4
Dim TempSensor as A5

// Water detection levels

Const WaterTransitionLevel = 600   // Level that trips between a seeinf water and no water


// Pressure sensor inputs
Dim WLevelRaw as Ushort
Dim PreCapPressureRaw as Ushort


// Optical Sensors and levels
Dim WLevelLowRaw as Ushort
Dim WLevelHighRaw as Ushort
Dim BubbleRaw as Ushort
Dim WaterOptoTransitionLevel as Ushort  = 600 /// temp setting <<<<<<<<<<<<<<<<<

// Boolean status of the water seen across sensor
Dim WaterLevelHigh as Boolean
dim WaterLevelLow as Boolean
Dim BubbleSeen as Boolean

// Temperature Sensor
Dim TempSensorRaw as Ushort


// Solinods
Dim InletSolinoid as D0
Dim DrainSolinoid As D1


// Dims for the Raw Level values

   Dim SampleLevelToBuffer as bit
   Dim RawUpperBufferCounter as byte           // The element in the array pointer
   Const RawUpperBufferNo = 5
   Dim RawUpperLevelArray(RawUpperBufferNo) as Ushort         // The Array used
   Dim LowLevelFlag as bit                      // Flag used to watch for an up ward going water level to save the raw


   // Volume and flow level D
   Dim RawLevelStart as Ushort
   Dim RawLevelTop as Ushort
   Dim RawVolumeRange as Ushort   
   
   // Subs and Functions
   
   // Valve control subs
   
   Inline Sub OpenDrain()
      High(DrainSolinoid)
   end sub
   
   Inline Sub CloseDrain()
      Low(DrainSolinoid)
   End Sub
   
   Inline Sub OpenInlet()
      High(InletSolinoid)
   End Sub
   
   Inline Sub CloseInlet()
      Low(InletSolinoid)
   End Sub                 
   
   Inline Function IsThereWater(InputSensor as ushort) as boolean
      If InputSensor < WaterOptoTransitionLevel then
         Return True
      Else
         Return False
      End if
   End Function
   
   
   
   

sub OnTickEvent() handles Tick.OnTick

   

   // Read the Pressure Levels
   WLevelRaw = Adc.Read(WLevel)     
   PreCapPressureRaw = Adc.Read(WCPress)
   
   // Read the Bubble sense inputs
   WLevelLowRaw = Adc.Read(WLLow)
   WLevelHighRaw = Adc.Read(WLHigh)
   BubbleRaw = Adc.Read(BSen) 
   
   // Read the Temp sensor
   TempSensorRaw = Adc.Read(TempSensor) 
                                   
   
   // Process the Bubble Level inputs
   
   
   // Check the Low Level Opto
   If IsThereWater(WLevelLowRaw) then                    // If we see water
      WaterLevelLow = False                              // Flag it up to say we have water flag is to say water level is not below sensor 
      If LowLevelFlag = 0  then                          // If last time the level was below the sensor
          RawLevelStart = WLevelRaw                      // Record the level at which we see the water
          LowLevelFlag = 1                               // Change the level flag
      End if
   Else                                                  // Otherwise
      WaterLevelLow = True                               // Flag to say no water so bottom sensor hit
   End if                       
       
   
   // Check the High Level Opto
   If IsThereWater(WLevelHighRaw) then                   // If the level indicates there is water sensed   
      WaterLevelHigh = True                              // Mark it so
      If SampleLevelToBuffer = 0 then                    // First time we see it go high this time we save the value to the buffer
         SampleLevelToBuffer = 1
         RawUpperLevelArray(RawUpperBufferCounter) = WLevelRaw   
         
         // Inc the counter and roll over if need be
         RawUpperBufferCounter += 1             
         if RawUpperBufferCounter >= RawUpperBufferNo then
            RawUpperBufferCounter = 0
         End if
      End if
   Else                                                  // If there is no water then..
         WaterLevelHigh = False   
                 
   End if
         
   // Check the Bubble opto
   If IsThereWater(BubbleRaw) then
      BubbleSeen = False
   Else
      BubbleSeen = true
   End if

   
   
   // State Machines controlling the system
   
   dim MainState as byte
   Const MStateOff = 0                   // This State has the unit in Off mode
   Const MStateStartUp = 1               // Starting up
   Const MStateCycle = 2                 // Cycling state
   Const MStateSample = 3                // One Shot sample
   Const MStateShutDown = 4              // Shutting down
   
   Dim SubStateMCycle as Byte            // Sub State machine inside The Main cycleing state
   Const SCFilling = 0                  // In filling phase
   Const SCEmptying = 1                 // In Emptying Phase
   
   Select MainState
      Case MStateOff
         CloseDrain
         CloseDrain
      case MStateStartUp
         OpenDrain
         OpenInlet
         
      Case MStateCycle
         
               
      Case MStateSample
         
      Case MStateShutDown
     
         
   end select
         
 
         
     
   
end sub

 Sub InitialiseControlInterrupt()
     
      WaterOptoTransitionLevel = 600        // temp config of water level
      // Read the Stored Value for the current Top water level And load it into the array

         
      For RawUpperBufferCounter = 0 to RawUpperBufferNo    // cycle through the whole array
          //RawUpperLevelArray(RawUpperBufferCounter) = VALUE
      Next
      RawUpperBufferCounter = 0             
     
     
      // Loads of other code to set up here as well
     
 End Sub
 
 
     

sub Main()

   'loop forever...
   while true
   end while
End Sub
Timbo
 
Posts: 93
Joined: Fri May 03, 2013 7:51 pm

Re: Initialising the ADC

Postby Timbo » Fri May 15, 2015 1:27 pm

There must be a clash somewhere in my code as

Dim Temp as Ushort = adc.read(A0)

Works fine
Timbo
 
Posts: 93
Joined: Fri May 03, 2013 7:51 pm

Re: Initialising the ADC

Postby Timbo » Fri May 15, 2015 1:32 pm

Ok it seems that it will only set up the Adc properly if the code is in the main loop or it could be that it is not seeing the .read in the event handler

But I have a work around I just added that line to my main code to do a read and it set up the adc properly
Timbo
 
Posts: 93
Joined: Fri May 03, 2013 7:51 pm

Re: Initialising the ADC

Postby Coccoliso » Fri May 15, 2015 1:39 pm

Tick.bas does not have the event OnTime so comment Handles

Code: Select all
Sub OnTickEvent() ' handles Tick.OnTick


.. add Tick calculation functions ..

Code: Select all
private _NextTimeoutValue               as uinteger = 0
private _LastTime                       as uinteger = 0
private _TimeInSecs                     as byte = 1 

'****************************************************************************
'* Name    : SetNextTimeToDoSomething                                       *
'* Purpose : Set next timeout value                                         *
'****************************************************************************
private sub SetNextTimeToDoSomething(value as uinteger)
   _NextTimeoutValue = value
   _LastTime = tick.ms
end sub

'****************************************************************************
'* Name    : IsTimeToDoSomething                                            *
'* Purpose : Check to see if timeout ocurred                                *
'****************************************************************************
private function IsTimeToDoSomething() as boolean
   return (tick.ms - _LastTime) >= _NextTimeoutValue
end function


.. in main test timeout period
Code: Select all
Sub Main()
   'loop forever...
   while true
      If IsTimeToDoSomething() Then
        OnTickEvent()
        SetNextTimeToDoSomething((_TimeInSecs * 1000))
      End If
   end while
End Sub
User avatar
Coccoliso
 
Posts: 177
Joined: Sat Sep 27, 2014 10:02 am

Re: Initialising the ADC

Postby Timbo » Fri May 15, 2015 1:42 pm

Hi

I did add an event handler to my tick module, sorry I forgot about that

Code: Select all
' testing against nothing will remove all code inside the
   ' if statement if no event handler is assigned in the main code
   ' block - so is optional, but recommended...
   if OnTick <> nothing then       
      if _time mod 10 = 0 then       ' trigger every 10 ms
         RaiseEvent OnTick()         ' raise event
      end if
   end if


Thanks for the other stuff
Timbo
 
Posts: 93
Joined: Fri May 03, 2013 7:51 pm


Return to Language

Who is online

Users browsing this forum: No registered users and 2 guests

cron

x