PDA

View Full Version : blinking problem with PIC16f887



larzazral
- 25th January 2010, 14:52
I'm using PIC16f887 with 20MHZ osc...my led connect to RD2.

Define OSC 20

TRISD = 0
PORTD = 0

MAIN:

PORTD.2 = 1
PAUSE 500
PORTD.2 = 0
PAUSE 500

GOTO MAIN
end

why my led not blinking with 500ms ?? when i remove define osc 20...my led blinking correctly (500ms).

mehmetOzdemir
- 25th January 2010, 15:11
Manuel says ;




These definitions must be in all upper case.



Define >>>>> DEFINE

Archangel
- 25th January 2010, 18:10
Hello larzazral,
I do not see a config statement in your code which means either:
A. you simply did not share them with us, or
B. You are using the default config statement located in the 16f887.inc
file in the root directory of PBP.

The 16f887.inc file is as below:

NOLIST
ifdef PM_USED
LIST
include 'M16F88x.INC' ; PM header
device pic16F887, intrc_osc_noclkout, wdt_on, mclr_on, lvp_off, protect_off
XALL
NOLIST
else
LIST
LIST p = 16F887, r = dec, w = -302
INCLUDE "P16F887.INC" ; MPASM Header
__config _CONFIG1, _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_ON & _LVP_OFF & _CP_OFF
NOLIST
endif
LIST

You will take note of the OSCILLATOR config . . . intrc_osc_noclkout
which obfuscates the use of the crystal / resonator. Which is to say, it runs on internal at 4 mhz and will not DO 20MHZ. To use the crystal, . . . Put a semi colon just before the config statement and install your own config in your code <B> OR </B> simply change the configs to use the "high speed osc. ". Look at this thread, no study it, absorb it, know it, seriously, if you do not study anything else, study this, it is that important.
http://www.picbasic.co.uk/forum/showthread.php?t=543

Dennis
- 25th January 2010, 20:40
Hi larzazral

Check what Joe said and read it all carefully and then check if you have an external oscillator (20 MHZ crystal).

In the code below I have the defines setup and also ocsillator setting for the internal clock , code is commented as much as possible!

Hope this helps

Dennis



'*************************************
'Blink and LED for 16F887 on PORTd.2
'*************************************
'Un-comment the lines below if you need to
'@__config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_ON & '_LVP_OFF & _CP_OFF

'Ocsillator selections here
'OSCCON = %01100001 for 4mhz, or OSCCON = %01110001for 8mhz
' OSCCON = %01110001 'Int CLK 8MHz
' ANSEL= %11111111 '$0F = disable A/D converter
' option_reg = 7 'switch pull-ups ON
'END of oscillator selections

'timer/oscillator defines

OSCCON = %01110001 'Int CLK 8MHz
DEFINE OSC 8 '8MHz << NOTICE UPPERCASE ON DEFINE

'END of timer/oscillator defines


ANSEL = %00000000 'All digital
OPTION_REG.7 = 0 'Weak pull-ups enabled



'port clear
PORTA=0
PORTB=0
PORTC=0
PORTD=0
PORTE=0
'end of port clear

'Port IO directions and presets for port pins begin here
'TRISX = %76543210 << TRIS bit order numbering
'TRISA = %11111111 << 'All pins are outputs
'// Define port pins as inputs and outputs ...
TRISA = %00000000 'all output
TRISB = %00000000 'all output
TRISC = %00000000 'all output
TRISD = %00000000 'all output
TRISE.0 = 0 'all output
TRISE.1 = 0 'all output
TRISE.2 = 0 'all output
'End of Port IO directions and presets for port pins begin here

'includes begin here
INCLUDE "modedefs.bas"

'end of includes

'variables begin here

LED var PORTd.2 ' Alias PORTD.0 to LED

'end of variables





blinky: ' a program label called blinky
high LED ' LED on
Pause 500 ' Delay for .5 seconds
low led 'LED off
Pause 500 ' Delay for .5 seconds

Goto blinky ' Go back to blinky label and do it all over again
End
'End of all code

Bruce
- 25th January 2010, 22:35
The PBP default config settings for this PIC are;
device pic16F887, intrc_osc_noclkout, wdt_on, mclr_on, lvp_off, protect_off

So, it's actually running on the internal oscillator.

And the PIC default setting of OSCCAL (for the internal oscillator) is for 4MHz.

Define OSC 20 forces PBP tro assume it's running at 20MHz, but its actually running on
the 4MHz internal osc, so Define OSC 20 screws the timing .. when it's running at 4MHz.

For it to work as expected, at 20MHz, you would need to connect an external 20MHz
crystal (or resonator), and change config settings to;

device pic16F887, hs_osc, wdt_on, mclr_on, lvp_off, protect_off.

Then use the DEFINE OSC 20.

Archangel
- 26th January 2010, 02:04
The PBP default config settings for this PIC are;
device pic16F887, intrc_osc_noclkout, wdt_on, mclr_on, lvp_off, protect_off

So, it's actually running on the internal oscillator.

And the PIC default setting of OSCCAL (for the internal oscillator) is for 4MHz.

Define OSC 20 forces PBP tro assume it's running at 20MHz, but its actually running on
the 4MHz internal osc, so Define OSC 20 screws the timing .. when it's running at 4MHz.

For it to work as expected, at 20MHz, you would need to connect an external 20MHz
crystal (or resonator), and change config settings to;

device pic16F887, hs_osc, wdt_on, mclr_on, lvp_off, protect_off.

Then use the DEFINE OSC 20.
See Bruce,
We need your book ;)
Can I get that on Amazon ?