Subversion Repositories svn.mios

Rev

Rev 51 | Go to most recent revision | Details | Last modification | View Log | RSS feed

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