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 with external Crystal on PIC16F88

Status
Not open for further replies.

BlackOps

Full Member level 5
Joined
Jan 1, 2005
Messages
279
Helped
14
Reputation
28
Reaction score
3
Trophy points
1,298
Location
AZERBAIJAN
Activity points
2,496
pic16f88 external oscillator

Hello, i am remaking AN580 from Microchip. to make it work under PIC16F88. remaked the code, and have built circuit in Proteusbut it doesnt seem to work at all... can anyone see whats wrong to code? thanks
Code:
;*********************************************************************************** 
; 
; 
; 
; 
;*********************************************************************************** 

#include <p16F88.inc> 
    
__CONFIG   _CONFIG1, _CP_OFF & _CCP1_RB0 & _DEBUG_ON & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_ON & _PWRTE_ON & _WDT_OFF & _XT_OSC ;_INTRC_IO 



RESET_ADDR      EQU   0x00       
ISR_ADDR      EQU   0x04     
COMRAM_ADDR      EQU 0x70    

   CBLOCK COMRAM_ADDR 
status_temp              
w_temp                   
count                  ; first count variable 
count2                  ; second count variable 
   ENDC 


   ORG RESET_ADDR 

START 
   goto   SETUP        
   ORG      ISR_ADDR 
 

;==================================================  
PER_INT_V
BCF STATUS, RP0 ; Bank0
BTFSC PIR1, TMR1IF ; Timer1 overflowed?
GOTO T1_OVRFL ; YES, Service the Timer1 Overflow Interrupt
;
; Should NEVER get here
;
ERROR1 ; NO, Unknown Interrupt Source
BSF PORTB, 1 ; Toggle a port pin to indicate error
BCF PORTB, 1
GOTO ERROR1
;
T1_OVRFL
BCF PIR1, TMR1IF ; Clear Timer1 Interrupt Flag
MOVLW 0x80 ; Since doing key inputs, clear TMR1
MOVWF TMR1H ; for 1 sec overflow.

 ; Do Interrupt stuff here
   	movlw    b'00000001'    
   	xorwf    PORTB,f

RETFIE ; Return / Enable Global Interrupts
;================================================================
;


;==============================================================
SETUP ; POWER_ON Reset (Beginning of program)
CLRF STATUS ; Do initialization (Bank0)

;--------------------
  	banksel   TRISB 

   	movlw   b'11000000' 
   	movwf   TRISB 
    
   	banksel PORTB 
   	clrf   PORTB 
    
   	;banksel OSCCON 
   ;	movlw    b'01100000' ; should be 4 mhz 
   ;	movwf    OSCCON 
;-----------------------

BCF T1CON, TMR1ON ; Timer1 is NOT incrementing

 ; Do Initialization stuff here

MOVLW 0x80 ; TIM1H:TMR1L = 0x8000 gives 1 second
MOVWF TMR1H ; overflow, at 32 KHz.
CLRF TMR1L ;
;
CLRF INTCON
CLRF PIR1
BSF STATUS, RP0 ; Bank1
CLRF PIE1 ; Disable all peripheral interrupts
;

BSF PIE1, TMR1IE ; Enable TMR1 Interrupt
;
; Initialize the Special Function Registers (SFR) interrupts
;
BCF STATUS, RP0 ; Bank0
CLRF PIR1 ;
BSF INTCON, PEIE ; Enable Peripheral Interrupts
BSF INTCON, GIE ; Enable all Interrupts
;
MOVLW 0x0E
MOVWF T1CON ; Enable T1 Oscillator, Ext Clock, Async, prescaler = 1
BSF T1CON, TMR1ON ; Turn Timer1 ON
;


MAINLOOP 
SLEEP
GOTO MAINLOOP ; Sleep, wait for TMR1 interrupt

end
 

timer1 external pic

i have also set the processor speed to 32.768KHz, and as you see i configured pins RB7,6 as INPUT. and attached 32.768 KHZ XTAL there, is it correct?
 

