完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
电子发烧友论坛|
我有一个测量土壤湿度的传感器,其中模拟值的范围从1.75和lt;v & lt;2.1(当在MCU引脚测量)。我想从AN4读取值,这是在PIN RB2上,现在我只使用PrtTF发送UART1上的值。在这一点上,我看到的值范围从2920到~ 2990,只是随便蹦蹦跳跳,独立于我对湿度传感器所做的事情。我觉得我错过了一些东西,因为ADC应该是相当直接的。这里是我的代码,如果你想对我的代码中的一般内容发表评论,请做!
以上来自于百度翻译 以下为原文 I have a sensor for measuring moisture in soil, where the analog value ranges from 1.75 I want to read the value from AN4, which is on pin RB2, and for now I just send the value over UART1 using printf. At this point I'm seeing values ranging from ~2920 to ~2990, just bouncing around randomly, independent of what I do to the moisture sensor. I feel like I'm missing something here as ADC should be fairly straight forward to do. So here is my code. Also, if you want to comment on general things in my code, please do! void adc_init(){ AD1CON1 = 0; AD1CON2 = 0; AD1CON3 = 0; AD1CON4 = 0; AD1CON5 = 0; AD1CHS = 0; AD1CSSL = 0; // Pin configurations: TRISBbits.TRISB2 = 1; // Set pin RB2 as input ANSBbits.ANSB2 = 1; // Enable ADC on pin RB2 // AD1CON1: AD1CON1bits.ADSIDL = 1; // Disable module when device is idle AD1CON1bits.MODE12 = 1; // 12-bit mode AD1CON1bits.FORM = 0b00; // Output form is absolute decimal, unsigned, right justified AD1CON1bits.SSRC = 0b0111; // Auto convert after sampling AD1CON1bits.ASAM = 0; // Sampling begins when SAMP bit is set // AD1CON2: AD1CON2 = 0x0000; // VDD and VSS as ref, no scanning (default values) // AD1CON3: AD1CON3bits.SAMC = 0b11111; // Sample 31 T_AD AD1CON3bits.ADCS = 0b00000011; // T_AD = 6 T_CY AD1CHSbits.CH0NA = 0b00100; // Sample AN4 on chan A // Enable module: AD1CON1bits.ADON = 1; } void testADC(){ util_pinConfigPCB(); uart_init9600(1); adc_init(); while(1){ AD1CON1bits.SAMP = 1; while(!AD1CON1bits.DONE); AD1CON1bits.SAMP = 0; printf(" %d", ADC1BUF0); __delay_ms(1000); } } int main(void){ testADC(); while(1){} return 0; } void util_pinConfigPCB(){ // Disable ADCs: ANSA = 0x0000; ANSB = 0x0000; __builtin_write_OSCCONL(OSCCON & 0xDF); // to clear IOLOCK RPOR4bits.RP8R = 7; // SPI1 DO --> Pin RP8 RPOR4bits.RP9R = 8; // SPI1 CLK --> Pin RP9 RPOR5bits.RP10R = 3; // UART 1 TX --> Pin RP10 RPOR7bits.RP14R = 5; // UART 2 TX --> Pin RP14 RPINR19bits.U2RXR = 15; // UART 2 RX --> Pin RP15 __builtin_write_OSCCONL(OSCCON | 0x40); // to set IOLOCK } void uart_init9600(uint8_t channel){ if(channel == 1){ // Configure UART 1 module: U1MODEbits.STSEL = 0; // 1 stop bit U1MODEbits.PDSEL = 0; // 8-bit data, no parity U1BRG = 25; // FCY / (16*baudrate) - 1 U1MODEbits.UARTEN = 1; // Enable UART 1 U1STAbits.UTXEN = 1; // Enable UART transmitter } else if(channel == 2){ U2MODEbits.STSEL = 0; U2MODEbits.PDSEL = 0; U2BRG = 25; U2MODEbits.UARTEN = 1; U2STAbits.UTXEN = 1; U2STAbits.URXEN = 1; // Enable UART receiver } } |
|
相关推荐
4个回答
|
|
|
请注意,对于一个3.3伏的工作电压,一个范围,比如说,从4096的70个数代表56毫伏的噪音,这就让我问……传感器是如何连接到你的板上的(也就是说,导线有多长)?描述连接。你有任何接近MCU的东西,可以减少连接所带来的噪音吗?如何验证ADC测量?通过串口打印这些值?或者什么?这是我的建议…首先:断开传感器。应用固定的,已知的电压到您的引脚。最好的情况是一个电池非常短的线索,你的MCU。尽可能精确地测量电池电压和VDD,并计算预期的ADC读数。其次:告诉我们每秒读取读数时会发生什么。我从来没有(是,从来没有)用“一劳永逸”的策略来做这样的测试。我的经验是,MCU内部的东西需要一些时间来“安定下来”,然后才开始认真的做生意。也许这是重要的,也许不是。我建立了我的PIC24F好奇心板(一个带有PIC24FJ128GA204)的电位计上有2伏。我测量VDD为3.32伏特。我每秒测量一次,打印每个值,以及最小值和最大值。几分钟后,我发现最小读数是2456(1.9907)。最大读数为2487(2.0158)。31个计数的范围代表大约25毫伏的噪音。注意:该板有一个10K罐,在锅的刮水器和MC10的10之间串联有一个330欧姆的电阻。电路板有一个电容器,从锅雨刮器到地面。这不是人口稠密,但是,我大概可以安装一个电容器来减少噪音。我没有尝试过。底线:我看不出代码有什么“根本错误”。重要的是在附加外部电路像传感器之前,在最有利的条件下评估ADC测量。如果传感器在物理上与电路板分离,你一定要考虑从引线中拾取的噪声。
以上来自于百度翻译 以下为原文 Note that for an operating voltage of 3.3 Volts, a range of, say, 70 counts out of 4096 represents something like 56 millivolts of noise. Which leads me to ask... How is the sensor connected to your board (i.e. how long are the leads)? Describe the connection. Do you have anything close to the MCU that might reduce noise picked up by the connection? How are you verifying the ADC measurement? By printing the values through the serial port? Or what? Here's my suggestion... First of all: Disconnect the sensor. Apply a fixed, known voltage to your pin. Best case would be a battery with very short leads to your MCU. Measure the battery voltage and the Vdd as accurately as possible, and calculate the expected ADC reading. Secondly: Tell us what happens when you take readings every second. I never (yes, never) do tests like this with "one-and-done" strategy. My experience is that things inside the MCU need some time to "settle down" before getting down to serious business. Maybe this is significant; maybe not. I set up my PIC24F Curiosity board (the one with a PIC24FJ128GA204) with 2.0 Volts on the potentiometer. I measured Vdd to be 3.32 volts. I took measurements every second and printed each value, along with minimum and maximum values. After a few minutes, I found Minimum reading is 2456 (1.9907... Volts) Maximum reading is 2487 (2.0158... Volts) The range of 31 counts represents about 25 millivolts of noise. Notes:
Bottom line: I don't see anything fundamentally "wrong" with your code. The important thing is to evaluate the ADC measurement under most favorable conditions before attaching external circuitry like a sensor. If the sensor is physically separated from the board, you will certainly have to consider noise picked up from the leads. Regards, Dave |
|
|
|
|
|
戴夫,谢谢你的回答!传感器是外部的,大约30厘米的线索。我真的没有任何东西可以把固定电压施加到引脚上,但我可以在实验室里再尝试一次。所以我测试的方式是这样的:用多表测量VDD,我看到VDD=3.29伏。让传感器干,用多表测量引脚RB2上的电压,我看到了。VYRB2SORID=2.22V.检查UART上的输出,我看到像2950 +/- 50的值。(2950/4064)*3.29 V=2.4V(让我们称之为AdS乾y)。将传感器淹没并用多表测量引脚RB2上的电压,我看到VYRB2YWATE=1.88 V。检查UART上的输出,我看到与步骤3几乎相同的值。当我用一个多仪表测量传感器时,我就看不出有什么不同。如果我完全断开传感器,这个值也不会真正改变,这让我相信这里的东西完全错了。我会尝试同样的代码。一个芯片坐在一个面包板上,以及一个固定的电压。这个东西运行在一个非常贫民窟PCB我做,所以它可能被噪音污染。编辑:添加一些额外的问题:ADC通道A和B通道之间的区别是什么?通道A为端口A和引脚B端口B引脚?不知何故,我需要配置AVYDD和AVSS?我使用的包没有任何引脚,所以我假设它与VYDD和VYSS相同。
以上来自于百度翻译 以下为原文 Thanks for a descriptive answer Dave! The sensor is external, approx 30 cm leads. I don't really have anything to apply a fixed voltage to the pin with right here, but I could always try that at the lab another day. So the way I'm testing is like this:
The value doesn't really change when I completely disconnect the sensor either, which makes me believe that something is completely wrong here. I'll try the same code on a chip sitting on a breadboard, as well as with a fixed voltage. This thing runs on a very ghetto PCB i made, so it's probably contaminated with noise. EDIT: Adding some additional questions:
|
|
|
|
|
|
我道歉。因为我使用了一个不同的ADC输入PIN,我的代码已经到位,我没有注意到您的频道选择的问题。我看到的东西与AD1CHS有任何关系,并且设置了负的ADC输入为AN4,正输入为AN0。(参考数据表中的寄存器246,DS300 10038 C),您需要的是将负输入设置为VREF/AVSS,并将正输入设置为AN4。(对于你的芯片,AVss是VSS),就是:试试看。问候,戴夫。
以上来自于百度翻译 以下为原文 I apologize. Since I was using a different ADC input pin, and my code for that was already in place, I didn't notice the problem with your channel select. The things I see having anything to do with AD1CHS are AD1CHS = 0; and AD1CHSbits.CH0NA = 0b00100; // Sample AN4 on chan A This sets up the negative ADC input to be AN4 and the positive input to be AN0. (Refer to Register 24-6 in the Data Sheet, DS30010038C.) What you need is to have negative input set to Vref/AVss and the positive input set to AN4. (For your chip, AVss is Vss) That is: AD1CHSbits.CH0SA = 0b00100; // Sample ADC on channel AN4 Try it that way. Regards, Dave |
|
|
|
|
|
所有的工作和噪音都是低的(+/-几个单位),所以我的PCB似乎毕竟不是贫民窟。我不能相信我错过了,通过这些值和数据表几十次试图修复它。无论如何,尼古莱。
以上来自于百度翻译 以下为原文 That totally works and noise is low (+/- a few units) so my PCB doesn't seem to be that ghetto after all. Can't believe I missed that, been through those values and the datasheet tens of times trying to fix that. Thanks anyways, - Nikolai |
|
|
|
|
只有小组成员才能发言,加入小组>>
MPLAB X IDE V6.25版本怎么对bootloader和应用程序进行烧录
475 浏览 0 评论
5795 浏览 9 评论
2334 浏览 8 评论
2224 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3530 浏览 3 评论
1126浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
1098浏览 1评论
我是Microchip 的代理商,有PIC16F1829T-I/SS 技术问题可以咨询我,微信:A-chip-Ti
873浏览 1评论
MPLAB X IDE V6.25版本怎么对bootloader和应用程序进行烧录
475浏览 0评论
/9
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-12-3 09:59 , Processed in 0.697987 second(s), Total 78, Slave 61 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191

淘帖
1281