7 |
kaklik |
1 |
// *************************************
|
|
|
2 |
// * Scamnum Specularis ** 20. 9. 2006 *
|
|
|
3 |
// *************************************
|
|
|
4 |
|
|
|
5 |
#include ".\menic.h"
|
|
|
6 |
|
|
|
7 |
#define TXo PIN_B5 // To the transmitter modulator
|
|
|
8 |
#include ".\AX25.c" // Podprogram pro prenos telemetrie
|
|
|
9 |
char AXstring[40]; // Buffer pro prenos telemetrie
|
|
|
10 |
|
|
|
11 |
#define REFSTART 95 // Hodnota odpovidajici 1,2V pri nezatizenych clancich
|
|
|
12 |
#define DUTY1 2 // Delka sepnuti PWM v us pro sero
|
|
|
13 |
#define DUTY2 19 // Delka sepnuti PWM v us pro jednu zarovku 500W
|
|
|
14 |
|
|
|
15 |
// Tabulka hodnot prevodniku pro ruzna napajeci napeti
|
|
|
16 |
// 80 3,99V
|
|
|
17 |
// 90 3,54V
|
|
|
18 |
// 100 3,20V
|
|
|
19 |
// 105 3,04V
|
|
|
20 |
// 107 2,99V
|
|
|
21 |
// 110 2,91v
|
|
|
22 |
// 200 1,61V
|
|
|
23 |
|
|
|
24 |
#define PIEZO PIN_A6 // Jeden pol piezo reproduktoru
|
|
|
25 |
#define LED PIN_A7 // LED / druhy pol piezo reproduktoru
|
|
|
26 |
#define MENIC PIN_B3 // Spinani tranzistoru menice
|
|
|
27 |
#define REFPWR PIN_B1 // Napajeni zdroje referencniho napeti
|
|
|
28 |
#define MOTOR PIN_B2 // Spinani tranzistoru motoru
|
|
|
29 |
#define SW1 PIN_B6 // DIPswitch 1
|
|
|
30 |
#define SW2 PIN_B7 // DIPswitch 2
|
|
|
31 |
#define NAPETI 4 // Cislo kanalu DA prevodniku pro mereni napeti
|
|
|
32 |
#define PWM 0 // Cislo kanalu DA prevodniku pro nastaveni PWM
|
|
|
33 |
#define ROZJEZD 1 // Cislo kanalu DA prevodniku pro nastaveni rozjezdu
|
|
|
34 |
|
|
|
35 |
#byte SSPBUF = 0x13 // Adresy registru jednotky SSP
|
|
|
36 |
#byte SSPCON1 = 0x14
|
|
|
37 |
#byte SSPSTAT = 0x94
|
|
|
38 |
|
|
|
39 |
// Vzorky pro generovani PWM pomoci SSP
|
|
|
40 |
unsigned int8 const Pattern[8] = {0x02,0x06,0x0e,0x1e,0x3e,0x7e,0xfe,0xff};
|
|
|
41 |
unsigned int8 MotorPattern;
|
|
|
42 |
|
|
|
43 |
#INT_SSP
|
|
|
44 |
void IntSSP()
|
|
|
45 |
{
|
|
|
46 |
SSPBUF=MotorPattern; // znovu vysli stejnou hodnotu
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
void pip() // pipnuti a bliknuti
|
|
|
50 |
{
|
|
|
51 |
int1 beep;
|
|
|
52 |
int i;
|
|
|
53 |
|
|
|
54 |
for(i=0; i<250; i++)
|
|
|
55 |
{
|
|
|
56 |
output_bit(PIEZO, beep);
|
|
|
57 |
beep=~beep;
|
|
|
58 |
output_bit(LED, beep);
|
|
|
59 |
delay_us(100);
|
|
|
60 |
};
|
|
|
61 |
output_high(PIEZO);
|
|
|
62 |
output_high(LED);
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
void cvak() // pouze cvaknuti piezoelementem bez bliknuti
|
|
|
66 |
{
|
|
|
67 |
output_bit(PIEZO,~input(PIEZO));
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
void main()
|
|
|
71 |
{
|
|
|
72 |
int8 n;
|
|
|
73 |
int8 perioda;
|
|
|
74 |
int8 razeni;
|
|
|
75 |
int8 uclanku;
|
|
|
76 |
|
|
|
77 |
setup_timer_0(RTCC_INTERNAL);setup_wdt(WDT_1152MS);
|
|
|
78 |
setup_timer_1(T1_DISABLED);
|
|
|
79 |
setup_adc_ports(ADC_CLOCK_INTERNAL|sAN0|sAN1|sAN4|VSS_VDD);
|
|
|
80 |
setup_adc(ADC_CLOCK_INTERNAL);
|
|
|
81 |
setup_spi(FALSE);
|
|
|
82 |
setup_comparator(NC_NC_NC_NC);
|
|
|
83 |
setup_vref(FALSE);
|
|
|
84 |
|
|
|
85 |
output_low(MENIC); // Vsechno zastav
|
|
|
86 |
output_low(MOTOR);
|
|
|
87 |
output_low(REFPWR);
|
|
|
88 |
output_high(LED);
|
|
|
89 |
output_high(PIEZO);
|
|
|
90 |
|
|
|
91 |
set_adc_channel(NAPETI); // Vyber AD kanal pro pomerne mereni napeti
|
|
|
92 |
set_pwm1_duty(0); // Spust PWM, ale zatim s trvalou 0 na vystupu
|
|
|
93 |
setup_ccp1(CCP_PWM);
|
|
|
94 |
setup_timer_2(T2_DIV_BY_1,100,1); // perioda
|
|
|
95 |
|
|
|
96 |
setup_oscillator(OSC_4MHZ|OSC_INTRC); // rozjed PIC (zvysi se odber)
|
|
|
97 |
|
|
|
98 |
// jsme v depu?
|
|
|
99 |
set_pwm1_duty(DUTY1); // PWM pro zatizeni clanku pri malem osvetleni
|
|
|
100 |
n=0;
|
|
|
101 |
do
|
|
|
102 |
{
|
|
|
103 |
output_low(REFPWR); // Vypni napajeni napetove reference, aby se setrilo
|
|
|
104 |
delay_ms(100);
|
|
|
105 |
if (4==n++) {cvak(); n=0;}; // Kazdy 4. pruchod cvakni
|
|
|
106 |
output_high(REFPWR); // Zapni napajeni napetove reference
|
|
|
107 |
delay_ms(1); // Pockej na ustaleni napeti
|
|
|
108 |
} while (read_adc()>REFSTART); // Odkryl starter clanky?
|
|
|
109 |
output_low(REFPWR); // Vypni napajeni napetove reference, aby se setrilo
|
|
|
110 |
|
|
|
111 |
// port_b_pullups(TRUE); // Zapni pullupy, aby slo cist DIPswitch
|
|
|
112 |
// do
|
|
|
113 |
// {
|
|
|
114 |
// Precti trimry
|
|
|
115 |
set_adc_channel(PWM); // Zjisteni nastaveni PWM
|
|
|
116 |
delay_us(100); // Pockej na prepnuti kanalu A/D prevodniku
|
|
|
117 |
perioda=(read_adc()>>1)+40; // rozsah: 40 az 167
|
|
|
118 |
|
|
|
119 |
set_adc_channel(ROZJEZD); // Zjisteni nastaveni rozjezdu
|
|
|
120 |
delay_us(100); // Pockej na prepnuti kanalu A/D prevodniku
|
|
|
121 |
razeni=read_adc()>>1; // rozsah: 0 az 127
|
|
|
122 |
|
|
|
123 |
// Rozjeti menice
|
|
|
124 |
set_pwm1_duty(0); // Zastav PWM, aby slo nastavit jinou frekvenci
|
|
|
125 |
setup_timer_2(T2_DIV_BY_1,perioda,1); // perioda
|
|
|
126 |
set_pwm1_duty(DUTY2); // PWM pro zatizeni clanku pri jedne 500W zarovce
|
|
|
127 |
|
|
|
128 |
pip(); // potvrzeni odstartovani / servisni mod
|
|
|
129 |
delay_ms(500);
|
|
|
130 |
|
|
|
131 |
// } while (input(SW1)); // Je zapnut servisni mod? SW1(OFF)=ANO
|
|
|
132 |
// port_b_pullups(FALSE); // Vypni pullupy, aby se setrilo energii
|
|
|
133 |
|
|
|
134 |
// Regulace nabíjení
|
|
|
135 |
output_high(REFPWR); // Prepni AD na mereni napeti clanku
|
|
|
136 |
set_adc_channel(NAPETI); // Prepni Zmer napeti clanku
|
|
|
137 |
delay_us(50); // Pockej na prepnuti kanalu A/D prevodniku
|
|
|
138 |
for(n=1;n<=10;n++)
|
|
|
139 |
{
|
|
|
140 |
// Zmer napeti (aktualni zatizeni clanku)
|
|
|
141 |
uclanku=read_adc();
|
|
|
142 |
// Uprav parametry menice
|
|
|
143 |
if (uclanku>105)
|
|
|
144 |
{
|
|
|
145 |
perioda++;
|
|
|
146 |
}
|
|
|
147 |
else
|
|
|
148 |
{
|
|
|
149 |
perioda--;
|
|
|
150 |
}
|
|
|
151 |
set_pwm1_duty(0); // Zastav PWM, aby slo nastavit jinou frekvenci
|
|
|
152 |
setup_timer_2(T2_DIV_BY_1,perioda,1); // perioda
|
|
|
153 |
set_pwm1_duty(DUTY2); // PWM pro zatizeni clanku pri jedne 500W zarovce
|
|
|
154 |
delay_ms(100);
|
|
|
155 |
}
|
|
|
156 |
output_low(REFPWR);
|
|
|
157 |
// posli telemetrii
|
|
|
158 |
sprintf(AXstring,"p=%u r=%u u=%u", perioda, razeni, uclanku); // Convert DATA to String.
|
|
|
159 |
SendPacket(&AXstring[0]);
|
|
|
160 |
|
|
|
161 |
delay_ms(13000); // Pockame 14s
|
|
|
162 |
|
|
|
163 |
// rozjezd
|
|
|
164 |
SSPSTAT = 0; // inicializace SPI jednotky
|
|
|
165 |
SSPCON1 = 0x22; // SPI OSC/64
|
|
|
166 |
MotorPattern=0x02; // prvni data pro vyslani
|
|
|
167 |
SSPBUF=MotorPattern;
|
|
|
168 |
enable_interrupts(global);
|
|
|
169 |
enable_interrupts(INT_SSP); // az budou vyslana, prijde interrupt od SSP
|
|
|
170 |
|
|
|
171 |
for(n=0; n<=7; n++) // razeni osmi rychlostnich stupnu
|
|
|
172 |
{
|
|
|
173 |
MotorPattern=Pattern[n];
|
|
|
174 |
delay_ms(razeni); // Pockej na dalsi razeni
|
|
|
175 |
};
|
|
|
176 |
SSPSTAT = 0;
|
|
|
177 |
SSPCON1 = 0; // SPI stop
|
|
|
178 |
disable_interrupts(INT_SSP); // zastav preruseni od SSP
|
|
|
179 |
|
|
|
180 |
// jedem co to da
|
|
|
181 |
output_high(MOTOR);
|
|
|
182 |
|
|
|
183 |
/*
|
|
|
184 |
if (TRUE) // Venkovni jizda?
|
|
|
185 |
{
|
|
|
186 |
delay_ms(5000); // Venku muzeme jet 5s
|
|
|
187 |
set_pwm1_duty(0); // Zastav menic
|
|
|
188 |
// output_low(MOTOR); // Zastav motor
|
|
|
189 |
while(TRUE) restart_wdt(); // Cekej porad
|
|
|
190 |
};
|
|
|
191 |
delay_ms(3000);
|
|
|
192 |
*/
|
|
|
193 |
delay_ms(1300); // Nemeli bysme jet dele nez 1,3s :-)
|
|
|
194 |
output_low(MOTOR); // Zastav motor
|
|
|
195 |
// Menic je stale zapnut a zatezuje clanky
|
|
|
196 |
|
|
|
197 |
while(TRUE); // Cekej, dokud se neztrati svetlo,
|
|
|
198 |
// nebo prijde WatchDog
|
|
|
199 |
|
|
|
200 |
}
|