pic16f88 using tmr1

I would leave the the PIC using the internal oscillator. The Timer1 counter with an external oscillator source is probably all the accuracy you need for this type of project. That PIC has a number of different oscillator modes to choose from.
 

tmr1 rtc

yes i did experiment with itnernal oscillator also,did set the appropriate bits, but its not working....its not jumping to the interrupt service routine...why?
 

how to connect oscillator for pic16f88

The obvious error is the _DEBUG_ON statement. This will disable RB6 & RB7 which happen to be the TIMER1 OSC. You cannot use an ICD with the 16F88 when using a crystal on TIMER1
 

    BlackOps

    Points: 2
    Helpful Answer Positive Rating
mikrobasic timer1 rtc

I haven't implemented the Timer1 external oscillator, so I have no personal experience with this. The datasheet for the 16F88 does show how to setup T1CON and the ISR.

I would test a couple things. First, is your PIC running? That is, are your configurations and oscillator settings okay? Next, can you create a simple interrupt using Timer1 when it's connected to the internal PIC clock? Since your using a simulator, can the simulator produce an external clock for the T1osc pins?

From the datasheet:
Code:
RTCinit
       BANKSEL  TMR1H
       MOVLW    0x80         ; Preload TMR1 register pair
       MOVWF    TMR1H        ; for 1 second overflow
       CLRF     TMR1L
       MOVLW    b’00001111’  ; Configure for external clock,
       MOVWF    T1CON        ; Asynchronous operation, external oscillator
       CLRF     secs         ; Initialize timekeeping registers
       CLRF     mins
       MOVLW    .12
       MOVWF    hours
       BANKSEL  PIE1
       BSF      PIE1, TMR1IE ; Enable Timer1 interrupt
       RETURN
RTCisr
      BANKSEL   TMR1H
      BSF       TMR1H, 7     ; Preload for 1 sec overflow
      BCF       PIR1, TMR1IF ; Clear interrupt flag
      INCF      secs, F      ; Increment seconds
      MOVF      secs, w
      SUBLW     .60
      BTFSS     STATUS, Z    ; 60 seconds elapsed?
      RETURN                 ; No, done
      CLRF      seconds      ; Clear seconds
      INCF      mins, f      ; Increment minutes
      MOVF      mins, w
      SUBLW     .60
      BTFSS     STATUS, Z    ; 60 seconds elapsed?
      RETURN                 ; No, done
      CLRF      mins         ; Clear minutes
      INCF      hours, f     ; Increment hours
      MOVF      hours, w
      SUBLW     .24
      BTFSS     STATUS, Z    ; 24 hours elapsed?
      RETURN                 ; No, done
      CLRF      hours        ; Clear hours
      RETURN                 ; Done
 

mikrobasic 32khz external oscillator

thanks for help blueroomelectronics i will notice it for future.

xorcose, i have remaked my program to work with internal 4MHz oscillator. and it doesnt work again. my formula is: Delay formula = (TMR1H:TMR1L * 4 * count) / 4 000 000

it doesnt jump to the ISR... i think problem is trivial.. and one more thing, when i was debugging it i noticed that when i do write value of b'01001000' to T1CON, T1CON gets loaded with the value of b'00001000' why is it so? why T1RUN bit is not setting? i think that if i do use internal oscillator i must set it, no?

here is the code:
Code:
;*********************************************************************************** 
; 
; Delay formula = (TMR1H:TMR1L * 4 * count) / 4 000 000
; 
; 
;*********************************************************************************** 

#include <p16F88.inc> 
    
__CONFIG   _CONFIG1, _CP_OFF & _CCP1_RB0 & _DEBUG_ON & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_ON & _PWRTE_ON & _WDT_OFF & _INTRC_IO 

RESET_ADDR      EQU   	0x00        
ISR_ADDR      	EQU   	0x04      
COMRAM_ADDR     EQU 	0x70    

   CBLOCK COMRAM_ADDR 
