PDA

View Full Version : 16f887 44 pin demo board code problem?



jessey
- 5th December 2008, 07:21
Hello,

I just got my new version of PBP 2.50 the other day and am trying to write a simple program to light LEDs using a PicKit 2 programmer on the demo board that came with my PICKIT 2 Debug Express. It's really strange that the LEDs do turn on/off on the demo board because the code to turn them on/off is inside an IF Push_Button = Is_Pressed THEN that expects a button press to turn them on/off and it runs the code, flashing the LEDs in the proper sequence without a button press? The other code inside the IF Push_Button = Is_Not_Pressed THEN doesn't turn the LEDs on/off when pressed? The push button has no effect in the code.

I wasn't sure how to set the internal oscillator but after reading the data sheet for the 16f887 it said: The Fail-Safe Clock Monitor is designed to detect a failure of the external clock source and switch automatically to the internal oscillator which it seems to have done because the LEDs are turning on/off. According to the data sheet the oscillator is running at 31 kHz and to select other frequencies I need to set the OSCCON register. Am I correct in thinking that for 4 MHz I would include in my program OSCCON %01100xxx, I'm not sure what bits 0, 1 and 2 would be set to? Could anyone give me some insight about these setting?

I checked and triple checked the schematics for the demo board to be certain that all my tris settings are correct for the LED's and for the on board push button and anything else that I could think of that could cause this but I must be missing something really simple, like when I thought I had ver 2.47 of PBP but instead had ver 2.45.....

Does anyone here see any obvious mistakes in my code below, any help would really be appreciated.

Thanks
jessey



' Name : Blink_Led.BAS
' Author : Jessey Montgomery
' Notice :
' :
' Date : 02/12/2008
' Version : PicBasic Pro ver 2.50, MPASM v5.20, MicroCode
' Notes : Studio ver 3.0.0.5, PICKIT 2 programmer, 44 pin
' : PIC16F877-I/PT Microprocessor on PicKit 2 demo board.


; 2
; P
; C
; 1 C
; A L P /
; D C C I
; K S S C C
; C / / / S
; / O I K A O
; X D D C 1 1
; T S S S P T
; / / / / / /
; 6 5 4 3 2 1 0 3 2 1
; C C C D D D D C C C C
; R R R R R R R R R R N
; -------------------------------
; |* 4 4 4 4 4 3 3 3 3 35 34 |
; RC7/RX/DT<-->|1 4 3 2 1 0 9 8 7 6 33|<-->NC
; RD4<-->|2 32|<-->RC0/T1OSO/T1CKI
; RD5/P1B<-->|3 31|<---RA6/OSC2/CLKOUT
; RD6/P1C<-->|4 30|<---RA7/OSC1/CLKIN
; RD7/P1D<-->|5 29|<---VSS-
; -VSS<-->|6 PIC16F887 28|<---VDD+
; +VDD--->|7 27|<-->RE2/AN7
; RB0/AN12/INT<-->|8 26|<-->RE1/AN6
; RB1/AN10/C12IN3-<-->|9 25|<-->RE0/AN5
; RB2/AN8<-->|10 24|<-->RA5/AN4/SS/C2OUT
; RB3/AN9/PGM/C12IN2-<-->|11 2 3 4 5 6 7 8 9 0 23|<-->RA4/T0CKI/C1OUT
; | 1 1 1 1 1 1 1 1 2 21 22 |
; -------------------------------
; C C 1 G K T P | 1 + +
; N N 1 1 L A P 0 N N N
; N T C D V N I I I
; A / P P / I 2 2 1
; / 3 S S R 2 1 C C
; 4 1 C C L 1 C / /
; B N I I C C / F +
; R A / / M / 1 E F
; / 6 7 / U N R E
; 5 B B 3 W A V R
; B R R E P / C V
; R R L 1 / /
; U A | 3
; / R F N
; O E A
; N R /
; A V 3
; / / A
; 0 2 R
; A N
; R A
; /
; 2
; A
; R


Clear 'Set all ram registers to zero
PORTA = 0
PORTB = 0
PORTC = 0
PORTD = 0
PORTE = 0

'VSS VDD MCLR Ect. Pins

'VDD '(pin 7)
'VDD '(pin 28)
'VSS '(pin 6)
'VSS '(pin 29)
'NOT USED '(pin 12)
'NOT USED '(pin 13)
'NOT USED '(pin 33)
'NOT USED '(pin 34)


' Set The TRIS In/Out Pins
'PORTA PINS
TRISA.0 = 1 '(pin 19) NOT USED, VAR PORTA.0
TRISA.1 = 1 '(pin 20) NOT USED, VAR PORTA.1
TRISA.2 = 1 '(pin 21) NOT USED, VAR PORTA.2
TRISA.3 = 1 '(pin 22) NOT USED, VAR PORTA.3
TRISA.4 = 1 '(pin 23) NOT USED, VAR PORTA.4
TRISA.5 = 1 '(pin 24) NOT USED, VAR PORTA.5
TRISA.6 = 1 '(pin 31) NOT USED, VAR PORTA.6
TRISA.7 = 1 '(pin 30) NOT USED, VAR PORTA.7

