01.#include <ht45rm03.h>
02.#include "htddc.h"
03.
04.#pragma vector isr_comp @ 0x4
05.#pragma vector isr_int0 @ 0x8
06.#pragma vector isr_int1 @ 0xc
07.#pragma vector isr_pwm @ 0x10
08.#pragma vector isr_tmr0 @ 0x14
09.#pragma vector isr_tmr1 @ 0x18
10.//ISR for safequard
11.void isr_comp() // 比较器输出
12.{ _cf=0;
13.if(!_cmpop)//确认是否有效的低电平
14. {
15. _pwmh=0;_pwml=0;//关闭PWM
16._pc=0b00101010;
17._misc=0b00100100; //_misc.4=pwmctrl==>PWM总开关
18._pwmc=0x44; //死区开启,延迟0.33us
19.
20.pwm_enable_f=0;//xunhan_f=0;
21.runing_err_f=1;
22.delay_3s=60;
23. }
24.}
25.void isr_int0() // 外部INT0A,B,C
26.{
27.}
28.void isr_int1(){} // 外部INT1
29.void isr_pwm(){} // PWM
30.void isr_tmr0() // timer/event 0//电机霍尔检测
31.{
32. read_xy:
33. xy_temp =_pa;xy_temp &=0x70;
34. for(xy_i=2;xy_i>0;xy_i--)
35. {
36.xy_temp1=_pa;xy_temp1 &=0x70;
37.if(xy_temp!=xy_temp1)goto read_xy;
38. }
39. xy_temp>>=4;xy_temp&=0x07;
40._t0f=0; //33.33us once
41.ms++;sys_5ms++;time_f=1;
42.if(xy_temp!=xy_buf){xy_buf=xy_temp;xy_cnt=0;}
43.else xy_cnt++;
44.if(xy_cnt>=2)
45. { xy_cnt=0;
46. if(xy_buf==0||xy_buf==7){xy_err++;xy_err_f=1;_pc=djout120[0];}
47. else
48. {if(xy_err_f)
49. {xy_err_f=0;last_xy=xy_buf;}
50. if(xy_err!=0)xy_err--;
51. if(xy_err>9)xy_err--;
52. if(pwm_enable_f)_pc=djout120[xy_buf];
53. }
54.
55. }
56.}
57.void isr_tmr1() // timer/event 1//与PC的串行通讯1200波特率
58.{ _t1f=0;
59.if(tx_bit1_3==0)
60. { tx_bit1_3=2;
61. if(tx_bit_cnt==9)TX=0;
62. else
63. {if(tx_bit_cnt>0)
64. {if(tx_buf&1)TX=1;else TX=0;
65. tx_buf>>=1;
66. }
67. }
68. if(tx_bit_cnt==0)TX=1;
69. else tx_bit_cnt--;
70. }
71.else tx_bit1_3--;
72.//receive
73.if(rx_sta_f)
74. { if(rx_bit1_3==0)
75. { rx_bit1_3=2;
76. rx_buf>>=1;
77. if(RX)rx_buf |= 0x80;
78. rx_bit_cnt--;
79. if(rx_bit_cnt==0)
80. {rx_sta_f=0;rx_bak_f=1;_mp1=rx_buf;tx_buf=_iar1;tx_bit_cnt=9;//
81. }
82. }
83. else rx_bit1_3--;
84. }
85.else { if(!RX && !rx_bak_f){rx_sta_f=1;rx_bit1_3=2;rx_bit_cnt=8;}
86.if(RX)rx_bak_f=1;else rx_bak_f=0;
87. }
88.}
89.//=========================================
90.void offset_cmp(void)
91.{ unsigned char temp,temp1,tempa,i;
92._cmpc=0b10110000;_nop();_nop();//以CMP+为参考
93.temp=_cmpc & 0b01000000;//读cmp out
94.for(i=15;i>0;i--)
95. { _cmpc++;_nop();_nop();
96. temp1=_cmpc & 0b01000000;
97. if(temp!=temp1)break;
98. }
99.tempa=_cmpc & 0x0f;
100.
101._cmpc=0b10111111;_nop();_nop();
102.temp=_cmpc & 0b01000000;
103.for(i=15;i>0;i--)
104. { _cmpc--;_nop();_nop();
105. temp1=_cmpc & 0b01000000;
106. if(temp!=temp1)break;
107. }
108.i=_cmpc & 0x0f;
109.tempa+=i;tempa>>=1;tempa &= 0x0f;
110._cmpc=0b10000000;
111._cmpc |= tempa;
112.}
113.//------------------------------------
114.void offset_opa(void)
115.{ unsigned char temp,temp1,tempa,i;
116._opac=0b10100000;_nop();_nop();//以OPA-为参考
117.temp=_opac & 0b01000000;//读opa out
118.for(i=15;i>0;i--)
119. { _opac++;_nop();_nop();
120. temp1=_opac & 0b01000000;
121. if(temp!=temp1)break;
122. }
123.tempa=_opac & 0x0f;
124.
125._opac=0b10111111;_nop();_nop();
126.temp=_opac & 0b01000000;
127.for(i=15;i>0;i--)
128. { _opac--;_nop();_nop();
129. temp1=_opac & 0b01000000;
130. if(temp!=temp1)break;
131. }
132.i =_opac & 0x0f;
133.tempa+=i;tempa>>=1;tempa &= 0x0f;
134._opac=0b10000000;
135._opac |= tempa;
136.}
137.//=========================================
138.void djmode_set(void)//检测电机的类型60或120度的
139.{ unsigned char temph,templ,i;
140.temph = 0;templ = 0;
141.for(i=24;i>0;i--)
142. { _pbc |= 0b10000000;
143.
144. if(djmode_pin)temph++;
145. else templ++;
146. _delay(100);
147. }
148.if(temph>templ)djmode_f=1;
149.else djmode_f=0;
150.}
151.//=========================================
152.//测量A/D并平均8次
153.//=========================================
154.unsigned char ad_pro(unsigned char channel)
155.{ unsigned char i;
156. unsigned long temp;
157.channel &=0x3;
158._adcr =0b00011000;
159._adcr|=channel;
160.temp=0;
161.for(i=8;i>0;i--)
162. { _start=0;_nop();
163._start=1;//reset AD
164._start=0;//start AD
165.while(_eocb)_nop();
166.temp+=_adrh;
167. }
168.temp>>=3;
169.i=(unsigned char)temp;
170.return(i);
171.}
172.//===========================================
173.void nmos_chk(void)
174.{ unsigned char temp;
175.off_pwm_pro();
176._pc=0;//打开所有下桥
177._adcr =0b00011010;//电流AN2
178._delay(10);
179._start=0;_nop();
180._start=1;//reset AD
181._start=0;//start AD
182._delay(300);
183._pc=0b00101010;//关闭all nmos
184.while(_eocb)_nop();
185.temp=_adrh;
186.
187.if(temp>i_normal)nmos_err_f=1;
188.if(nmos_err_f)return;
189.
190._pc=0x3f;//打开所有上桥
191._adcr =0b00011010;//电流AN2
192._delay(10);
193._start=0;_nop();
194._start=1;//reset AD
195._start=0;//start AD
196._delay(300);
197._pc=0b00101010;//关闭all nmos
198.while(_eocb)_nop();
199.temp=_adrh;
200.if(temp>i_normal)nmos_err_f=1;
201.}
202.
203.//==========================================
204.void first_i(void)
205.{ unsigned char temp;
206.temp=ad_pro(2);//电流测量在AN2
207.if(temp>8){opa_err_f=1;temp=0;}
208.i_normal=temp+25;
209.i_max=temp+50;
210.}
211.//==========================================
212.void break_chk(void)
213.{ unsigned char temph,templ,i;
214.temph = 0;templ = 0;
215.for(i=16;i>0;i--)
216. { _pac |= 0b10000000;//pa7/int1 --break input
217.
218. if(break_pin)temph++;
219. else templ++;
220. _delay(100);
221. }
222.if(temph>templ)break_f=0;
223.else break_f=1;
224.}
225.//==========================================
226.void time_nms(void)//200us once
227.{
228.if(sys_5ms>=150){sys_5ms=0;sys50ms++;}//150*0.03333ms=5ms
229.else return;
230.if(sys50ms>=10)sys50ms=0;
231.else return;
232.
233.if(vol_del3s!=0)vol_del3s--;//50ms once
234.if(delay_3s!=0)delay_3s--;//50ms once
235.
236.if(disp_once==0)
237. { disp_once=96;//7s
238. if(nmos_err_f){flash_num=4;goto led_set_end;}
239. if(runing_err_f){flash_num=5;goto led_set_end;}
240. if(xy_err>6){flash_num=6;goto led_set_end;}
241. if(vol_low_f) {flash_num=8;goto led_set_end;}
242. if(break_f) {flash_num=2;goto led_set_end;}
243. if(opa_err_f) {flash_num=3;goto led_set_end;}
244.
245. if(pwm_enable_f)flash_num=0;//工作不亮
246. else flash_num=200;//不工作正常一直闪
247.
248. led_set_end:_nop();
249. }
250.else disp_once--;
251.if(disp_ms==0)
252. { if(flash_num!=0)
253. {disp_ms=8;flash_num--;}
254.
255. }
256.else disp_ms--;
257.if(disp_ms<4)off_led;else on_led;
258.}
259.//=========================================
260.// ;取样电阻7.5K+7.5K+1.2K
261.// ;40.V电压转换值:151
262.// ;41.V电压转换值:155
263.// ;42.V电压转换值:159
264.// ;43.V电压转换值:163
265.// ;44.V电压转换值:167
266.// ;45.V电压转换值:171
267.void vol_chk_pro(void)//电流因素未考虑
268.{
269.if(vol_buf>154)
270. { if(vol_before_f){vol_before_f=0;vol_del3s=60;}//3s=60*50ms
271. if(vol_del3s==0)vol_low_f=0;
272. }
273.if(vol_buf<142)
274. { if(!vol_before_f){vol_before_f=1;vol_del3s=60;}
275. if(vol_del3s==0)vol_low_f=1;
276. }
277.}
278.//=========================================================
279.//转速把电压范围处理
280.//自行车:限速模式下应为1.2V--4.2V,
281.//转把霍尔正常输出低电平为0.8-->1.0V
282.//=========================================================
283.void spd_chk_pro(void)
284.{ unsigned char temp,tt;
285.//if(xunhan_f)return;
286.temp=spd_buf;
287.if(spd_buf<59)spd_first_f=0;//ad59==>1.15v
288.if(temp>=65)temp-=65;//ad65==>1.27v
289.else temp=0;
290.if(spd_first_f)temp=0;//spd_first_f为上电有转把信号
291.if(temp>99)temp=100;