1 /*********************************************************************
  2     FileName:           Timers.c
  3     Dependencies:       See #includes
  4     Processor:          PIC32MZ
  5     Hardware:           MainBrain MZ
  6     Complier:           XC32 4.40
  7     Author:             Larry Knight 2023
  8 /*********************************************************************
  9  
 10     Software License Agreement:
 11  
 12     Licensed under the Apache License, Version 2.0 (the "License");
 13     you may not use this file except in compliance with the License.
 14     You may obtain a copy of the License at
 15 
 16     http://www.apache.org/licenses/LICENSE-2.0
 17 
 18     Unless required by applicable law or agreed to in writing, software
 19     distributed under the License is distributed on an "AS IS" BASIS,
 20     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 21     See the License for the specific language governing permissions and
 22     limitations under the License.
 23  
 24     Description:
 25         System Clock = 200 - 250 MHz
 26 
 27     File Description:
 28 
 29     Change History:
 30  
 31 /***********************************************************************/
 32 
 33 #include <xc.h>
 34 #include "MainBrain.h"
 35 
 36 int ADC0_result;
 37 float ADC6_result;
 38 
 39 //2KHz
 40 const uint16_t TMR1_FREQ_SET = 0xe500; 
 41 
 42 //System Clock = 1.8KHz
 43 void TMR1_init(void)
 44 {
 45     IPC1bits.T1IP = 6;
 46     IPC1bits.T1IS = 3;
 47     
 48     IEC0bits.T1IE = 1;
 49 
 50     T1CON = 0;
 51     T1CONbits.TCKPS = 0;
 52     IFS0bits.T1IF = 0;
 53     T1CONbits.ON = 1;
 54 }
 55 
 56 //Used for the Display back light PWM
 57 void TMR2_init(void)
 58 {
 59     IPC2bits.T2IP = 6;
 60     IPC2bits.T2IS = 2;
 61     
 62     IEC0bits.T2IE = 0;
 63     
 64     T2CONbits.ON = 1;
 65 }
 66 
 67 //used for the I/O time out 
 68 void TMR3_init(void)
 69 {
 70     T3CONbits.ON = 0;
 71     
 72     IPC3bits.T3IP = 6;
 73     IPC3bits.T3IS = 1;
 74     
 75     //Set the pre-scaler
 76     T3CONbits.TCKPS = 4;
 77     
 78     //This is the I/O timeout period
 79     //0xA702 = 3mS
 80     PR3 = 0xA702;
 81     
 82     IEC0bits.T3IE = 1;
 83     
 84     T3CONbits.ON = 0;
 85 }
 86 
 87 void TMR4_init(void)
 88 {
 89     T4CONbits.ON = 0;
 90     IPC4bits.T4IP = 5;
 91     IPC4bits.T4IS = 3;
 92     IEC0bits.T4IE = 1;
 93     T4CONbits.T32 = 1;
 94     IFS0bits.T4IF = 0;   
 95 }
 96 
 97 
 98 void TMR5_init(void)
 99 {
100     T5CONbits.ON = 0;
101     IPC6bits.T5IP = 5;
102     IPC6bits.T5IS = 2;
103     IEC0bits.T5IE = 1;
104     IFS0bits.T5IF = 0;
105 }
106 
107 //This timer is used for the heartbeat LED ON time
108 void TMR6_init(void)
109 {
110     T6CONbits.ON = 0;
111     T6CONbits.TCKPS = 7;
112     IPC7bits.T6IP = 5;
113     IPC7bits.T6IS = 1;
114     IEC0bits.T6IE = 1;
115     IFS0bits.T6IF = 0;
116 }
117 
118 //Timer 1
119 void __attribute__((vector(_TIMER_1_VECTOR), interrupt(ipl6srs), nomips16)) TMR1_handler()
120 {
121     //presets the TMR1 period
122     PR1 = TMR1_FREQ_SET;
123     
124     //Outputs proof of interrupt on pin 10
125     PORTGbits.RG6 = !PORTGbits.RG6;
126     
127     //Trigger a conversion
128     ADCCON3bits.GSWTRG = 1;
129     
130     //Wait the conversions to complete
131     while (ADCDSTAT1bits.ARDY0 == 0);
132     ADC0_result = ADCDATA0;
133     
134     while (ADCDSTAT1bits.ARDY6 == 0);
135     ADC6_result = ADCDATA6;
136 
137     //Clear interrupt flag
138     IFS0bits.T1IF = 0;
139 }
140 
141 //Timer 2
142 void __attribute__((vector(_TIMER_2_VECTOR), interrupt(ipl6srs), nomips16)) TMR2_handler()
143 {
144     //Clear interrupt flag
145     IFS0bits.T2IF = 0;
146 }
147 
148 //Timer 3
149 void __attribute__((vector(_TIMER_3_VECTOR), interrupt(ipl6srs), nomips16)) TMR3_handler()
150 {
151     SetPeripheralAddress(0);
152     T3CONbits.ON = 0;
153     
154     //indicates I/O timed out
155     PORTFbits.RF8 = 1;
156     
157     //Unlock the Directive to allow future I/O cycles
158     IO_Locked = false;
159     
160     //Clear interrupt flag
161     IFS0bits.T3IF = 0;
162 }
163 
164 //Timer 4
165 void __attribute__((vector(_TIMER_4_VECTOR), interrupt(ipl5srs), nomips16)) TMR4_handler()
166 {
167 
168     //Clear interrupt flag
169     IFS0bits.T4IF = 0;
170 }
171 
172 //Timer 5
173 void __attribute__((vector(_TIMER_5_VECTOR), interrupt(ipl5srs), nomips16)) TMR5_handler()
174 {
175 
176     //Clear interrupt flag
177     IFS0bits.T5IF = 0;
178 }
179 
180 //Timer 6
181 void __attribute__((vector(_TIMER_6_VECTOR), interrupt(ipl5srs), nomips16)) TMR6_handler()
182 {
183     //Controls LED0 ON time
184     // LED OFF
185     PORTFbits.RF3 = 0;   
186     
187     // Stop timer
188     T6CONbits.ON = 0;       
189     
190     //Clear interrupt flag
191     IFS0bits.T6IF = 0;
192 }
193 
194