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.

Migrate programfrom a PIC16f628 to a PIC16f628a

Status
Not open for further replies.

allenf

New Member
what differences in the config settings would i have to use to use a program written for a PIC16F628 on a PIC16F628a.
What other things would I need to do to the program to get it working.
Sorry but I just dont understand the differences on the datasheets
Here is the start of the 628 file


list p=16f628a ; list directive to define processor
#include <p16f628a.inc> ; processor specific variable definitions

__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _INTOSC_OSC_NOCLKOUT & _LVP_OFF
errorlevel -302 ; suppress banksel warning messages during assembly
errorlevel -311 ; suppress HIGH operator warning messages during assembly


; '__CONFIG' directive is used to embed configuration data 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.






;**********************************************************************
ORG 0x000 ; processor reset vector
goto main ; go to beginning of program


ORG 0x004 ; interrupt vector location
movwf w_temp ; save off current W register contents
movf STATUS,w ; move status register into W register
movwf status_temp ; save off contents of STATUS register





movf status_temp,w ; retrieve copy of STATUS register
movwf STATUS ; restore pre-isr STATUS register contents
swapf w_temp,f
swapf w_temp,w ; restore pre-isr W register contents
retfie ; return from interrupt


init CLRF PORTA ;Initialize PORTA by setting
;output data latches
BSF PORTA,LED
MOVLW 0X07 ;Turn comparators off and
MOVWF CMCON ;enable pins for I/O
;functions
BCF STATUS, RP1
BSF STATUS, RP0 ;Select Bank1
MOVLW b'11111101' ;Value used to initialize
;data direction
MOVWF TRISA ;Set RA<1> as output

MOVLW b'00000000' ;Value used to initialize
;data direction
MOVWF TRISB ;Set Rb<7:0> as outputs

BCF STATUS, RP0 ;Select bank 0
return ;return from subroutine




I take it that all the variables can be used on either chip so i wouldnt need to change them (or not??)
Many thnaks

allen
 
The ONLY assembler error you should be suppressing is error 302...the bank select warning. If you're getting any other errors during assembly you need to find out what the cause of the error is...NOT suppress it.

All you need for the config word for the F628A is -

Code:
            list                  p=16F628A, r=dec, w=-302
            include               <P16F628A.INC>
            __config              _LVP_OFF & _BOREN_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT

This is true for both the F628 and F628A. Code protection and data code protection are disabled by default so you do not need to program these config bits.

Another thing...your interrupt handler context save routine is incorrect. Should be -

Code:
		org		0x004			;interrupt vector

		movwf		w_temp
		swapf		STATUS,W
		movwf		status_temp
		movfw		PCLATH
		movwf		pclath_temp

;interrupt handler code goes here

		movfw		pclath_temp
		movwf		PCLATH
		swapf		status_temp,W
		movwf		STATUS
		swapf		w_temp,F
		swapf		w_temp,W
		retfie

One more thing...you have a return instruction as the last instruction in your code yet you don't have anything calling a subroutine. That will cause a stack underflow...remove it.
 
Last edited:
When I try and use the PCLATH line I just get a fistfull of errors???
Error[113] G:\628APROJECT\MODI1.ASM 80 : Symbol not previously defined (pclath_temp)
Error[113] G:\628APROJECT\MODI1.ASM 84 : Symbol not previously defined (pclath_temp)

