-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
1265 lines (1034 loc) · 42.4 KB
/
main.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* main.c
*
* This is programmed for C2000 launchpad (TMS320F28027)
* Also uses a nokia 5110 LCD
*
* 7/13/13
* ADC1 to read 6 buttons in series with resistor ladder
* resistor values r1 = 1k , r2 , r3, r4 , r5, r6 = 2.2k
* display value, button #, ect. on nokia 5110
* button is attched at J5_5 (ADCINA1)
*
* 9/27/13
* added pot on ADCINA3 for variable PWM output
* pot is attached to pin j5_4 (ADCINA3)
* removed ADC3 pot is too noisy
*
* 12/18/13
* added truth table and gpio for motor control VNH5091A-E
*
* 8/27/14
* added encoder for PWM adjustment maybe? instead of pot
*
* 8/29/14
* added timer with interrupt for cycling motor
*
* 12/20/14
* removed ADC3 and pot unnecessary code
* cleaned up encoder code
* added switch case for PWM adjustment
*
* 4/4/15
* added uart for logging results on a pc
*
* 4/6/15
* readded ADC3 for monitoring relay from w127 load cell
* added jog function
* added cycle function
* added change direction function
*
* 4/11/15
* added ChaN's xprintf for easy uart AWESOME!! thanks ChaN!
* added support for mode select on ADC1 button 1 --not good --rework
*
* 5/21/15
* Timer interrupt code is now working during cycle return function
* ADC buttons bounce too much!
* todo add switch for mode select instead of button, use ADC6 to read switch
*/
#include "DSP28x_Project.h" // Device Header file and Examples Include File
#include <math.h>
#include <stdbool.h>
#include "f2802x_common/include/adc.h"
#include "f2802x_common/include/clk.h"
#include "f2802x_common/include/flash.h"
#include "f2802x_common/include/gpio.h"
#include "f2802x_common/include/pie.h"
#include "f2802x_common/include/pll.h"
#include "f2802x_common/include/wdog.h"
#include "f2802x_common/include/spi.h"
#include "f2802x_common/include/pwm.h"
#include "f2802x_common/include/timer.h"
#include "f2802x_common/include/sci.h"
// AIO library
#include "AIO.h"
// nokia5110 libraries
#include "nokia5110.h"
// for easy uart thanks ChaN!!
#include "xprintf.h"
//GPIO defines for VNH5019A-E motor controller chip
#define VNH5019AE_inA GPIO_Number_1
#define VNH5019AE_inA_Hi GPIO_setHigh(myGpio, VNH5019AE_inA);
#define VNH5019AE_inA_Lo GPIO_setLow(myGpio, VNH5019AE_inA);
#define VNH5019AE_inA_Toggle GPIO_toggle(myGpio, VNH5019AE_inA);
#define VNH5019AE_inB GPIO_Number_2
#define VNH5019AE_inB_Hi GPIO_setHigh(myGpio, VNH5019AE_inB);
#define VNH5019AE_inB_Lo GPIO_setLow(myGpio, VNH5019AE_inB);
#define VNH5019AE_inB_Toggle GPIO_toggle(myGpio, VNH5019AE_inB);
#define VNH5019AE_DiagA GPIO_Number_3
#define VNH5019AE_DiagA_Hi GPIO_setHigh(myGpio, VNH5019AE_DiagA);
#define VNH5019AE_DiagA_Lo GPIO_setLow(myGpio, VNH5019AE_DiagA);
#define VNH5019AE_DiagB GPIO_Number_4
#define VNH5019AE_DiagB_Hi GPIO_setHigh(myGpio, VNH5019AE_DiagB);
#define VNH5019AE_DiagB_Lo GPIO_setLow(myGpio, VNH5019AE_DiagB);
// For figuring the PWM frequency
#define TPWM 0.133
// encoder stuff
#define enc1 GPIO_Number_6 // connect to J2_8 on launchpad
#define enc1High GPIO_setHigh(myGpio, enc1);
#define enc1Low GPIO_setLow(myGpio, enc1);
#define enc2 GPIO_Number_5 // connect to J6_6 on launchpad
#define enc2High GPIO_setHigh(myGpio, enc2);
#define enc2Low GPIO_setLow(myGpio, enc2);
//#define checkEnc1 GpioDataRegs.GPADAT.bit.enc1; // data register for enc1 bit 6 0x6
//#define checkEnc2 GpioDataRegs.GPADAT.bit.enc2; // data register for enc2 bit 5 0x5
#define portA GpioDataRegs.GPADAT.all
#define debugLed GPIO_Number_12 // connect to J2_3 on launchpad
#define debugLedHigh GPIO_setHigh(myGpio, debugLed);
#define debugLedLow GPIO_setLow(myGpio, debugLed);
#define toggleDebugLed GPIO_toggle(myGpio, debugLed);
#define pin32 GPIO_Number_32 // connect to J6_7 on launchpad
#define pin32High GPIO_setHigh(myGpio, pin32);
#define pin32Low GPIO_setLow(myGpio, pin32);
#define pin32Toggle GPIO_toggle(myGpio, pin32);
/*
#define pin33 GPIO_Number_33 // connect to J6_8 on launchpad
#define pin33High GPIO_setHigh(myGpio, pin33);
#define pin33Low GPIO_setLow(myGpio, pin33);
#define pin33Toggle GPIO_toggle(myGpio, pin33);
*/
// Configure the period for timer
#define EPWM1_TIMER_TBPRD_MAX 64500 // Period register
#define EPWM1_TIMER_TBPRD_MIN 190
#define EPWM1_MAX_CMPA 32600 // compare period
#define EPWM1_MIN_CMPA 95 // compare period
#define EPWM1_START_TBPRD 3760 // 1Khz
#define EPWM1_START_CMPA 1880 // 1Khz
/// pwm stuff
typedef struct{
PWM_Handle myPwmHandle;
uint16_t EPwmMaxCMPA;
uint16_t EPwmMinCMPA;
uint16_t EPwm1_CMPA;
// uint16_t EPwm1_CMPB;
uint16_t EPwm1_TBPRD;
}EPWM_INFO;
EPWM_INFO epwm1_info;
/////////// Global variables //////////////////////////
int16_t pressCount = 0; // for button presses to toggle mode
int16_t volts; // converted adc voltage
char voltsToSend[8];
char adcAsString[4];
char freqToSend[6];
char stringToSend[4];
int16_t ADC3[7]; // raw ADCINA3 reading take 7 readings and do an average of
int16_t ADC1[7]; // raw ADCINA1 reading take 7 readings and do an average of
int16_t ADC4[7]; // raw ADCINA4 reading take 7 readings and do an average of
int16_t ADC3_average = 0; // averaged ADC data for relay from W127 unit
int16_t ADC1_average = 0; // averaged ADC data for buttons
int16_t ADC4_average = 0; // averaged ADC data for current sense on VNH5091AE
int16_t ADC3_value = 0;
int16_t ADC1_value = 0;
int16_t ADC4_value = 0;
volatile int16_t c = 0; // global counter for adc isr's taken place
volatile uint16_t secCounter = 0; // second counter for end of cycle actuator return
bool jogFlag = false; // flag for jogging the actuator
bool cycleFlag = false; // flag for cycling actuator
float pwmFreq = 0; // for figuring the PWM frequency
/// Encoder stuff ///
char encCntToSend[2]; //for encoder count on lcd
const int16_t table[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0}; // increment of angle for all possible bit codes
//bool flag = false;
int16_t encPos = 0;
int16_t angle = 0;
int16_t oldAngle = 0;
uint16_t encUpdate(int16_t value);
/// for uart ///
//char * msg;
bool transmit = true; // start off true to transmit beginning data
/// actator stuff ///
bool CW = true; // default to CW
enum Mode{
jog,cycle,stop
};
enum Mode mode = stop; // default stopped on startup
//// for timer ///
//uint32_t timerPeriod = 0; // default to 1 sec interrupt
/// interrupts ///
interrupt void spiRxFifoIsr(void);
interrupt void adc_Isr(void);
interrupt void xint1_isr(void); //// these are for the encoder
interrupt void xint2_isr(void);
interrupt void timer0_isr(void);
interrupt void timer1_isr(void);
//interrupt void epwm1_isr(void);
/// function prototypes ///
void gpio_init(void);
void spi_init(void);
void adc_init(void);
void epwm_init(void);
void timer0_init(void);
void scia_init(void);
void scia_xmit(char a);
//void scia_msg(char * msg);
void itoa(unsigned int val, char *str);
void ftoa(float f, char *buf, int decPlaces);
int round(float);
float figureFreq(uint16_t TBPRD);
long map(long x, long in_min, long in_max, long out_min, long out_max);
void update_compare(EPWM_INFO*);
/// functions for VNH5091AE ///
void VNH5091AE_changeDir(void);
void VNH5091AE_CCW(void);
void VNH5091AE_CW(void);
void VNH5091AE_STOP(void);
void VNH5091AE_CYCLE(void);
void VNH5091AE_JOG(void);
void VNH5091AE_BRAKE_TO_VCC(void);
void VNH5091AE_BRAKE_TO_GND(void);
void VNH5091AE_FAULT_FIX(void);
void VNH5091AE_RETURN(void);
ADC_Handle myAdc;
CLK_Handle myClk;
FLASH_Handle myFlash;
GPIO_Handle myGpio;
PIE_Handle myPie;
SPI_Handle mySpi;
CPU_Handle myCpu;
PLL_Handle myPll;
WDOG_Handle myWDog;
PWM_Handle myPwm1;
TIMER_Handle myTimer; //, myTimer1;
SCI_Handle mySci;
void main(void) {
// Initialize all the handles needed for this application
myPwm1 = PWM_init((void *)PWM_ePWM1_BASE_ADDR, sizeof(PWM_Obj));
myAdc = ADC_init((void *)ADC_BASE_ADDR, sizeof(ADC_Obj));
myClk = CLK_init((void *)CLK_BASE_ADDR, sizeof(CLK_Obj));
myCpu = CPU_init((void *)NULL, sizeof(CPU_Obj));
myFlash = FLASH_init((void *)FLASH_BASE_ADDR, sizeof(FLASH_Obj));
myGpio = GPIO_init((void *)GPIO_BASE_ADDR, sizeof(GPIO_Obj));
myPie = PIE_init((void *)PIE_BASE_ADDR, sizeof(PIE_Obj));
myPll = PLL_init((void *)PLL_BASE_ADDR, sizeof(PLL_Obj));
myWDog = WDOG_init((void *)WDOG_BASE_ADDR, sizeof(WDOG_Obj));
mySpi = SPI_init((void *)SPIA_BASE_ADDR, sizeof(SPI_Obj));
mySci = SCI_init((void *)SCIA_BASE_ADDR, sizeof(SCI_Obj));
myTimer = TIMER_init((void *)TIMER0_BASE_ADDR, sizeof(TIMER_Obj));
//myTimer1 = TIMER_init((void *)TIMER1_BASE_ADDR, sizeof(TIMER_Obj));
// Perform basic system initialization
WDOG_disable(myWDog);
CLK_enableAdcClock(myClk);
(*Device_cal)();
CLK_disableAdcClock(myClk);
//Select the internal oscillator 1 as the clock source
CLK_setOscSrc(myClk, CLK_OscSrc_Internal);
// Setup the PLL for x12 /2 which will yield 60Mhz = 10Mhz * 12 / 2
PLL_setup(myPll, PLL_Multiplier_12, PLL_DivideSelect_ClkIn_by_2);
// If running from flash copy RAM only functions to RAM
#ifdef _FLASH
memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize);
#endif
// Enable XCLOCKOUT to allow monitoring of oscillator 1
// GPIO_setMode(myGpio, GPIO_Number_18, GPIO_18_Mode_XCLKOUT);
// CLK_setClkOutPreScaler(myClk, CLK_ClkOutPreScaler_SysClkOut_by_1);
gpio_init();
AIO_init();
spi_init();
nokia_init();
adc_init();
timer0_init();
scia_init();
CLK_disableTbClockSync(myClk); // disable sync clock
epwm_init();
CLK_enableTbClockSync(myClk); // enable sync clock
// Disable the PIE and all interrupts
// PIE_setDebugIntVectorTable(myPie);
PIE_enable(myPie);
CPU_disableGlobalInts(myCpu);
CPU_clearIntFlags(myCpu);
CPU_enableGlobalInts(myCpu);
CPU_enableDebugInt(myCpu);
xputs("\nCarabiner Testing Machine!\n");
// Main program loop - continually sample ADC inputs
for(;;) {
//Force start of conversion on SOC0, SOC1, SOC3, SOC4
ADC_forceConversion(myAdc, ADC_SocNumber_0); // discard this conversion per errata
ADC_forceConversion(myAdc, ADC_SocNumber_1); // ADCina1 reading
ADC_forceConversion(myAdc, ADC_SocNumber_3); // ADCina3 reading
ADC_forceConversion(myAdc, ADC_SocNumber_4); // ADCina4 reading for current reading on VNH5019A-E
switch(encPos){
case 1:
epwm1_info.EPwm1_TBPRD = 1878; //set the TBPRD to 2Khz
update_compare(&epwm1_info); // update the EPwm registers
break;
case 2:
epwm1_info.EPwm1_TBPRD = 1252; //set the TBPRD to 3Khz
update_compare(&epwm1_info); // update the EPwm registers
break;
case 3:
epwm1_info.EPwm1_TBPRD = 938; //set the TBPRD to 4Khz
update_compare(&epwm1_info); // update the EPwm registers
break;
case 4:
epwm1_info.EPwm1_TBPRD = 750; //set the TBPRD to 5Khz
update_compare(&epwm1_info); // update the EPwm registers
break;
case 5:
epwm1_info.EPwm1_TBPRD = 626; //set the TBPRD to 6Khz
update_compare(&epwm1_info); // update the EPwm registers
break;
case 6:
epwm1_info.EPwm1_TBPRD = 536; //set the TBPRD to 7Khz
update_compare(&epwm1_info); // update the EPwm registers
break;
case 7:
epwm1_info.EPwm1_TBPRD = 470; //set the TBPRD to 8Khz
update_compare(&epwm1_info); // update the EPwm registers
break;
case 8:
epwm1_info.EPwm1_TBPRD = 418; //set the TBPRD to 9Khz
update_compare(&epwm1_info); // update the EPwm registers
break;
case 9:
epwm1_info.EPwm1_TBPRD = 376; //set the TBPRD to 10Khz
update_compare(&epwm1_info); // update the EPwm registers
break;
case 10:
epwm1_info.EPwm1_TBPRD = 342; //set the TBPRD to 11Khz
update_compare(&epwm1_info); // update the EPwm registers
break;
case 11:
epwm1_info.EPwm1_TBPRD = 312; //set the TBPRD to 12Khz
update_compare(&epwm1_info); // update the EPwm registers
break;
case 12:
epwm1_info.EPwm1_TBPRD = 288; //set the TBPRD to 13Khz
update_compare(&epwm1_info); // update the EPwm registers
break;
case 13:
epwm1_info.EPwm1_TBPRD = 268; //set the TBPRD to 14Khz
update_compare(&epwm1_info); // update the EPwm registers
break;
case 14:
epwm1_info.EPwm1_TBPRD = 250; //set the TBPRD to 15Khz
update_compare(&epwm1_info); // update the EPwm registers
break;
case 15:
epwm1_info.EPwm1_TBPRD = 234; //set the TBPRD to 16Khz
update_compare(&epwm1_info); // update the EPwm registers
break;
case 16:
epwm1_info.EPwm1_TBPRD = 222; //set the TBPRD to 17Khz
update_compare(&epwm1_info); // update the EPwm registers
break;
case 17:
epwm1_info.EPwm1_TBPRD = 208; //set the TBPRD to 18Khz
update_compare(&epwm1_info); // update the EPwm registers
break;
case 18:
epwm1_info.EPwm1_TBPRD = 198; //set the TBPRD to 19Khz
update_compare(&epwm1_info); // update the EPwm registers
break;
case 19:
epwm1_info.EPwm1_TBPRD = 188; //set the TBPRD to 20Khz
update_compare(&epwm1_info); // update the EPwm registers
break;
default:
epwm1_info.EPwm1_TBPRD = EPWM1_START_TBPRD; //set the TBPRD to 1Khz
update_compare(&epwm1_info); // update the EPwm registers
break;
}
writeString(0,0,">> PWM");
writeString(0,1,"Enc Count");
writeString(0,3,"ADC1 Buttons");
writeString(35,4,"Pressed");
itoa(encPos, encCntToSend);
writeString(68,1," ");
writeString(68,1,encCntToSend);
pwmFreq = figureFreq(epwm1_info.EPwm1_TBPRD);
ftoa(pwmFreq, freqToSend, 4);
writeString(0,2,freqToSend);
if (pwmFreq >= 1.0) {
writeString(48,2,"Khz");
} else {
clearSome(47,16,54,24);
}
if (jogFlag) // Jog
VNH5091AE_JOG();
if (cycleFlag)
VNH5091AE_CYCLE(); // Cycle
if (transmit) {
xprintf("Encoder count = %d\n", encPos);
ftoa(volts, voltsToSend, 2);
xputs("ADC3 Voltage = ");
xputs(voltsToSend);
xprintf("\nPress count = %d\n", pressCount);
xprintf("Mode is %d\n", mode);
if (CW){
xputs("Direction = CW\n");
}else{
xputs("Direction = CWW\n");
}
transmit = false;
}
}
}
/////////////////// functions /////////////////////////////////////////////////////////
void gpio_init(void){
// set up gpio pins for uart/sci
GPIO_setPullUp(myGpio, GPIO_Number_28, GPIO_PullUp_Enable);
GPIO_setPullUp(myGpio, GPIO_Number_29, GPIO_PullUp_Disable);
GPIO_setQualification(myGpio, GPIO_Number_28, GPIO_Qual_ASync);
GPIO_setMode(myGpio, GPIO_Number_28, GPIO_28_Mode_SCIRXDA);
GPIO_setMode(myGpio, GPIO_Number_29, GPIO_29_Mode_SCITXDA);
// Set up pins 32 as output
GPIO_setMode(myGpio, pin32, GPIO_32_Mode_GeneralPurpose);
GPIO_setDirection(myGpio, pin32, GPIO_Direction_Output);
GPIO_setPullUp(myGpio, pin32, GPIO_PullUp_Disable);
// debug led GPIO 12
GPIO_setMode(myGpio, debugLed, GPIO_12_Mode_GeneralPurpose);
GPIO_setDirection(myGpio, debugLed, GPIO_Direction_Output);
GPIO_setPullUp(myGpio, debugLed, GPIO_PullUp_Disable);
// set up GPIO's for nokia
GPIO_setMode(myGpio, nokiaVcc, GPIO_33_Mode_GeneralPurpose);
GPIO_setMode(myGpio, nokiaRst, GPIO_17_Mode_GeneralPurpose);
GPIO_setMode(myGpio, nokiaDc, GPIO_7_Mode_GeneralPurpose);
GPIO_setMode(myGpio, nokiaBlight, GPIO_34_Mode_GeneralPurpose);
GPIO_setMode(myGpio, nokiaSce, GPIO_19_Mode_GeneralPurpose);
GPIO_setDirection(myGpio, nokiaVcc, GPIO_Direction_Output);
GPIO_setDirection(myGpio, nokiaRst, GPIO_Direction_Output);
GPIO_setDirection(myGpio, nokiaDc, GPIO_Direction_Output);
GPIO_setDirection(myGpio, nokiaBlight, GPIO_Direction_Output);
GPIO_setDirection(myGpio, nokiaSce, GPIO_Direction_Output);
GPIO_setPullUp(myGpio, nokiaVcc, GPIO_PullUp_Disable);
GPIO_setPullUp(myGpio, nokiaRst, GPIO_PullUp_Disable);
GPIO_setPullUp(myGpio, nokiaDc, GPIO_PullUp_Disable);
GPIO_setPullUp(myGpio, nokiaBlight, GPIO_PullUp_Disable);
GPIO_setPullUp(myGpio, nokiaSce, GPIO_PullUp_Disable);
// Configure GPIO #'s 1-4 for VHN5019AE
GPIO_setMode(myGpio, VNH5019AE_inA, GPIO_1_Mode_GeneralPurpose);
GPIO_setMode(myGpio, VNH5019AE_inB, GPIO_2_Mode_GeneralPurpose);
GPIO_setMode(myGpio, VNH5019AE_DiagA, GPIO_3_Mode_GeneralPurpose);
GPIO_setMode(myGpio, VNH5019AE_DiagB, GPIO_4_Mode_GeneralPurpose);
GPIO_setDirection(myGpio, VNH5019AE_inA, GPIO_Direction_Output);
GPIO_setDirection(myGpio, VNH5019AE_inB, GPIO_Direction_Output);
GPIO_setDirection(myGpio, VNH5019AE_DiagA, GPIO_Direction_Output);
GPIO_setDirection(myGpio, VNH5019AE_DiagB, GPIO_Direction_Output);
GPIO_setPullUp(myGpio, VNH5019AE_inA, GPIO_PullUp_Disable);
GPIO_setPullUp(myGpio, VNH5019AE_inB, GPIO_PullUp_Disable);
GPIO_setPullUp(myGpio, VNH5019AE_DiagA, GPIO_PullUp_Disable);
GPIO_setPullUp(myGpio, VNH5019AE_DiagB, GPIO_PullUp_Disable);
VNH5019AE_inA_Lo; // start out with both inA and inB low (brake to Gnd)
VNH5019AE_inB_Lo;
//VNH5019AE_DiagA_Hi ; // these are pulled up in hardware
//VNH5019AE_DiagB_Hi ;
//set up encoder on GPIO#'s 5&6
GPIO_setMode(myGpio, enc1, GPIO_6_Mode_GeneralPurpose);
GPIO_setMode(myGpio, enc2, GPIO_5_Mode_GeneralPurpose);
GPIO_setDirection(myGpio, enc1, GPIO_Direction_Input);
GPIO_setDirection(myGpio, enc2, GPIO_Direction_Input);
GPIO_setPullUp(myGpio, enc1, GPIO_PullUp_Enable);
GPIO_setPullUp(myGpio, enc2, GPIO_PullUp_Enable);
GPIO_setQualification(myGpio, enc1, GPIO_Qual_Sample_6);
GPIO_setQualification(myGpio, enc2, GPIO_Qual_Sample_6);
GPIO_setQualificationPeriod(myGpio, enc1, 0xFF);
GPIO_setQualificationPeriod(myGpio, enc2, 0xFF);
//enc1 is XINT1
GPIO_setExtInt(myGpio, enc1, CPU_ExtIntNumber_1);
//enc1 is XINT3
//GPIO_setExtInt(myGpio, enc1, CPU_ExtIntNumber_3);
//enc2 is XINT2
GPIO_setExtInt(myGpio, enc2, CPU_ExtIntNumber_2);
/////// this sets up encoder interrupts ////////
//Configure XINT1 to trigger on falling edge
PIE_setExtIntPolarity(myPie, CPU_ExtIntNumber_1, PIE_ExtIntPolarity_RisingAndFallingEdge);
//Configure XINT3 to trigger on falling edge
//PIE_setExtIntPolarity(myPie, CPU_ExtIntNumber_3, PIE_ExtIntPolarity_RisingEdge);
//Configure XINT2 to trigger on falling edge
PIE_setExtIntPolarity(myPie, CPU_ExtIntNumber_2, PIE_ExtIntPolarity_RisingAndFallingEdge);
//Register XINT1 interrupt handler in the PIE vector table
PIE_registerPieIntHandler(myPie, PIE_GroupNumber_1, PIE_SubGroupNumber_4, (intVec_t)&xint1_isr);
//Register XINT3 interrupt handler in the PIE vector table
//PIE_registerPieIntHandler(myPie, PIE_GroupNumber_12, PIE_SubGroupNumber_1, (intVec_t)&xint1_isr);
//Register XINT2 interrupt handler in the PIE vector table
PIE_registerPieIntHandler(myPie, PIE_GroupNumber_1, PIE_SubGroupNumber_5, (intVec_t)&xint2_isr);
PIE_enableInt(myPie, PIE_GroupNumber_1, PIE_InterruptSource_XINT_1);
//PIE_enableInt(myPie, PIE_GroupNumber_12, PIE_InterruptSource_XINT_3);
PIE_enableInt(myPie, PIE_GroupNumber_1, PIE_InterruptSource_XINT_2);
//Enable XINT1
PIE_enableExtInt(myPie, CPU_ExtIntNumber_1);
//Enable XINT3
//PIE_enableExtInt(myPie, CPU_ExtIntNumber_3);
//Enable XINT2
PIE_enableExtInt(myPie, CPU_ExtIntNumber_2);
//CPU_enableInt(myCpu, CPU_IntNumber_12);
CPU_enableInt(myCpu, CPU_IntNumber_1); //enable the enc1 and enc2 interrupt
}
void spi_init(void){
// enable Spia clock
CLK_enableSpiaClock(myClk);
//set up GPIO's for SPI
GPIO_setPullUp(myGpio, nokiaMosi, GPIO_PullUp_Disable);
GPIO_setPullUp(myGpio, nokiaClk, GPIO_PullUp_Disable);
GPIO_setPullUp(myGpio, nokiaSce, GPIO_PullUp_Disable);
GPIO_setQualification(myGpio, nokiaMosi, GPIO_Qual_ASync);
GPIO_setQualification(myGpio, nokiaClk, GPIO_Qual_ASync);
GPIO_setQualification(myGpio, nokiaSce, GPIO_Qual_ASync);
GPIO_setMode(myGpio, nokiaMosi, GPIO_16_Mode_SPISIMOA);
GPIO_setMode(myGpio, nokiaClk, GPIO_18_Mode_SPICLKA);
GPIO_setMode(myGpio, nokiaSce, GPIO_19_Mode_SPISTEA_NOT);
// Resets the serial peripheral interface (SPI)
SPI_reset(mySpi);
// Initializes the serial peripheral interface (SPI) object handle
SPI_enable(mySpi);
// Enables the serial peripheral interface (SPI) transmit and receive channels
SPI_enableChannels(mySpi);
// Enable master mode
SPI_setMode(mySpi, SPI_Mode_Master);
// Enables the serial peripheral interface (SPI) masater/slave transmit mode
SPI_enableTx(mySpi);
// Sets the serial peripheral interface (SPI) character length
SPI_setCharLength(mySpi, SPI_CharLength_8_Bits);
//SPI_enableLoopBack(mySpi);
// Sets the serial peripheral interface (SPI) clock phase
SPI_setClkPhase(mySpi, SPI_ClkPhase_Normal);
// Sets the serial peripheral interface (SPI) clock polarity
SPI_setClkPolarity(mySpi, SPI_ClkPolarity_OutputFallingEdge_InputRisingEdge);
// Enables the serial peripheral interface (SPI) over-run interrupt
// SPI_enableOverRunInt(mySpi);
SPI_enableInt(mySpi);
// Sets the serial peripheral interface (SPI) baud rate
SPI_setBaudRate(mySpi, (SPI_BaudRate_e)0x00);
//SPI_setBaudRate(mySpi, 0x00);
// Controls pin inversion of STE pin
// SPI_setSteInv(mySpi, SPI_SteInv_ActiveLow);
// Register interrupt handlers in the PIE vector table
PIE_registerPieIntHandler(myPie, PIE_GroupNumber_6, PIE_SubGroupNumber_1, (intVec_t)&spiRxFifoIsr);
PIE_enableInt(myPie, PIE_GroupNumber_6, PIE_InterruptSource_SPIARX);
//PIE_enableInt(myPie, PIE_GroupNumber_6, PIE_InterruptSource_SPIATX);
CPU_enableInt(myCpu, CPU_IntNumber_6);
// Set so breakpoints don't disturb xmission
SPI_setPriority(mySpi, SPI_Priority_FreeRun);
}
void adc_init(void){
ADC_reset(myAdc);
CLK_enableAdcClock(myClk);
// Initialize the ADC
ADC_enableBandGap(myAdc);
ADC_enableRefBuffers(myAdc);
ADC_powerUp(myAdc);
ADC_enable(myAdc);
ADC_setVoltRefSrc(myAdc, ADC_VoltageRefSrc_Int);
ADC_setIntPulseGenMode(myAdc, ADC_IntPulseGenMode_During); //ADCINT1 trips 1 cycle after AdcResults latch
ADC_enableInt(myAdc, ADC_IntNumber_1); //Enable ADCINT1
ADC_setIntSrc(myAdc, ADC_IntNumber_1, ADC_IntSrc_EOC1); //Connect ADCINT1 to EOC1
ADC_setSocTrigSrc(myAdc, ADC_SocNumber_0,ADC_SocTrigSrc_Sw); // software trigger
ADC_setSocTrigSrc(myAdc, ADC_SocNumber_1,ADC_SocTrigSrc_Sw); // software trigger
ADC_setSocTrigSrc(myAdc, ADC_SocNumber_3,ADC_SocTrigSrc_Sw); // software trigger
ADC_setSocTrigSrc(myAdc, ADC_SocNumber_4,ADC_SocTrigSrc_Sw); // software trigger
ADC_setSocChanNumber (myAdc, ADC_SocNumber_0, ADC_SocChanNumber_A0); //Set SOC0 channel select to ADCINA0
ADC_setSocChanNumber (myAdc, ADC_SocNumber_1, ADC_SocChanNumber_A1); //Set SOC1 channel select to ADCINA1
ADC_setSocChanNumber (myAdc, ADC_SocNumber_3, ADC_SocChanNumber_A3); //Set SOC3 channel select to ADCINA3
ADC_setSocChanNumber (myAdc, ADC_SocNumber_4, ADC_SocChanNumber_A4); //Set SOC4 channel select to ADCINA4
ADC_setSocSampleWindow(myAdc, ADC_SocNumber_0, ADC_SocSampleWindow_7_cycles); //Set SOC0 acquisition period to 7 ADCCLK
ADC_setSocSampleWindow(myAdc, ADC_SocNumber_1, ADC_SocSampleWindow_20_cycles); //Set SOC1 acquisition period to 20 ADCCLK
ADC_setSocSampleWindow(myAdc, ADC_SocNumber_3, ADC_SocSampleWindow_20_cycles); //Set SOC3 acquisition period to 20 ADCCLK
ADC_setSocSampleWindow(myAdc, ADC_SocNumber_4, ADC_SocSampleWindow_20_cycles); //Set SOC4 acquisition period to 20 ADCCLK
ADC_setIntMode(myAdc,ADC_IntNumber_1 ,ADC_IntMode_ClearFlag); //new interrupt will not happen until the flag is cleared
//ADC_setIntMode(myAdc,ADC_IntNumber_1 ,ADC_IntMode_EOC); //new interrupt will happen at the EOC regardless of flag
PIE_registerPieIntHandler(myPie, PIE_GroupNumber_10, PIE_SubGroupNumber_1, (intVec_t)&adc_Isr);
PIE_enableAdcInt(myPie, ADC_IntNumber_1);
CPU_enableInt(myCpu, CPU_IntNumber_10);
// Set the flash OTP wait-states to minimum. This is important
// for the performance of the temperature conversion function.
FLASH_setup(myFlash);
}
// Epwm
void epwm_init(void){
CLK_enablePwmClock(myClk, PWM_Number_1);
// Set up GPIO#'s 0 & 1 for EPWM1
GPIO_setPullUp(myGpio, GPIO_Number_0, GPIO_PullUp_Disable);
GPIO_setMode(myGpio, GPIO_Number_0, GPIO_0_Mode_EPWM1A);
//GPIO_setPullUp(myGpio, GPIO_Number_1, GPIO_PullUp_Disable);
//GPIO_setMode(myGpio, GPIO_Number_1, GPIO_1_Mode_EPWM1B);
// Setup TBCLK
PWM_setPeriod(myPwm1, EPWM1_START_TBPRD); // Start out at 1Khz
PWM_setPhase(myPwm1, 0x0000); // Phase is 0
PWM_setCount(myPwm1, 0x0000); // Clear counter
// Set Compare values
PWM_setCmpA(myPwm1,EPWM1_START_CMPA); // Set compare A value to half of TBPRD (1880) for 50% duty cycle 1Khz square wave
//PWM_setCmpB(myPwm1, EPWM1_MAX_CMPA); // for CMPB
// Setup counter mode
//PWM_setCounterMode(myPwm1, PWM_CounterMode_UpDown); // Count up/down
PWM_setCounterMode(myPwm1, PWM_CounterMode_Stop); // Count mode is stopped durind startup
PWM_disableCounterLoad(myPwm1); // Disable phase loading
PWM_setHighSpeedClkDiv(myPwm1, PWM_HspClkDiv_by_8); // Clock ratio to SYSCLKOUT/8
PWM_setClkDiv(myPwm1, PWM_ClkDiv_by_1);
// Setup shadowing
PWM_setShadowMode_CmpA(myPwm1, PWM_ShadowMode_Shadow);
PWM_setLoadMode_CmpA(myPwm1, PWM_LoadMode_Either);
//PWM_setShadowMode_CmpB(myPwm1, PWM_ShadowMode_Shadow);
//PWM_setLoadMode_CmpB(myPwm1, PWM_LoadMode_Either);
// Set actions
PWM_setActionQual_CntUp_CmpA_PwmA(myPwm1, PWM_ActionQual_Set); // Set PWM1A on event A, up count
PWM_setActionQual_CntDown_CmpA_PwmA(myPwm1, PWM_ActionQual_Clear); // Clear PWM1A on event A, down count
//PWM_setActionQual_CntUp_CmpB_PwmB(myPwm1, PWM_ActionQual_Set);
//PWM_setActionQual_CntDown_CmpB_PwmB(myPwm1, PWM_ActionQual_Clear);
// Set up interrupts
//PWM_setIntMode(myPwm1, PWM_IntMode_CounterEqualZeroOrPeriod); // Select INT on Zero or period event
//PWM_enableInt(myPwm1); // Enable INT
//PWM_setIntPeriod(myPwm1, PWM_IntPeriod_ThirdEvent); // Generate INT on 3rd event
// Register interrupt handlers in the PIE vector table
//PIE_registerPieIntHandler(myPie, PIE_GroupNumber_3, PIE_SubGroupNumber_1,
//(intVec_t) &epwm1_isr);
//Enable EPWM INTn in the PIE: Group 3 interrupt 1
//PIE_enablePwmInt(myPie, PWM_Number_1);
//Enable CPU INT3 which is connected to EPWM1 INT:
//CPU_enableInt(myCpu, CPU_IntNumber_3);
epwm1_info.myPwmHandle = myPwm1; // Set the pointer to the ePWM module
}
void timer0_init(void){
// Configure CPU-Timer0 to interrupt every 1 second:
// 60MHz CPU Freq, 50 millisecond Period (in uSeconds)
// ConfigCpuTimer(&CpuTimer0, 60, 500000); // 1/2 second interrupt
TIMER_stop(myTimer);
TIMER_setPeriod(myTimer, 50000000); // 1 second interrupt
TIMER_setPreScaler(myTimer, 0);
TIMER_reload(myTimer);
TIMER_setEmulationMode(myTimer, TIMER_EmulationMode_StopAfterNextDecrement);
//TIMER_setEmulationMode(myTimer, TIMER_EmulationMode_RunFree);
//TIMER_setEmulationMode(myTimer, TIMER_EmulationMode_StopAtZero);
TIMER_enableInt(myTimer);
// Register interrupt handlers in the PIE vector table
PIE_registerPieIntHandler(myPie, PIE_GroupNumber_1, PIE_SubGroupNumber_7,(intVec_t) &timer0_isr);
// Enable TINT0 in the PIE: Group 1 interrupt 7
PIE_enableTimer0Int(myPie);
// Enable CPU int1 which is connected to CPU-Timer 0.
CPU_enableInt(myCpu, CPU_IntNumber_1);
/*
TIMER_stop(myTimer1);
TIMER_setPeriod(myTimer1, 50000000); // 1 second interrupt
TIMER_setPreScaler(myTimer1, 0);
TIMER_reload(myTimer1);
//TIMER_setEmulationMode(myTimer, TIMER_EmulationMode_StopAfterNextDecrement);
TIMER_setEmulationMode(myTimer1, TIMER_EmulationMode_RunFree);
TIMER_enableInt(myTimer1);
// Register interrupt handlers in the PIE vector table
PIE_registerSystemIntHandler(myPie, PIE_SystemInterrupts_TINT1, (intVec_t)& timer1_isr);
// Enable CPU int13 which is connected to CPU-Timer 1.
CPU_enableInt(myCpu, CPU_IntNumber_13);
*/
}
// ftoa for converting a float to a string
//arguments: f = float to convert, buf = an array to write to, decPlaces = # of decimal places
void ftoa(float f, char *buf, int decPlaces) {
//int pos = 0, i, dp, num;
int pos = 0;
int i = 0;
int dp = 0;
int num = 0;
if (f < 0) {
buf[pos++] = '-';
f = -f;
}
dp = 0;
while (f >= 10.0) {
f = f / 10.0;
dp++;
}
unsigned int total = dp + decPlaces;
for (i = 0; i < total; i++) {
num = f;
f = f - num;
if (num > 9) {
buf[pos++] = '#';
} else {
buf[pos++] = '0' + num;
}
if (dp == 0) {
buf[pos++] = '.';
}
f = f * 10.0;
dp--;
} // loop
buf[pos++] = 0; // null
}
// base-10 itoa for positive numbers. Populates str with a null-terminated string.
// arguments: val = interger to be changed , str = an array to write to
void itoa(unsigned int val, char *str) {
int temploc = 0;
int digit = 0;
int strloc = 0;
char tempstr[5]; //16-bit number can be at most 5 ASCII digits;
// Get the number of digits
if (val >= 1000)
digit = 4;
else if (val >= 100)
digit = 3;
else if (val >= 10)
digit = 2;
else
digit = 1;
do {
digit = val % 10;
tempstr[temploc++] = digit + '0';
val /= 10;
} while (val > 0);
// reverse the digits back into the output string
while (temploc > 0)
str[strloc++] = tempstr[--temploc];
str[strloc] = 0;
}
float figureFreq(uint16_t TBPRD) { // for 7.5 Mhz clock (sysClk/8) .133us TBPRD
float tFreq = 2 * TBPRD * TPWM;
float freq = 1 / tFreq;
return freq * 1000; // for Mhz
}
/*
// rounding function
int round(float number) {
return (number >= 0) ? (int) (number + 0.5) : (int) (number - 0.5);
}
*/
// mapping function
long map(long x, long in_min, long in_max, long out_min, long out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void update_compare(EPWM_INFO *epwm_info){
// update TBPRD register
PWM_setPeriod(epwm_info->myPwmHandle,epwm_info->EPwm1_TBPRD); //update TBPRD
if (epwm1_info.EPwm1_TBPRD <= 190 ){ // 190 = 19.9 Khz
epwm1_info.EPwm1_CMPA = EPWM1_MIN_CMPA ; // make the CMPA MAX (95) as to not divide 0
PWM_setCmpA(epwm_info->myPwmHandle,epwm_info->EPwm1_CMPA); //update CMPA
}else
epwm1_info.EPwm1_CMPA = epwm1_info.EPwm1_TBPRD/2 ; // divide TBPRD by 2 for 50% duty cycle
PWM_setCmpA(epwm_info->myPwmHandle,epwm_info->EPwm1_CMPA); //update CMPA
// epwm1_info.EPwm1_CMPB= epwm1_info.EPwm1_TBPRD/2 ; // divide TBPRD by 2 for 50% duty cycle and shift 8 for EPWM
//PWM_setCmpA(epwm_info->myPwmHandle,epwm_info->EPwm1_CMPA); //update CMPA
// update TBPRD register
//PWM_setPeriod(epwm_info->myPwmHandle,epwm_info->EPwm1_TBPRD); //update TBPRD
// update CMPA register
//PWM_setCmpA(epwm_info->myPwmHandle,epwm_info->EPwm1_CMPA); //update CMPA
//PWM_setCmpB(epwm_info->myPwmHandle,epwm_info->EPwm1_CMPB); //update CMPB
}
// This code taken from Michael Kellett's
// Interfacing micro-controllers with incremental shaft encoders
uint16_t encUpdate(int16_t value){
//static uint16_t angle = 0; // encoder position
//static int16_t oldAngle = 0; // old position data
oldAngle = oldAngle << 2; // shift the old angle left 2 places
oldAngle |= (value & 0x03); // or in the new port value and
angle += table[(oldAngle & 0xf)]; // index into table for the direction value
if (angle <= 0) { // don't count past 0 or 19
angle = 0;
} else if (angle >= 19) {
angle = 19;
}
return angle;
}
void scia_init(void)
{
CLK_enableSciaClock(myClk);
// 1 stop bit, No loopback
// No parity,8 char bits,
// async mode, idle-line protocol
SCI_disableParity(mySci);
SCI_setNumStopBits(mySci, SCI_NumStopBits_One);
SCI_setCharLength(mySci, SCI_CharLength_8_Bits);
SCI_enableTx(mySci);
SCI_enableRx(mySci);
SCI_enableTxInt(mySci);
SCI_enableRxInt(mySci);
SCI_setBaudRate(mySci, SCI_BaudRate_115_2_kBaud);
SCI_enableFifoEnh(mySci);
SCI_resetTxFifo(mySci);
SCI_clearTxFifoInt(mySci);
SCI_resetChannels(mySci);
SCI_setTxFifoIntLevel(mySci, SCI_FifoLevel_Empty);
SCI_resetRxFifo(mySci);
SCI_clearRxFifoInt(mySci);
SCI_setRxFifoIntLevel(mySci, SCI_FifoLevel_4_Words);
SCI_setPriority(mySci, SCI_Priority_FreeRun);
SCI_enable(mySci);
xdev_out(scia_xmit); // Pointer to the output stream for ChaN's xprintf
}
// Transmit a character from the SCI
void scia_xmit(char a){
while(SCI_getTxFifoStatus(mySci) != SCI_FifoStatus_Empty){}
//SCI_putDataBlocking(mySci, a);
//SCI_putDataNonBlocking(mySci, a);
SCI_putData(mySci, a);
}
/*
void scia_msg(char * msg){
int i;
i = 0;
while(msg[i] != '\0')
{
scia_xmit(msg[i]);
i++;
}
}
*/
////////////////// truth table ///////////////////////////////
/*
* InA InB DiagA/EnA DiagB/EnB Operating Mode
*
* 1 1 1 1 Brake to Vcc
* 1 0 1 1 Clockwise
* 0 1 1 1 Counter clockwise
* 0 0 1 1 Brake to Gnd
*/
///////////////// functions for VNH5091AE //////////////////////////
void VNH5091AE_changeDir(void) {// this switches the actuator direction
if (CW == true) {
xputs("Direction = CCW\n");
CW = false;
} else {
xputs("Direction = CW\n" );
CW = true;
}
}
void VNH5091AE_CCW(void){
VNH5019AE_inA_Lo;
VNH5019AE_inB_Hi;
//VNH5019AE_DiagA_Hi; // these are pulled high in hardware
//VNH5019AE_DiagB_Hi;
}
void VNH5091AE_CW(void){
VNH5019AE_inA_Hi;
VNH5019AE_inB_Lo;
//VNH5019AE_DiagA_Hi; // these are pulled high in hardware
//VNH5019AE_DiagB_Hi;
}
void VNH5091AE_BRAKE_TO_VCC(void){
//CLK_disablePwmClock(myClk, PWM_Number_1 ); // disable the PWM1 clock
//epwm1_info.EPwm1_TBPRD = 0; // set TBPRD to 0
//PWM_setPeriod(epwm_info->myPwmHandle,epwm_info->EPwm1_TBPRD); // set the period to 0 for no PWM
PWM_setCounterMode(myPwm1, PWM_CounterMode_Stop); // stop PWM
VNH5019AE_inA_Hi;
VNH5019AE_inB_Hi;
//VNH5019AE_DiagA_Hi; // these are pulled high in hardware
//VNH5019AE_DiagB_Hi;
}
void VNH5091AE_BRAKE_TO_GND(void){