PDA

View Full Version : Will This program work



deepgfishing
- 22nd June 2007, 18:20
Hello.

I am new to this as I explained in previous thread.

i am creating a tilt sensor as a project.

I have a board that has two accelerometers on it. The accelerometers are fed into my pic18F242 microcontroller. I want the microcontroller to spit out what the accelerometer reading is in digital format so i can read it via hyperterminal on my computer.

If that works i will feed the accelerometer data to an RF device and do it remotely but i have to see if i get data from microcontroller first.

Does this program look like it will work?

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

DEFINE ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 15 ' Set sampling time in uS

tx var portB.0
rx var portA.0
n9600 con 16468

adcVar VAR word[4] ' Create variable to store results
channel var byte
byteVar var byte[4]
inByte var byte


' set Sleep pin high:
''HIGH PORTD.0

' Set PORTA to all input
TRISA = %11111111

' Set up ADCON1:
' All are analog inputs
ADCON1 = %10000000

main:
' read all channels of the accelerometer:
for channel = 0 to 3 ' read the two accelerometers from pin 0 - 3
ADCIN channel, adcVar[channel]
' convert to a byte:
byteVar[channel] = adcVar[channel] /4
pause 1
next

' send results out the serial port:
serout2 tx, n9600, [byteVar[0],byteVar[1],byteVar[2], byteVar[3]]
GoTo main

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

Ioannis
- 22nd June 2007, 20:24
It looks OK to me (except the Next which is orphan! Needs the variable to increase), but sure only testing will prove this.

A couple of improovments:

Since the chip you intent to use has UART why not use the HSEROUT command instead SEROUT2?

Like this

HSEROUT [bytevar str\4]

Also I don't quite understand why do you use the

byteVar[channel] = adcVar[channel] /4

If you want to convert the 10-bit ADC result just type this:

byteVar[channel] = adcVar[channel] >>2

and the 10-bit result will be converted to 8-bit.

Even better use the DEFINE to set ADC to 8 bits.

So it might be like that:

main:
' read all channels of the accelerometer:
for channel = 0 to 3 ' read the two accelerometers from pin 0 - 3
ADCIN channel, bytevar[channel]
pause 1
next channel

' send results out the serial port:
Hserout [byteVar str\4]
GoTo main

Ioannis

deepgfishing
- 22nd June 2007, 22:45
How did i ever live my life before the days of the internet. Thanks a lot for your advice I will modify my program and test it out. Any other advice someone has?