完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
嗨,黑帮,有人能看看我的简短代码,并告诉我是否我已经设置了我的AdCON/安塞尔寄存器正确。我已经有一段时间了,似乎不太清楚我出了什么问题。我期待在两个LED的输出事件发生时,但我没有得到输出。我想知道逻辑是错误的还是我的设置是错误的。感谢任何输入!
以上来自于百度翻译 以下为原文 Hi gang, Can someone look over my brief code and give me an idea of whether I have setup my ADCON/ANSEL registers properly. I have been at it for a bit and can't quite seem to figure out where I am going wrong. I am expecting output on two LED's when events occur, but I am getting no output. I am wondering if the logic is incorrect or if my setup is wrong. Thanks in advance for any input! // CONFIG #pragma config FOSC = INTRCIO // Oscillator Selection bits (INTOSC oscillator: I/O function on GP4/OSC2/CLKOUT pin, I/O function on GP5/OSC1/CLKIN) #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled) #pragma config PWRTE = OFF // Power-Up Timer Enable bit (PWRT disabled) #pragma config MCLRE = ON // GP3/MCLR pin function select (GP3/MCLR pin function is MCLR) #pragma config BOREN = OFF // Brown-out Detect Enable bit (BOD disabled) #pragma config CP = OFF // Code Protection bit (Program Memory code protection is disabled) #pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled) #include #include #include #define _XTAL_FREQ 4000000 // Setting internal clock to 4MHz int main(void) { // Variable Declaration int AN3_AD_IN_110 = 0; // variable to hold value of A/D input from U2 Allegro chip, 110v line. int AN2_AD_IN_220 = 0; // " from U3 Allegro chip, 220v line. // This signal is baseline of 2.5VDC with a 0-2.5VAC signal riding on top. int max = 511, min = 511, max1 = 511, min1 = 511; // variables for min/max comparisons on both channels 110/220 int temp_value = 0, temp_value1= 0, peak = 0, temp_peak = 0; // Hold temp values to compare ADC against int count = 0; //ADC Setup ADCON0bits.ADFM = 0; // Left justified A/D results ADCON0bits.VCFG = 0; // Voltage reference set to Vdd ADCON0bits.CHS0 = 1; // Channel select 0 register set to 1 to select AN1 ADCON0bits.ADON = 1; // Turn A/D converter on TRISIO = 0b000010; // AN1 is input :old comments //0b001100; // Only AN2 and AN3/MCLR are inputs all else outputs. GPIO = 0b000000; // Turn off all outputs initially //Analog Select Register Setup ANSELbits.ADCS = 0b011; // Clock derived from internal osc - max of 500kHz - problem with 4MHz xtal freg?? ANSELbits.ANS1 = 1; // Sets AN1 as analog input (page 46 of pic675 manual states this and trisio need to be set) // Start A/D conversion process ADCON0bits.GO = 1; while (ADCON0bits.nDONE) { AN3_AD_IN_110 = ADRESL + (ADRESH << 8); // 1024 bit resolution 5v/1023 = 4.88mV per bit if (AN3_AD_IN_110 >= max) // check for 511 (1/2 Vcc) or greater { max = AN3_AD_IN_110; // set variable max to 511 or greater } if (AN3_AD_IN_110 <= min) // check variable min for 511 or less { min = AN3_AD_IN_110; // set var. min to 511 or less } temp_peak = max - min; // temporary peak value established and used to compare later if (count < 128) // set counter to prevent infinite loop { count = count + 1; // incrementing counter } if (count == 128) { _delay(1); GPIO4 != GPIO4; _delay(1); // write a =! GPIO statement to turn led GP4 on and off every 128 cycles } if ((temp_peak >= 144) && (count == 128) // control statement - if peak value is above threshold and we iterated 129 times && (max >= 584) && (min <= 438)) // and if 0.354V above and below Vcc - (110v threshold) then proceed { _delay(2); // 2 second delay before turning on //GPIO = 0b000001; // set GP0 high to turn on relay GPIO = 0b100000; //REMOVE ONLY FOR TESTING // this is to check code on dev. board - GP5 outermost LED to corner should light //ADCON0bits.GO_DONE = 1; // do A/D measurement count = 0; // reset count variable to zero for next go around } if ((temp_peak <= 130) && (max <= 560) && (min >= 490)) { _delay(5); // statement checks to see if current has dropped below threshold GPIO = 0b000000; //waits 5 seconds then shuts off GP5 } } } |
|
相关推荐
1个回答
|
|
这是错误的:你应该等待,什么都不做,直到转换完成……[代码]
以上来自于百度翻译 以下为原文 This is wrong: while (ADCON0bits.nDONE) { You should wait and do nothing, until the conversion is completed...[/code] |
|
|
|
只有小组成员才能发言,加入小组>>
5321 浏览 9 评论
2076 浏览 8 评论
1982 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3254 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2293 浏览 5 评论
835浏览 1评论
730浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
674浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
728浏览 0评论
622浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-2-2 19:08 , Processed in 1.042788 second(s), Total 44, Slave 38 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号