PDA

View Full Version : How to read analog signals?



thm ov3rkill
- 21st February 2009, 02:46
Well I wanted to know how to read analog signal and if its true i would want it to flash a led or pulse it. Would this be possible also what commands would i use with this thanks guys!

mackrackit
- 21st February 2009, 03:02
Well I wanted to know how to read analog signal and if its true i would want it to flash a led or pulse it. Would this be possible also what commands would i use with this thanks guys!
This might help
http://www.google.com/custom?q=ADC&sa=Google+Search&cof=S%3Ahttp%3A%2F%2Fwww.picbasic.co.uk%2Fforum%3B AH%3Aleft%3BLH%3A37%3BL%3Ahttp%3A%2F%2Fwww.crownhi ll.co.uk%2Flogo.gif%3BLW%3A174%3BAWFID%3Af08c2ab0d 3333e22%3B&domains=picbasic.co.uk&sitesearch=picbasic.co.uk

thm ov3rkill
- 21st February 2009, 13:11
Hi , Mack

I was wanting to do this on the small 12f683 package I couldent really find it in the search results.

Bruce
- 21st February 2009, 15:21
Here's an example of reading an analog voltage with a 12F683. I'll let you figure out the LED part...;}


@ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF

DEFINE OSC 8 ' Internal 8MHz
DEFINE DEBUG_REG GPIO ' Define DEBUG port
DEFINE DEBUG_BIT 1 ' Set GPIO.1 as DEBUG serial output pin
DEFINE DEBUG_BAUD 9600 ' DEBUG baud rate = 9600 bps
DEFINE DEBUG_MODE 1 ' 1 = inverted, 0 = true

DEFINE ADC_BITS 10 ' 10-bit resolution
DEFINE ADC_CLOCK 5 ' Set clock source to Frc/16
DEFINE ADC_SAMPLEUS 50 ' Sample time in uS before A/D conversion is started
Quanta CON 1251 ' For 10-bit A/D +Vref = 5V : 5V/1023*256=1.251=Quanta

' PIN# NAME USE & CONNECTION
' 1 Vdd +5VDC power supply
' 2 GPIO.5 N/C (N/C = NO CONNECTION)
' 3 GPIO.4 N/C
' 4 GPIO.3 N/C
' 5 GPIO.2 N/C
' 6 GPIO.1 Serial output pin to PC serial port RX pin
' 7 GPIO.0 A/D input pin
' 8 Vss Power supply ground (also connect to DB-9 pin #5)
'-------------------------------------------------------------------------

CMCON0 = 7 ' Disable comparator
ANSEL = %00000001 ' Set GPIO.0 to A/D
ADCON0.7 = 1 ' Right justify for 10-bit
TRISIO = %00000001 ' GP.1 = serial out, GPIO.0 = A/D in, rest outputs
OSCCON = %01110000 ' Internal 8MHz osc
ADval VAR WORD ' A/D conversion result

MAIN:
ADCIN 0, ADval ' Read A/D
ADval = ADval */ Quanta
DEBUG DEC ADval DIG 3,".",DEC3 ADval,"~"
PAUSE 200
GOTO MAIN

END

thm ov3rkill
- 22nd February 2009, 01:12
Hey bruce thanks a lot Bielive it or not i learned a lot form that small little part I as wondering if You can read the analog and pulse low on the same pin?

thm ov3rkill
- 23rd February 2009, 03:33
I did a little bit more research on this But its pretty hard what i wanted to do is read a analog voltage off a circiut board andif that reading is true I want it to pulse low through the same pin reading the voltage will this be possible?


edit: bruce can you help me explain what ever line means it will give me a betetr understanding of how to see if i can use the if then statment to flash the led

Archangel
- 23rd February 2009, 08:31
I did a little bit more research on this But its pretty hard what i wanted to do is read a analog voltage off a circiut board andif that reading is true I want it to pulse low through the same pin reading the voltage will this be possible?


edit: bruce can you help me explain what ever line means it will give me a betetr understanding of how to see if i can use the if then statment to flash the ledHere is the hangup in your question:In English, True / False statements are digital statements, so when you say, "and if that reading is true ". . . we are not sure what you want "true" to represent. Please explain further . . . Does " true" represent a specific voltage, or just any voltage? Are you trying to test for a specific voltage? Bruce gave you the code above to receive the value of the voltage, so what do you want to do with that information?

