- #include <p18f4520.h>
- #include <delays.h>
- #include <adc.h>
- #include <timers.h>
- #include <pwm.h>
- #include "evm_lcd.h"
- #include <stdio.h> //use sprintf
- void delay_ms(long a);
- #define OSC_CLOCK 10
- void LCD1(unsigned int);
- void LCD2(unsigned int);
- void ADC0(void);
- void ADC1(void);
- char buf1[3],buf2[3],buf3[5];
- unsigned int b,c;
- #pragma config OSC=HS, BOREN=OFF, BORV = 2, PWRT=ON, WDT=OFF, LVP=OFF
- void main ()
- {
- OpenLCD();
- WriteCmdLCD(0x01);
- LCD_Set_Cursor(0,0);
- TRISCbits.TRISC2 = 0; // 設定CCP1為輸出,啟動蜂鳴器
- //OpenTimer2(TIMER_INT_OFF&T2_PS_1_4&T2_POST_1_1); //開啟TIMER2
- T2CON=0x05; // 0000 0101
- PIE1bits.TMR2IE=0; //disable TIMER2 interrupt
- while(1)
- {
- ADC1(); //pwm period
- ADC0(); //pwm duty cycle
- PIR1bits.TMR2IF=0; //clear TIMER2 interrupt flag
- }
- }
- void LCD1(unsigned int v)
- {
- LCD_Set_Cursor( 0,0 );
- sprintf(buf1,"Duty Cycle=%u",v );
- putsLCD(buf1);
- delay_ms(1000);
- WriteCmdLCD( 0x01 );
- }
- void LCD2(unsigned int k)
- {
- float zz,yy;
- unsigned int zz1,zz2,zz3;
- yy=k;
- zz=1.0/((yy+1)*4.0*(1.0/10000000.0)*4.0); //浮點運算
- zz2=zz/1.0; //取整數
- zz1=zz/1000.0; //取整數
- zz3=(zz2-zz1*1000) ; //取小數點後三位
- LCD_Set_Cursor( 1,0 );
- sprintf(buf2,"Period=%u",k);
- putsLCD(buf2);
- delay_ms(1000);
- WriteCmdLCD( 0x01 ); //清除LCD顯示資料
- LCD_Set_Cursor( 0,0 );
- sprintf(buf3,"Freq=%u.%03uKHZ",zz1,zz3);
- putsLCD(buf3);
- delay_ms(1000);
- WriteCmdLCD( 0x01 );
- }
- void delay_ms(long a)
- {
- long i;
- int us2TCY;
- us2TCY=(10*OSC_CLOCK)>>2; //10*10/4=25
- for(i=0;i<a;i++)
- Delay100TCYx(us2TCY); //25*100*(4/10^7)=1ms
- }
- void ADC0()
- {
- ADCON0=0x01; // 選擇AN0通道轉換,開啟ADC模組
- ADCON1=0x0E; // 使用VDD,VSS為參考電壓,設定AN0為類比輸入
- ADCON2=0xBA; // 結果向右靠齊並設定轉換時間為Fosc/32,採樣時間為20TAD
- Delay10TCYx(5);
- ConvertADC() ;
- while(BusyADC());
- b=(ReadADC()>>2);
- if (b>c)
- b=c; //limit duty cycle<=period
- SetDCPWM1(b); //set PWM duty cycle
- LCD1(b);
- }
- void ADC1()
- {
- ADCON0=0x05; // 選擇AN1通道轉換,開啟ADC模組
- ADCON1=0x0D; // 使用VDD,VSS為參考電壓,設定AN1為類比輸入
- ADCON2=0xBA; // 結果向右靠齊並設定轉換時間為Fosc/32,採樣時間為20TAD
- Delay10TCYx(5);
- ConvertADC() ;
- while(BusyADC());
- c=(ReadADC()>>2);
- OpenPWM1(c); // 設定CCP1為PWM週期功能 PWM uses only Timer2=>period
- LCD2(c);
- }
2013年10月22日 星期二
VR1,VR2調整PWM 波寬後 顯示頻率至LCD
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言