【问题标题】:ADC for PIC18F47J53 code unexpected behavior when passing by value按值传递时 PIC18F47J53 的 ADC 代码出现意外行为
【发布时间】:2017-06-26 20:55:58
【问题描述】:

我正在使用 MPLAB v3.51 实现 PIC18F47J53 的 ADC。

为什么主函数中变量CH的值没有正确传递给带参数channel的函数ADC_Read

unsigned int ADC_Read(unsigned char channel);
void ADC_Initialize();

#pragma config ADCSEL = BIT12                  //12 bit conversion mode enabled

#define ANCON_AiBi4         ANCON0bits.PCFG1   // Analog Input channels

int main(int argc, char** argv) 
{
    unsigned int j=0;
    unsigned char CH; 

    ADC_Initialize();

    while (1) 
    {
        CH = 1;
        j = ADC_Read(CH); //store the result of adc in j.    
    }  
    return (0);
}


void ADC_Initialize()
{
     ADCON0 = 0x01;   //00000001      ADC ON, AVss, AVdd 
     ADCON1 = 0xBD;   //10111101      R justified, Normal op, 20TAD, FOSC/16
}


/* Select the analog channel */
unsigned int ADC_Read(unsigned char channel)
{
    ADCON0 &= 0xC3;                     //11000011    Clearing the Channel Selection Bits
    ADCON0 |= channel<<2;               //Setting the required Bits
    ADCON0bits.GO_DONE = 1;
    while(ADCON0bits.GO_DONE);
    return ((ADRESH * 256) + ADRESL);   //Returns Result
}

生成的汇编代码

!unsigned int ADC_Read(volatile unsigned char channel)
0xB0: MOVFF FSR2, POSTINC1
0xB2: NOP
0xB4: MOVFF FSR1, FSR2
0xB6: NOP
0xB8: SUBFSR 2, 0x2
!{
!    ADCON0 &= 0xC3;                     //11000011    Clearing the Channel Selection Bits
0xBA: MOVLW 0xC3
0xBC: ANDWF ADCON0, F, ACCESS
!    ADCON0 |= channel<<2;               //Setting the required Bits
0xBE: MOVF [0x0], W, ACCESS
0xC0: MULLW 0x4
0xC2: MOVF PROD, W, ACCESS
0xC4: IORWF ADCON0, F, ACCESS
!    ADCON0bits.GO_DONE = 1;
0xC6: BSF ADCON0, 1, ACCESS
!    while(ADCON0bits.GO_DONE);
0xC8: BTFSC ADCON0, 1, ACCESS
0xCA: BRA 0xC8
!    return ((ADRESH * 256) + ADRESL);   //Returns Result
0xCC: MOVF ADRESH, W, ACCESS
0xCE: MOVLB 0x0
0xD0: MOVWF 0xC, BANKED
0xD2: CLRF 0xD, BANKED
0xD4: MOVFF 0xC, 0xD
0xD6: NOP
0xD8: MOVLB 0x0
0xDA: CLRF 0xC, BANKED
0xDC: MOVF ADRES, W, ACCESS
0xDE: MOVLB 0x0
0xE0: ADDWF 0xC, W, BANKED
0xE2: MOVLB 0x0
0xE4: MOVWF __tmp_0, BANKED
0xE6: MOVLW 0x0
0xE8: MOVLB 0x0
0xEA: ADDWFC 0xD, W, BANKED
0xEC: MOVLB 0x0
0xEE: MOVWF 0xB, BANKED
0xF0: MOVFF __tmp_0, PROD
0xF2: NOP
0xF4: MOVFF 0xB, PRODH
0xF6: NOP
0xF8: BRA 0xFA
!}
0xFA: SUBFSR 1, 0x1
0xFC: MOVFF INDF1, FSR2
0xFE: NOP
0x100: RETURN 0

配置位源代码

// PIC18F47J53 Configuration Bit Settings

// 'C' source line config statements

#include <p18F47J53.h>

// CONFIG1L
#pragma config WDTEN = ON       // Watchdog Timer (Enabled)
#pragma config PLLDIV = 1       // PLL Prescaler Selection (No prescale (4 MHz oscillator input drives PLL directly))
#pragma config CFGPLLEN = OFF   // PLL Enable Configuration Bit (PLL Disabled)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset (Enabled)
#pragma config XINST = ON       // Extended Instruction Set (Enabled)

// CONFIG1H
#pragma config CPUDIV = OSC1    // CPU System Clock Postscaler (No CPU system clock divide)
#pragma config CP0 = OFF        // Code Protect (Program memory is not code-protected)

