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.

Timer1 Assembler for PICkit1

Status
Not open for further replies.

stony

New Member
I am a former AVRmega8535 programmer. a new job and now it is PICkit1 and pic12f675

Does anyone have a simple Timer1 assembler application, running on this device, switching on and off a single diode. I am programming in MPlab
 
The pickit1 Charles plexus the leds on the board I have some code for it but I have to find it
 
This is a simple code to flash a LED, don't forget to setup the PIC first, ie CMCOn and turn TMR1 on etc.

Code:
// simple PIC 12F675 4MHz TMR1 flash LED

flash_loop
	movlw 0xFF	; load number of loops
	movwf count	

	btfss TMR1H,7	; wait for TMR1
	goto $-1
	bcf TMR1H,7
	decfsz count,f	; count loops
	goto $-4
	bsf GPIO,0	; then turn LED on

	btfss TMR1H,7	; wait for TMR1
	goto $-1
	bcf TMR1H,7
	decfsz count,f	; count loops
	goto $-4
	bcf GPIO,0	; then turn LED off

	goto flash_loop
 

Attachments

  • leds are like this.PNG
    leds are like this.PNG
    69.2 KB · Views: 206
Gee I'm dumb. I thought PICKit1 was a programmer (ie like the PICKit2) that he was using to program a 12F675.

Thanks for the correction. :) And I think we might have lost the OP...
 
Test on timer1, PICkit1, PIC12F675

Thank you for your replies so far, I tried to build a Timer1 my self but unfortunaly it did not work. I have tried the interrupt alone by calling it from the main procedure, of cause it dident blink

is it posible for you to find the faults

Code:
;******************************************************************************
; test for using timer1 on a PICkit1 flashing a single diode 
;
;******************************************************************************
; processor defined ;
	list      p=12F675		 	; list directive to define processor
	#include <p12f675.inc>		; processor specific variable definitions
;======================================================================================================
; MPLAB stuff here

	LIST b=5, n=97, t=ON, st=OFF
	; absolute listing tabs=5, lines=97, trim long lines=ON, symbol table=OFF

	__CONFIG  _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _CPD_OFF
;==============================================================================

STATUS		equ 03h
TRISA		equ	85h
PORTA		equ	05h
w_temp		equ	20h
w_status	equ	21h

;==============================================================================
; Code here

	org 0x000 					; Set program memory base at reset vector 0x000 reset
	goto setup					; set up ints and port stuff

	org 0x004					; Interrupt vector, int handler code comes next.
;==============================================================================
;  INTERRUPT HANDLER     (runs this code each timer0 interrupt)
;******************************************************************************
int_handler						; first we preserve w and status register
	movwf 	w_temp      		; save off current W register contents
	movf	STATUS,w          	; move status register into W register
	movwf 	w_status      	 	; save off contents of STATUS register

	movlw 	b'00010000'			; mask for bit 3
	xorwf 	PORTA,f				; toggle PORTA,bit3 (toggle the led)

	BCF 	INTCON,T0IF			; reset the tmr0 interrupt flag

	movf 	w_status,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
;******************************************************************************
;  SETUP	(runs this only once at startup)
;******************************************************************************
setup					; goto label
;******************************************************************************
;  Timer1 --- TIMER1 CONTROL REGISTER address 10h
;******************************************************************************
	movlw b'00000001'			; Used to set up timer
		 ;  x-------			; 7, not applicable
	 	 ;  -x------			; 6, 0=>timer is on / 1=>timer is on if T1G is low
		 ;  --x-----			; 5  	prescale selection 
		 ;  ---x----			; 4		11 = 1:8 / 10 = 1:4 / 01 = 1:2 / 00 = 1:1 Prescale Value
		 ;  ----x---			; 3  1 = LP oscillator is enabled for Timer1 clock / 0 = LP oscillator is off
		 ;  -----x--			; 2  1 = Do not synchronize external clock input / 0 = Synchronize external clock input
		 ;  ------x-			; 1  1 = External clock from T1OSO/T1CKI pin (on the rising edge)/0 = Internal clock (FOSC/4)
		 ;  -------x			; 0  1 = Enables Timer1 / 0 = Stops Timer1
	movwf T1CON					; load data into OPTION_REG