Now I`m really stuffed then LOL cos I dont get that at all
 
That's because you have to define the label pclath_temp along with the rest of your label definitions. Also, w_temp, status_temp and pclath_temp should be equated to address values in the "common RAM" space (0x70-0x7F).

Also...context saving PCLATH is not just an F628/F628A thing. It should be done on ALL 16F PICs. Some even context save FSR along with those 3.
 
Last edited:
K sorry to sound so negative but I still cant get my head round this I have been at this project now forover 6 months and got absolutly nowhere with it.
I am certain now that a pic is only useful for turning an led on and off and is basically a glorified expensive switch. and good for absolutly nothing apart from wasting alot of time and energy on needless to say the hard drive space and sleepless night pondering over this.
All I want to do is nigels 5.3 exersise on a pic 16f628a I have seen videos of it working but i`m now begining to supsect that either the codes all over the web are completely worthless and it video trickery Or that PICs just dont like me .
I run them through in proteus first to simulate it works fine i spend a hour or so building the breadboard up ( i did build all nigels modules to no avail it still wont work) only to find that either all the leds are lit or all the leds are off I just give up totally now the whole lot cango in the bin as far as I`m concerned because NOTHING i try seem to work apart from turn an led on and off by itself or turn it on using a switch.
So after spending about £200 on all this stuff and six monthswith nothing to show its time I feel to pull the plug. I did goto microchip direct but got sort of try here try there (as you do ) which is fine if iwas week one day one on this but after 6+months now I have read just about every tutorial on PICS that are out there I have five books on it and still nothing makes any sense.
I learn pretty quickly but this one is just totally beyond me. and thats pretty hard to do seeing some of the things I have repaired in my time..
Many thanks for your help Jon

Allen
 
Your assessment is dead wrong. If you still think you're right, watch this video -

https://www.youtube.com/watch?v=RGPFUBxctP8

Now let's test your PIC. Assemble and load this code as shown below, then connect an LED with a 330R series resistor between any pin on PORTB and the (-) rail of your supply. It should flash the LED. If it does this, then the PIC is fine and something is either wrong with your hardware or your code. If this turns out to be the case, post your complete code and I will help you troubleshoot it.

Code:
		list		p=16F628A, r=dec, w=-302
		include		<P16F628A.INC>
		__config	_LVP_OFF & _BOREN_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT

		

		cblock		0x20
				COUNT1
				COUNT2
				COUNT3
		endc

		org		0x000		;reset vector
		goto		START

START		movlw		0x07		;disable on chip comparator
		movwf		CMCON
		clrf		PORTA		;clear port A output latch
		clrf		PORTB		;clear port B output latch
		banksel		TRISA		;bank 1
		clrf		TRISA		;PORTA all outputs
		clrf		TRISB		;PORTB all outputs
		banksel		0		;bank 0

MAIN		comf		PORTB,F		;toggle bit states in PORTB
		call		Delay
		goto		$-2

Delay		movlw		0xFF
		movwf		COUNT1
		movwf		COUNT2
		decfsz		COUNT1,F
		goto		$-1
		decfsz		COUNT2,F
		goto		$-3
		return

		end
 
K give me five mins cheers jon . I know my assumtion IS probably wrong and that it layssomewhere in the coding imjust stuffed to find where as there is actually NO info anywhere as to the migration between chips they just refer back to obsolete datasheets for pics that are either well out of date or unavailible

Allen
 
Yup that blinks the led at about half a second pulse rate on all port b pins

Way to go that THREE programs i got working now (lol) one turns an led on plain one turns ledon with a switch and this one cool

Many Thanks
Allen
 
OK this means that the PIC is OK as is the config word.

A note on "migration" from the F628 to the F628A....there IS no "migration" needed. The ONLY difference is how they implement the code protect bits in the config word. But we're not even messing with those bits so we need not worry about that.

Now...what this tells us is that the PIC is good and that there must be something incorrect in either your hardware or your software. Can you please post your COMPLETE code and let me take a look?
 
Last edited:
K this wasnt written by me its a take on Nigels tutial .3 IR Control
but it does what I want (in the simulator at least and Ican actually read the bits ofcode where i can change the command codes)
The receiver module is a vishay tsop 4836 (basicly the same though at 36khZ giving out a logic1 until remote key is pressed when it goes logic0 Receiver connects to port A0 portB is all outputs toggled on / off with remote. This is as is and has a few errors in there (wrong case being used, so it is totally unabridged or mutilated by me{ LOL if you get my meaning)
View attachment 61335

Code:
;**********************************************************************


	list      p=16f628            ; list directive to define processor
	#include <p16f628.inc>        ; processor specific variable definitions
	
	__CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _XT_OSC & _MCLRE_ON & _LVP_OFF

; '__CONFIG' directive is used to embed configuration data 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
w_temp        	EQU     0x30        ; variable used for context saving 
status_temp   	EQU     0x31        ; variable used for context saving
irerror			EQU		0x32		; variable used for possible errors while receiving
irtimer			EQU		0x33		; variable to save the bitlenght time
ircounter		EQU		0x34		; variable used as a counter for the bits
ircmd			EQU		0x35		; variable wich hold the received command
iradr			EQU		0x36		; variable wich hold the received address
irtemp			EQU		0x37		; variable used for calc ircommand to outputs
delay1			EQU		0x38
delay2			EQU		0x39

led				EQU		0x01		; variable to assign LED output
ir				EQU		0x00		; variable to assign IR input




;**********************************************************************
		ORG     0x000             ; processor reset vector
 		goto    main              ; go to beginning of program


		ORG     0x004             ; interrupt vector location
		movwf   w_temp            ; save off current W register contents
		movf	STATUS,w          ; move status register into W register
		movwf	status_temp       ; save off contents of STATUS register


; isr code can go here or be located as a call subroutine elsewhere


		movf    status_temp,w     ; retrieve copy of STATUS register
		movwf	STATUS            ; restore pre-isr STATUS register contents
		swapf   w_temp,f
		swapf   w_temp,w          ; restore pre-isr W register contents
		retfie                    ; return from interrupt


init	CLRF 	PORTA				;Initialize PORTA by setting
									;output data latches
		BSF		PORTA,LED
		MOVLW 	0X07 				;Turn comparators off and
		MOVWF 	CMCON				;enable pins for I/O
									;functions
		BCF 	STATUS, RP1
		BSF 	STATUS, RP0 		;Select Bank1
		MOVLW 	b'11111101'			;Value used to initialize
									;data direction
		MOVWF 	TRISA 				;Set RA<1> as output
									
		MOVLW 	b'00000000'			;Value used to initialize
									;data direction
		MOVWF 	TRISB 				;Set Rb<7:0> as outputs
									
		BCF		STATUS, RP0			;Select bank 0
		return						;return from subroutine

read_sony   	BSF     irerror,0		;Set the errorbit (will be cleared later when receiving a good code)
            	BCF     INTCON,GIE		;Disable interupts temporary
            	CLRWDT					;Clear the watchdog timer
            	BSF     STATUS,RP0		;Select bank1
            	MOVLW   b'10000101'		;Sets the prescaler and timer
            	MOVWF   OPTION_REG	
            	BCF     STATUS,RP0		;Select bank0
            	BTFSC   PORTA,IR		;If it was just a short parasite on the ir line skip reading code with error.
            	GOTO    end_sony
            	CLRF    TMR0			;Clear timer to measure startbit lenght
sony_st1    	BTFSS   PORTA,IR		;Measure the startbit
            	GOTO    sony_st1
            	MOVF    TMR0,0			;move value of bitlength to irtimer
            	MOVWF   irtimer
            	MOVLW   d'32'			;If startbit lenght less then 2ms then end reading code with error.
            	SUBWF   irtimer,0
            	BTFSS   STATUS,C
            	GOTO    end_sony			
            	MOVLW   d'213'			;If startbit was longer then 2,6ms then end reading code with error.
            	ADDWF   irtimer,0		;Because when not valid startbit ir code is not sony protocol
            	BTFSC   STATUS,C		;So startbit must be between 2ms and 2,6ms.
            	GOTO    end_sony				
            	MOVLW   d'7'			;Set ir counter to receive 7 commandbits.
            	MOVWF   ircounter
rd_sony_cmd 	CLRF    TMR0			;Measure the first part of the bitlength: If if is not between 300 and 900µs
sony_prt1_c 	BTFSC   PORTA,IR		;Then exit inmediatly the routine with an error
            	GOTO    sony_prt1_c
            
				MOVF    TMR0,0
            	MOVWF   IRTIMER
				
            	MOVLW   d'5'
            	SUBWF   irtimer,0
            	BTFSS   STATUS,C
            	GOTO    end_sony
			
            	
				MOVLW   d'241'
            	ADDWF   irtimer,0
            	BTFSC   STATUS,C
            	GOTO    end_sony
            	CLRF    TMR0
				
            	CLRWDT					;Clear the watchdog
sony_prt2_c 	BTFSS   PORTA,IR		;Measure part2 of the command bits and test if the length is between 300 and 1600µs
            	GOTO    sony_prt2_c		;If between these ranges test if second bitpart is shorter or longer than 900µs
            	MOVF    TMR0,0
            	MOVWF   IRTIMER
            	MOVLW   d'5'
            	SUBWF   irtimer,0
            	BTFSS   STATUS,C
            	GOTO    end_sony
            	MOVLW   d'230'
            	ADDWF   irtimer,0
            	BTFSC   STATUS,C
            	GOTO    end_sony
            	MOVLW   d'241'
            	ADDWF   irtimer,0
            	BTFSC   STATUS,C
            	GOTO    sony_its_1
sony_its_0  	BCF     STATUS,C		;if shorter than 900 µs the received bit is a zero
            	RRF     IRCMD,1
            	GOTO    nxt_sony_cmd
sony_its_1  	BSF     STATUS,C
             	RRF     IRCMD,1
nxt_sony_cmd	DECFSZ  ircounter,1		;test if all command bits are processed if not receive next
            	GOTO    rd_sony_cmd		;if longer than 900µs the received bit is a one
            	BCF     STATUS,C
            	RRF     IRCMD,1			;Rotate ircmd one position to become correct command
				
            	MOVLW   d'5'			;set ircounter to receive 5 address bits
            	MOVWF   IRCOUNTER
rd_sony_adr  	CLRF    TMR0
sony_prt1_a  	BTFSC   PORTA,IR		;measure firtst bitlenght if shorter than 300 µs or longer than  900µs
            	GOTO    sony_prt1_a		;then exit inmediatly with error
            	MOVF    TMR0,0
            	MOVWF   IRTIMER
            	MOVLW   d'5'
            	SUBWF   irtimer,0
            	BTFSS   STATUS,C
            	GOTO    end_sony
            	MOVLW   d'241'
            	ADDWF   irtimer,0
            	BTFSC   STATUS,C
            	GOTO    end_sony
            	CLRF    TMR0
            	CLRWDT					;clear watchdog timer
sony_prt2_a  	BTFSS   PORTA,IR		
            	GOTO    sony_prt2_a		;measure second bitlenght if shorter than 300µs or longer than 1600µs
            	MOVF    TMR0,0			;then exit with error
            	MOVWF   IRTIMER			;If between these ranges test if the bit is a one or zero (shorter or longer than 900µs)
            	MOVLW   d'5'
            	SUBWF   irtimer,0
            	BTFSS   STATUS,C
            	GOTO    end_sony
            	MOVLW   d'230'
            	ADDWF   irtimer,0
            	BTFSC   STATUS,C
            	GOTO    end_sony
            	MOVLW   d'241'
            	ADDWF   irtimer,0
            	BTFSC   STATUS,C
            	GOTO    sony_its_1_a
sony_its_0_a   	BCF     STATUS,C		;if second bitpart was shorter than 900µs it's a zero
            	RRF     IRADR,1
            	GOTO    nxt_sony_adr
sony_its_1_a  	BSF     STATUS,C		;if second bitpart was longer than 900µs it's a one
            	RRF     IRADR,1
nxt_sony_adr  	DECFSZ  ircounter,1		;Test if all bits are processed if not receive next
            	GOTO    rd_sony_adr
            	BCF     STATUS,C		;Rotate tree time the iradr to become the correct address
            	RRF     IRADR,1
            	BCF     STATUS,C
            	RRF     IRADR,1
            	BCF     STATUS,C
            	RRF     IRADR,1
            	CLRF    irerror			;Because every bitlength was measured between correct limits no errors occured
				bcf		portA,led

end_sony    	BCF     INTCON,T0IF		;Clear timer0 interrupt flag to avoid an interrupt
            	BSF     STATUS,RP0
            	MOVLW   b'00000101'
            	MOVWF   OPTION_REG
            	BCF     STATUS,RP0
            	RETURN

ir_to_outp		movf	ircmd,W			;Save ircmd in irtemp to prevent loosing the received code
				movwf	irtemp			
				sublw	d'10'			;Check if ir command is higher or equal then 10 if so exit and do nothing
				btfss	STATUS,C
				goto 	end_output
				movf	irtemp,W		;Check if ir command is 9 (button 0) if so clear all outputs
				sublw	d'9'
				btfss	status,Z
				goto	not_button0
				clrf	portB
				goto	end_output				
not_button0		movf	irtemp,W		;Check if ir command is 0 (button1) if so toggle bit 0
				btfss	status,Z
				goto	not_button1
				movlw	b'00000001'
				xorwf	PORTB,1					
				goto	end_output		
not_button1		movf	irtemp,W		;Check if ir command is 1 (button2) if so toggle bit 1
				sublw	d'1'
				btfss	status,Z
				goto	not_button2
				movlw	b'00000010'
				xorwf	PORTB,1
				goto	end_output
not_button2		movf	irtemp,W		;Check if ir command is 2 (button3) if so toggle bit 2
				sublw	d'2'
				btfss	status,Z
				goto	not_button3
				movlw	b'00000100'
				xorwf	PORTB,1
				goto	end_output
not_button3		movf	irtemp,W		;Check if ir command is 3 (button4) if so toggle bit 3
				sublw	d'3'
				btfss	status,Z
				goto	not_button4
				movlw	b'00001000'
				xorwf	PORTB,1
				goto	end_output
not_button4		movf	irtemp,W		;Check if ir command is 4 (button5) if so toggle bit 4
				sublw	d'4'
				btfss	status,Z
				goto	not_button5
				movlw	b'00010000'
				xorwf	PORTB,1
				goto	end_output
not_button5		movf	irtemp,W		;Check if ir command is 5 (button6) if so toggle bit 5
				sublw	d'5'
				btfss	status,Z
				goto	not_button6
				movlw	b'00100000'
				xorwf	PORTB,1
				goto	end_output				
not_button6		movf	irtemp,W		;Check if ir command is 6 (button7) if so toggle bit 6
				sublw	d'6'
				btfss	status,Z
				goto	not_button7
				movlw	b'01000000'
				xorwf	PORTB,1
				goto	end_output
not_button7		movf	irtemp,W		;Check if ir command is 7 (button8) if so toggle bit 7
				sublw	d'7'
				btfss	status,Z
				goto	end_output
				movlw	b'10000000'
				xorwf	PORTB,1
				goto	end_output
end_output		return

delayrout1		movlw	d'255'
				movwf	delay2
looping2		movlw	d'255'
				movwf	delay1
looping1		nop
				nop
				decfsz	delay1,1
				goto	looping1
				decfsz	delay2,1
				goto 	looping2
				return

main			call	init
test			call	read_sony
				btfsc	irerror,0
				goto 	test
				call	ir_to_outp
				call	delayrout1
				goto 	test

; remaining code goes here



		END                       ; directive 'end of program'

After 6 months +ofworking on this I may finally be getting somewhere

Many Thanks

Allen
 
OK...are you using a crystal oscillator as shown in the schematic?
 
It also I belive "ignores" the address code and only sees the command codes (whih is fine as the remote I`m going to use sends out one command on one address and another on another address.
It simulates in proteus perfectly with a "standard of the web transmitter set to whatever command codes you bang into the receiver and the proteus IRlink
Proteus only has the 628a not a 628 and like i say it works fine (so in theroy it should work in practice ) with a bit of modifying for the config side.
It just wont go on a breadboard though
Allen
 
