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.

Timer1 oscillator mode

Status
Not open for further replies.

PA3040

Advanced Member level 3
Advanced Member level 3
Joined
Aug 1, 2011
Messages
883
Helped
43
Reputation
88
Reaction score
43
Trophy points
1,308
Activity points
6,936
Dear All

My attached picture for PIC 16f877a seven segment display clock using timer1 external clock

Oscillator mode is enable correctly and my actual hardware is working but Proteus does not working
Please advice, Am I connected correctly watch Crystal to T1OS0 and T1OS1 pins

when I select the internal clock source then it is working properly

Code:
bsf     T1CON,TMR1CS     ; External clock from pin RC0/T1OSO/T1CKI (on the rising edge)
			bsf		T1CON,T1OSCEN  ;Oscillator is enabled
			bcf		T1CON,T1SYNC  ;Synchronize external clock input
    		bsf     T1CON,TMR1ON     ; turn TMR1 on
    		bsf     T1CON,T1CKPS0    ; set Timer1 Pre-scale to 1:8 using
    		bsf     T1CON,T1CKPS1

osc.JPG


Please advice
 
Last edited:

Dear All

My attached picture for PIC 16f877a seven segment display clock using timer1 external clock

Oscillator mode is enable correctly and my actual hardware is working but Proteus does not working
Please advice, Am I connected correctly watch Crystal to T1OS0 and T1OS1 pins

when I select the internal clock source then it is working properly

Code:
bsf     T1CON,TMR1CS     ; External clock from pin RC0/T1OSO/T1CKI (on the rising edge)
			bsf		T1CON,T1OSCEN  ;Oscillator is enabled
			bcf		T1CON,T1SYNC  ;Synchronize external clock input
    		bsf     T1CON,TMR1ON     ; turn TMR1 on
    		bsf     T1CON,T1CKPS0    ; set Timer1 Pre-scale to 1:8 using
    		bsf     T1CON,T1CKPS1

View attachment 91519


Please advice


Hi PA,

Isis does not recognize crystals attached to the pic, be it the main oscillator or some thing like timer1.

The main oscillator is set by Right clicking the Pic chip and Editing its properties for the frequency you need.

For signal inputs like timer1 and other ports you have to use the Generator Fuction, select the type of signal you require, then Right Click the generator and edit its properties for the frequency etc as show in the picture
 

Attachments

  • timer1.jpg
    timer1.jpg
    161.3 KB · Views: 89
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
I don't use Proteus but based on peoples experiences I read on here, I wouldn't trust it. If you want to simulate, I would suggest you inject 32768Hz into T1OSCI and just trust it would work in real life with a crystal fitted.

Brian.
 

Hi PA,

Isis does not recognize crystals attached to the pic, be it the main oscillator or some thing like timer1.

The main oscillator is set by Right clicking the Pic chip and Editing its properties for the frequency you need.

For signal inputs like timer1 and other ports you have to use the Generator Fuction, select the type of signal you require, then Right Click the generator and edit its properties for the frequency etc as show in the picture


Hi wp100

Thanks for the reply
Yes, it is working now

Thanks
PA3040

- - - Updated - - -

I don't use Proteus but based on peoples experiences I read on here, I wouldn't trust it. If you want to simulate, I would suggest you inject 32768Hz into T1OSCI and just trust it would work in real life with a crystal fitted.

Brian.

Dear Brian Thanks for the reply

You mean use hardware for testing?

I would think S/W more helpful for testing and easy to build and use but not accurate as you pointed out


Thanks
PA3040

- - - Updated - - -

Dear All,

I found following command from AN580 application note

Code:
BSF TRISC, T1OSO ; RC0 needs to be input for the oscillator to function

In proteus it does not valid

Am I correct?

Please advice

- - - Updated - - -

Note: The loading of either TMR1H or TMR1L
causes the prescaler to be cleared.When
Timer1 is in operation, extreme care
should be taken in modifying either the
TMR1H or TMR1L registers, since this
automatically configures the prescaler
to 1.

