1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Why does this work?

Discussion in 'Oshonsoft' started by steve langner, Mar 25, 2017.

  1. steve langner

    steve langner New Member

    Joined:
    Aug 2, 2014
    Messages:
    27
    Likes:
    0
    Couple of strange things occur with this code which I hope someone can explain and, hopefully, illustrate a better way of configuring the PIC 16F886 to flash a LED at a variable rate.

    First, why do two config words appear in the PIC simulator at the beginning of the code (this is of course shown in the simulator config bits)
    Second, Despite the fact that all pins are set digital (AllDigital statement), the Led in the actual hardware flashes quite happily and the flash rate is controllable with the pot as though the input is analog and linear? The simulator, however, shows the input, as one might expect, as digital.
    Code (basic):

    'PIC 16F886 variable flash rate
    Define CONFIG = 0x2074
    Define CONFIG2 = 0x3eff
    Define CLOCK_FREQUENCY = 4
    'Define SIMULATION_WAITMS_VALUE = 1  'used for simulation only
    TRISA = %00000000
    TRISB = %00100000    ' AN13
    TRISC = %00000000
    AllDigital
    PORTA = 0           'to start
    PORTB = 0            'to start
    PORTC = 0           'to start
    Dim pot As Word

    main:
       Adcin 13, pot
       pot = pot / 2  'limits slowest flash-rate to 510 mSecs
       RC3 = 1
       WaitMs 10
       RC3 = 0
       WaitMs pot
       Goto main
    End
     
     
    Last edited by a moderator: Mar 25, 2017
  2. sagor1

    sagor1 Member

    Joined:
    Dec 11, 2014
    Messages:
    52
    Likes:
    2
    All you are delaying is the time it takes the loop to go back and read the pot again. In all cases, the LED flashes for only 10mS regardless of the pot value, something that would be hard to see by eye. I don't see how you can see it, the flash is very short itself.
    Simulator does show it as digital input only in the microprocessor view, because the code was told to do so. If you tell the code that pin is analog, the simulator should set up an analog input slider. Simulator can only go by what you tell it, it can only "assume" what the chip really does in real life.

    Now, as for the chip, the default is analog input and must be defined as digital to be otherwise. If you look at figure 3-10 in the 886 datasheet, you will see that the input pin goes directly to the A/D converter, regardless of settings other that weak pullup if enabled (would pull the pin up to 5V). So, in real life, the A/D may work by fluke, but not to be trusted...

    EDIT: Just add ANSELH value to make the simulator provide an analog input for testing. ie:
    PORTA = 0 'to start
    PORTB = 0 'to start
    PORTC = 0 'to start
    ANSELH = %00100000
    Dim pot As Word
     
    Last edited: Mar 27, 2017
  3. steve langner

    steve langner New Member

    Joined:
    Aug 2, 2014
    Messages:
    27
    Likes:
    0
    Thanks for that, sagor1. Just in passing, my code is designed to operate a homemade stroboscope. So the on time was intentionally fixed, and the frequency made adjustable by varying the off time. I'm using a modified cheap torch with multiple ultra-bright LEDs for the strobe light source. The 10 mSec flash duration is easily seen in a room during the day, and is uncomfortably bright in the dark. In fact, for a particular experiment, I needed to reduce the flash duration to just 1 mSec, and this still illuminated the rotating target quite adequately in subdued lighting.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Superdat

    Superdat Member

    Joined:
    Mar 3, 2015
    Messages:
    115
    Likes:
    4

    You are not seeing a single 10mS LED flash then nothing but a very fast sequence of 10mS pulses (the loop controlled by ADC) which acts like a strobe.
    OMG that's what you were trying to do ;-)
    I think that sometimes Oshonsoft does the bits you leave out, calling ADCIN means the input pin needs to be analogue. I think the ADC routine does that for you.
    I've noticed a few times when I've been clever and setup ADC, PWM etc using PIC/AVR direct commands but still used ADCIN etc, if I remove them later it makes no difference.
    I've also noticed that what happens in the Simulator doesn't always happen in real life. That's why I now use development boards to test anything that's vaguely complicated.
     
    Last edited: May 6, 2017
  6. steve langner

    steve langner New Member

    Joined:
    Aug 2, 2014
    Messages:
    27
    Likes:
    0
    That' interesting, S and would explain why the thing behaves like an analog input: because it is when adcin is called! Thanks for that.
     

Share This Page