OK...are you using a crystal oscillator as shown in the schematic?

Allen, this is an important question. If you're trying to use a 4 MHz crystal on a solderless breadboard, this might very well be your problem...

Good luck...

<added>

May I ask why the code you posted here is not the same as the code you posted on forum.microchip?
 
Last edited:
OK and are you 100% positive that your hardware/wiring is all correct?

I honestly have never experienced an issue with crystals on bread boards FWIW.
 
Last edited:
No i have tried changing it to intorc_osc (which if I`m right is the 4mhz internal clock)
but I have also tried it with the crystal to no avail.
I would prefer NOT to use external crystal if at all possible i dont like them and never have done too "perfect world"for my liking i have spent many unhappy hour changing them in various things because they gone pop for some reason. and "lock the equipment up" (buggers to indentify when they misfire too had that a lot too)
 
I quite honestly prefer a xtal oscillator. Not sure why you've had issues with them. So far everything points to something you're not doing correctly. But hopefully we can rectify this soon so you can be on your way to new discoveries in embedded electronics.
 
Yup Exactly as per schematic only difference is a vishay tsop 4836 instead (but basicly the same any 36Khz normally high to pull low receiver should work)
TSOP looking at face Pin 1 RIGHT hand side to + centre Pin 2 to - pin 3 Output to RA0 PIC pin 17. led between the pin 1 +led and pin 3 -led pulses led ON when remote is pressed ( which I`mled to believe on the datasheet is correct + until activated when it pulls low -
Outputs +led to pic PORTB -led to gnd
100nF decoupling cap between + & -
triedit both with mclr (pin4) connected +via 10k resistor) and left open
fed with a 7805 regulator 12vdc from fully charged battery.
I did a test program to check all the outputs lit correctly (they did) so im pretty certain theres no problems with the connection side of things ( it simulates on those connections apart from the confirm led which stays off??? not certain why unless it is not reading the signal right in the code)
 
Last edited:
Sounds like you've got the correct connections but your pin numbers are off...

**broken link removed**
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top