📄 ad9835_v2.lst
字号:
203 1 return TempNum;
204 1 }
205
206 //--------------------------------------
207 uchar Key_scan(void)
208 {uchar sccode,recode;
209 1 P1=0xf0;
210 1 if((P1&0xf0)!=0xf0) /* 若有健按下*/
211 1 {del_ms(12); /*延时去抖动*/
212 2 if((P1&0xf0)!=0xf0)
213 2 {sccode=0xfe; /*逐行扫描初值*/
214 3 while((sccode&0x10)!=0)
215 3 {P1=sccode; /*输出行扫描码*/
216 4 if((P1&0xf0)!=0xf0) /*本行有键按下*/
217 4 {recode=(P1&0xf0)|0x0f;
218 5 return((~sccode)+(~recode)); /*返回特征字节码*/
219 5 }
220 4 else
221 4 sccode=(sccode<<1)|0x01; /*行扫描码左移一位*/
222 4 }
223 3 }
224 2 }
225 1 return No_key; /*无键按下返回0*/
226 1 }
227
228 //-------------------------------
229 void Key_num(unsigned char n)//输入数字并显示
230 {
231 1 if(step==0){Long_bee();}
232 1 else {
233 2 buf[step]=n;n=n|0x30;
234 2 LCD_set_xy(cur,0); //地址
235 2 LCD_write_char(0,n);
236 2 step--;cur++;}
237 1 key=No_key;
238 1 }
239
240 //---------------------------
241 void bee()//低有效蜂鸣
C51 COMPILER V7.50 AD9835_V2 04/15/2008 17:47:52 PAGE 5
242 {
243 1 P2=P2&0xFE;//P2.0=0
244 1 del_ms(100);
245 1 P2=P2|0x01;//P2.0=1
246 1 }
247 //---------------------------
248 void Long_bee(void)//长蜂鸣
249 {
250 1 BEP=0;//PB0=1
251 1 del_ms(400);
252 1 BEP=1;//PB0=0
253 1 }
254 //----------------------------------------------
255 void delay_nus(uint n) //N us延时函数
256 {
257 1 uint i=0;
258 1 for (i=0;i<n;i++)
259 1 _nop_();
260 1 }
261
262 //--------------------------------------
263 void del_ms(uint n)//n毫秒延时
264 { uchar j;
265 1 while(n--)
266 1 {for(j=0;j<125;j++);}
267 1 }
268
269 //---------------------------
270 void disp(void)//显示
271 {
272 1 uchar disbuf[8];
273 1 uchar i;
274 1 for(i=0;i<=7;i++)
275 1 {disbuf[i]=freq[i]|0x30;}
276 1 if(disbuf[7]==0x30)
277 1 {disbuf[7]=0x20;
278 2 if(disbuf[6]==0x30)
279 2 {disbuf[6]=0x20;
280 3 if(disbuf[5]==0x30)
281 3 {disbuf[5]=0x20;
282 4 if(disbuf[4]==0x30)
283 4 {disbuf[4]=0x20;
284 5 if(disbuf[3]==0x30)
285 5 {disbuf[3]=0x20;
286 6 if(disbuf[2]==0x30)
287 6 {disbuf[2]=0x20;
288 7 }
289 6 }
290 5 }
291 4 }
292 3 }
293 2 }
294 1 LCD_set_xy(0,1);//地址
295 1 LCD_write_char(0,disbuf[7]);
296 1 LCD_write_char(0,disbuf[6]);
297 1 LCD_write_char(0,disbuf[5]);
298 1 LCD_write_char(0,disbuf[4]);
299 1 LCD_write_char(0,disbuf[3]);
300 1 LCD_write_char(0,disbuf[2]);
301 1 LCD_write_char(0,disbuf[1]);
302 1 LCD_write_char(0,0x2e);//小数点
303 1 LCD_write_char(0,disbuf[0]);
C51 COMPILER V7.50 AD9835_V2 04/15/2008 17:47:52 PAGE 6
304 1 LCD_write_str(9,1," Hz TJX");
305 1
306 1 }
307
308 //--------------------------------
309 void AD9835_byte(uchar a)//发一个字节
310 {
311 1 uchar n=0;
312 1 for(n=0;n<8;n++)
313 1 {
314 2 if(((a<<n)&0x80)==0){DAT=0;} //DATA=0 MSB is output first
315 2 else{DAT=1;} //DATA=1
316 2
317 2 CLK=0; //SCLK=0
318 2 _nop_();
319 2 _nop_();
320 2 CLK=1; //SCLK=1
321 2
322 2 }
323 1 }
324 //---------------------------
325 void AD9835_word(uchar *p)//发一个字两个字节
326 {
327 1
328 1 SYC=0;//FSYNC=0
329 1
330 1 AD9835_byte(*p);
331 1 p++;
332 1 AD9835_byte(*p);
333 1
334 1 SYC=1;//FSYNC=1
335 1 }
336
337 //--------------------------------
338 void AD9835_init(void)//passed!
339 {
340 1 uchar dds[2]={0xF8,0X00};
341 1
342 1 AD9835_word(dds);
343 1
344 1 dds[0]=0x33;
345 1 dds[1]=F_word[0];
346 1 AD9835_word(dds);
347 1 dds[0]=0x22;
348 1 dds[1]=F_word[1];
349 1 AD9835_word(dds);
350 1 dds[0]=0x31;
351 1 dds[1]=F_word[2];
352 1 AD9835_word(dds);
353 1 dds[0]=0x20;
354 1 dds[1]=F_word[3];
355 1 AD9835_word(dds);
356 1
357 1 dds[0]=0xC0;
358 1 dds[1]=0x00;
359 1 AD9835_word(dds);
360 1
361 1 }
362 //-------------------------------------
363 //Fc=50MHz K=85.8993459 passed!!
364 //计算出频率寄存器的四个字节值
365 void AD9835_calc(void)
C51 COMPILER V7.50 AD9835_V2 04/15/2008 17:47:52 PAGE 7
366 {
367 1 unsigned long z=0;
368 1 float x;
369 1 x=(freq[0]*0.1+freq[1]*1+freq[2]*10+freq[3]*100+freq[4]*1000+freq[5]*10000
370 1 +freq[6]*100000+freq[7]*1000000)*85.8993459;
371 1 z=x;
372 1 F_word[3]=(char)z;
373 1 F_word[2]=(char)(z>>8);
374 1 F_word[1]=(char)(z>>16);
375 1 F_word[0]=(char)(z>>24);
376 1 }
377
378 //------------------------------------
379 int main(void)
380 {
381 1 uchar smalkey=0,flag=0;//位置
382 1 uchar i,j,k1=No_key,k2=No_key;
383 1 uchar dot=0xff;//小数点cursor
384 1 uchar intc=0;//整数位,有效数位
385 1
386 1 del_ms(100);
387 1 LCD_init();
388 1 LCD_write_str(0,0,"InputF= ");//显示第一行
389 1
390 1 del_ms(100);
391 1 disp(); //第二行显示默认值800Hz
392 1 AD9835_calc();
393 1 AD9835_init(); //输出800Hz正弦波
394 1
395 1 while(1)
396 1 {
397 2 del_ms(200);
398 2 k2=Key_scan();
399 2 if (k2!=No_key)
400 2 {key=k2;k2=No_key;bee();
401 3 key=Key_table(key);
402 3 switch(key)
403 3 {case 1:
404 4 {Key_num(1);}break;
405 4 case 2:
406 4 {Key_num(2);}break;
407 4 case 3:
408 4 {Key_num(3);}break;
409 4 case 4:
410 4 {Key_num(4);}break;
411 4 case 5:
412 4 {Key_num(5);}break;
413 4 case 6:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -