Continue to Site

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.

  • 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.

Why does this work?

Status
Not open for further replies.
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:
'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:
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:
'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

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:
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

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.
 
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:
That' interesting, S and would explain why the thing behaves like an analog input: because it is when adcin is called! Thanks for that.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top