PIC18 LaurTec Library  3.3.1
Open Source C Library for PIC18 Microcontrollers based on C18 - XC8 Compilers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
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 #ifndef __XC8
43  #include <delays.h>
44 #endif
45 
46 
47 #include <delay.h>
48 
49 volatile unsigned char delay_quartz_frequency_value = 20;
50 volatile unsigned int clock_counter_reference = 0;
51 
52 #ifdef DELAY_INTERRUPT_BLOCKING
53  volatile unsigned char delay_flag_GIE = 0;
54  volatile unsigned char delay_flag_PEIE = 0;
55 #endif
56 
57 //************************************************************
58 // delay_ms function implementation
59 //************************************************************
60 
61 void delay_ms (unsigned int value_ms) {
62 
63  #ifdef DELAY_INTERRUPT_BLOCKING
64  delay_flag_GIE = INTCONbits.GIE;
65  // Disable Interrupt
66  INTCONbits.GIE = 0;
67 
68  delay_flag_PEIE = INTCONbits.PEIE;
69  // Disable Interrupt
70  INTCONbits.PEIE = 0;
71  #endif
72 
74 
75  while (clock_counter_reference) {
76  #ifndef __XC8
77  //1ms Delay at 1MHz trimmed down to 24 instead of 25
78  //to compensate the main loop
79  Delay10TCYx (24);
80  #endif
81 
82  #ifdef __XC8
83  //1ms Delay at 1MHz trimmed down to 240 instead of 250
84  //to compensate the main loop
85  _delay(240);
86  #endif
87 
89  }
90 
91  #ifdef DELAY_INTERRUPT_BLOCKING
92  // Reload old settings
93  INTCONbits.GIE = delay_flag_GIE;
94  INTCONbits.PEIE = delay_flag_PEIE;
95  #endif
96 }
97 
98 //************************************************************
99 // delay_s function implementation
100 //************************************************************
101 
102 void delay_s (unsigned char value_s) {
103 
104  unsigned char repeat_loop;
105 
106  for (repeat_loop = 0; repeat_loop < value_s; repeat_loop++)
107  delay_ms (1000);
108 }
109 
110 
111 //************************************************************
112 // setQuartz function implementation
113 //************************************************************
114 
115 void delay_set_quartz (unsigned char frequency) {
116 
117  delay_quartz_frequency_value = frequency;
118 
119 }
120