thm ov3rkill
- 23rd February 2009, 23:23
well joe thanks for replying what I want to do is... I want to be able to test a analog voltage peroid. And if that analog volatge is in fat true then I want it to pulse low out of the same pin testing that voltage.

edit i dont want to see how much the voltage is i want to see when it read analog

LinkMTech
- 24th February 2009, 00:46
edit i dont want to see how much the voltage is i want to see when it read analog

I'm taking a stab at this. I believe when you say "true" you are wanting to say "through", meaning "complete" or "done", is this correct?
And if that is the case, then it can be done but your circuit will have to be made to monitor your analog signal and have an LED at the same pin.
It would be simpler to use one pin for the analog signal input then turn ON an LED on another pin (setup as an output) when the A/D is completed or "through".

Archangel
- 24th February 2009, 05:36
I want to be able to test a analog voltage peroid.

Ok you want to test an analog voltage, what are you testing for? Do you mean any value greater than zero ?
And if that analog volatge is in fat true then I want it to pulse low out of the same pin testing that voltage.

edit i dont want to see how much the voltage is i want to see when it read analogSo if I understand correctly . . .

If voltage is > 0 then
PortA.0 = 0 'set port latch to low status before enabling tris
TRISA.0 = 0 ' Turn input into output enabling port to pull low.
else
TrisA.0 = 1 ' Keep port in high impedance input status
endif

Where PortA.0 in this example is your analog input / output port

thm ov3rkill
- 25th February 2009, 02:50
Hey joe Well this is the thing I want to setup a little project . You may remmeber me from the xbox 360 thread. But i didint get a chance on that. So i wanted to seeif i can acually get to read the trigger press and pulse out.


Also I wanted this to be done on a pic 12f683

Well The reason i want it on the same pin because I dont think i can kill 2 birds with one stone meaning as There is only on spot to read and fire of the controller which is called the wiper. I have seen this done before. So lets say i pull the trigger the pic reads the analog then it pulses low.

Archangel
- 25th February 2009, 03:20
Hey joe Well this is the thing I want to setup a little project . You may remmeber me from the xbox 360 thread. But i didint get a chance on that. So i wanted to seeif i can acually get to read the trigger press and pulse out.


Also I wanted this to be done on a pic 12f683

Well The reason i want it on the same pin because I dont think i can kill 2 birds with one stone meaning as There is only on spot to read and fire of the controller which is called the wiper. I have seen this done before. So lets say i pull the trigger the pic reads the analog then it pulses low.I did not forget . . . Here is what I am having trouble understanding "i pull the trigger the pic reads the analog then it pulses low" Exactly what do you mean by "the PIC reads the analog", What exactly must it see, to then make the pic pulse low? What state is the pin in when the condition is NOT met ? What state is it in when you want the pic to fire ?

Acetronics2
- 25th February 2009, 09:33
Hi Joe,

What I understand here is ...

IF input pin level is LOW, the Pic must Hold The pin LOW ...

Question ( useful ! )

When must the Pic release the pin ???


In clear Words ...

I pull the trigger of my gun ... and I want it to make a machine gun ... instead of single shot.

OR

I Want to transform an astable button into a bistable one ( ? )

Or something like that....


Looks using Comparator offers more Here ... Even if a Timer might be fired ...
12F629 ???

Alain

thm ov3rkill
- 27th February 2009, 01:50
hey Thanks ace.

Well again in words. The xbox 360 controller triggers have a analog signal when you press then down thats what it fires off...

so lets say I want to pull my trigger. if the pic reads( or sees what ever you want to call it) the analog signal that just came trough when i pressed the trigger it makes that same pin into a output and starts to pulses low and when i let go of it it stops and just goes back up top^^=].

LinkMTech
- 1st March 2009, 21:02
Here's something untested you can try. Two methods of rapid fire but you also need to add a 330 Ohm resistor between the button and the input so the PIC can sense a press then drive the same pin as an output even if you keep holding the button low.
I used the names pulse_fire and rapid_fire just to make them seperate.
If it does work for you, adjust the PAUSE times to suit your needs.


