-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.c.bak
176 lines (160 loc) · 3.74 KB
/
main.c.bak
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
#define F_CPU 10000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
#include "UART.h"
#include "Hardware.h"
#include "Timer.h"
#include <avr/interrupt.h>
#include <math.h>
double freq=4;
double phase=0; //in rads
int dutyA;
double A=1.0;
#define maxValTimer 0x1ff
//#define Kv 900 //motor sky
#define Kv 62.5
//#define Kv 416
//#define maxFreq 570 //motor sky
#define maxFreq 39.68
//#define maxFreq 50
#define P 0.1
#define period 0.000064*8
//#define maxFreq 264
//This speed is for 12.7Volts: Formula: Poles*Kv*V/60. Poles=3, Kv=900
ISR(SIG_OVERFLOW1) {
//We enter this routine with a frequency of 15625Hz
//One count is 0.000064s
//static double freq=400;
// static double counter=0;
static double time=0;
int dutyB;
int dutyC;
if (freq==0) {
time=0;
}
if (time*freq >=1) {
time=(1/freq)-time;
//counter=0;
}
dutyA=A*maxValTimer*sin(2*M_PI*freq*time+phase);
dutyB=A*maxValTimer*sin(2*M_PI*freq*time-2.0*M_PI/3.0+phase);
dutyC=A*maxValTimer*sin(2*M_PI*freq*time-4.0*M_PI/3.0+phase);
//counter+=1;
time+=0.000064*8;
if (dutyA<0) {
OCR1A=0;
OCR3A=-dutyA;
} else {
OCR1A = dutyA;
OCR3A =0;
}
if (dutyB<0) {
OCR1B=0;
OCR3B=-dutyB;
} else {
OCR1B = dutyB;
OCR3B = 0;
}
if (dutyC<0) {
OCR1C=0;
OCR3C=-dutyC;
} else {
OCR1C = dutyC;
OCR3C = 0;
}
}
void delayms(uint16_t millis) {
//uint16_t loop;
while ( millis ) {
_delay_ms(1);
millis--;
}
}
int main(void) {
//int i;
FILE *u0;
double error;
double perror;
double dfreq=4;
double tfreq;
double dphase=0;
InitHardware();
#if defined( __AVR_LIBC_VERSION__ )
u0 = fdevopen( UART0_PutCharStdio, UART0_GetCharStdio );
#else
u0 = fdevopen( UART0_PutCharStdio, UART0_GetCharStdio, 0 );
#endif
OCR1A = 0x00;
OCR1B = 0;
OCR1C = 0;
OCR3A = 0;
OCR3B= 0 ;
OCR3C=0;
timer13Start();
while(1) {
LED_ON( BLUE );
LED_OFF( RED );
//printf("Led Blue On \n");
delayms(50);
LED_OFF( BLUE );
LED_ON( RED );
//printf("Led Blue OFF \n");
//TCNT1=0;
printf("Timer value: %04x ", TCNT1);
printf("Duty Cycle: %d \n", dutyA);
//printf("Control Registers 1: %02x 2: %02x 3: %02x\n", TCCR1A, TCCR1B, TCCR1C);
//printf("XVID crystal divider %02x\n",XDIV);
/*for ( i = 0; i < 100; i++ )
{
WaitForTimer0Rollover();
}*/
if ( UART0_IsCharAvailable() )
{
char ch = getchar();
printf( "Read: '%c'\n", ch );
if ( ch == ' ' )
{
printf( "*** Set the new frequency\n" );
//ch = getchar();
int rfreq;
scanf("%d",&rfreq);
printf( "*** Received %d. Continuing...\n",rfreq );
if (rfreq==0) {
freq=0;
printf( "*** Set the phase\n");
int rphase;
scanf("%d",&rphase);
printf( "*** Received %d. Continuing...\n",rphase);
dphase=rphase*2*M_PI/360;
}
dfreq=rfreq;
//freq=dfreq;
//A=freq/maxFreq;
//printf("Setting Amplitud to: %d \n",(int) (100*A));
}
}
perror=dphase-phase;
phase=phase+P*perror;
printf("Phase: %d \n", (int) (phase*360/(2*M_PI)));
error=dfreq-freq;
if (error>8.0) {
error=8.0;
}
if (error<-8.0) {
error=-8.0;
}
tfreq=freq+P*error;
A=tfreq/maxFreq;
if (A<0.6) {
A=0.6;
}
if (freq>maxFreq) {
A=1.0;
}
freq=tfreq;
printf("Frequency: %d \n",(int) freq);
delayms(50);
}
return 0;
}