Micro Developments Forum Index Micro Developments
Micro Controler and Electronic Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

AD conversion with PIC

 
Post new topic   Reply to topic    Micro Developments Forum Index -> PICBasic
View previous topic :: View next topic  
Author Message
chrismicro
Site Admin


Joined: 16 Jan 2008
Posts: 15
Location: South Africa

PostPosted: Fri Feb 22, 2008 10:32 pm    Post subject: AD conversion with PIC Reply with quote

Here is a sample program in PIC basic to do AD conversion it can be used on any PIC device with AD channels
Code:
'****************************************************************
'*  Name    : CarMonitor.PBP                                     *
'*  Author  : Chris Bezuidenhout                                *
'*  Notice  : Copyright (c) 2007 Micro Developments             *
'*          : All Rights Reserved                               *
'*  Date    : 2007/10/23                                        *
'*  Version : 1.1.1                                             *
'*  Notes   :                                                   *
'*          :                                                   *
'****************************************************************
INCLUDE "Modedefs.Bas"

       OSCCON = %01100000                  'set the intrc osc change bit 4 to logic  1 for 8 MHz
       OSCTUNE = %000000
       
DEFINE    OSC      4     ' Set IRC Frequency
DEFINE ADC_BITS 10       ' Set number of bits in result
DEFINE ADC_CLOCK 1    ' Set clock source (rc = 3)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in microseconds
         
IgnIn    var     word
Altin    var     word
Oilin    var     word
TempIn   var     word
ResIn    var     word               'Reserve input
IgnOut   var     portb.0
AltOut   var     portb.1
OilOut   var     portb.2
TempOut  var     portb.3
Buzzer   var     portb.4
ComO     var     portb.5
SigOut   var     portb.6
sp2      var     portb.7
Spin1    var     porta.6
Spin2    var     porta.7
X        var     byte
Y        var     byte


TRISA = %11111111          'Porta 0-4 all analog 5 input only pull high
TRISB = %00000000
ADCON0 = %11000001
Pause 2000 ' Wait .5 second
ADCON1 = %10000010 ' Set PORTA analog and RIGHT justify result



OPTION_REG = $7f        ' Enable PORTB pullups

pause 500
portb = 0
low Buzzer
' Test:
'low portb.6
'pause 100
'high portb.6
'high Ignout
'pause 500
'low ignout
'pause 500
'high altout
'pause 500
'low altout
'pause 500
'high Oilout
'pause 500
'low oilout
'pause 500
'high tempout
'pause 500
'low tempout
' pause 100
 
'goto test     
       
       
Main:       
       gosub CIgn
       gosub CAlt
       gosub COil
       gosub CTemp
       gosub Send
       
       
Ignition:
       if ignin = 0 then
       high Ignout
       gosub Send
       goto main
       endif
       if ignin > 300 then
       low ignout
       goto Alternator
       endif
'       if ignin > 0 and ignin <300> 300 then  Alternator
       
       for x = 1 to 2
       high Ignout
       high buzzer
       pause 200
       low buzzer
       pause 200
       next x
       pause 1000
       next y   
       
'       endif
Alternator:
           if ignin = 0 then goto main
       low ignout       
       if Altin > 1020 then
       high altout  '?????????Kalibreer
       for y = 1 to 3
       gosub CAlt
       gosub Send
       if altin <1020> 10 then    'This variable have to be calibrated to sensor unit
       high Oilout           'for a plain on/of it must be set to 0, the pot at minimum
       for y = 1 to 3        'if there is some contact resistance it can be calibrated acordingly
       gosub COil            'For a analog (resistive) sensor it have to be calibrated to
       gosub Send            'the sensor
       if oilin < 10 then Temperature    'So is this one
       for x = 1 to 6
       high buzzer
       pause 200
       low buzzer
       pause 200     
       next x
       pause 1000
       next y
       endif
       
Temperature:
            if ignin = 0 then goto main
       low Oilout
       if tempin <85> 85 then main
       for x = 1 to 8
       high buzzer
       pause 200
       low buzzer
       pause 200
       next x
       pause 1000 
       next y
       endif
       low tempout
   goto main         
CIgn:
       ADCON0 = %11000101             'start ADC  A0
       gosub notdone
       IgnIn.highbyte = ADRESH
       IgnIn.Lowbyte = ADRESL
       write 0,ignin.highbyte
       write 1,ignin.lowbyte
       return
CAlt:       
       ADCON0 = %11001101             'start ADC A1
       gosub notdone
       altin.highbyte = ADRESH
       altin.Lowbyte = ADRESL
       write 2,Altin.highbyte
       write 3,Altin.lowbyte
       return
COil:       
       ADCON0 = %11010101             'start ADC A2
       gosub notdone
       oilin.highbyte = ADRESH
       oilin.Lowbyte = ADRESL
       write 4,oilin.highbyte
       write 5,oilin.lowbyte
       return
CTemp:       
       ADCON0 = %11011101             'start ADC A3
       gosub notdone
       TempIn.highbyte = ADRESH
       TempIn.Lowbyte = ADRESL
       write 6,tempin.highbyte
       write 7,tempin.lowbyte
       return
CRes:       
       ADCON0 = %11100101             'start ADC A4
       gosub notdone
       resin.highbyte = ADRESH
       resin.Lowbyte = ADRESL
       Return       
Send:
       serout SigOut, N2400,["A",IgnIn.highbyte,IgnIn.lowbyte,altin.highbyte,altin.lowbyte,Oilin.highbyte,Oilin.lowbyte,Tempin.highbyte,Tempin.lowbyte,resin.highbyte,resin.lowbyte]
       return
notdone: Pause 5
       If ADCON0.2 = 1 Then notdone' Wait for low on bit-2 of ADCON0, conversion finished
       return
end

_________________
To become free, the Boervolk have to get the Afrikaner out of his soul and reject it, to regain his Boer pride.
Om vry te word, moet die Boervolk eers die Afrikaner uit sy siel haal en verwerp, om sy Boere trots te herwin.
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Micro Developments Forum Index -> PICBasic All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

ip-location
Powered by phpBB © 2001, 2005 phpBB Group