Serial randomness!


Closed Thread
Results 1 to 6 of 6
  1. #1

    Default Serial randomness!

    Hi everyone,

    Hoping you might be able to help me with a little project I’ve been working on….

    I’ve been trying to create a code that reads in a value (b0) from an
    a/d converter, this then goes to a timer routine which loops ‘b0’ amount of times and then sends PORTB.3 high. I’ve programmed the following onto a pic16f628a and then added it to the circuit…. from first impressions it all looks great! However…. By watching the LED’s attached to pins 5,6,7 and 8 I can see that it’s adding up a different amount each time, even though the value from the A/D converter should be the same. Can anyone spot where I’m going wrong here as it’s left me pretty confused!

    Many thanks, Alex

    -----------------------------------------------

    CMCON = 7
    INCLUDE "modedefs.bas"
    TRISB = 1
    TRISA.0 = 1
    ti var byte 'Initial time delay
    b0 var byte 'serial value from a/d converter
    timer var byte
    ti = 1000 'timer value
    timer = 0
    b0 = 0


    start:

    portb = 128 'flash
    pause 400
    portb = 0
    pause 400
    IF porta.0 = 1 THEN start


    Serial_in:

    low 1 'bring I/O clock low
    low 2
    pause 1000
    b0.7 = PORTB.0
    pulsout 1,1
    pause 500
    b0.6 = PORTB.0
    pulsout 1,1
    pause 500
    b0.5 = PORTB.0
    pulsout 1,1
    pause 500
    B0.4 = PORTB.0
    pulsout 1,1
    pause 500
    b0.3 = PORTB.0
    pulsout 1,1
    pause 500
    b0.2 = PORTB.0
    pulsout 1,1
    pause 500
    b0.1 = PORTB.0
    pulsout 1,1
    pause 500
    b0.0 = PORTB.0
    pulsout 1,1
    pause 500
    high 2
    pause 1000



    Timer_routine:

    b0 = b0 * 5
    if timer > b0 then Execute 'check
    portb = 16
    pause ti
    timer = timer + 1
    portb = 32
    pause ti
    timer = timer + 1
    portb = 64
    pause ti
    timer = timer + 1
    portb = 128
    pause ti
    timer = timer + 1
    goto Timer_routine


    Execute:

    timer = 0
    portb = 8 'send signal to relay
    pause 4000
    b0 = 0 'reset b0
    goto start

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by london alex View Post
    Hi everyone,
    Hoping you might be able to help me with a little project I’ve been working on….
    I’ve been trying to create a code that reads in a value (b0) from an
    a/d converter, this then goes to a timer routine which loops ‘b0’ amount of times and then sends PORTB.3 high. I’ve programmed the following onto a pic16f628a and then added it to the circuit…. from first impressions it all looks great! However…. By watching the LED’s attached to pins 5,6,7 and 8 I can see that it’s adding up a different amount each time, even though the value from the A/D converter should be the same. Can anyone spot where I’m going wrong here as it’s left me pretty confused!
    Many thanks, Alex
    Where's the ADC code?
    The output from the ADC should be the same...key word is 'should'.
    Is your ADC input varying at all?
    Are you sampling it slow enough as the datasheet suggests?
    Are your parameters for the ADC (sample time, etc.) correct?
    What is the input impedance to your ADC pin?
    For all I know, there's a nuclear fission reactor power generating station connected to AN0 on the PIC, or it could be a small butterfly. I don't know.

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    seems to be an external ADC on PORTB.0. Setting sampling time, clock source etc... on a 16F628 that will be hard

    However i don't think it's working like that. If we knew the ADC model, we could elaborate a bit much. Almost sure it's an SPI type... SHIFTIN/SHIFTOUT would help.. but.. i only guess
    Last edited by mister_e; - 8th January 2008 at 20:34.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  4. #4


    Did you find this post helpful? Yes | No

    Default

    Yeah sorry guys, should have been a bit clearer! I'm using an external TLC549CP A/D converter which has:

    ADC 'data out' pin attached to pin 0 on the pic.
    ADC i/o clock pin attached to pin 1 on the pic and..
    ADC CS(active) pin attached to pin 2...

    Attached to the ADC is a potentiometer which is supplying the variable voltage (0-5V). Throughout my testing I haven't been adjusting it and so the serial output should remain the same if it was working correctly.

    Haven't tried SHIFTIN/SHIFTOUT but will have a look into it... cheers!!

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Alex,

    When you do something like PORTB = 8, it sets all the pins in PORTB.

    Since PORTB.2 is the A/D's Chip Select pin, every time you turn on an LED or Relay, you are also Enabling the A/D converter. Probably getting it confused.

    And that chip can be clocked at up to 1 Mhz.
    6 seconds is an awful long time to read 8 bits.
    <br>
    DT

  6. #6


    Did you find this post helpful? Yes | No

    Default

    Hey Darrel,

    Thank you loads for your response! I'm currently away from home and so unable to test your theory, but i'm convinced that will be the cause of the issue! I'll keep you updated as to when I can test it and thanks to everyone who helped.

    Cheers,
    Alex

Similar Threads

  1. Replies: 33
    Last Post: - 19th March 2010, 03:02
  2. Dynamic USB Serial Number (PIC18F4550)
    By awmt102 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 16th July 2009, 17:03
  3. PIC18F4680 to PC via MAX232 (RS232 serial) no output
    By opticsteam1 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 14th April 2008, 20:39
  4. interfacing to the pc serial port
    By kelangfei in forum General
    Replies: 4
    Last Post: - 7th October 2007, 22:35
  5. Replies: 2
    Last Post: - 23rd July 2006, 08:16

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

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