status_temp              
w_temp                    
count                  	; count variable 
   ENDC 

   ORG RESET_ADDR 

START 
   goto   SETUP        
   ORG      ISR_ADDR 
  

;================================================================
; START OF THE INTERRUPT SERVICE ROUTINE
;================================================================
INTERRUPT

   	movwf   w_temp            
   	swapf   STATUS, w        
   	movwf   status_temp; 

	MOVLW 	0xd8 						; Since doing key inputs, clear TMR1 
	MOVWF 	TMR1H 						; for 1 sec overflow. 
	MOVLW 	0xf0 						; Since doing key inputs, clear TMR1 
	MOVWF 	TMR1L 						; for 1 sec overflow. 
 
   	decfsz  count, f      				;decrement count, exit ISR if count iz zero 
   	goto   	INTEXIT 

   	movlw   .100                
   	movwf   count 
										; Do Interrupt stuff here 
	movlw    b'00000001'    
	xorwf    PORTB,f 

INTEXIT
	banksel	PIR1
	BCF 	PIR1, TMR1IF 				; Clear Timer1 Interrupt Flag 
   	swapf 	status_temp, w 
   	movwf 	STATUS 
   	swapf 	w_temp, f        
   	swapf 	w_temp, w 

RETFIE 									; Return / Enable Global Interrupts 
;================================================================ 
; END OF THE INTERRUPT SERVICE ROUTINE
;================================================================


;============================================================== 
; START OF THE SETUP ROUTINE (RUNS ONLY ONCE)
;================================================================

SETUP
 										; POWER_ON Reset (Beginning of program) 
	CLRF STATUS 						; Do initialization (Bank0) 

;-------------------- 
   banksel   TRISA 

    movlw   b'00000000' 
    movwf   TRISA 

   banksel  TRISB 
	;bsf		PORTA,5
    movlw   b'00000000' 
    movwf   TRISB 
    
    banksel PORTB 
    clrf   	PORTB 
    
   ;banksel OSCCON 
   ;   movlw    b'01100000' 			; should be 4 mhz 
   ;   movwf    OSCCON 
;----------------------- 

	banksel T1CON
	BCF 	T1CON, TMR1ON 				; Timer1 is NOT incrementing 

 										; Do Initialization stuff here 

	MOVLW 	0xd8 						; TIM1H:TMR1L = 0xd8f0 gives 1 second 
	MOVWF 	TMR1H 						; overflow, with: 4Mhz Crystal,count = .100, 
	MOVLW 	0xf0 						; Prescaler of 1 and Internal divide by 4
	MOVWF 	TMR1L 						  
 
	CLRF 	INTCON 
	CLRF 	PIR1 
	banksel PIE1  
	CLRF 	PIE1 						; Disable all peripheral interrupts 


	BSF 	PIE1, TMR1IE 				; Enable TMR1 Interrupt 
 
	; Initialize the Special Function Registers (SFR) interrupts 
	banksel PIR1
	CLRF 	PIR1 ; 
	movlw	b'11000000'					; GIE = 1, PEIE = 1
	movwf	INTCON


	MOVLW 	b'01001000'
	MOVWF 	T1CON 						; Enable T1 Oscillator, Int Clock/4 , prescaler = 1 
	BSF 	T1CON, TMR1ON 				; Turn Timer1 ON 

banksel		PORTA
bsf		PORTA,0
 

;================================================================
; MAIN PROGRAM START
;================================================================
MAINLOOP 
	SLEEP 
GOTO MAINLOOP ; Sleep, wait for TMR1 interrupt
;================================================================
; MAIN PROGRAM END
;================================================================ 
END
 

mikrobasic interrupts 16f88

First of all, the osc of the PIC and the osc of timer1 is totally different, you cannot use the timer1 osc to run the PIC.

