|
PIC16F18323 ADC to PWM
|
|
新會員
|
各位前輩好目前使用PIC16F18323 ADC功能以及CCP功能來控制PWM DUTY
目前遇到困難在我的ADC接受外部是用可變電阻調變0~100K
但是在式波器上PWM顯示DUTY的部分卻不是線性調變
下面這邊是我的PWM輸出,而adc 以及CCPREG兩個都是選擇從右邊數 而我的RP2沒有帶入255,是用其他值去計算出來的目前已計算過為79
系統Osc 32Mhz,pwm freq :100khz
PR2 = (_XTAL_FREQ/(PWM_freq*4*1)) - 1;
void duty(unsigned int adcvaule) { if(adcvaule<1023) //total 10bit data { //adc 10bit conver back*duty formula duty cycle //adc converter Vin=ADdata*(Vref+ - Vref-)/1023(2^10 bits) adcvaule=(((float)adcvaule)/1023)*(4*(PR2+1)); //duty cycle * 4*pr2+1 // adcvaule = ((float)adcvaule/1023)*(_XTAL_FREQ/(PWM_freq*1)); //CCP1 select FMT0 H 2bit L 8bit // duty reg is CCPR1H CCPR1L //define CCPW is total CPPR1H<1:0>+CCPR1L<7:0> sum of the 10 bits reg // CCPR1H<1:0> is using CCPW <9:8> bits // CCPR1L<7:0> is using CCPW <7:0> bits CCPR1H =(adcvaule>>8);// put duty first data still at 0 & 1 need to put at 8&9 CCPR1L = (adcvaule & ADRESL); //CCPR1L has 8 bit use or left it and leave the last over 2bit } }
發表於: 2020/5/13 20:29
|
|