-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPWM.c
More file actions
127 lines (94 loc) · 3.52 KB
/
PWM.c
File metadata and controls
127 lines (94 loc) · 3.52 KB
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
#include <xc.h>
#include "HardwareDef.h"
#include "PWM.h"
#include <stdio.h>
#include <dsp.h>
#include <math.h>
#include "UART.h"
//PWM Variables definitions
int Sin[LEN_SIN]; //SIN Buffer
long sin_step;
float sin_freq;
#ifdef DEBUG1
float step_array[LEN_SIN];
int OC1R_array[LEN_SIN];
char f_debug_sin; //Flag inidicates buffer is ready. For debuging level 1
#endif
extern char buff[60];
/******************************************************************************
* Function: PWM1_Init(int F_pwm1)
*
* Input: int F_pwm1 -> unimplemented
* Output: none
*
* Description: Initialize Output Compare Module as PWM
******************************************************************************/
void PWM1_Init(int F_pwm1){
/*---------------- OC - PWM - Remapeo de Pines ------------------------------*/
__builtin_write_OSCCONL(OSCCON & 0xBF); // Unlock Registers
// Configure Output Functions (Table 10-3)
RPOR2bits.RP5R = 18; // Assign OC1 To Pin RP5
// RPOR1bits.RP2R = 19; // Assign OC2 To Pin RP2 //Not implemented for MEII
// RPOR1bits.RP3R = 20; // Assign OC3 To Pin RP3 //Not implemented for MEII
__builtin_write_OSCCONL(OSCCON | 0x40); // Lock Registers
/*-------------OC- PWM - FIN Remapéo de Pines --------------------------------*/
// The following code example will set the Output Compare 1 module
OC1CON = 0x0000; // Turn off Output Compare 1 Module
OC1R = 0x0080; // Initialize Compare Register1 50%
OC1RS = 0x0080; // Initialize Secondary Compare Register1 50%
//Timer2 in initialized and enabled on Timer2_Init()
// PR2 = 0x3E80; // Initialize PR2 with 16000 counts = 1Khz
//START!! OC1
OC1CON = 0x0006; // Load new compare mode to OC1CON
//interrupt is managed by T2
return;
}
/******************************************************************************
* Function: sin_Init(float frecuencia);
*
* Input: float frecuencia -> initial Sin Output Frecuency (in Hz)
* Output: none
*
* Description: Initialize Sin Array
******************************************************************************/
void sin_Init(float f_sin_init) {
int i;
sin_step = set_sin_step( f_sin_init);
#ifdef DEBUG1
SendStringPolling("\r\nsin_step: ");
SendIntPolling(sin_step);
SendStringPolling("\r\nstep_delta: ");
SendIntPolling(step_delta);
#endif
for (i = 0; i < LEN_SIN; i++) {
Sin[i] = (int) ((1 + cosf(2 * PI / LEN_SIN * i)) * (vm_PWM / 2)); //Senoide de paso 0.5 grados
if (Sin[i] < 1)
Sin[i] = 1;
}
#ifdef DEBUG1
SendStringPolling("\r\nSTART Sin[i]:\r\n");
for (i = 0; i < LEN_SIN; i++){
SendFloatPolling(Sin[i]);
SendStringPolling("\r\n");
}
SendStringPolling("END Sin[i]\r\n");
#endif
return;
}
/******************************************************************************
* Function: sin_set_step(float f_sin)f
*
* Input: float f_sin -> Sin Output Frecuency (in Hz)
* Output: long paso_fp -> step
*
* Description: return step un fixed point format.
* for example, 4,14 is returned as 414
******************************************************************************/
long set_sin_step(float f_sin){
float paso;
long paso_fp;
paso = LEN_SIN / ( f_PWM / f_sin );
paso = paso * 100;
paso_fp = (long) paso;
return paso_fp;
}