If I understand the datasheet right, this PIC will run on 31kHz internal osc with your settings.
I'd recommend to sort out how to use the internal osc first (for instance do an infinite loop where you toggle a pin and do some delay in between, measuring the freq/time on the pin will give you a hint on how fast you're really running).
 

pic t1osc circuit crystal

well, i forgot to uncomment the following code:
Code:
   banksel OSCCON 
   movlw    b'01100000' 			; should be 4 mhz 
   movwf    OSCCON

now oscillator is 4Mhz... but program stil not working.. its not jumping to ISR, any ideas?
 

32768 timer1

I'll run it through MPLABs Simulator (I suggest you try that if you already haven't)
In the mean time download the Dragonfly assembly manual from my site, the demo program uses TIMER1 and a 32768Hz crystal to run a clock display. It's interrupt driven and uses the CCP1 "special event" mode to generate interrupts 512 times a second. It's for the new 16F886 but should be a breeze to modify for the 16F88

PS set bit 2 in OSCCON to make sure it turns on the internal RC clock (overrides the config setting)

so
movlw 0x62
movwf OSCCON
 

formula pic timer1

Assuming that the registers are set up correctly, which seems so, be sure that you have oscillation from the 32K crystal. Is your crystal loaded correctly with the right size capacitors?
 

pic timer1 with 32768

blueroomelectronics, no this didnt help.
xorcise, as u can see i am trying to achieve 1 second with internal 4Mhz oscillator... i am not using external crystal now... look at my ast code
thanks
 

oscillator extern timer1 proteus

If you're using the internal 4MHz clock it's only 1% accurate. No good for a real time clock. The 32768 Hz crystal is ideal though.
 

4 mhz external crystal 16f88 osccon

ok, but how about external 20MHz clock? is it very accurate as 32.768 one?
 

pic16f88 oscillator problems

No the cheap 32768 Hz watch crystal is the most accurate. Crystals are rated in PPM and the faster they go the less accurate they generally are.

Added after 1 minutes:

What are you trying to do with your 1Hz clock?
 

pic16f88 rtc

i am just experimenting with Timer1 module, need more examples, will look for ur code also.
 

timer1+pic+real time+mikrobasic

blueroomelectronics said:
No the cheap 32768 Hz watch crystal is the most accurate. Crystals are rated in PPM and the faster they go the less accurate they generally are.
That's not true.

You're probably thinking that a 40 Hz error on a 4 MHz crystal is more accurate than a 400 Hz error on a 40 MHz crystal. Both are 10 ppm errors. But when you divide those clocks down to a 1 second interval (or any interval) you'll find that both produce the exact same timing.

Using a 32.768 KHz watch crystal is an advantage when it's used with an RTC chip along with battery backup, or with special low power Timer 1 oscillator which operates in 'sleep' mode.

It's actually easier to adjust or trim higher frequency crystals. I have 20-MHz clocks which are accurate to within 6 seconds/year which is much better than what you get from most 32.768 KHz solutions.
 

    BlackOps

    Points: 2
    Helpful Answer Positive Rating
mikrobasic 16f88 oscillator 8 mhz

thanks for ur reply Mike, could u please send me sources and schmeatics of ur programs dealing with Timer1? (and if u can other timer modules too please, i cant find much in the internet) thanks very much in advance! my mail is: teslashock(at)msn.com
 

microchip rtcisr

Timer 1's oscillator block will only resonate with 32KHz crystal and a pair of 33pf caps. You cannot have a 4MHz crystal on pins RB6,7 won't work.
Have you tried running your program on MPLABs simulator? It's free and makes short work of finding problems with your code.
 

pic16f88 programming external oscillator

yes i did try, i tried it in MPLAB SIM and in PROTEUS. so it doesnt work neither there nor here. and one more question, can i use 20MHz crystal on RB6,7 then? or i must use ONLY 32.768Khz? and do i have to connect another crystal to OSI inputs? or only ONE crystal on RB6 and RB7? thanks
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top