;******************************************************************************
;  Timer1 --- PIE1 — PERIPHERAL INTERRUPT ENABLE REGISTER 1 --- 8Ch
;******************************************************************************
	bsf		STATUS,5			; shift to bank1 control registers
	movlw b'00000001'			; Used to enable interrupts only bit0 is used for timer 1
		 ;  -------x			; 0  1 = Enables Timer1 interrupt
	movwf 	T1CON				; load data into OPTION_REG
	bcf		STATUS,5			; shift to bank0 working registers
;******************************************************************************
;  Timer1 --- TMR1H --- 0Fh   and   TMR1L --- 0Eh
;******************************************************************************
	movlw 	22h					; Counter value ready in Working reg
	movwf 	TMR1H				; value loaded to counters high register
	movlw 	22h					; Counter value ready in Working reg
	movwf 	TMR1H				; value loaded to counters low register
;******************************************************************************
;  Port setup  
;******************************************************************************
	clrf 	PORTA				; port is Cleared no output yet
	movlw b'11001111'			; only bit 4 snd 5 are set as output rest is indput
	bsf		STATUS,5			; shift to bank1 control registers
	movwf 	TRISA				; send mask to porta
	bcf		STATUS,5			; shift to bank0 working registers

	goto 	main				; start main program
;******************************************************************************
;  MAIN     (main program loop)
;******************************************************************************
main							; Main program rest til next interupt
main_loop					; 
	nop							; in this program main do nothing
	goto main_loop			; keep running the main code.
;==============================================================================
	end					; no code after this point.
;==============================================================================
 
Last edited:
That looks like some of my ancient assembler code that you have modified. :) Where did you get it?

There are a few obvious faults, you need to seriously go through it. Your interrupt is timer0 but you seem to want timer1.

The int toggles RA4 which is NOT an output pin. You include says PIC 12F675 which doesn't have PORTA or RA4 anyway. You loaded T1CON twice, but one says PIE1. Then you load TMR1H twice.

Even if you sort out the ports and PIC include and get it to flash a LED in the interrupt it will probably flash too fast to see and just look liek the LED is on all the time.

Like I said, spend some serious effort and go right through the code a couple of times...
 
Test on timer1, PICkit1, PIC12F675

yes the code take basis in another code i think it is yours

; ZERO-ERROR ONE SECOND TIMER
; (Roman Black 2001, public domain, use it as you like)
;
; INTERRUPT VERSION
;
; for PIC 16F84 at 4 MHz (or most PICs)
; (this code has been assembled with MPLAB and hardware tested)
; (for best viewing set TABS=5 in MPLAB editor)
;
; Note! See text: Zero-error 1 second timing algorithm
;
; Generates an event every second (or other period) from any PIC
; with any clock frequency.
;
; This version uses the timer0 overflow interrupt.
; Code can be adapted for different clock speeds, period lengths
; and accuracy levels.

I have made the changes you told, but some of the comments i did
not quite understod:
here is the changes and some comments

1: i now clear the timer1 interruptflag "bit0 in PIR1"
2: You write "int toggles RA4" i thouht i used internal "clock/4" and
only output was the interrupt.
3: Port names has ben changed
PORTA as vel as PB is placed at 05H
TRISA as vel as TRISPB is placed at 85h
4: Now the T1CON, PIE1, TMR1H and TMR1L are all set op,
TMR1H and TMR1L will be tuned when i get the interrupt to work
5: About the test: When i insert the "int_handler" in "main" the
"Bit4 of GP" will flash very fast, in fact i will se a dimmed light
But i can see that if the LED flashes the interupt will work

I hope you can help me thoug

Here is the edited program
Code:
;******************************************************************************
; test for using timer1 on a PICkit1 flashing a single diode 
;
;******************************************************************************
; processor defined ;
	list      p=12F675		 	; list directive to define processor
	#include <p12f675.inc>		; processor specific variable definitions
;======================================================================================================
; MPLAB stuff here

	LIST b=5, n=97, t=ON, st=OFF
	; absolute listing tabs=5, lines=97, trim long lines=ON, symbol table=OFF

	__CONFIG  _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _CPD_OFF
;==============================================================================

STATUS		equ 03h
TRIS_GP		equ	85h
PORT_GP		equ	05h
w_temp		equ	20h
w_status	equ	21h

;==============================================================================
; Code here

	org 0x000 					; Set program memory base at reset vector 0x000 reset
	goto setup					; set up ints and port stuff

	org 0x004					; Interrupt vector, int handler code comes next.
