Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Binary Counting on PORTB to 255, PIC16

Status
Not open for further replies.

jamesr

Newbie level 6
Newbie level 6
Joined
Dec 4, 2013
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
136
Hi,

Probably a simple question, but I have written a program to count in binary (i.e. output to LED's on PORTB) which is fairly simple and counts from 0 to 16. This works fine in simulation and when programmed onto a PIC16F877A running on a multi matrix eblocks device. I.e the LED's count up in binary.

I now have to change this to count to 255, which again should be straight forward. I have changed the counting code to go up to 255 and from looking at the values assigned to PORTB at 255, it is indeed b'11111111' . I am using MPLAB for the simulation and can observe the value 255 in the PORTB register. However when I deploy this code to the chip, it doesn't seem to light up the bit 6,7 LED's.

I have been told that in order to count up to the maximum (8 bits) that some additional setup/configuration is required for the PIC, but I am unable to find this information anywhere or not sure where to look, I have the data sheet but I'm not sure what config I should be looking for?

Does any one know or is aware of additional setup's that are required for counting to 255 with an 8 bit register on PORTB?

Thanks
James
 
Last edited:

first of all post the code second is post the pins of PORTB If the portb is having any other operation in the last pin you shd look and turn off the other option..
 

Is your code like this?


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
unsigned int i = 0;
 
while(1){
 
    for(i = 0; i < 256; i++){
        PORTB = i;
        Delay_ms(500);
    }
 
}

 

Thanks, in order to simplify the program I have used the below just to light up all LED's on all the ports as a test. However I get the same issue.


When you say the Pins have other options, from what I understand the TRISB command sets all PORTB pins to output. The data sheet states the pins RB6,7 have PGD/PGC on them, are you saying that I need to configure them so they are output pins and not using PGC/PGD?

BANKSEL TRISB
CLRF TRISB
BANKSEL PORTB
CLRF PORTB


test

movlw b'11111111'
movwf PORTB
goto test

- - - Updated - - -

Hi,

No, I am writing this using Assembler, code posted.

Thanks
 

I think the semicolon depends on the compiler you are using. I have not posted the full code, yes there is a start/end. The code above works, as in compiles and makes most of the LED's light up. I think the point here is about configuration of the PINS on PORTB that I'm not aware of?

Would this be a fair assumption? I can't see in the data sheet any mention of config in order to use the PORTB pins as outputs other than using TRISB to set the I/O direction.
 

Hi,

Post you complete code including the Configuration parameters, then we can see whats going wrong.

Also can you show the MM board /s you are using, it could be that something is reserving RB6/7 for programming, though its more normal for a dev board to release them back to the user after programming.


Edit - Are you running from the 877a chip in Debug mode ?
 
Last edited:
  • Like
Reactions: jamesr

    jamesr

    Points: 2
    Helpful Answer Positive Rating
Code:
list		p=16f877A	; list directive to define processor
	#include	<p16f877A.inc>	; processor specific variable definitions
	
	__CONFIG _CP_OFF & _DEBUG_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _WRT_OFF & _LVP_OFF & _CPD_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	0x7D		; variable used for context saving 
status_temp	EQU	0x7E		; variable used for context saving
pclath_temp	EQU	0x7F		; variable used for context saving			


;**********************************************************************
	ORG     0x000             ; processor reset vector

	nop			  ; nop required for icd
  	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	PCLATH,w	  ; move pclath register into w register
	movwf	pclath_temp	  ; save off contents of PCLATH register

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

	movf	pclath_temp,w	  ; retrieve copy of PCLATH register
	movwf	PCLATH		  ; restore pre-isr PCLATH register contents
	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


main

; remaining code goes here


;	*** main code goes here ***

		BANKSEL	TRISB
		CLRF	TRISB
		BANKSEL	PORTB
		CLRF	PORTB

	test

		movlw b'11111111'
		movwf PORTB
		goto test


	END                       ; directive 'end of program'

- - - Updated - - -

Hi,

I have since added _DEBUG_OFF and _LVP_OFF as per the data sheet for the multi function pins on port B.

however I still can't get the Lit up. I've programmed and removed the connection to the eblocks and reset etc..

Do you think I might have missed another config value?

The eblocks is EB-006.

thanks
 
Last edited by a moderator:

This is a total stab in the dark, but since the DEBUG bit is active low, I'd try _DEBUG_ON ... or try setting all the config bits manually.

Do you prefer writing assembler to C? I haven't used assembler for about 10 years!
 

Hi,

Your code looked fine, but to be sure I built it, OK, then programmed in to a chip and it does work on all 8 bits of PortB

On my diy dev board rb6 and rb7 has to be physically switched between either the programmer or the dev board.

Was looking at the pdf for that e006 dev board the other day, its no too clear if anything disconnects the programmer from the those 2 pins but neither does it say you cannot uses them in the rest of your circuit.
While I do have a switch to disconnect those 2 pins from the programmer, it is possible to isolate them enough with components though again could not see anything clearly in that manual

Do you still have to use MM own programming software or part of MPLAB ? - either way, after programming is there a menu option to turn off the programmer /debugger ? - the debugger also uses rb6/7.

I did purchase an older MM dev board and was able to use their forum, perhaps worth trying as they seemed helpful at the time.


Assume you can get your code to work on all 8 bits of PortD , yes ?

Just added a delay routine to your code so its easier to test, the 4s delay is based on a 4meg crystal .


Code:
	list p=16f877A ; list directive to define processor
	#include <p16f877A.inc> ; processor specific variable definitions

	__CONFIG _CP_OFF & _DEBUG_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _WRT_OFF & _LVP_OFF & _CPD_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 0x7D ; variable used for context saving
status_temp EQU 0x7E ; variable used for context saving
pclath_temp EQU 0x7F ; variable used for context saving


	cblock	0x20		;  another way to specify user registers
	d1,d2,d3			;  delay work files
	endc



	;************************************************* *********************
	ORG 0x000 ; processor reset vector
	
	nop ; nop required for icd
	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 PCLATH,w ; move pclath register into w register
	movwf pclath_temp ; save off contents of PCLATH register
	
	; isr code can go here or be located as a call subroutine elsewhere
	
	movf pclath_temp,w ; retrieve copy of PCLATH register
	movwf PCLATH ; restore pre-isr PCLATH register contents
	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
	
	
main
	
	; remaining code goes here
	
	
	; *** main code goes here ***
	
	BANKSEL TRISB
	CLRF TRISB
	BANKSEL PORTB
	CLRF PORTB
	
test
	
	movlw b'11111111'
	movwf PORTB
	call	DELAY4s
	comf	PORTB
	call	DELAY4s
	goto test
	
	
;  SUBROUTINES
	
DELAY4s						; 4 SECOND DELAY
		movlw	0x23
		movwf	d1
		movlw	0xB9
		movwf	d2
		movlw	0x09
		movwf	d3
Delay_0
		decfsz	d1, f
		goto	dly1
		decfsz	d2, f
dly1	goto	dly2
		decfsz	d3, f
dly2	goto	Delay_0
	
		return

	END ; directive 'end of program'
 
  • Like
Reactions: jamesr

    jamesr

    Points: 2
    Helpful Answer Positive Rating
Hi,

Thanks for trying the program, yes I've moved the PORT to D and it's working ok. Seems there must be something with the eblocks that I need to figure out. I thought the debug off command should have done something but it hasn't. I can run it on PORTD fine, just need to find out what the block issue is.

Thanks
James
 

Hi,

Seems thats where the problem is..

Did find this Old quote in the MM forum

Apart from the config statement it in your code it might be overidden by a setting in your MM software .

5) Background debug (if applicable to your target device). Leave this Disabled. If left Enabled this will use pins RB6 and RB7 and you will get some puzzling results if any I/P or O/P device are connected to these pins.

Also looked at the forum registration page, no restrictions about joining so should post your problem in there, sure you will get a quick answer even if you have not bought the board direct from them.
 

Hi,

Thanks for the all the help, wp100 you were right about the debug on the board, it turns out there was a jumper setting for ICD that needed to be disconnected once programmed. Not obvious in the use guide either so just by trying different switches I got it to work.

Many thanks.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top