Will This program work


+ Reply to Thread
Results 1 to 3 of 3
  1. #1

    Default Will This program work

    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

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

  2. #2
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,181

    Default

    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

  3. #3

    Red face

    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?

Similar Threads

  1. Help with DS18B20 program
    By presario1425 in forum mel PIC BASIC Pro
    Replies: 38
    Last Post: - 22nd August 2012, 01:50
  2. My PIC program won't work! Plz Help?!
    By beckyzammi in forum mel PIC BASIC
    Replies: 2
    Last Post: - 4th March 2010, 19:26
  3. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 09:26
  4. Replies: 5
    Last Post: - 5th February 2006, 17:51
  5. Simple Interrupt Program
    By eoasap in forum General
    Replies: 5
    Last Post: - 28th October 2005, 17:22

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts