【问题标题】:ADC value in pic 16f1827图 16f1827 中的 ADC 值
【发布时间】:2015-09-10 17:50:17
【问题描述】:

我想检查 adc 的结果,当满足这个条件时,我希望我的微控制器在 RB5 引脚上输出一个数字信号。 请检查代码。是否正确。? Becoz 当我运行模拟时,它在 proteus 中无法正常工作。 这是代码。

#define USE_OR_MASKS
#include <xc.h>
#include <stdlib.h>



// CONFIG1
#pragma config FOSC = INTOSC    
// Oscillator Selection (INTOSC oscillator:        I/O function on CLKIN pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = OFF      // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#pragma config CP = OFF         // Flash Program Memory Code Protection    (Program memory code protection is disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = OFF       // Internal/External Switchover (Internal/External Switchover mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)

// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = ON         // Low-Voltage Programming Enable (Low-voltage programming enabled)

void InitSfr(void);
int ReadADC(unsigned char ch);
void InitADC(void);
void Delay_ms(unsigned long time);




 //************ Global Variables ****************************
 int value;
 unsigned int result;

//************ I2C MASTER ****************************
void main(void)
{

InitSfr();
InitADC();

LATBbits.LATB3=0;
LATBbits.LATB5=0;
//checking for the ADC result
do
{
//value=ReadADC(0);
//LATB=value;
//unsigned long int max = (value/1023)*5;
  ADCON0bits.CHS0 = 0;
  ADCON0bits.CHS1 = 0;
  ADCON0bits.CHS2 = 0;
  ADCON0bits.CHS3 = 0;
  ADCON0bits.CHS4 = 0;
  ADCON0bits.ADON = 1;  // enable A/D converter
//  ADCON0 = 0b00000001 + (ch<<2);  // set channel to read
    Delay_ms(5);
    GO_nDONE =1;        // 16F1827 start conversion
    while(GO_nDONE){};                  // wait for conversion

    result = (ADRESH << 8) + ADRESL;

    if (result>512)
    {
    LATBbits.LATB5=1;
    Delay_ms(10);
    LATBbits.LATB5=!LATBbits.LATB5;
    Delay_ms(10);
    }
   else
   {
    LATBbits.LATB3=1;
    Delay_ms(10);
    LATBbits.LATB3=!LATBbits.LATB3;
    Delay_ms(10);
   }
   }  while(1);

                                           //PIC16 Register Initialization
   }//end Main

   void InitSfr(void)
    {
    ANSELA = 0b00000001;  // RA0 analog input
    TRISA  = 0b00100001;  // RA0, RA5 inputs
    ANSELB = 0b00000000;  // PORTB all digital outputs
    TRISB  = 0b11100111;  // PORTB all outputs


    // Configure FVR to 4.096 V
    FVRCON = 0b11000011 ;

    }
   //initialise the ADC
   void InitADC()
   {

    // Configure ADCON1=0b1000011
    ADCON1bits.ADNREF  = 0; // Vref- is connected to ground
    ADCON1bits.ADPREF0 = 1; // Vref+ is 4.096 V
    ADCON1bits.ADPREF1 = 1;
    ADCON1bits.ADCS0   = 0; // Use conversion clock, Fosc/2
    ADCON1bits.ADCS1   = 0; // Fosc = 500 KHz
    ADCON1bits.ADCS2   = 0;
    ADCON1bits.ADFM    = 1;  // result is right Justified

    }
 void Delay_ms(unsigned long time)
  {
    unsigned int  j;
    while(time--)
   {
    for(j = 0; j<260; j++)
    {
     int x=0;
    }
   }
 }

【问题讨论】:

  • 请告诉我们您期望发生的事情和实际发生的事情

标签: pic mplab adc


【解决方案1】:

您好,void InitSfr(void) 中有错误。

错误是您将TRISB Bit 5 设置为1 的输入

由于您希望 PORTB 作为输出,那么您需要正确设置 TRISB

TRISB  = 0b00000000;  // PORTB all outputs
PORTB  = 0b00000000;  // It is good to make all low by default

【讨论】:

  • 这是正确的,但有一个小细节:在具有锁存寄存器的 PIC 上,最好写入它们而不是端口,所以LATB = 0b00000000;
猜你喜欢
  • 2020-11-12
  • 2021-11-20
  • 2021-11-18
  • 2023-02-12
  • 1970-01-01
  • 2014-09-07
  • 1970-01-01
  • 2014-05-13
  • 2017-11-16
相关资源
最近更新 更多