PIC16F877使用MPLAB用C语言在LED上显示ADC的结果

发布于 2025-01-15 19:39:00 字数 814 浏览 6 评论 0原文

我使用 PIC16F877,目的是选择通道 4 在 PortD LED 上显示模拟输入 AN4 值。近似值约为1V。我写了一个代码,但是无论我如何运行我的代码,GPIO 监视器都没有反应。 顺便问一下,我是否在 while(1){} 循环中编写 ReadADC1() ?我尝试过,但没有任何帮助。谢谢。

#include <xc.h>
#define LEDs    PORTD   
#include "prologue.c"
unsigned char ReadADC1(void) {
    ADCON0 |= 0b00000010;
    while ( (ADCON0 & 0b00000010) );
    return ADRESH;
}

main ()
{
//  declare variables if any required
    
    TRISA= 0B00100000;   
    ANSEL=0B00010000;
    
    ADCON0 = 0b11010001;
    ADCON1 = 0b10000000; 
    
    LEDs=ReadADC1();

    //*** your code for initialisation if required

    //*** end of your initialisation

    //***  your code for the superloop
    while (1) {

    }
    
    //*** end of the superloop
} 

GPIO 引脚监视器没有反应。我多次重启IDE。

I used PIC16F877 and my purpose is to choose CHANNEL 4 to display the analogue input AN4 value on PortD leds. The approximate value is about 1V. I wrote a code and however, no matter how I ran my code, there're not reaction with the GPIO monitor.
By the way, do I write ReadADC1() in the while(1){} loop? I tried that, but there's no help. Thanks.

#include <xc.h>
#define LEDs    PORTD   
#include "prologue.c"
unsigned char ReadADC1(void) {
    ADCON0 |= 0b00000010;
    while ( (ADCON0 & 0b00000010) );
    return ADRESH;
}

main ()
{
//  declare variables if any required
    
    TRISA= 0B00100000;   
    ANSEL=0B00010000;
    
    ADCON0 = 0b11010001;
    ADCON1 = 0b10000000; 
    
    LEDs=ReadADC1();

    //*** your code for initialisation if required

    //*** end of your initialisation

    //***  your code for the superloop
    while (1) {

    }
    
    //*** end of the superloop
} 

There's no reaction with the GPIO pins monitor. I restarted the IDE many times.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红墙和绿瓦 2025-01-22 19:39:00

默认情况下,所有端口都配置为输入。如果您想使用端口作为输出,则必须更改配置:

TRISD = 0x00;

另一个问题:
该控制器中没有 ANSEL 寄存器,您必须使用 ADCON1 寄存器进行选择(数字或模拟输入)。

By default all ports are configured as an input. If you want want use a port as an output you had to change the configuration:

TRISD = 0x00;

Another issue:
There is no ANSEL register in this controller, you had to do the selection (digital or analog input) with ADCON1register.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文