完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
嗨,我刚开始使用PIC12F1571,我想看看是否有人能帮忙把一个ADC值转换成两个字节,可以用来设置PWM占空比?我试图用RA2对ADC进行采样,并根据ADC上看到的电压输出两个PWM信号(RA5和RA4)。我正在寻找使PWM占空比与ADC上的电压匹配。我已经测试了PWM代码,一切都看起来不错,但在阅读数据表和添加代码,我不确定如何分割ADC变量来设置PWM占空比。
以上来自于百度翻译 以下为原文 Hi, I am new to working with PIC12F1571 and was looking to see if someone can help to convert an ADC value into two byte which can be used to set the PWM duty cycle? I am trying to sample an ADC with RA2 and output two PWM signals (RA5 and RA4) based on the voltage seen on the ADC. I was looking to make the PWM duty cycle match the voltage on the ADC. I have tested the PWM code and everything looks good, but after reading the datasheet and adding the code, I am not sure how to split up the ADC variable to set the PWM Duty Cycle. #include #pragma config FOSC = INTOSC // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN #pragma config WDTE = ON // Watchdog Timer Enable bit (WDT disabled) #pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled) #pragma config MCLRE = ON // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD) #pragma config CP = OFF // Code Protection bit (Program memory code protection is enable) #pragma config BOREN = ON // Brown Out Detect (BOR enabled) //PWM OFF->ON??ON->OFF????????(10%??????????????????) #define _XTAL_FREQ 8000000 //Frequency 8MHz /* * */ void main() { // APFCONbits.P2SEL = 0x01; // Set PWM2 to RA4 APFCONbits.P1SEL = 0x01; // Set PWM1 to RA5 //========================OSCILLATOR SET UP =================================== OSCCONbits.SCS1 = 1; // internal clock OSCCONbits.IRCF = 0b1110; // 8 MHz //===================PWM SET UP================================================ PWM2CLKCON = 0b00000001; // HFINTOSC clock, no prescaler PWM2PHH = 0x00; // phase PWM2PHL = 0x00; PWM2DCH = 0x01; PWM2DCL = 0xf4; PWM2PRH = 0x03; PWM2PRL = 0xE7; PWM2LDCONbits.LDA = 1; // Load OF, PH, DC, and PR buffers PWM2CON = 0b11000000; // module enable, output enable, normal polarity, standard mode PWM1CLKCON = 0b00000001; // HFINTOSC clock, no prescaler PWM1PHH = 0x00; // phase PWM1PHL = 0x00; PWM1DCH = 0x01; PWM1DCL = 0xf4; PWM1PRH = 0x03; PWM1PRL = 0xE7; PWM1LDCONbits.LDA = 1; // Load OF, PH, DC, and PR buffers PWM1CON = 0b11000000; // module enable, output enable, normal polarity, standard mode //===================== INTIZZLLLLL============================================= ANSELA = 0b00000100; // all pins to digital, except RA2 is analog. LATA = 0b00000000; // all port bits off int adc_result; //where we will store the ADC result in TRISAbits.TRISA2 = 1; //PORTAbits.RA2 = 1; LATAbits.LATA2 = 1; TRISAbits.TRISA4 = 0; PORTAbits.RA4 = 0; TRISAbits.TRISA5 = 0; PORTAbits.RA5 = 0; //--- set the ADC1 to the options selected in the User Interface // GO_nDONE stop; ADON enabled; CHS AN0; ADCON0 = 0x01; // ADFM left; ADPREF VDD; ADCS FOSC/2; ADCON1 = 0x00; // TRIGSEL no_auto_trigger; ADCON2 = 0x00; // ADRESL 0; ADRESL = 0x00; // ADRESH 0; ADRESH = 0x00; //-------------- // start ADC //-------------- ADCON0bits.CHS = 0x02; // Turn on the ADC module ADCON0bits.ADON = 1; // Acquisition time delay // Start the ADC conversion ADCON0bits.GO_nDONE = 1; //-------------- // Set PWM Duty Cycle roughly 0 //-------------- PWM2DCH = 0x00; // set duty cycle registers PWM2DCL = 0x00; // Duty = 0.0 PWM2LDCONbits.LDA = 1; // Load OF, PH, DC, and PR buffers PWM1DCH = 0x00; // set duty cycle registers PWM1DCL = 0x00; // Duty = 0.0 PWM1LDCONbits.LDA = 1; // Load OF, PH, DC, and PR buffers while(1) { //------------------- // Read ADC Pin //------------------- // Start the conversion ADCON0bits.GO_nDONE = 1; // Wait for the conversion to finish while (ADCON0bits.GO_nDONE); // Conversion finished, return the result adc_result = ((ADRESH << 8) + ADRESL); //------------------- // Set PWM based based on ADC value // ADC 0 == PWM Duty Cycle 0% // ADC 1024 == PWM Duty Cycle 100% //------------------- PWM2DCH = ; // set duty cycle registers PWM2DCL = ; // PWM2LDCONbits.LDA = 1; // Load OF, PH, DC, and PR buffers PWM1DCH = ; // set duty cycle registers PWM1DCL = ; // PWM1LDCONbits.LDA = 1; // Load OF, PH, DC, and PR buffers __delay_us(1000); } } |
|
相关推荐
3个回答
|
|
如果您将PWM周期设置为1024计数(即10位),那么您可以直接使用ADC值作为占空比计数。
以上来自于百度翻译 以下为原文 If you set your PWM period to be 1024 counts (i.e. 10 bits), then you can just use the ADC value directly as the duty cycle count. |
|
|
|
嗨,如果我将ADC值读为:int adc_result;//我们将把ADC结果存储在in adc_result=((ADRESH<<8)+ADRESL)的地方;我如何转换adc_result,以便填充PWM占空比?PWM2DCH=0x00;//设定占空比寄存器PWM2DCL=0x00;//占空比=0
以上来自于百度翻译 以下为原文 Hi, If I read the ADC value as: int adc_result; //where we will store the ADC result in adc_result = ((ADRESH << 8) + ADRESL); How can I convert the adc_result in order to populate the PWM duty cycle? PWM2DCH = 0x00; // set duty cycle registers PWM2DCL = 0x00; // Duty = 0.0 |
|
|
|
我的建议是:PWM2DCH=ADRESH;PWM2DCL=ADRESL;
以上来自于百度翻译 以下为原文 What I was suggesting was: PWM2DCH = ADRESH; PWM2DCL = ADRESL; |
|
|
|
只有小组成员才能发言,加入小组>>
5243 浏览 9 评论
2035 浏览 8 评论
1955 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3208 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2258 浏览 5 评论
778浏览 1评论
671浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
597浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
679浏览 0评论
577浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-24 11:46 , Processed in 1.340053 second(s), Total 83, Slave 66 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号