' This one fires off 5 shots per press or continueously if button held
pulse_fire:
i=0 ' Reset increment counter
TRISIO = %00000001 ' Port 0 input, rest outputs
while GPIO.0=1: Wend ' Wait here until button pressed
TRISIO = %00000000 ' Port 0 now an output
for i=1 to 5 ' Set number of rapid fires per button press
low GPIO.0 ' Hold output LOW
pause 50 ' Held LOW for 50ms
high GPIO.0 ' Turn OFF output
pause 50 ' Keep OFF for 50 ms
next i '
goto pulse_fire ' Go back, continue if button still pressed or
' wait until pressed again.

' This one just continueously fires as long as button is pushed
rapid_fire:
TRISIO = %00000001 ' Port 0 input, rest outputs
while GPIO.0=1: Wend ' Wait here until button pressed
TRISIO = %00000000 ' Port 0 now an output
low GPIO.0 ' Hold output LOW
pause 50 ' Held LOW for 50ms
high GPIO.0 ' Turn OFF output
pause 50 ' Keep OFF for 50 ms
goto rapid_fire ' Go back, continue as long as button pressed


Here's the schematic to the resistor add on:

thm ov3rkill
- 3rd March 2009, 00:08
hey thank you very much ill test it out tommorw. Well one question is this setup detecting a analog voltage then fires rapidfire

LinkMTech
- 3rd March 2009, 01:38
You might be seeing a varying pulse width that looks like analog with your meter.



Hey after playing around and some testing with my new multimeter I found out this, The top pin is always low or ground the middle pin is 1.41 volt When the trigger is not held down, But when you start pressing the trigger it slowly starts decreasing, the more you hold it down the less the voltage gets. When i held the trigger down all the way It was at 0.31-0.30 volts thats afcourse when its down all the the way but even if you press it a little bit the voltage will decrease a little bit and the bottem pin of the 3 is at 1.61 volt all the time and doesent decrease nor increase. This is afcourse on this version of the controller while the other fires off high but not low like this
From: http://www.picbasic.co.uk/forum/showpost.php?p=68144&postcount=27

Put a scope on it if you have one to make sure.
Just a thought.

thm ov3rkill
- 3rd March 2009, 03:11
na pretty sure did research and saw other projects in devlopment in assemply

here is a pdf to read from it will help a lot I just want to finish this sucka up and play with it in the game =].

here is the pdf on all the info on the other project i also have it.

here I downlaoded it on rs since it exceeds ins 200kb's.


http://rapidshare.com/files/204649264/Opensource_Rapidfire_X360_Project2_2.pdf.html

thm ov3rkill
- 5th March 2009, 00:35
small bump

thm ov3rkill
- 7th March 2009, 22:56
still bumping

mackrackit
- 7th March 2009, 23:06
If you post the code that YOU wrote that is giving trouble maybe someone will take a look at it... But who knows...

thm ov3rkill
- 8th March 2009, 03:31
Ye i will try to post a code once i get a list of commands or a setup on how to read analog signals I mean I jsut need osme info thats all.

DaveC3
- 8th March 2009, 03:33
Here is a rapid fire project I made for my son. It has four states, 1) off, 2) 20 shot/sec, 3) 10shots/sec and 4) 5 shots/sec. You control the speed by tapping on the SYNC switch on the 360 controller. I used a 12F683. This will only work for the newer (CG or common ground) controllers.
The x-box trigger is a pot. when the trigger is pulled the wiper voltage goes from near zero to 3 volts. This circuit changes the pin (GP0) from input (tri-state) to a output with zero volts. There may be a little overkill, I was playing with Darrel's Instant Interrupts ;)



@ __CONFIG _FCMEN_OFF & _IESO_OFF & _CP_OFF & _CPD_OFF & _BOD_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTOSCIO

ANSEL = %00000000 'Make sure Analog functions are turned OFF (should not be needed)
CMCON0 = 7 'comparators off
OPTION_REG.7 = 0 'enable weak pullups
TRISIO = %00100101
WPU = %00000100 'weak pullup
OPTION_REG.6 = 0 'falling edge trigger

;***** VARIABLE DEFINITIONS******************

STATE Var byte ;programmed state

'';*********************************************** *************************
' _______
' VDD -|1 8|- VSS
' GP5 -|2 7|- GP0 TriggerR out ->
' GP4 -|3 6|- GP1
' GP3 -|4_____5|- GP2 <- sync
'
'
'
'
'*********GPIO pin names********************************************* *****
LED var GPIO.5 'led output useing player 4 led
Sync var GPIO.2 'sync switch input
TRIGGERR var GPIO.0 'rapid trigger output
'************************************************* *********************

