C:\MPLABX_Projects\P18F27Q10_tests_UART1_ADC_I2C_SPI.X\mcc_generated_files\tmr4.c
  1 /**
  2   TMR4 Generated Driver File
  3 
  4   @Company
  5     Microchip Technology Inc.
  6 
  7   @File Name
  8     tmr4.c
  9 
 10   @Summary
 11     This is the generated driver implementation file for the TMR4 driver using PIC10 / PIC12 / PIC16 / PIC18 MCUs
 12 
 13   @Description
 14     This source file provides APIs for TMR4.
 15     Generation Information :
 16         Product Revision  :  PIC10 / PIC12 / PIC16 / PIC18 MCUs - 1.81.6
 17         Device            :  PIC18F27Q10
 18         Driver Version    :  2.11
 19     The generated drivers are tested against the following:
 20         Compiler          :  XC8 2.30 and above 
 21         MPLAB             :  MPLAB X 5.40
 22 */
 23 
 24 /*
 25     (c) 2018 Microchip Technology Inc. and its subsidiaries. 
 26     
 27     Subject to your compliance with these terms, you may use Microchip software and any 
 28     derivatives exclusively with Microchip products. It is your responsibility to comply with third party 
 29     license terms applicable to your use of third party software (including open source software) that 
 30     may accompany Microchip software.
 31     
 32     THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 
 33     EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY 
 34     IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS 
 35     FOR A PARTICULAR PURPOSE.
 36     
 37     IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, 
 38     INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND 
 39     WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP 
 40     HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO 
 41     THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL 
 42     CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT 
 43     OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS 
 44     SOFTWARE.
 45 */
 46 
 47 /**
 48   Section: Included Files
 49 */
 50 
 51 #include <xc.h>
 52 #include "tmr4.h"
 53 
 54 /**
 55   Section: Global Variables Definitions
 56  */
 57 extern volatile unsigned int  Timer4_Tick;
 58 extern volatile char Timer4_Flag;
 59 extern volatile unsigned int Max_Timer4_Ticks;
 60 
 61 
 62 void (*TMR4_InterruptHandler)(void);
 63 
 64 /**
 65   Section: TMR4 APIs
 66 */
 67 void TMR4_Set_Fosc(uint8_t T4FOSC)
 68 {
 69      if ( (T4FOSC>0) && (T4FOSC<6)) T4CLKCON=T4FOSC;
 70 }
 71 
 72 
 73 void TMR4_Initialize(void)
 74 {
 75     T4CLKCON = 0x04; // T4CS LFINTOSC = 31 Khz 
 76     // T4CLKCON = 0x01;  /// FOSC/4 =16MHz
 77     // T4PSYNC Not Synchronized; T4MODE Software control; T4CKPOL Rising Edge; T4CKSYNC Synchronized; 
 78     T4HLT = 0x20; 
 79     T4RST = 0x00; // T4RSEL T4INPPS pin;
 80     T4PR = 0xF1;// PR4 241;
 81     T4TMR = 0x00; // TMR4 0; 
 82     PIR4bits.TMR4IF = 0; // Clearing IF flag before enabling the interrupt.
 83     PIE4bits.TMR4IE = 1;// Enabling TMR4 interrupt.
 84     // Set Default Interrupt Handler
 85     TMR4_SetInterruptHandler(TMR4_DefaultInterruptHandler);
 86     //TMR4ON on; T4CKPS =110  Prescaler= 1:64;  T4OUTPS =0001 => postscaler=1:2;
 87     //T4CON = 0b11100001; //0xE1;  divise par 128   voir DS page 344  1sec
 88      T4CON = 0b01111111; // Prescaler=1:128 et postscaler=1:16 =>  2048   8mS
 89     
 90 }
 91 
 92 void TMR4_ModeSet(TMR4_HLT_MODE mode)
 93 {
 94    // Configure different types HLT mode
 95     T4HLTbits.MODE = mode;
 96 }
 97 
 98 void TMR4_ExtResetSourceSet(TMR4_HLT_EXT_RESET_SOURCE reset)
 99 {
100     //Configure different types of HLT external reset source
101     T4RSTbits.RSEL = reset;
102 }
103 
104 void TMR4_Start(void)
105 {
106     // Start the Timer by writing to TMRxON bit
107     T4CONbits.TMR4ON = 1;
108     Timer4_Tick=0;
109     Timer4_Flag=0;
110      PIE4bits.TMR4IE = 1;
111 }
112 
113 void TMR4_StartTimer(void)
114 {
115     TMR4_Start();
116 }
117 
118 void TMR4_Stop(void)
119 {
120     // Stop the Timer by writing to TMRxON bit
121     T4CONbits.TMR4ON = 0;
122 }
123 
124 void TMR4_StopTimer(void)
125 {
126     TMR4_Stop();
127 }
128 
129 uint8_t TMR4_Counter8BitGet(void)
130 {
131     uint8_t readVal;
132 
133     readVal = TMR4;
134 
135     return readVal;
136 }
137 
138 uint8_t TMR4_ReadTimer(void)
139 {
140     return TMR4_Counter8BitGet();
141 }
142 
143 void TMR4_Counter8BitSet(uint8_t timerVal)
144 {
145     // Write to the Timer4 register
146     TMR4 = timerVal;
147 }
148 
149 void TMR4_WriteTimer(uint8_t timerVal)
150 {
151     TMR4_Counter8BitSet(timerVal);
152 }
153 
154 void TMR4_Period8BitSet(uint8_t periodVal)
155 {
156    PR4 = periodVal;
157 }
158 
159 void TMR4_LoadPeriodRegister(uint8_t periodVal)
160 {
161    TMR4_Period8BitSet(periodVal);
162 }
163 
164 
165 void TMR4_ISR(void)
166 {
167     // clear the TMR4 interrupt flag
168     PIR4bits.TMR4IF = 0;
169  //   Timer4_Tick++;
170  // if (Timer4_Tick>10) 
171  // {
172  //    Timer4_Flag=1;
173  //    PIE4bits.TMR4IE = 0;
174  //    T4CONbits.TMR4ON = 0;
175  //}   
176      
177     // ticker function call;
178     // ticker is 1 -> Callback function gets called everytime this ISR executes
179    TMR4_CallBack();
180 }
181 
182 void TMR4_CallBack(void)
183 {
184     // Add your custom callback code here
185     // this code executes every TMR4_INTERRUPT_TICKER_FACTOR periods of TMR4
186     if(TMR4_InterruptHandler)
187     {
188         TMR4_InterruptHandler();
189     }
190 }
191 
192 void TMR4_SetInterruptHandler(void (* InterruptHandler)(void)){
193     TMR4_InterruptHandler = InterruptHandler;
194 }
195 
196 void TMR4_DefaultInterruptHandler(void){
197     // add your TMR4 interrupt custom code
198     // or set custom function using TMR4_SetInterruptHandler()
199         PIR4bits.TMR4IF = 0;
200        Timer4_Tick++;
201    // 10 secondes delay 
202    // avec T4CLKCON = 0x04 =LFINTOSC = 31 Khz
203     //if (Timer4_Tick>9)
204        // avec FOSC/4=16MHz   diviseur 128x16=2048  PR4=125 =>  16mS
205     if (Timer4_Tick>(Max_Timer4_Ticks-1))  // ..2sec
206      {
207      Timer4_Flag=1;
208      PIE4bits.TMR4IE = 0;
209      T4CONbits.TMR4ON = 0;
210      }   
211     
212 }
213 
214 /**
215   End of File
216 */