2 |
kaklik |
1 |
//----------------------------
|
|
|
2 |
// Sagitta - 9. 9. 2005
|
|
|
3 |
//----------------------------
|
|
|
4 |
|
|
|
5 |
#include ".\menic.h"
|
|
|
6 |
|
|
|
7 |
#define RAZENI 55 // Po kolika ms se bude menit rychlost pri rozjizdeni
|
|
|
8 |
#define REFSTART 120 // Hodnota komparatoru odpovidajici 1,7V pri
|
|
|
9 |
// nezatizenych solarnich clancich
|
|
|
10 |
#define DUTY1 2 // Delka sepnuti PWM v us pro sero
|
|
|
11 |
#define DUTY2 20 // Delka sepnuti PWM v us pro jednu zarovku 500W
|
|
|
12 |
|
|
|
13 |
// Tabulka napeti vuci referencni LED (cca 1,7V)
|
|
|
14 |
// hodnota prevodniku / napeti na clancich
|
|
|
15 |
// 250 1,6V
|
|
|
16 |
// 200 2,1V
|
|
|
17 |
// 150 2,8V
|
|
|
18 |
// 145 2,9V
|
|
|
19 |
// 120 3,6V
|
|
|
20 |
// 110 4,0V
|
|
|
21 |
|
|
|
22 |
#define MENIC PIN_B3 // Spinani tranzistoru menice
|
|
|
23 |
#define REFPWR PIN_B1 // Napajeni zdroje referencniho napeti
|
|
|
24 |
#define MOTOR PIN_B2 // Spinani tranzistoru motoru
|
|
|
25 |
|
|
|
26 |
#byte SSPBUF = 0x13 // Registry jednotky SSP
|
|
|
27 |
#byte SSPCON1 = 0x14
|
|
|
28 |
#byte SSPSTAT = 0x94
|
|
|
29 |
|
|
|
30 |
// Vzorky pro generovani PWM pomoci SSP
|
|
|
31 |
unsigned int8 const Pattern[8] = {0x02,0x06,0x0e,0x1e,0x3e,0x7e,0xfe,0xff};
|
|
|
32 |
unsigned int8 MotorPattern;
|
|
|
33 |
|
|
|
34 |
#INT_SSP
|
|
|
35 |
void IntSSP() // Vyvola se po odeslani znaku pres SSP
|
|
|
36 |
{
|
|
|
37 |
SSPBUF=MotorPattern; // znovu vysli stejnou hodnotu
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
void main()
|
|
|
41 |
{
|
|
|
42 |
int8 n;
|
|
|
43 |
int8 cekej;
|
|
|
44 |
int8 perioda;
|
|
|
45 |
int8 rozjezd;
|
|
|
46 |
|
|
|
47 |
// Inicializace casovacu a prevodniku
|
|
|
48 |
setup_timer_0(RTCC_INTERNAL);setup_wdt(WDT_1152MS);
|
|
|
49 |
setup_adc_ports(ADC_CLOCK_INTERNAL|sAN1|sAN4|VSS_VDD);
|
|
|
50 |
setup_adc(ADC_CLOCK_INTERNAL);
|
|
|
51 |
setup_spi(FALSE);
|
|
|
52 |
setup_timer_1(T1_DISABLED);
|
|
|
53 |
setup_comparator(NC_NC_NC_NC);
|
|
|
54 |
setup_vref(FALSE);
|
|
|
55 |
|
|
|
56 |
output_low(MENIC); // Vsechno zastav
|
|
|
57 |
output_low(MOTOR);
|
|
|
58 |
output_low(REFPWR);
|
|
|
59 |
|
|
|
60 |
set_adc_channel(4); // Vyber AD kanal pro pomerne mereni napeti
|
|
|
61 |
set_pwm1_duty(0); // Spust PWM, ale zatim s trvalou 0 na vystupu
|
|
|
62 |
setup_ccp1(CCP_PWM); // PWM pro zatizeni clanku v seru
|
|
|
63 |
setup_timer_2(T2_DIV_BY_1,100,1); // perioda PWM
|
|
|
64 |
|
|
|
65 |
setup_oscillator(OSC_4MHZ|OSC_INTRC); // rozjed PIC (zvysi se odber)
|
|
|
66 |
|
|
|
67 |
// jsme v depu?
|
|
|
68 |
set_pwm1_duty(DUTY1); // delka sepnuti
|
|
|
69 |
do
|
|
|
70 |
{
|
|
|
71 |
output_low(REFPWR);
|
|
|
72 |
delay_ms(100);
|
|
|
73 |
output_high(REFPWR); // zapni napajeni napetove reference (LED)
|
|
|
74 |
delay_ms(20);
|
|
|
75 |
} while (read_adc()>REFSTART); // Odkryl starter clanky?
|
|
|
76 |
output_low(REFPWR);
|
|
|
77 |
|
|
|
78 |
// Precti trimr
|
|
|
79 |
set_adc_channel(1); // Zjisteni nastaveni trimru PWM
|
|
|
80 |
delay_us(100); // Pockej na prepnuti kanalu A/D prevodniku
|
|
|
81 |
perioda=(read_adc()>>1)+40; // rozsah: 40 az 167
|
|
|
82 |
set_pwm1_duty(0); // Zastav PWM, aby slo zmenit periodu
|
|
|
83 |
setup_timer_2(T2_DIV_BY_1,perioda,1); // Zapis nastavenou periodu
|
|
|
84 |
|
|
|
85 |
// nabíjíme
|
|
|
86 |
set_pwm1_duty(DUTY2); // delka sepnuti PWM
|
|
|
87 |
delay_ms(12500);
|
|
|
88 |
|
|
|
89 |
// rozjezd
|
|
|
90 |
SSPSTAT = 0; // inicializace SPI jednotky
|
|
|
91 |
SSPCON1 = 0x22; // SPI OSC/64
|
|
|
92 |
MotorPattern=Pattern[0];
|
|
|
93 |
SSPBUF=MotorPattern; // 1. data pro vyslani
|
|
|
94 |
enable_interrupts(global);
|
|
|
95 |
enable_interrupts(INT_SSP); // az budou vyslana, prijde preruseni
|
|
|
96 |
|
|
|
97 |
for(n=0; n<=7; n++) // razeni osmi rychlostnich stupnu
|
|
|
98 |
{
|
|
|
99 |
MotorPattern=Pattern[n];
|
|
|
100 |
delay_ms(RAZENI); // Pockej na dalsi razeni
|
|
|
101 |
};
|
|
|
102 |
SSPSTAT = 0;
|
|
|
103 |
SSPCON1 = 0; // SPI stop
|
|
|
104 |
disable_interrupts(INT_SSP); // zastav preruseni od SSP
|
|
|
105 |
|
|
|
106 |
// jedem co to da
|
|
|
107 |
output_high(MOTOR);
|
|
|
108 |
delay_ms(1300); // dyl jak 1,3s bysme nemeli jet :)
|
|
|
109 |
output_low(MOTOR); // zastav motor
|
|
|
110 |
// Menic je stale zapnut a zatezuje clanky
|
|
|
111 |
|
|
|
112 |
while(TRUE); // Cekej, dokud se neztrati svetlo,
|
|
|
113 |
// nebo prijde WatchDog
|
|
|
114 |
}
|