Programming help ...!!

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Hello everyone,

I want to learn PIC assembly coding, i have completed few testing with LED blinking!!
I need help in Timer of PIC16f676 how to count the input pulse from a switch..
I have seen its data-sheet but i am not getting how to use it, please help me to use TIMER register.
or suggest a nice tutorial for learning it and more of PIC16f676.


Thanks
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Hi,

I have seen that tutorial and it help also. So, i need to make a counter which will read external switch upto some digit.
so, I know a instruction for testing bit of uC called BTFSS/C PORTA/C, BIT ...so, how to set the timer with this instruction or something similar to this??
 

MMcLaren

Joined Feb 14, 2010
861
Hi Ritesh,

May I ask how you want to display the count? Will you use discrete LEDs for a binary count of 0 to 7 (three LEDs) or 0 to 15 (four LEDs), or will you use a 7-segment display for a decimal count of 0 to 9? Also, are you using two switches ('up' and 'down'), and if so, what pins are they connected to and are they "active low" or "active high"?

You could use a delay subroutine or delay subsystem instead of TMR0 or TMR1 but if you really want to try and use one of the timers, we can probably help you figure out a strategy or method for using them.

Regards, Mike
 

be80be

Joined Jul 5, 2008
2,072
The timer can count on T0CKI pin you can gate the pin and count a period of time witch can be used for lots of things.

Or just count a pin and save in a variable using a loop.
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
HI, i have seen and read about timer set bit but there a one doubt how to increase timer register value by external switch or input pin??
 

Felo

Joined Feb 20, 2012
91
Sorry, I'm late I was making myself some lunch there it goes

This is the core file

Rich (BB code):
LIST P=16F676
#INCLUDE P16F676.INC
RADIX HEX
__CONFIG 3F84H		;OSC 
ERRORLEVEL -302
ERRORLEVEL -305

#INCLUDE 16F676 TMR0 COUNT EXT.INC

;INICIO CODIGO
ORG	0X000
GOTO	SETUP
ORG	0X004 ;MANEJADOR DE INTERRUPCION
GOTO ISR

SETUP					; GOTO LABEL
;------------------
;DESHABILITAR ANALOGOS, SETUP MODO FUNCIONAMIENTO
	BANCO0
	MOVLW	0X07
	MOVWF	CMCON		;APAGA COMPS, RA0,1,2,3 DIG I/O PINS.					;
;SET PORTS Y REGS PROGRAMADOR
	BANCO1
	MOVLW	B'00000100'
	MOVWF	TRISA
	MOVLW	B'00000000'
	MOVWF	TRISC
	MOVWF	ANSEL
	MOVLW	B'11000000'
	MOVWF	OPTION_REG
	MOVLW	B'10010000'	;ENABLE INTE AND GIE
	MOVWF	INTCON
	BANCO0
	CLRF	PORTA
	CLRF	PORTC
;HABILITA INTERRP PERIPHERA
;	MOVLW	B'11111000'
;	ANDWF	INTCON,F	; CLEAR ALL INTERRUPT FLAGS
;	BSF	INTCON,GIE	; ENABLE GLOBAL INTERRUPT
;	BSF	INTCON,PEIE	;HABILITA INTS PERIFERALS
;	BSF	INTCON,INTE ;HABILITA RB0 INTERR
;BORRADO REGS PROGRAMADOR
;DEL 0X20 AL 0X6F
	MOVLW 0X20
	MOVWF FSR
	CLRF  INDF
	INCF  FSR
	MOVLW 0X60 ;+1 (traslape)
	XORWF FSR,W
	BTFSS STATUS,Z
	GOTO  $-5  
	BANCO0

MAIN
	NOP
	GOTO	MAIN



ISR 
;	;-------------------------------------------------
						; FIRST WE PRESERVE W AND STATUS REGISTER
	MOVWF 	W_TEMP ;SAVE EVERYTHING
	SWAPF 	STATUS, W
	MOVWF 	STATUS_TEMP
	MOVFW 	FSR
	MOVWF 	FSR_TEMP
	MOVFW 	PCLATH
	MOVWF 	PCLATH_TEMP