INCLUDE "DT_INTS-14.bas" ; Base Interrupt System
INCLUDE "ReEnterPBP.bas" ; Include if using PBP interrupts

ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler TMR1_INT, ReloadTMR1, ASM, no ; MUST be first
INT_Handler TMR1_INT, _TriggerOut, PBP, yes
INT_Handler INT_INT, _ChangeState, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM
TMR1H = $A0 ' load timer1 with $00FF
TMR1L = $B3

T1CON = $00
@ INT_ENABLE TMR1_INT ; enable Timer 1 interrupts
@ INT_ENABLE INT_INT ; enable external (INT) interrupts

;--- Change these to match the desired interrupt frequency -------------------
;--- See http://DarrelTaylor.com/DT_INTS-14/TimerTemplate.html for more Info.
@Freq = 20 ; Frequency of Interrupts in Hz
@Prescaler = 1 ; Timers Prescaler setting
T1CON = $00 ; $30 = Prescaler 1:8, TMR1 OFF
; $00=1:1, $10=1:2, $20=1:4, $30=1:8 -- Must match @Prescaler value



State = 0

goto Main
'************************************************* **************************
ChangeState: ' Three states rapid fire 20 SPS 10 SPS and off
pause 10
if sync = 0 then ' Switch debounce
if state = 3 then state = 0
select case state
case 0 'twenty shots per sec
gosub starttimer
case 1 'ten shots per sec
T1CON = $11
case 2 ' five shots per second
T1CON = $21

case 3 'off
T1CON = $00 'timer off
TRISIO = %00100101
end select
state = state + 1
endif

@ INT_RETURN

'************************************************* **************************
TriggerOut:
TRIGGERR = 0 'Trigger 0
TRISIO.0 = 0 ' Trigger output the 0
low LED 'turn on led
TRISIO.5 = 0 'make led a output
pause 25 'pause 25 ms
high led 'turn off led
TRISIO.0 = 1 'put trigger in tristate mode (float)
input LED 'put led in tristate mode (PCP method)
@ INT_RETURN
'************************************************* **************************

Main:

'nothing happening here yet just using interupts for this project

goto main
;---[TMR1 reload - interrupt handler]-----------------------------------------
ASM ; Calculate Timer Reload Constant
ReloadInst = 8 ; # of Intructions used to reload timer
if ((Prescaler == 1)||(Prescaler == 2)||(Prescaler == 4)||(Prescaler == 8))
MaxCount = 65536 + (ReloadInst / Prescaler)
TimerReload = MaxCount - (OSC*1000000/4/Prescaler/Freq)
if ((TimerReload < 0) || (TimerReload > (65535-ReloadInst)))
error Invalid Timer Values - check "OSC", "Freq" and "Prescaler"
endif
else
error Invalid Prescaler
endif
ENDASM

@Timer1 = TMR1L ; map timer registers to a word variable
Timer1 VAR WORD EXT
TimerReload CON EXT ; Get the External Constant
TMR1ON VAR T1CON.0 ; Alias the Timers ON/OFF bit

;---Reload Timer1------
ASM
ReloadTMR1
MOVE?CT 0, T1CON, TMR1ON ; 1 stop timer
MOVLW LOW(TimerReload) ; 1 Add TimerReload to the
ADDWF TMR1L,F ; 1 value in Timer1
BTFSC STATUS,C ; 1/2
INCF TMR1H,F ; 1
MOVLW HIGH(TimerReload) ; 1
ADDWF TMR1H,F ; 1
MOVE?CT 1, T1CON, TMR1ON ; 1 start timer
INT_RETURN
ENDASM
;---Start/Stop controls -----
StartTimer:
Timer1 = TimerReload ; Load Timer
TMR1ON = 1 ; start timer
RETURN

StopTimer:
TMR1ON = 0 ; stop timer
RETURN


Hope it helps a little

Dave

mackrackit
- 8th March 2009, 05:00
Ye i will try to post a code once i get a list of commands or a setup on how to read analog signals I mean I jsut need osme info thats all.
Maybe you missed post #4?
Bruce also has some really good info here
http://www.rentron.com/PIC12C67XAD.htm
You will have to look at it from an ANSEL perspective though.