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.

the easiest code not working for me

Status
Not open for further replies.

piclearner

New Member
Hi,

I am VERY new on pics and and have been learning on this forum, datasheets, etc. I just programmed my first pic 12C508A and obviously the program is not working... :eek:

I think it should be easy and in the simulator kind of worked, but not in real life... does anybody has a suggestion? I know there should be basic errors but i'm really a begginer (you will see it)

THANKS if someone can take a look to the code...

***************************************

list p=12c508a ; list directive to define processor
#include <p12c508a.inc> ; processor specific variable definitions

__CONFIG _CP_OFF & _WDT_ON & _MCLRE_ON & _ExtRC_OSC

; '__CONFIG' directive is used to embed configuration word within .asm file.
; The lables following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.




;***** VARIABLE DEFINITIONS
temp EQU 0x07 ;example variable definition
ret EQU 0x08
delay1 EQU 0x09
delay2 EQU 0x0A







;**********************************************************************
ORG 0x1FF ; processor reset vector

; Internal RC calibration value is placed at location 0x1FF by Microchip
; as a movlw k, where the k is a literal value.

ORG 0x000 ; coding begins here
movwf OSCCAL ; update register with factory cal value

; remaining code goes here

routine
bsf 0x06, 0
movlw 0xFF
movwf delay1
loopa
movlw 0xFF
movwf ret
loopb
decfsz ret,f
goto loopb
decfsz delay1,f
goto loopa
bcf 0x06, 0

bsf 0x06, 1
movlw 0xFF
movwf delay2
loopc
movlw 0xFF
movwf ret
loopd
decfsz ret,f
goto loopd
decfsz delay2,f
goto loopc

bcf 0x06, 1

start
nop ; example code
movlw 0x02 ; example code
movwf temp ; example code
tris b'0001000' ; 0,1,2 as outputs
btfss 0x06,3
call routine
goto start


END ; directive 'end of program'
 
You should really not use the 12c508a, because it can only be programmed ones and thats it. Start with 16F628 or 12F629 if you better like 8 pin device's they can be programmed over and over again.
 
As the previous poster stated the 12c508a is a very bad choice to learn on. If you can get the 12F509A then it is better as you can program it more than once.

As for your code, it is very close to working code. The only thing stopping it working was the tris instruction. To write to the TRIS register you place the value in w before transferring it with the tris instruction.
Code:
		movlw	b'0001000'
		tris	GPIO 		; 0,1,2 as outputs
With the above change your code should work (I think). However, you have your initialisation code after your main code which is rather unconventional. I rearranged your code so it does the initialisation first and then calls the subroutine.
Code:
;***************************************  

		list	p=12c508a	; list directive to define processor
		#include <p12c508a.inc> ; processor specific variable definitions

		__config _CP_OFF & _WDT_ON & _MCLRE_ON & _ExtRC_OSC 

; '__CONFIG' directive is used to embed configuration word within .asm file.  
; The lables following the directive are located in the respective .inc file.  
; See respective data sheet for additional information on configuration word.  

;***** VARIABLE DEFINITIONS  
temp		equ	0x07		;example variable definition
ret		equ	0x08 
delay1		equ	0x09 
delay2		equ	0x0A 

;************************************************* *********************  
		org	0x1FF		; processor reset vector

; Internal RC calibration value is placed at location 0x1FF by Microchip  
; as a movlw k, where the k is a literal value.  

		org	0x000		; coding begins here
		movwf	OSCCAL		; update register with factory cal value 

; remaining code goes here  

start
		nop			; example code
		movlw	0x02		; example code
		movwf	temp		; example code
		movlw	b'0001000'
		tris	GPIO 		; 0,1,2 as outputs
loop		btfss	GPIO,3   ;<--- I would remove this initially
		call	routine 
		goto	loop

routine
		bsf	GPIO, 0 
		movlw	0xFF 
		movwf	delay1
loopa
		movlw	0xFF 
		movwf	ret
loopb
		decfsz	ret,f
		goto	loopb
		decfsz	delay1,f
		goto	loopa
		bcf	GPIO, 0 

		bsf	GPIO, 1 
		movlw	0xFF 
		movwf	delay2
loopc
		movlw	0xFF 
		movwf	ret
loopd
		decfsz	ret,f
		goto	loopd
		decfsz	delay2,f
		goto	loopc

		bcf	GPIO, 1 
		retlw	0



		end			; directive 'end of program'

If it doesn't work then try removing the btfss instruction. Also check that you have the correct external RC combination or switch to the internal oscillator.

BTW, when you post code, if you type
Code:
 before it and
after it then it will keep its format.

Mike.
 
thank you both!

Yes it was a bad choice to begin with this pic, I wil try tomorrow to find the 12F509A!

Mike,

Thanks for rearranging the code, just one question, I am using GP3 as my input to trigger the routine, so you suggest

btfss GPIO,3 ;<--- I would remove this initially

Am i reading the input on this pin wrong? should I

movlw GPIO
btfss w,3

THANKS again! (I told you I am VERY new!)
 
Before you blow another chip, change this:
__CONFIG _CP_OFF & _WDT_ON & _MCLRE_ON & _ExtRC_OSC

to this:
__CONFIG _CP_OFF & _WDT_OFF & _MCLRE_ON & _ExtRC_OSC

If you run your code in MPLAB SIM, without programming the PIC, you'll have more chips left over for a project. ;)
 
Last edited:
btfss GPIO,3 ;<--- I would remove this initially

Am i reading the input on this pin wrong? should I

No, you are reading it correct. I suggested removing it in case your external circuit didn't work for some reason. Without that line the LEDs will always flash and you will at least know that your code is running. Programming is best done in small steps, get the LEDs flashing first and then get them working with a push button.

As kcriste suggest, the simulator is a very good way to learn.

Mike.
 
thank you both!

Yes it was a bad choice to begin with this pic, I wil try tomorrow to find the 12F509A!

Mike,

Thanks for rearranging the code, just one question, I am using GP3 as my input to trigger the routine, so you suggest

btfss GPIO,3 ;<--- I would remove this initially

Am i reading the input on this pin wrong? should I

movlw GPIO
btfss w,3

THANKS again! (I told you I am VERY new!)

No, use not the 12F509A as far i remember it's also just a one time programmable you should select 12F629
 
thank you both!

Yes it was a bad choice to begin with this pic, I wil try tomorrow to find the 12F509A!

Mike,

Thanks for rearranging the code, just one question, I am using GP3 as my input to trigger the routine, so you suggest

btfss GPIO,3 ;<--- I would remove this initially

Am i reading the input on this pin wrong? should I

movlw GPIO
btfss w,3

THANKS again! (I told you I am VERY new!)

The 12F509. its companion 16F505 and many other good learning chips are available as free samples from Microchip. If you live in the US, delivery is usually within five days.

Go to the Microchip site and examine their sample program. If you state "educational purposes" there will be no question as to the reason for your request.

I have asked for samples several times, and have never been refused.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top