;0000000000000000

SERVICE
	NOP		;SETUP SOME DEBOUNCE RUTINE HERE
	NOP		;TOGGLE RC0 FOR DEMONSTRATION ONLY
	MOVLW	B'00000001'
	XORWF	PORTC


;000000000000000
INT_EXIT
	BCF 	INTCON,INTF	; RESET THE INTF FLAG	
	MOVFW 	PCLATH_TEMP
	MOVWF 	PCLATH
	MOVFW 	FSR_TEMP
	MOVWF 	FSR
	SWAPF 	STATUS_TEMP, W
	MOVWF 	STATUS
	SWAPF 	W_TEMP, F
	SWAPF 	W_TEMP, W
	RETFIE				; RETURN FROM INTERRUPT

	RETFIE

END
This is the include file where the macros, and other preliminaries are

Rich (BB code):
 ;MACROS PARA CAMBIO DE BANCO RAPIDO
BANCO0	MACRO
		BCF	STATUS,RP0
		BCF	STATUS,RP1
		ENDM

BANCO1	MACRO
		BSF	STATUS,RP0
		BCF	STATUS,RP1
		ENDM
;++++DEFINICION EQUIVALENCIAS
#DEFINE MOD1 PORTA,1


;*******DEFINICION DE VARIABLES DEL PROGRAMADOR
CBLOCK 0X0020
D1
D2
D3
D4
ENDC

;DEFINICION VARS REGS RETORNO INTERRUPCION

CBLOCK 0X0050
W_TEMP
STATUS_TEMP
PCLATH_TEMP
FSR_TEMP
ENDC
Compile and enjoy, go ahead and ask for any directions, this is a very coarse implementation but it will get you on track

Attached there is a gif of the circuit in proteus, hope it helps
 

Attachments

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Thanks, I have MAPLAB IDE 8.33v so, the problem is that i want to simulate the code and want to watch the register value and output port as there any simulator which can do this??
as my oshon evaluation period is expired!!
 

Felo

Joined Feb 20, 2012
91
It will be awkward watching the simulation in MPLAB SIM, so I strongly recomend you get a hold of proteus, is the best tool for this and I would be able to help you a lot more that way.

Happy hunting
 

Pencil

Joined Dec 8, 2009
272
Thanks, I have MAPLAB IDE 8.33v so, the problem is that i want to simulate the code and want to watch the register value and output port as there any simulator which can do this??
as my oshon evaluation period is expired!!
Why wouldn't you be able to simulate the code in MPLAB?

This is how I would do it:

Create a project with the "Project Wizard" (see user's manual).

Enter your code and get it to build sucessfully.

Choose MPLAB SIM as your Debugger.

View file registers by choosing: View->File Registers

You can also simulate a signal on a pin by using the stimulus dialog.
Debugger->Stimulus->New Wookbook->Pin/Register actions (tab)

Obviously this is a very simplified explanation, but I hope it puts
you on the right track.
 

Felo

Joined Feb 20, 2012
91
Yes you could, but as I stated before is kind of awkward to visualise it on the SIM, specially if you don't have many milleage with it.
Proteus is better for this.
 

Pencil

Joined Dec 8, 2009
272
Yes you could, but as I stated before is kind of awkward to visualise it on the SIM, specially if you don't have many milleage with it.
Proteus is better for this.
Felo,

I was typing while you were posting. I wasn't meaning to
contradict you in any way, I didn't see your post until after
I posted. Feel free to continue, All help is welcome.
 
Last edited:

t06afre

Joined May 11, 2009
5,934
It will be awkward watching the simulation in MPLAB SIM, so I strongly recomend you get a hold of proteus, is the best tool for this and I would be able to help you a lot more that way.
Happy hunting
But Isis Proteus is far from free. So if the OP do not have access to proteus via his school. I think Proteus will be out of question:rolleyes:. I would suggest that the OP learn to use the MPLAB. By using MPLAB you can cover a lot ground then it comes to debugging.
 
Top