LTlib LaurTec Library  4.0.3
Open Source C Library for Microchip Microcontrollers based on XC8 Compiler
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
LTlib_delay.c
Go to the documentation of this file.
1 /*******************************************************************************
2 
3 Author : Mauro Laurenti
4 Version : 1.4
5 
6 Created on Date : 04/09/2006
7 Last update : 15/12/2014
8 
9 CopyRight 2006-2014 all rights are reserved
10 
11 ********************************************************
12 SOFTWARE LICENSE AGREEMENT
13 ********************************************************
14 
15 The usage of the supplied software imply the acceptance of the following license.
16 
17 The software supplied herewith by Mauro Laurenti (the Author) is intended for
18 use solely and exclusively on Microchip PIC Microcontroller (registered mark).
19 The software is owned by the Author, and is protected under applicable
20 copyright laws. All rights are reserved.
21 Any use in violation of the foregoing restrictions may subject the
22 user to criminal sanctions under applicable laws, as well as to civil liability
23 for the breach of the terms and conditions of this license.
24 Commercial use is forbidden without a written acknowledgement with the Author.
25 Personal or educational use is allowed if the application containing the
26 following software doesn't aim to commercial use or monetary earning of any kind.
27 
28 THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,
29 WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
30 TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
31 PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE AUTHOR SHALL NOT,
32 IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
33 CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
34 
35 *******************************************************************************/
36 
37 
38 #ifdef __XC8
39  #include <xc.h>
40 #endif
41 
42 #include <LTlib_delay.h>
43 
44 volatile unsigned char delay_quartz_frequency_value = 20;
45 volatile unsigned int clock_counter_reference = 0;
46 
47 #ifdef DELAY_INTERRUPT_BLOCKING
48  volatile unsigned char delay_flag_GIE = 0;
49  volatile unsigned char delay_flag_PEIE = 0;
50 #endif
51 
52 //************************************************************
53 // delay_ms function implementation
54 //************************************************************
55 
56 void delay_ms (unsigned int value_ms) {
57 
58  #ifdef DELAY_INTERRUPT_BLOCKING
59  delay_flag_GIE = INTCONbits.GIE;
60  // Disable Interrupt
61  INTCONbits.GIE = 0;
62 
63  delay_flag_PEIE = INTCONbits.PEIE;
64  // Disable Interrupt
65  INTCONbits.PEIE = 0;
66  #endif
67 
69 
70  while (clock_counter_reference) {
71  #ifndef __XC8
72  //1ms Delay at 1MHz trimmed down to 24 instead of 25
73  //to compensate the main loop
74  Delay10TCYx (24);
75  #endif
76 
77  #ifdef __XC8
78  //1ms Delay at 1MHz trimmed down to 240 instead of 250
79  //to compensate the main loop
80  _delay(240);
81  #endif
82 
84  }
85 
86  #ifdef DELAY_INTERRUPT_BLOCKING
87  // Reload old settings
88  INTCONbits.GIE = delay_flag_GIE;
89  INTCONbits.PEIE = delay_flag_PEIE;
90  #endif
91 }
92 
93 //************************************************************
94 // delay_s function implementation
95 //************************************************************
96 
97 void delay_s (unsigned char value_s) {
98 
99  unsigned char repeat_loop;
100 
101  for (repeat_loop = 0; repeat_loop < value_s; repeat_loop++)
102  delay_ms (1000);
103 }
104 
105 
106 //************************************************************
107 // setQuartz function implementation
108 //************************************************************
109 
110 void delay_set_quartz (unsigned char frequency) {
111 
112  delay_quartz_frequency_value = frequency;
113 
114 }
115