-
Notifications
You must be signed in to change notification settings - Fork 0
/
H_Bridge.c
232 lines (194 loc) · 6.45 KB
/
H_Bridge.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**
Generated main.c file from MPLAB Code Configurator
@Company
Microchip Technology Inc.
@File Name
main.c
@Summary
This is the generated main.c using PIC24 / dsPIC33 / PIC32MM MCUs.
@Description
This source file provides main entry point for system initialization and application code development.
Generation Information :
Product Revision : PIC24 / dsPIC33 / PIC32MM MCUs - 1.167.0
Device : dsPIC33CH128MP508
The generated drivers are tested against the following:
Compiler : XC16 v1.50
MPLAB : MPLAB X v5.35
*/
/*
(c) 2020 Microchip Technology Inc. and its subsidiaries. You may use this
software and any derivatives exclusively with Microchip products.
THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION
WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.
IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE
TERMS.
*/
/**
Section: Included Files
*/
#include "mcc_generated_files/system.h"
#include "mcc_generated_files/pin_manager.h"
#include "mcc_generated_files/sccp1_tmr.h"
#include "mcc_generated_files/tmr1.h"
#include "mcc_generated_files/adc1.h"
/*
Main application
*/
void sensor_status(void)
{
_LATE3 = 1; //latch_E3 is used as a flag to calculate RPM, is set to 1 or High
}
void delay(void) //defining a user defined delay using a timer
{
bool status = true;
SCCP1_TMR_Start();
while(status)
{
SCCP1_TMR_SecondaryTimerTasks();
if(SCCP1_TMR_SecondaryTimer16ElapsedThenClear() == true)
{
SCCP1_TMR_Stop();
status = false;
}
}
}
int main(void)
{
/*Initialize the device*/
SYSTEM_Initialize();
ADC1_Enable();
ADC1_ChannelSelect(channel_AN0);
/*Timer interrupt for RPM count*/
TMR1_SetInterruptHandler(sensor_status); //after every 60 seconds the Interrupt handler will point to the location of sensor_status function
TMR1_Start();
_LATE3 = 0; //by default the latch is set to 0 or Low
uint16_t sensor_value = 0; //used for detecting an input from the opto-coupler
uint16_t rpm_count = 0; //used in rpm calculation
uint16_t rpm = 0; //rpm value
uint16_t value = 0; //ADC value of POT
while (1)
{
ADC1_SoftwareTriggerEnable();
SCCP1_TMR_Counter16BitSecondarySet(10);
delay();
ADC1_SoftwareTriggerDisable();
if(ADC1_IsConversionComplete(channel_AN0))
{
value = 4296 - ADC1_ConversionResultGet(channel_AN0); //mapping between the ADC value of POT and the delay used for making steps in the stepper motor
}
SCCP1_TMR_Counter16BitSecondarySet(value); //delay value is set
/*RPM calculation*/
if(_LATE3!=0)
{
rpm = rpm_count/2; //use of an encoder wheel with two holes
rpm_count = 0; //resetting of rpm_count to count for the next 60s
_LATE3 = 0; //latch_E3 status is reset
}
/*Makes the stepper move 1.8 degree clockwise direction*/
IN1_SetHigh();
IN2_SetLow();
IN3_SetLow();
IN4_SetLow();
/*Sensing part*/
sensor_value = Sensor_GetValue();
if(sensor_value != 0)
{
if(_LATE2 != 1) //latch_E2 is used as a status for avoiding miscounting
{
_LATE2 = 1;
rpm_count++;
}
}
if(sensor_value == 0)
{
if(_LATE2 != 0)
{
_LATE2 = 0;
}
}
delay();
/*Makes the stepper move 1.8 degree clockwise direction*/
IN3_SetHigh();
IN1_SetLow();
IN2_SetLow();
IN4_SetLow();
/*Sensing part*/
sensor_value = Sensor_GetValue();
if(sensor_value != 0)
{
if(_LATE2 != 1) //latch_E2 is used as a status for avoiding miscounting
{
_LATE2 = 1;
rpm_count++;
}
}
if(sensor_value == 0)
{
if(_LATE2 != 0)
{
_LATE2 = 0;
}
}
delay();
/*Makes the stepper move 1.8 degree clockwise direction*/
IN2_SetHigh();
IN1_SetLow();
IN3_SetLow();
IN4_SetLow();
/*Sensing part*/
sensor_value = Sensor_GetValue();
if(sensor_value != 0)
{
if(_LATE2 != 1) //latch_E2 is used as a status for avoiding miscounting
{
_LATE2 = 1;
rpm_count++;
}
}
if(sensor_value == 0)
{
if(_LATE2 != 0)
{
_LATE2 = 0;
}
}
delay();
/*Makes the stepper move 1.8 degree clockwise direction*/
IN4_SetHigh();
IN1_SetLow();
IN2_SetLow();
IN3_SetLow();
/*Sensing part*/
sensor_value = Sensor_GetValue();
if(sensor_value != 0)
{
if(_LATE2 != 1) //latch_E2 is used as a status for avoiding miscounting
{
_LATE2 = 1;
rpm_count++;
}
}
if(sensor_value == 0)
{
if(_LATE2 != 0)
{
_LATE2 = 0;
}
}
delay();
}
return 1;
}
/**
End of File
*/