Dear All the above note says that any write to TMR1L or TMR1H will reset to the timer1 prescaller to 1 : 1 Am I correct?
 

I could be wrong about TRISC but I would have thought that connecting the oscillator would disconnect the normal port input and output so the TRIS register wouldn't matter.

The prescaler CONTENTS are cleared when writing to TMR1, not the division ratio. The prescaler is essentially another counter between the clock source and TMR1, when you write to TMR1, the counter value is reset to zero. If it wasn't done that way the timing wouldn't be reliable because an unpredictable value left in the prescalers counter would reduce TMR1's accuracy.

Brian.
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear betwixt

Thanks for the reply
My actual hardware is working now with external clock crystal ( 32768Hz)
asynchronous mode,
prescaller 1:1
Timer Preloaded value 0x80( TMR1H ) both initiate and ISR

Cristal is connected very closed to the PIn's and Capacitor too. body of crystal also properly grounded

But it is not showing accurate second ( More Fast )

Then I connected two capacitors (22pF) to crystal and ground then second increment become normal and have to check accuracy, monitoring long time

Conclusion :- The accuracy depend on capacitor value. Am I correct

Can we have equation to archive correct Capacitor value

Please advice
Thanks in advance
 
Last edited:

That is correct. There is no formula for calculatig the capacitors. Their value is very low in comparison to the internal capacitances in the crystal itself, the PIC and between wiring in the construction. The value would also change with temperature as all these 'stray' capacitances are themselves prone to change with temperature as well. The classic fix is to use a small trimmer capacitor at one end of the crystal or to apply a correction in software periodically. If you use a trimmer you can either tweak it by trial and error or use a frequency counter to set the exact frequency.

For example, I have a customer with a unit located in a remote location that sends back data and a time code, the clock is not easily reached so at 03:30 every night a small +/- adjustment is applied to the seconds count to keep it accurate. The amount of adjustrment is calculated and sent to it from a master control system which is based on a cesium fountain clock (should be fairly accurate!!). Doing it at that time of the night avoids date changes, daylight savngs time starting and finishing and leap second adjustments being made.

Brian.
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear Brian Thanks for the reply

I checked the my clock around three hours and I observed that 9 seconds less than the actual

Dear all please give me suggestions to improve clock more accurate

Please advice
Thanks in advance
 

You can check it isn't a software problem, after that I'm afraid you either have to adjust it with a trimmer capacitor or use the daily correction method. Can you post your complete ISR code first please. There are some things in the code that might make it run slowly, for example there could be a few instructions before reloading the timer, although each instruction is very fast to execute, the cumulative time of being used many times may account for the slow running. Optimizing the ISR might help.

Brian.
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Hi,

I have built and used Timer1 RTC for many projects over many years and they are accurate to a couple of seconds a week.

They are built using standard parts and no software or hardware trimming.

Like the main oscillator, have you got Timer1s crystal and caps as close to the pic as possible ?
Have you built it on a breadboard, the thin leads of the crystal and caps can give contact problems in them, so loosing the signals /time

Post your code and circuit for us to see /check...
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Hi,


Post your code and circuit for us to see /check...
Dear WP100 / Brian

Please look in to the ISR in my program

Code:
ISR			call    Save_Regs 
			btfss		PIR1,TMR1IF
			goto 		Finish_Int
			bcf			PIR1,TMR1IF	
			movlw		0X80
			movwf		TMR1H			
			incf		seg1
		
			call    Restore_Regs
			retfie


Save_Regs                           
    movwf	Tmp_WREG               		
    swapf	STATUS,W                
    movwf	Tmp_STATUS             
    movf	PCLATH,W                
    movwf	Tmp_PCLATH
    return
    

Finish_Int	
Restore_Regs                        
    movf    Tmp_PCLATH,W            
    movwf   PCLATH
    swapf   Tmp_STATUS,W            
    movwf   STATUS
    swapf   Tmp_WREG,F              
    swapf   Tmp_WREG,W
    return
Timer1 prescaller set to 1 : 1

Have you built it on a breadboard, the thin leads of the crystal and caps can give contact problems in them, so loosing the signals /time

