forked from pavius/rotary-encoder-timer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.c
113 lines (97 loc) · 2.81 KB
/
common.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**************************************************************************
*
* FILE NAME: common.c
* FILE DESCRIPTION: Common routines
*
* FILE CREATION DATE: 24-05-2004
*
*==========================================================================
* Copyright (c) 2004 NIR Diagnostics Inc., Ontario, Canada
*
* This document contains confidential information which is protected by
* copyright and is proprietary to NIR Diagnostics Inc., Ontario, Canada. No part
* of this document may be used, copied, disclosed, or conveyed to another
* party without prior written consent of NIR Diagnostics Inc., Ontario, Canada.
***************************************************************************
*
* Modification history:
* --------------------
* 01a,24may03 erd written
*
***************************************************************************/
#include "sysdef.h"
#include "common.h"
#include "sys.h"
// for time delays
unsigned char time_delayUsLeft;
// ==========================================================================
// uint_8 sys_calculateChecksum(uint_8 *buffer, uint_8 size)
//
// Caluclates an 1's complement checksum over contents of buffer
//
// Arguments:
// buffer: ptr to start of buffer on which chksum is calculated over
// size: size, in bytes, of buffer
//
// Return value:
// checksum value
//
uint_8 sys_calculateChecksum(uint_8 *buffer, uint_8 size)
{
uint_8 chksum = 0;
// calculate the checksum over the buffer
while (size--)
{
chksum += *buffer;
buffer++;
}
// return the checksum
return (~chksum + 1);
}
// ==========================================================================
// uint_16 calculateChecksum16(uint_8 *buffer, uint_16 size)
//
// Caluclates an 16bit checksum over contents of buffer
//
// Arguments:
// buffer: ptr to start of buffer on which chksum is calculated over
// size: size, in bytes, of buffer
//
// Return value:
// checksum value
//
uint_16 sys_calculateChecksum16(uint_8 *buffer, uint_16 size)
{
uint_16 chksum = 0;
// calculate the checksum over the buffer
while (size--)
{
chksum += *buffer;
buffer++;
}
// return the checksum
return (~chksum + 1);
}
// ==========================================================================
// void time_delayMs(int_16 ms)
//
// delay one mS
//
// Arguments:
// ms: the amount of miliseconds to delay
//
void time_delayMs(int_16 ms)
{
uint_8 i;
do
{
// reset i
i = 4;
do
{
// delay a quarter of a ms
time_delayUs(250);
CLRWDT();
} while(--i);
} while(--ms);
}