;==============================================================================
;  INTERRUPT HANDLER     (runs this code each timer1 interrupt)
;******************************************************************************
int_handler						; first we preserve w and status register
	movwf 	w_temp      		; save off current W register contents
	movf	STATUS,w          	; move status register into W register
	movwf 	w_status      	 	; save off contents of STATUS register

	movlw 	b'00010000'			; mask for bit 3
	xorwf 	PORT_GP,f			; toggle PORTA,bit3 (toggle the led)

	BCF 	PIR1,TMR1IF			; Cleared timer1 interrupt register overflowed 

	movf 	w_status,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
;******************************************************************************
;  SETUP	(runs this only once at startup)
;******************************************************************************
setup					; goto label
;******************************************************************************
;  Timer1 --- TIMER1 CONTROL REGISTER address 10h
;******************************************************************************
	movlw b'00000001'			; Used to set up timer
		 ;  x-------			; 7, not applicable
	 	 ;  -x------			; 6, 0=>timer is on / 1=>timer is on if T1G is low
		 ;  --x-----			; 5  	prescale selection 
		 ;  ---x----			; 4		11 = 1:8 / 10 = 1:4 / 01 = 1:2 / 00 = 1:1 Prescale Value
		 ;  ----x---			; 3  1 = LP oscillator is enabled for Timer1 clock / 0 = LP oscillator is off
		 ;  -----x--			; 2  1 = Do not synchronize external clock input / 0 = Synchronize external clock input
		 ;  ------x-			; 1  1 = External clock from T1OSO/T1CKI pin (on the rising edge)/0 = Internal clock (FOSC/4)
		 ;  -------x			; 0  1 = Enables Timer1 / 0 = Stops Timer1
	movwf T1CON					; load data into OPTION_REG
;******************************************************************************
;  Timer1 --- PIE1 — PERIPHERAL INTERRUPT ENABLE REGISTER 1 --- 8Ch
;******************************************************************************
	bsf		STATUS,5			; shift to bank1 control registers
	movlw b'00000001'			; Used to enable interrupts only bit0 is used for timer 1
		 ;  -------x			; 0  1 = Enables Timer1 interrupt
	movwf 	PIE1				; load data into OPTION_REG
	bcf		STATUS,5			; shift to bank0 working registers
;******************************************************************************
;  Timer1 --- TMR1H --- 0Fh   and   TMR1L --- 0Eh
;******************************************************************************
	movlw 	22h					; Counter value ready in Working reg
	movwf 	TMR1H				; value loaded to counters high register
	movlw 	22h					; Counter value ready in Working reg
	movwf 	TMR1L				; value loaded to counters low register
;******************************************************************************
;  Port setup  
;******************************************************************************
	clrf 	PORT_GP				; port is Cleared no output yet
	movlw b'11001111'			; only bit 4 snd 5 are set as output rest is indput
	bsf		STATUS,5			; shift to bank1 control registers
	movwf 	TRIS_GP				; send mask to porta
	bcf		STATUS,5			; shift to bank0 working registers
;******************************************************************************
;  Global interupt enable 
;******************************************************************************
	movlw b'10000000'			; only bit 4 snd 5 are set as output rest is indput
		  ; 1-------			; GIE: Global Interrupt Enable bit --- 1
		  ; -1------			; PEIE: Peripheral Interrupt Enable bit --- 1
	movwf 	TRIS_GP				; send mask to porta
	goto 	main				; start main program
;******************************************************************************
;  MAIN     (main program loop)
;******************************************************************************
main							; Main program rest til next interupt
main_loop						; 
	nop							; in this program main do nothing
	goto main_loop				; keep running the main code.
;==============================================================================
	end							; no code after this point.
;==============================================================================
 
Hi again. There were still a ton of bugs in that, including your int would not happen and wrong LED and others.

It now uses the TMR1 int, and TMR1 is set for 1:8 prescale (125kHz) so it should roll over at 125000 / 65536 or about twice per second.

Make sure a LED is connected to GP4, then hopefully this will now flash a LED unless there are more bugs...

I think you might need to try some different programming techniques. When you get like 10 separate bugs in a program it probably means you are working too fast and not checking things. It helps to read the datasheet for info on every PIC register you are setting up, the 12F675 datasheet is great and has a clear easy page for each register, like for INTCON and for T1CON etc.