'PORTB PINS
TRISB.0 = 1 '(pin 8) Push_Button VAR PORTB.0
TRISB.1 = 1 '(pin 9) NOT USED, VAR PORTB.1
TRISB.2 = 1 '(pin 10) NOT USED, VAR PORTB.2
TRISB.3 = 1 '(pin 11) NOT USED, VAR PORTB.3
TRISB.4 = 1 '(pin 14) NOT USED, VAR PORTB.4
TRISB.5 = 1 '(pin 15) NOT USED, VAR PORTB.5
TRISB.6 = 1 '(pin 16) NOT USED, VAR PORTB.6
TRISB.7 = 1 '(pin 17) NOT USED, VAR PORTB.7

'PORTC PINS
TRISC.0 = 1 '(pin 32) NOT USED, VAR PORTC.0
TRISC.1 = 1 '(pin 35) NOT USED, VAR PORTC.1
TRISC.2 = 1 '(pin 36) NOT USED, VAR PORTC.2
TRISC.3 = 1 '(pin 37) NOT USED, VAR PORTC.3
TRISC.4 = 1 '(pin 42) NOT USED, VAR PORTC.4
TRISC.5 = 1 '(pin 43) NOT USED, VAR PORTC.5
TRISC.6 = 1 '(pin 44) NOT USED, VAR PORTC.6
TRISC.7 = 1 '(pin 1) NOT USED, VAR PORTC.7

'PORTD PINS
TRISD.0 = 0 '(pin 38) Led_1 VAR PORTD.0
TRISD.1 = 0 '(PIN 39) Led_2 VAR PORTD.1
TRISD.2 = 0 '(pin 40) Led_3 VAR PORTD.2
TRISD.3 = 0 '(pin 41) Led_4 VAR PORTD.3
TRISD.4 = 0 '(pin 2) Led_5 VAR PORTD.4
TRISD.5 = 0 '(pin 3) Led_6 VAR PORTD.5
TRISD.6 = 0 '(pin 4) Led_7 VAR PORTD.6
TRISD.7 = 0 '(pin 5) Led_8 VAR PORTD.7

'PORTE PINS
TRISE.0 = 1 '(pin 25) NOT USED, VAR PORTE.0
TRISE.1 = 1 '(pin 26) NOT USED, VAR PORTE.1
TRISE.2 = 1 '(pin 27) NOT USED, VAR PORTE.2
TRISE.3 = 1 '(pin 18) NOT USED, VAR PORTE.3

@ __config _CONFIG1, _INTRC_OSC_CLKOUT & _WDT_ON & _MCLRE_ON & _LVP_OFF & _CP_OFF

Push_Button VAR PORTB.0 '(pin 8)
Led_1 VAR PORTD.0 '(pin 38)
Led_2 VAR PORTD.1 '(PIN 39)
Led_3 VAR PORTD.2 '(pin 40)
Led_4 VAR PORTD.3 '(pin 41)
Led_5 VAR PORTD.4 '(pin 2)
Led_6 VAR PORTD.5 '(pin 3)
Led_7 VAR PORTD.6 '(pin 4)
Led_8 VAR PORTD.7 '(pin 5)

a VAR BIT ' used in IF THEN's instead of GOTO's
A = 0

Is_Pressed CON 0
Is_Not_Pressed CON 1
_On CON 1
_Off CON 0

IF a = 0 THEN MainLoop

MainLoop:

IF Push_Button = Is_Not_Pressed THEN
PORTD.0 = 1
PORTD.1 = 1
PORTD.2 = 1
PORTD.3 = 1
PORTD.4 = 1
PORTD.5 = 1
PORTD.6 = 1
PORTD.7 = 1
pause 500
PORTD.0 = 0
PORTD.1 = 0
PORTD.2 = 0
PORTD.3 = 0
PORTD.4 = 0
PORTD.5 = 0
PORTD.6 = 0
PORTD.7 = 0
endif

IF Push_Button = Is_Pressed THEN
Led_1 = _On
PAUSE 350
Led_1 = _Off
Led_2 = _On
PAUSE 350
Led_2 = _Off
Led_3 = _On
PAUSE 350
Led_3 = _Off
Led_4 = _On
PAUSE 350
Led_4 = _Off
Led_5 = _On
PAUSE 350
Led_5 = _Off
Led_6 = _On
PAUSE 350
Led_6 = _Off
Led_7 = _On
PAUSE 350
Led_7 = _Off
Led_8 = _On
PAUSE 350
Led_8 = _Off
ENDIF

pause 100

IF a = 0 THEN MainLoop

END

Acetronics2
- 5th December 2008, 08:58
Hi, Jessey

at first sight,

You also have to disable Comparators and declare ADC shared pins as digital ...

Alain

PS: PIC16F877-I/PT Microprocessor or 16 F 887 ???

mackrackit
- 5th December 2008, 11:18
My config line.


@__config _CONFIG1, _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _LVP_OFF & _CP_OFF

Bruce
- 5th December 2008, 17:14
Am I correct in thinking that for 4 MHz I would include in my program OSCCON %01100xxx, I'm not sure what bits 0, 1 and 2 would be set to?
Yes. OSCCON = %01100000 would set it to 4MHz. Bit 0 SCS allows you to switch from the osc set in config to internal. Bits 1, 2 and 3 are read only bits that indicate the oscillator status.

Look at the modified version of your example attached here for a few other comments. I haven't tested this, but it should help get you up & running.

jessey
- 6th December 2008, 07:39
Thanks Everyone,

Once again I proved to myself how (dumb) forgetful I am , I now remember the countless number of times I've read newbies asking why their programs won't work and the responses have always been to check the data sheets for the device your using for Comparators and such. I'll definitely be paying more attention to that the next time I use a new unfamiliar micro.

Hey Bruce, I really appreciate you laying out my program and explaining all the settings in your comments, it would have taken me forever to read and try to figure out the datasheet for all those settings not to mention additional posts. The program works great now but one thing I wanted to mention is that my 44 pin demo board doesn't have a crystal installed on the board even though it reflects that in the schematics, there are pads on the board for the crystal and related components but those pads are all un-populated. Even though the program worked good using _INTRC_OSC_NOCLKOUT I changed it to _INTRC_OSC_CLKOUT, am I right in doing that and also is there a way for me to verify that the program is running on 4 MHz without using an oscilloscope?

Thanks Again
jessey

mackrackit
- 6th December 2008, 08:19
Even though the program worked good using _INTRC_OSC_NOCLKOUT I changed it to _INTRC_OSC_CLKOUT, am I right in doing that
In CLKOUT mode you can send a clock pulse signal out to another device.
NOCLKOUT you can use the pins for I/Os


and also is there a way for me to verify that the program is running on 4 MHz without using an oscilloscope?

Program a simple blinky with one LED that will blink at a certain rate. If you code has
DEFINE OSC 4
and the chip is running at 4MHz, then the LED will blink at the correct rate.

jessey
- 6th December 2008, 09:42
Thanks Dave,

I added the code below in the MainLoop to check out your explanation and I can see that the timing is correct at 4 MHz and runs twice as fast at 8 MHz, that's cool!



IF Push_Button = Is_Pressed THEN
OSCCON = MHz8
ELSE
OSCCON = MHz4
ENDIF


Thanks
jessey

jessey
- 7th December 2008, 11:30
Hello,
I was wondering if someone could suggest why this code below doesn't work to change the oscillator speed of the Internal Oscillator while the program is running but the other code block below does work? Another thing I was pondering is, shouldn't I be setting bit 0 of the OSCCON register to 1? The datasheet for the 16f887 says this about bit 0:
1 = Internal oscillator is used for system clock
0 = Clock source defined by FOSC<2:0> of the CONFIG1 register.



'this doesn't work
IF Push_Button = Is_Pressed THEN
IF OSCCON = %01100000 THEN
OSCCON = %01110000
ELSE
OSCCON = %01100000
ENDIF
WHILE Push_Button = Is_Pressed
PORTD = %10000001 ' Show that button press has stopped the code here
WEND
ENDIF


'this works
IF Push_Button = Is_Pressed THEN
IF B = 0 THEN
B = 1
ELSE
B = 0
ENDIF
WHILE Push_Button = Is_Pressed
PORTD = %10000001 ' Show that button press has stopped the code here
WEND
ENDIF
IF B = 0 THEN OSCCON = %01110000 ' 8 MHz
IF B = 1 THEN OSCCON = %01100000 ' 4 MHz

Thanks
jessey

Acetronics2
- 7th December 2008, 13:14
hi,

I think it is "not so good" to change osc settings WHILE you touch the button ( debounce issue guaranteed !!! ...PORTD can show anything but truth !!! )

good reason for #2 to work better ... ONLY better.

I ( it's me ...) Would look for button state CHANGES instead of simply the button state ...

Alain

mackrackit
- 7th December 2008, 14:17
Another thing I was pondering is, shouldn't I be setting bit 0 of the OSCCON register to 1? The datasheet for the 16f887 says this about bit 0:
1 = Internal oscillator is used for system clock
0 = Clock source defined by FOSC<2:0> of the CONFIG1 register.
See post #4
You have the OSC set for internal at the start. In the config settings. You do not need to change the source.