3769 |
kakl |
1 |
#include ".\main.h" |
|
|
2 |
|
|
|
3 |
|
|
|
4 |
//#define MAXDUTY (int16)160 // 50 kHz |
|
|
5 |
//#define MAXDUTY (int16)400 // 20 kHz |
|
|
6 |
//#define MAXDUTY (int16)800 // 10 kHz |
|
|
7 |
#define MAXDUTY (int16)1023 // 7.8 kHz |
|
|
8 |
#define HALFDUTY (int16)MAXDUTY/2 |
|
|
9 |
#define DIVISOR 255 // 7.8 kHz |
|
|
10 |
//#define DIVISOR 199 // 10 kHz |
|
|
11 |
//#define DIVISOR 99 // 20 kHz |
|
|
12 |
//#define DIVISOR 39 // 50 kHz |
|
|
13 |
|
|
|
14 |
void main() |
|
|
15 |
{ |
|
|
16 |
int16 reg=MAXDUTY; |
|
|
17 |
int16 current; |
|
|
18 |
int16 counter=0; |
|
|
19 |
int16 avg=0; |
|
|
20 |
|
|
|
21 |
setup_adc_ports(sAN0|VSS_VREF); |
|
|
22 |
setup_adc(ADC_CLOCK_DIV_32); |
|
|
23 |
setup_spi(SPI_SS_DISABLED); |
|
|
24 |
setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1); |
|
|
25 |
setup_timer_1(T1_DISABLED); |
|
|
26 |
setup_timer_2(T2_DIV_BY_1,DIVISOR,1); |
|
|
27 |
setup_ccp1(CCP_PWM); |
|
|
28 |
setup_ccp2(CCP_PWM); |
|
|
29 |
set_pwm1_duty(MAXDUTY); |
|
|
30 |
set_pwm2_duty(MAXDUTY); |
|
|
31 |
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard |
|
|
32 |
setup_oscillator(OSC_8MHZ); |
|
|
33 |
|
|
|
34 |
set_adc_channel(0); |
|
|
35 |
|
|
|
36 |
delay_ms(1); |
|
|
37 |
|
|
|
38 |
|
|
|
39 |
while(TRUE) |
|
|
40 |
{ |
|
|
41 |
output_low(PIN_D0); |
|
|
42 |
delay_us(40); |
|
|
43 |
output_high(PIN_D0); |
|
|
44 |
delay_ms(3); |
|
|
45 |
} |
|
|
46 |
|
|
|
47 |
|
|
|
48 |
set_pwm2_duty(HALFDUTY); |
|
|
49 |
delay_ms(10); |
|
|
50 |
set_pwm2_duty((int16)850); |
|
|
51 |
// set_pwm2_duty((int16)650); |
|
|
52 |
|
|
|
53 |
while(TRUE); |
|
|
54 |
|
|
|
55 |
while(TRUE) |
|
|
56 |
{ |
|
|
57 |
current=read_adc(); |
|
|
58 |
if (current<200) |
|
|
59 |
{ |
|
|
60 |
if (reg>HALFDUTY) reg--; |
|
|
61 |
} |
|
|
62 |
else |
|
|
63 |
{ |
|
|
64 |
if (reg<MAXDUTY) reg++; |
|
|
65 |
} |
|
|
66 |
set_pwm2_duty(reg); |
|
|
67 |
/* |
|
|
68 |
avg=avg/10*9+reg/10; |
|
|
69 |
//delay_ms(5); |
|
|
70 |
if (counter==60000) |
|
|
71 |
{ |
|
|
72 |
set_pwm2_duty(avg); |
|
|
73 |
while(TRUE){}; |
|
|
74 |
} |
|
|
75 |
counter++; |
|
|
76 |
*/ |
|
|
77 |
} |
|
|
78 |
} |