Anyway, good luck! :)

Code:
;******************************************************************************
; test for using timer1 on a PICkit1 flashing a single diode 
;
;******************************************************************************
; processor defined ;
	list      p=12F675		; list directive to define processor
	#include <p12f675.inc>		; processor specific variable definitions
;======================================================================================================
; MPLAB stuff here

	LIST b=5, n=97, t=ON, st=OFF
	; absolute listing tabs=5, lines=97, trim long lines=ON, symbol table=OFF

	__CONFIG  _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _CPD_OFF
;==============================================================================
; Code here

	org 0x000 			; Set program memory base at reset vector 0x000 reset
	goto setup			; set up ints and port stuff

	org 0x004			; Interrupt vector, int handler code comes next.
;==============================================================================
;  INTERRUPT HANDLER     (runs this code each timer1 interrupt)
;******************************************************************************
int_handler				; first we preserve w and status register
	movwf 	w_temp      		; save off current W register contents
	movf	STATUS,w          	; move status register into W register
	movwf 	w_status      	 	; save off contents of STATUS register

	movlw 	b'00010000'		; mask for bit 4
	xorwf 	PORT_GP,f		; toggle PORTGP,bit3 (toggle the led)

	BCF 	PIR1,TMR1IF		; Cleared timer1 interrupt register overflowed 

	movf 	w_status,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
;******************************************************************************
;  SETUP	(runs this only once at startup)
;******************************************************************************
setup					; goto label
;******************************************************************************
;  Timer1 --- TIMER1 CONTROL REGISTER address 10h
;******************************************************************************
	movlw b'00110001'		; Used to set up timer
	     ;  x-------		; 7, not applicable
	     ;  -x------		; 6, 0=>timer is on / 1=>timer is on if T1G is low
	     ;  --x-----		; 5  	prescale selection 
	     ;  ---x----		; 4  11 = 1:8 / 10 = 1:4 / 01 = 1:2 / 00 = 1:1 Prescale Value
	     ;  ----x---		; 3  1 = LP oscillator is enabled for Timer1 clock / 0 = LP oscillator is off
	     ;  -----x--		; 2  1 = Do not synchronize external clock input / 0 = Synchronize external clock input
	     ;  ------x-		; 1  1 = External clock from T1OSO/T1CKI pin (on the rising edge)/0 = Internal clock (FOSC/4)
	     ;  -------x		; 0  1 = Enables Timer1 / 0 = Stops Timer1
	movwf T1CON			; load data into OPTION_REG
;******************************************************************************
;  Timer1 --- PIE1  PERIPHERAL INTERRUPT ENABLE REGISTER 1 --- 8Ch
;******************************************************************************
	bsf	STATUS,5		; shift to bank1 control registers
	movlw b'00000001'		; Used to enable interrupts only bit0 is used for timer 1
	     ;  -------x		; 0  1 = Enables Timer1 interrupt
	movwf 	PIE1			; load data into OPTION_REG
	bcf	STATUS,5		; shift to bank0 working registers
;******************************************************************************
;  Timer1 --- TMR1H --- 0Fh   and   TMR1L --- 0Eh
;******************************************************************************
	movlw 	0h			; Just clear TMR1 and let it roll each flash
	movwf 	TMR1H			; 
	movlw 	0h			; 
	movwf 	TMR1L			; 
;******************************************************************************
;  Port setup  
;******************************************************************************
	clrf 	PORT_GP			; port is Cleared no output yet
	movlw b'00000000'		; GP4  = output to LED
	bsf	STATUS,5		; shift to bank1 control registers
	movwf 	TRIS_GP			; send mask to porta
	bcf	STATUS,5		; shift to bank0 working registers
;******************************************************************************
;  INTCON Global interupt enable 
;******************************************************************************
	movlw b'11000000'			; only bit 4 snd 5 are set as output rest is indput
	      ; 1-------			; GIE: Global Interrupt Enable bit --- 1
	      ; -1------			; PEIE: Peripheral Interrupt Enable bit --- 1
	movwf 	INTCON				; 
	goto 	main				; start main program
;******************************************************************************
;  MAIN     (main program loop)
;******************************************************************************
main						; Main program rest til next interupt
main_loop					; 
	nop					; in this program main does nothing
	goto main_loop				; keep running the main code.
;==============================================================================
	end					; no code after this point.
;==============================================================================
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top