-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain_TP2A_PWM.cpp
282 lines (214 loc) · 7.81 KB
/
Main_TP2A_PWM.cpp
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*--------------------------------------------------------*/
// Fichier Main_TP2A_PWM
/*--------------------------------------------------------*/
// Description : Canevas TP2A_PWM
//
// Auteur : C. Huber
// Création : 05.03.2014
#include <GenericTypeDefs.h>
#include <stdio.h>
#include <stdlib.h>
#include <plib.h>
#include <tgmath.h>
#include "SK32MX795F512L.h"
#include "Mc32Delays.h"
#include "TextDisplay.h"
#include "Mc32GestionPWM.h"
#include "Mc32GestADC.h"
//=====================================----------------------------------------
// Fuses configuration
//=====================================----------------------------------------
// Set configuration fuses (but only once)
// Set clock configuration
// Input clock = HS 20MHz
#pragma config POSCMOD = HS // Primary Oscillator mode = HS
#pragma config FSOSCEN = OFF // Secondary Oscillator Disable
#pragma config IESO = OFF // Internal External Switch Over bit Disable
#pragma config FNOSC = PRIPLL // Primary oscillator (XT, HS, EC) w/ PLL
#pragma config OSCIOFNC = OFF // Disabled output on clko pin
#pragma config FCKSM = CSDCMD // Clock Switching Disabled, Clock Monitoring Disabled
//Set system PLL configuration (Quartz 8 MHz)
// #pragma FPLLIDIV = DIV_5 // Divide by 5 clock for input to pll ->4MHz
#pragma config FPLLIDIV = DIV_2 // Divide by 2 clock for input to pll ->4MHz
#pragma config FPLLMUL = MUL_20 // Multiply by 20 -> pll output = 80MHz
#pragma config FPLLODIV = DIV_1 // System clock = pll Divide by 1 = 80MHz
// Set peripheral clk div
#pragma config FPBDIV = DIV_8 // Divide by 8
// Set power up timer
#pragma PUT = ON
// set USB
#pragma config FVBUSONIO = OFF // VBUS_ON pin is controlled by the Port Function
#pragma config FUSBIDIO = OFF // USBID pin is controlled by the Port Function
// Set CAN
#pragma config FCANIO = ON // Default CAN IO Pins
// Set Ethernet
#pragma config FETHIO = ON // Default Ethernet IO Pins
#pragma config FMIIEN = ON // MII enabled
// set watchdog
#pragma config FWDTEN = OFF // Disabled
// Set code protect
#pragma config CP = OFF // Disabled
// Set boot flash code protect
#pragma config BWP = OFF // Disabled
// Set ICD3 channel
#pragma config ICESEL = ICS_PGx2 // ICE pins are shared with PGC2, PGD2
// Set debug bit
#pragma config DEBUG = ON // Enabled
//=====================================----------------------------------------
// définitions des constantes et des variables
//=====================================----------------------------------------
int cpt;
char stringaff[20] = {"Test Int & Timer1"};
int ChenillardPhase = 0;
int compteurMain;
uint8_t Compt_4ms;
//=====================================----------------------------------------
// prototypes (nécessaires si des fonctions sont appelées avant d'être définies,
// par exemple pour les interruptions)
//=====================================----------------------------------------
//=====================================----------------------------------------
// définitions et mise en place des fonctions d'interruptions
// les fonctions d'interruptions peuvent être définies n'importe où dans le code,
// mais généralement elles sont définies au début de la partie programme
//=====================================----------------------------------------
//=====================================----------------------------------------
// définitions pour les Timers
//=====================================----------------------------------------
#define SYS_FREQ (80000000L) //80 MHz
#define PB_DIV 1
// #define PB_FREQ (SYS_FREQ / PB_DIV)
#define PB_FREQ (SYS_FREQ / PB_DIV)
//Timer1
#define PRESCALE 256
#define T1_TICK ((1000000.0 * PRESCALE)/PB_FREQ) // en us
// 25 ms => 25'000 us
#define VAL_PR1 (25000 / T1_TICK)
//Timer3
#define PRESCALE_3 64
#define T3_TICK ((1000000.0 * PRESCALE_3)/PB_FREQ) // en us
//7 ms => 7'000 us
#define VAL_PR3 (7000 / T3_TICK)
//Timer4
#define PRESCALE_4 256
#define T4_TICK ((1000000.0 * PRESCALE_4)/PB_FREQ) // en us
// 40 us
#define VAL_PR4 (40.0 / T4_TICK)
volatile int32_t line2 = 0;
volatile uint32_t line3 = 0;
volatile int32_t line4 = 0;
volatile uint16_t PulseStopOC3 = 0;
//=====================================----------------------------------------
// fonction main
//=====================================----------------------------------------
int main (void){
// disable JTAG port to free RA0, RA1, RA4 et RA5
mJTAGPortEnable(0);
// memory wait states fine tuning
// TO BE CHECKED
//SYSTEMConfigPerformance(SYS_FREQ);
int Pb_freq = SYSTEMConfigPerformance(SYS_FREQ);
// Etablit PB_CLOCK à 4 MHz
// Default SK32MX775F512L i/os config and values
SK32MX795F512L_IO_Default();
// Configuration Timer1 cycle 25 ms
// Internal clock, Prescaler 256
OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_256, VAL_PR1);
// Set up the timer interrupt with a priority of 4
ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_4);
// Configuration Timer4 cycle 40 us
// Internal clock, Prescaler 1
OpenTimer4(T4_ON | T4_SOURCE_INT | T4_PS_1_256, VAL_PR4);
// Set up the timer4 interrupt with a priority of 4 (provisoir)
ConfigIntTimer4(T4_INT_ON | T4_INT_PRIOR_6);
// Initialisation timer et OCx pour PWM
GPWM_InitTimerAndOCx();
// Init AD
InitADC();
// lcd init
TextDisplay lcd = TextDisplay("E0", "E1", "E2", "E3", "E4", "E5", "E6", "E7");
lcd << "Local Settings" << endl;
lcd << "TP2A PWM 2013-2014 " << endl;
lcd << "Samuel Dolt" << endl;
lcd << "France Maillard" << endl;
delay_ms(3000);
lcd.clear();
// enable multi-vector interrupts
INTEnableSystemMultiVectoredInt();
while(1){
// Ne rien faire (juste un comptage)
compteurMain++;
lcd.set_cursor(2,1);
lcd << "Vsignee :" << line2 << " " << endl;
lcd << "Vnon_signee :" << line3 << " " << endl;
lcd << "Angle :" << line4 << " " << endl;
delay_ms(100);
}
return 0;
} // End main
extern "C"
{
// Réponse à l'interruption du Timer1 (cycle de 25 ms)
void __ISR(_TIMER_1_VECTOR, IPL4SOFT) Timer1Handler(void)
{
uint16_t resultat_ad0, resultat_ad1;
uint8_t ValDegre;
// Marqueur activité
LED0_W = 1;
// clear the interrupt flag
mT1ClearIntFlag();
// Lecture de l'ADC sur le canal 0
resultat_ad0 = MyReadADC(0);
// Transformation du resultat en % (ADC 10 Bit)
//line3 = 99* (resultat_ad0 / 1023.0);
line2 = (198* (resultat_ad0 / 1023.0)) - 99;
line3 = abs(line2);
// Calcul valeur du duty cycle
SetDCOC2PWM(abs(line2) * ((25 * 80) / 100));
STBY_HBRIDGE = 1;
if (line2 < 0)
{
AIN1_HBRIDGE = 1; // RD12
AIN2_HBRIDGE = 0; // RD13
}
else
{
AIN2_HBRIDGE = 1;
AIN1_HBRIDGE = 0;
}
// Execute le traitement complet
GPWM_DoSettings();
//traitement AD canal 1
//lecture de l'AD canal 1
resultat_ad1 = MyReadADC(1);
//Calcul de 0 à 180
ValDegre = (resultat_ad1 / 1023.0) * 180;
//Determiner la pulse_stop pour OC3
SetPulseOC3((7000-(ValDegre*10+600))/T3_TICK, 0);
//Transformation de 0 à 180 en -90 à 90
line4 = ValDegre - 90;
// Marqueur activité
LED0_W = 0;
} // End T1 ISR
// Réponse à l'interruption du Timer4 (cycle 40 us)
void __ISR(_TIMER_4_VECTOR, IPL6SOFT) Timer4Handler(void)
{
// Marqueur activité
LED1_W = 1;
// clear the interrupt flag
mT4ClearIntFlag();
// Traitement PWM software
GPWM_DoPWMSoftware();
Compt_4ms ++;
if (Compt_4ms >= 99)
{
Compt_4ms = 0;
LED2_W = 1;
}
else if (Compt_4ms == (uint8_t) line3)
{
LED2_W = 0;
}
// Marqueur activité
LED1_W = 0;
}
}