Subversion Repositories svn.mios

Rev

Rev 52 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
53 tk 1
; $Id: mios_timer.inc 53 2008-01-30 22:52:41Z tk $
1 tk 2
;
3
; MIOS Timer Routines
4
;
5
; ==========================================================================
6
;
7
;  Copyright 1998-2006 Thorsten Klose (tk@midibox.org)
8
;  Licensed for personal non-commercial use only.
9
;  All other rights reserved.
10
;
11
; ==========================================================================
12
 
13
;; --------------------------------------------------------------------------
14
;;  FUNCTION: MIOS_TIMER_Init
15
;;  C_DECLARATION: void MIOS_TIMER_Init(unsigned char mode, unsigned int period)
16
;;  DESCRIPTION: initializes the timer which calls USER_Timer periodically
17
;;  and starts it. The resolution of the timer is 100nS with Prescaler 1:1,
18
;;  200nS with 1:2, 400nS with 1:4, 800nS with 1:8. The period (number of
19
;;  clocks) is specified as 16 bit value.
20
;;  IN:	  prescaler value in WREG:
21
;;           0x00: 1:1
22
;;           0x01: 1:2
23
;;           0x02: 1:4
24
;;           0x03: 1:8
25
;;        number of ticks (low-byte) in MIOS_PARAMETER1
26
;;        number of ticks (high-byte) in MIOS_PARAMETER2
27
;;  C_IN: prescaler value in <mode>:
28
;;           0x00: 1:1
29
;;           0x01: 1:2
30
;;           0x02: 1:4
31
;;           0x03: 1:8
32
;;        number of ticks (0..65535) in <period>
33
;;  OUT:  -
34
;;  C_OUT:  -
35
;;  USES: BSR
36
;;  EXAMPLE:
37
;;	;; we want to setup the timer with a frequency of 500 Hz = 2 mS
38
;;	;; prescaler 1:1 should be used
39
;;	;; calculate the required number of clocks for this period:
40
;;	;; clocks = period / 100 nS = 2 mS / 100 nS = 20000
41
;;	;; calculate low and high byte:
42
;;	;;    low byte  = period & 0xff
43
;;	;;    high byte = period >> 8
44
;;	;; therefore:
45
;;	movlw	20000 & 0xff
46
;;	movwf	MIOS_PARAMETER1
47
;;	movlw	20000 >> 8
48
;;	movwf	MIOS_PARAMETER2
49
;;	movlw	0x00
50
;;	call	MIOS_TIMER_Init
51
;;
52
;;	;; now the USER_Timer function is called every 2 mS!
53
;;  C_EXAMPLE:
54
;;	// we want to setup the timer with a frequency of 500 Hz = 2 mS
55
;;	// prescaler 1:1 should be used
56
;;	// calculate the required number of clocks for this period:
57
;;	// clocks = period / 100 nS = 2 mS / 100 nS = 20000
58
;;	// therefore:
59
;;	MIOS_TIMER_Init(0x00, 20000);
60
;;
61
;;	// now the Timer() function is called every 2 mS!
62
;; --------------------------------------------------------------------------
63
MIOS_TIMER_Init
64
	;; load prescaler value to T3CON and stop timer
65
	swapf	WREG, W
66
	andlw	0x30
67
	movwf	T3CON
68
 
69
	;; copy reload values
70
	SET_BSR	MIOS_TIMER3_RELOAD_H
71
	comf	MIOS_PARAMETER2, W
72
	movwf	MIOS_TIMER3_RELOAD_H, BANKED
73
 	movwf	TMR3H
74
 
75
	comf	MIOS_PARAMETER1, W
76
	movwf	MIOS_TIMER3_RELOAD_L, BANKED
77
 	movwf	TMR3L
78
 
79
	;; start timer
80
	bsf	T3CON, TMR3ON
81
	return
82
 
83
;; --------------------------------------------------------------------------
84
;;  FUNCTION: MIOS_TIMER_ReInit
85
;;  C_DECLARATION: void MIOS_TIMER_ReInit(unsigned char mode, unsigned int period)
86
;;  DESCRIPTION: same like MIOS_TIMER_Init, but the timer won't be reset to
87
;;  allow a smooth re-initialization
88
;;  IN:	  prescaler value in WREG:
89
;;           0x00: 1:1
90
;;           0x01: 1:2
91
;;           0x02: 1:4
92
;;           0x03: 1:8
93
;;        number of ticks (low-byte) in MIOS_PARAMETER1
94
;;        number of ticks (high-byte) in MIOS_PARAMETER2
95
;;  C_IN: prescaler value in <mode>:
96
;;           0x00: 1:1
97
;;           0x01: 1:2
98
;;           0x02: 1:4
99
;;           0x03: 1:8
100
;;        number of ticks (0..65535) in <period>
101
;;  OUT:  -
102
;;  C_OUT:  -
103
;;  USES: BSR
104
;; --------------------------------------------------------------------------
105
MIOS_TIMER_ReInit
106
	;; load prescaler value to T3CON and stop timer
107
	swapf	WREG, W
108
	andlw	0x30
109
	movwf	T3CON
110
 
111
	;; copy reload values
112
	SET_BSR	MIOS_TIMER3_RELOAD_H
113
	comf	MIOS_PARAMETER2, W
114
	movwf	MIOS_TIMER3_RELOAD_H, BANKED
115
	;; 	movwf	TMR3H
116
 
117
	comf	MIOS_PARAMETER1, W
118
	movwf	MIOS_TIMER3_RELOAD_L, BANKED
119
	;; 	movwf	TMR3L
120
 
121
	;; start timer
122
	bsf	T3CON, TMR3ON
123
	return
124
 
125
;; --------------------------------------------------------------------------
126
;;  FUNCTION: MIOS_TIMER_Start
127
;;  C_DECLARATION: void MIOS_TIMER_Start(void)
128
;;  DESCRIPTION: (re)starts the timer if it has been stopped before
129
;;  IN:	  -
130
;;  OUT:  -
131
;;  USES: -
132
;; --------------------------------------------------------------------------
133
MIOS_TIMER_Start
134
	bsf	T3CON, TMR3ON
135
	return
136
 
137
;; --------------------------------------------------------------------------
138
;;  FUNCTION: MIOS_TIMER_Stop
139
;;  C_DECLARATION: void MIOS_TIMER_Stop(void)
140
;;  DESCRIPTION: stops the timer
141
;;  IN:	  -
142
;;  OUT:  -
143
;;  USES: -
144
;; --------------------------------------------------------------------------
145
MIOS_TIMER_Stop
146
	bcf	T3CON, TMR3ON
147
	return