// CONFIG2L
#pragma config OSC = INTOSC     // Oscillator (INTOSC)
#pragma config SOSCSEL = HIGH   // T1OSC/SOSC Power Selection Bits (High Power T1OSC/SOSC circuit selected)
#pragma config CLKOEC = ON      // EC Clock Out Enable Bit  (CLKO output enabled on the RA6 pin)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor (Enabled)
#pragma config IESO = ON        // Internal External Oscillator Switch Over Mode (Enabled)

// CONFIG2H
#pragma config WDTPS = 32768    // Watchdog Postscaler (1:32768)

// CONFIG3L
#pragma config DSWDTOSC = INTOSCREF// DSWDT Clock Select (DSWDT uses INTRC)
#pragma config RTCOSC = T1OSCREF// RTCC Clock Select (RTCC uses T1OSC/T1CKI)
#pragma config DSBOREN = ON     // Deep Sleep BOR (Enabled)
#pragma config DSWDTEN = ON     // Deep Sleep Watchdog Timer (Enabled)
#pragma config DSWDTPS = G2     // Deep Sleep Watchdog Postscaler (1:2,147,483,648 (25.7 days))

// CONFIG3H
#pragma config IOL1WAY = ON     // IOLOCK One-Way Set Enable bit (The IOLOCK bit (PPSCON<0>) can be set once)
#pragma config ADCSEL = BIT12   // ADC 10 or 12 Bit Select (12 - Bit ADC Enabled)
#pragma config MSSP7B_EN = MSK7 // MSSP address masking (7 Bit address masking mode)

// CONFIG4L
#pragma config WPFP = PAGE_127  // Write/Erase Protect Page Start/End Location (Write Protect Program Flash Page 127)
#pragma config WPCFG = OFF      // Write/Erase Protect Configuration Region  (Configuration Words page not erase/write-protected)

// CONFIG4H
#pragma config WPDIS = OFF      // Write Protect Disable bit (WPFP<6:0>/WPEND region ignored)
#pragma config WPEND = PAGE_WPFP// Write/Erase Protect Region Select bit (valid when WPDIS = 0) (Pages WPFP<6:0> through Configuration Words erase/write protected)
#pragma config LS48MHZ = SYS48X8// Low Speed USB mode with 48 MHz system clock bit (System clock at 48 MHz USB CLKEN divide-by is set to 8)

MPLAB variables view

ADCON0 and channel unexpected values

【问题讨论】:

  • 为什么你认为它没有被“转发”?但是我看到了在main 之前没有函数原型的潜在问题。
  • @MFF:尝试在代码中定义配置位以及使用#pragma config XINST = ON。编译器选项可能会简单地通知代码生成器预期的目标,而不会让 MCU 进入它,尽管我还不确定。如果没有其他验证,则在调试器或 HEX 映像中验证写入 FLASH 的底层配置位。
  • @MFF:恐怕我还没有完全关注你。 C18 编译器确实支持扩展指令集,最好与它一起使用。唉,这是一个过时且通常很糟糕的 C 编译器,但要注意非优化的 XC8 编译器可能不会有太大的改进。无论如何,当您启用编译器选项并将#pragma config 设置为 on 时会发生什么,并且当您在编程后检查它时 XINST 位是否读回为 on?
  • @MFF:这不是银行转换。编译器创建一个从 FSR1(虚拟堆栈指针)到 FSR2(基指针)的堆栈帧,方法是将先前的基指针压入堆栈并将新的堆栈帧分配为基指针。无论如何,这里使用的单库堆栈模型只影响低 8 位,并假定堆栈页面上的 FSR1H/FSR2H 是固定的。然而,无论出于何种原因,它们都出现了分歧。链接描述文件中可能存在问题(> 256 字节的堆栈?)或不正确的 CRT 启动代码。请发布地图/链接器文件并尝试追踪 FSR1H/FSR2H 的分歧
  • 顺便说一句,这是复杂架构的一个重大限制,对目标语言的支持令人不安。当一切正常时,一个好的编译器和优化器可能会隐藏缺陷和复杂性,但是无论它发生什么故障,程序员仍然需要系统的底层知识来解决它。

标签: c pass-by-value mplab adc pic18


【解决方案1】:

这是一个堆栈错误。但是,在我启用“多银行堆栈模型”后,它现在工作正常。谢谢大家

【讨论】:

    猜你喜欢
    • 2014-10-30
    • 1970-01-01
    • 2020-09-01
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    相关资源
    最近更新 更多