No I have design a PCB

Please help
 
Last edited:

Hi,

Your ISR looks the same as mine, wonder if your Timer1 set up is the same ?

The only other thing that could be causing a code problem is if somewhere else you are turning off the interrupts for a short while ??

Also assume you have tried a different crystal and caps ? - I just use the cheapo watch crystals.

Code:
; ***************************************************************************
; SETUP AND INIT TIMER1

	movlw	0x80			; preload tmr1h with h80 to give 1sec
	movwf	TMR1H
	clrf	TMR1L

	clrf	  INTCON
	clrf      PIR1
	bsf	  STATUS,RP0		; bank1
	clrf	  PIE1			; disable all ints
	bsf	  TRISC,0		; set bit0 as tmr osc
	bsf	  PIE1,TMR1IE		; enable tmr1 int
	bcf	  STATUS,RP0
	clrf	  PIR1
	bsf	  INTCON,PEIE		; ENABLE PERIPH INT
	bsf	  INTCON,GIE		; ENABLE GLOBAL INT
        movlw     0x0E			; set t1 functions/ osc,extck,asy,p1
	movwf     T1CON
	bsf	  T1CON,TMR1ON		; tm1 on

- - - Updated - - -
 

Attachments

  • 950969006-40.jpg
    950969006-40.jpg
    4.1 KB · Views: 71

Hi,

Your ISR looks the same as mine, wonder if your Timer1 set up is the same ?



Also assume you have tried a different crystal and caps ? - I just use the cheapo watch crystals.

Code:
; ***************************************************************************
[QUOTE]Your ISR looks the same as mine, wonder if your Timer1 set up is the same ?
[/QUOTE]

This is my Timer1 setup

[CODE]setup			
			clrf		PORTD		
			clrf		PORTC		
			banksel		TRISC		
			clrf		TRISD		
			clrf		TRISC		
			bsf		PIE1,TMR1IE		
			banksel		PORTC		
			movlw		0X80
			movwf		TMR1H			
			clrf		TMR1L		

			bsf     T1CON,TMR1CS     ; Set clock source Extranal
			bsf		T1CON,T1OSCEN	 ;1 = Oscillator is enabled
			bsf		T1CON,T1SYNC	 ;1 = Do not synchronize external clock input
    		bcf     T1CON,T1CKPS0    ; set Timer1 Pre-scale to 1:1 using
    		bcf     T1CON,T1CKPS1    ; T1CKPS0 and T1CKPS1
    		bsf     T1CON,TMR1ON     ; turn TMR1 on
			
			bcf		PIR1,TMR1IF		
			bsf		INTCON,PEIE		
			bsf		INTCON,GIE		
			
			clrf		seg1
			clrf		seg2
			clrf		seg3
			clrf		seg4
			return

The only other thing that could be causing a code problem is if somewhere else you are turning off the interrupts for a short while ??

No, Interrupt only off in the ISR ( GIE =0)

Please advice
 

The code looks OK but you can make it a little quicker. Instead of calling the routines to save and restore the registers, put them inside the ISR itself. They will only be used there anyway so all you are doing is adding an extra call and return instruction where they are not necessary. I suspect the capacitors are the problem, I suggest you replace one of them with a small 2-22pF trimmer so you can tweak the frequency exactly.

Brian.
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Hi,

What is your main oscillator frequency ? - assume it is something reasonably fast over 500Khz ?

You have not said if you tried another crystal or 2 new caps, they are easily damaged by soldering.
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
The code looks OK but you can make it a little quicker. Instead of calling the routines to save and restore the registers, put them inside the ISR itself. They will only be used there anyway so all you are doing is adding an extra call and return instruction where they are not necessary. I suspect the capacitors are the problem, I suggest you replace one of them with a small 2-22pF trimmer so you can tweak the frequency exactly.

Brian.

Dear Brian Thanks for the reply

Should you please explain how it quick when we bypass the the two subroutine ( call Save_Regs and call Restore_Regs )
Because TMR1L is incrementing?

Any latency errors?

