2013年10月22日 星期二

VR1,VR2調整PWM 波寬後 顯示頻率至LCD


  1. #include <p18f4520.h>
  2. #include <delays.h>
  3. #include <adc.h>
  4. #include <timers.h>
  5. #include <pwm.h>
  6. #include    "evm_lcd.h"
  7. #include    <stdio.h>    //use sprintf
  8. void delay_ms(long a);
  9. #define OSC_CLOCK 10
  10. void LCD1(unsigned int); 
  11. void LCD2(unsigned int); 
  12. void ADC0(void);
  13. void ADC1(void);
  14. char buf1[3],buf2[3],buf3[5];
  15. unsigned int b,c;  


  16. #pragma config OSC=HS, BOREN=OFF, BORV = 2, PWRT=ON, WDT=OFF, LVP=OFF 

  17. void main () 
  18. OpenLCD();
  19.     WriteCmdLCD(0x01);
  20.     LCD_Set_Cursor(0,0);     
  21. TRISCbits.TRISC2 = 0;                          // 設定CCP1為輸出,啟動蜂鳴器
  22. //OpenTimer2(TIMER_INT_OFF&T2_PS_1_4&T2_POST_1_1); //開啟TIMER2
  23. T2CON=0x05;                 // 0000 0101
  24. PIE1bits.TMR2IE=0;          //disable TIMER2 interrupt 
  25. while(1) 
  26. {
  27. ADC1();                   //pwm period  
  28. ADC0();                   //pwm duty cycle
  29. PIR1bits.TMR2IF=0;        //clear TIMER2 interrupt flag  
  30.    }
  31. }

  32. void LCD1(unsigned int v)
  33. {   
  34.      LCD_Set_Cursor( 0,0 );                        
  35.      sprintf(buf1,"Duty Cycle=%u",v );      
  36.      putsLCD(buf1);                                          
  37.      delay_ms(1000);                      
  38.      WriteCmdLCD( 0x01 );                

  39. void LCD2(unsigned int k)
  40. {  
  41. float zz,yy;
  42. unsigned int zz1,zz2,zz3;
  43. yy=k;   
  44. zz=1.0/((yy+1)*4.0*(1.0/10000000.0)*4.0); //浮點運算
  45. zz2=zz/1.0;                               //取整數
  46. zz1=zz/1000.0;                           //取整數 
  47. zz3=(zz2-zz1*1000) ;                  //取小數點後三位
  48. LCD_Set_Cursor( 1,0 );                         
  49.      sprintf(buf2,"Period=%u",k);      
  50.      putsLCD(buf2); 
  51.      delay_ms(1000); 
  52.      WriteCmdLCD( 0x01 );              //清除LCD顯示資料      
  53.      LCD_Set_Cursor( 0,0 );   
  54.      sprintf(buf3,"Freq=%u.%03uKHZ",zz1,zz3);
  55.      putsLCD(buf3);                      
  56.      delay_ms(1000);                      
  57.      WriteCmdLCD( 0x01 );                

  58. void delay_ms(long a)
  59. {
  60.   long i;
  61.   int us2TCY;
  62.   us2TCY=(10*OSC_CLOCK)>>2;                     //10*10/4=25
  63.   for(i=0;i<a;i++)
  64.   Delay100TCYx(us2TCY);                         //25*100*(4/10^7)=1ms
  65. }  

  66. void ADC0()
  67. {
  68. ADCON0=0x01; // 選擇AN0通道轉換,開啟ADC模組
  69.     ADCON1=0x0E; // 使用VDD,VSS為參考電壓,設定AN0為類比輸入
  70.     ADCON2=0xBA; // 結果向右靠齊並設定轉換時間為Fosc/32,採樣時間為20TAD
  71.     Delay10TCYx(5);  
  72.     ConvertADC() ;  
  73.     while(BusyADC());   
  74. b=(ReadADC()>>2); 
  75. if (b>c)  
  76.    b=c;        //limit duty cycle<=period
  77.     SetDCPWM1(b);          //set PWM duty cycle
  78.     LCD1(b);
  79.         
  80. }

  81. void ADC1()
  82. {
  83. ADCON0=0x05; // 選擇AN1通道轉換,開啟ADC模組
  84.     ADCON1=0x0D; // 使用VDD,VSS為參考電壓,設定AN1為類比輸入
  85.     ADCON2=0xBA; // 結果向右靠齊並設定轉換時間為Fosc/32,採樣時間為20TAD
  86.     Delay10TCYx(5);  
  87.     ConvertADC() ;     
  88.     while(BusyADC());  
  89.     c=(ReadADC()>>2);
  90. OpenPWM1(c);      // 設定CCP1為PWM週期功能 PWM uses only Timer2=>period
  91.     LCD2(c);
  92. }



沒有留言:

張貼留言