PDA

View Full Version : Cannot get blink example to work via PICBASIC PRO, but works via MPLAB IDE



patrickg
- 24th December 2012, 02:12
I hate to say this has been a miserable experience, I have had no luck. I am trying to get the blink led program to work. I have had success using my new PICkit2 programmer (as the first non-PICkit2 programmer did not work) with the MPLAB IDE v8.88 code, and it worked and blinks nicely. But when I use PICBasic Pro to code the PIC16F628A it seems fine, but the LED does not blink.

If I use this code in the MPLAB IDE v8.88 and program the PIC16F628A using RB0 as the LED port it works fine. (not my code)



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; PIC Microcontroller Tutorial Template ;
; ;
; By Brad Slattery 2009 ;
; www.bradsprojects.com ;
; [email protected] ;
; ;
; 'Designing electronic projects, ;
; to spread the name of Jesus' ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; NOTE: If you are brand new to programming, then don't concern yourself to ;
; much with all of these initial lines of code. You can read up all about this ;
; at a later date if you wish. I'm sure that you would rather make an LED flash ;
; or make something interesting happen straight away right? ;
; All you have to know for now is that we will be placing our code just underneath ;
; the SETUP label, and then underneath the BEGIN label. it's really quite simple! ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


LIST p=16f628a ; tell assembler what chip we are using (if you are using the 16f628a, then
include "P16f628a.inc" ; make sure you change this line and the previous line to read p16f628a
__config h'3f18' ; sets the configuration settings - internal oscillator, watchdog timer OFF
; Power Up Timer DISABLED, Master Clear DISABLED, Brown Out Detect DISABLED,
; Low Voltage Programming DISABLED, Data EE Read Protect Disabled,
; Code Protect OFF. (this will remain the same for all tutorials)


PC equ h'02' ; The program counter will be refered to as PC - The program counter is
; a little counter within the microcontroller to let itself know what line number
; it is upto when running a program. We can make the microcontroller jump to a certain
; line number by changing the value stored in PC. (we will do this in a later tutorial)

cblock h'20' ; Within this cblock and endc, we can define our variables. More info on this, later.
delay_1 ; These next two lines will set aside 1byte of ram for delay_1 and 1byte for delay_2
delay_2 ; what we will do with these variables is load them with a number each so that we can
endc ; count down from that number to zero. when we reach zero, we continue with the program!
; more info on these two variables can be found in the delay routine.



org h'0000' ; This line just tells the microcontroller what address to start running our program from.
; It will always be 0000 hex for all the tutorials.


movlw h'07' ; This will turn the comparators OFF.
movwf CMCON ; (we just want to use the ports as digital ports)


bsf STATUS, RP0 ; select bank 1 (to enable us to change the Input / Output status of our ports)
movlw b'00000000' ; set PORTB all outputs (A '0' means output, A '1' means input. We can set each
movwf TRISB ; We can set each bit individualy. Each port having 8-bits or 8 pins.
movlw b'00100000' ; set PORTA all outputs except for bit 5. Bit 5 is an input ONLY pin.
movwf TRISA ; It cannot be set to an output!
bcf STATUS, RP0 ; select bank 0




setup ; I always have a title labeled 'setup' this is where we set everything up
; i.e. our variables and flags etc.. so they are ready for the main program


begin ; This is where our main program starts.
bsf PORTB, 0 ; set PORTB pin 0 to a logic 1 (turns the LED on)
call delay ; call the delay so that the LED stays on for a while
bcf PORTB, 0 ; clear PORTB pin 0 to a logic 0 (turns the LED off)
call delay ; call the delay so that the LED stays off for a while
goto begin ; and now go and do it all again (it will run in a continuos loop)



delay ; here is a nice and simple delay routine
movlw d'255' ; copy the maximum number to our working register (decimal 255)
movwf delay_1 ; and now copy it from the w register to delay_1 and delay_2
movwf delay_2 ; Now the rest of the routine will focus on counting down to zero.
delay_loop ; We come back to this label when we have not yet reached zero.
decfsz delay_1, f ; decrement whatever is in delay_1 by 1 and store the answer back in delay_1
goto delay_loop ; if the answer is not zero, then go back to the delay_loop label. but if the
decfsz delay_2, f ; answer is zero then decrement delay_2 by one and store the answer in delay_2
goto delay_loop ; if the answer is not zero, then go back to delay_loop label. but if the answer
return ; is zero, then we have completed our delay and now we can return to our main program!



end ; We always need to have end at the end, even if we don't want the program
; to actually end, it still must be here!


If I then use this



' Name : BLINK.pbp
' Compiler : PICBASIC PRO Compiler 2.6
' Assembler : PM or MPASM
' Target PIC : 16F or 18F types
' Hardware : Non specific
' Oscillator : internal or external
' Keywords : BLINK LED
' Description : PICBASIC PRO program to to blink an LED connected
' to PORTB.0 about once a second
'

'The following program is a simple test to blink an LED on and off. Select a
'device in the dropdown list above, then click the Compile Only button on the
'toolbar to compile the program and generate a hex file.

' Configure pins for digital operation (uncomment as needed).
' These settings are intended as simple examples. For more detail,
' see the appropriate device datasheet for register descriptions.
'ANSEL = 000000 ' 16F88, 16F688, 16F690, 16F88x
'ANSELH = 000000 ' 16F690, 16F88x
'ADCON1 = 000111 ' 16F87x, 16F87xA, 18F452
'ADCON1 = 001111 ' 18F4620

LED VAR PORTB.0 ' Assign name "LED" to PORTB.0

mainloop:
High LED ' Turn on LED connected to PORTB.0
Pause 500 ' Delay for .5 seconds

Low LED ' Turn off LED connected to PORTB.0
Pause 500 ' Delay for .5 seconds

Goto mainloop ' Go back to loop and blink LED forever

End


And program the PIC, it does not work. Initially I thought it was the hookup between PBP and the PICkit2, but even when I dump out the hex file and load that manually into PICkit2 and program the PIC it still does not work.

Any help would be greatly appreciated. At this point I am likely missing something obvious, but I have been trying for so long now my eyes are blurry.

Thanks!
Patrick

mackrackit
- 24th December 2012, 02:58
How do you have the configuration set when using PBP? The ASM code is using the internal OSC while the default in PBP for the 16F628A uses an external OSC.

patrickg
- 24th December 2012, 05:00
Dave,

Thanks for the lightning fast response!!! Wow.

I was planning on using the internal oscillator. Since you pointed out the potential issue I am trying to determine how to change PBP to use the internal one. I have been messing around with the PIC16F628.PBPINC file with this instead



#CONFIG
ifdef PM_USED
;device pic16F628, xt_osc, wdt_on, pwrt_on, mclr_on, lvp_off, protect_off
device pic16F628, _INTRC_OSC_NOCLKOUT, wdt_on, pwrt_on, mclr_on, lvp_off, protect_off
else
;__config _XT_OSC & _WDT_ON & _PWRTE_ON & _MCLRE_ON & _LVP_OFF & _CP_OFF
__config _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_ON & _LVP_OFF & _CP_OFF
endif
#ENDCONFIG


But to no avail. Am I barking up the wrong tree? Where can I set this to internal? Thanks so much for your help with this! I really cannot believe I cannot figure this out.

Patrick

mackrackit
- 24th December 2012, 05:11
In your first post you were working with the 16F628A , but the you have the 16F628.inc file posted?

And do not forget to set OSCCON in your code for the speed.

mackrackit
- 24th December 2012, 05:31
Sorry, the 16F628A only has one internal speed.

Comment out the config lines in the *.inc file and add this to your code near the beginning.


'16F628A
@ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _LVP_OFF & _CP_OFF
DEFINE OSC 4

Sherbrook
- 24th December 2012, 12:44
Hi Patrick

You also need to set portb.0 TRIS register to output

TRISB = %11111110
or
TRISB.0 = 0

TRISB = %00000000

will set all PORTB pins as output

Phil

mackrackit
- 24th December 2012, 14:42
Hi Patrick

You also need to set portb.0 TRIS register to output

Phil
The HIGH / LOW command sets the TRIS for us. But it is good practice to get into.

patrickg
- 24th December 2012, 20:41
Dave and Phil, THANKS!!! You guys made my day! Who knew a little blinking light was all it took.

:smile:
Thanks a million, Merry Christmas!
Patrick