Please advice
 

The difference will not be great but I will explain:

You should still save the STATUS and W registers because you cannot tell when the interrupt will occur withing the flow of the rest of the program and these are likely to be trashed by the ISR. Remember that the bank select bits are in the STATUS register so they may be wrong to access TMR1 at entry and when set to TMR1, they may be wrong as the main program resumes.

However, TMR1 starts counting from 0x8000 as soon as that value is loaded into it and you want it to keep counting without any breaks. Therefore for best accuracy you should re-load it as soon as possible. At the moment you are adding an unneccessary extra subroutine call and return instruction just to reach the context saving code and get back again. If you forget that code being a subroutine and place it in-line with the entry and exit of the ISR it will save several instructions delay. Incidentally, it would be a good idea to 'banksel' TMR1 before loading it. Don't forgret you can add or subtract a little from the value you load into TMR1 to slightly adjust it's timing. For example, loading it with 0x8001 will slightly speed up the clock and 0x7FFF will make it slightly slower.

Brian.
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Hi,

What is your main oscillator frequency ? - assume it is something reasonably fast over 500Khz ?

You have not said if you tried another crystal or 2 new caps, they are easily damaged by soldering.

Hi



What is your main oscillator frequency ? 4Mhz

You have not said if you tried another crystal or 2 new caps,

I changed used Japan's crystal removed from Japan fax machine mother board and genuine 10pF capacitors removed from same

and I checked about one hour and observed that 3 second lagging than actual

Please advice

- - - Updated - - -

The difference will not be great but I will explain:

You should still save the STATUS and W registers because you cannot tell when the interrupt will occur withing the flow of the rest of the program and these are likely to be trashed by the ISR. Remember that the bank select bits are in the STATUS register so they may be wrong to access TMR1 at entry and when set to TMR1, they may be wrong as the main program resumes.




However, TMR1 starts counting from 0x8000 as soon as that value is loaded into it and you want it to keep counting without any breaks. Therefore for best accuracy you should re-load it as soon as possible. At the moment you are adding an unneccessary extra subroutine call and return instruction just to reach the context saving code and get back again. If you forget that code being a subroutine and place it in-line with the entry and exit of the ISR it will save several instructions delay. Incidentally, it would be a good idea to 'banksel' TMR1 before loading it. Don't forgret you can add or subtract a little from the value you load into TMR1 to slightly adjust it's timing. For example, loading it with 0x8001 will slightly speed up the clock and 0x7FFF will make it slightly slower.

Brian.

Dear Brian Thanks for the reply
I have small thing to verify

Let's we assume in ISR if we can update TMR1H before 255 Tcy, how should it effect than loading THR1H in ISR at first

Please advice
 

The timing starts as soon as you load TMR1, the prescaler is cleared so the full period commences from the time the new value is written to the timer registers. That is why it is better to load it as soon as possible when the ISR starts so you do not introduce an additional delay between one period ending and the next one starting.

Brian.
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear All my program is now working some extend accurately
I connect Trimmer to one pin of Crystal and 220k series resistor solved the problem

I can reset the second digit and minute digit in every 59 reached to the seven segment display




Code:
ssd			clrf		PORTD
			movf		seg1,w		; get digit to display
			call		table
			movwf		PORTD
			movlw		b'10000000'	; Active Display Selection
			movwf		PORTC
			call		delay
			
			clrf		PORTD
			movf		seg2,w
			call		table
			movwf		PORTD
			movlw		b'01000000'	; Active Display Selection
			movwf		PORTC
			call 		delay

                        movlw		.10
			xorwf		seg1,w		; test first digit
			btfss		STATUS,Z
			goto		ssd
			clrf		seg1		; Clear digit1
			incf		seg2		; Increment digit2
			
			movlw		.6
			xorwf		seg2,w		; test Second digit
			btfss		STATUS,Z
			goto		ssd
			clrf		seg1		; Clear digit1
			clrf		seg2		; Clear digit2
			goto         ssd



I should need to asm code help to reset hour segment reached to 24 and 12

please advice

Thanks in advance
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top