gpt4 book ai didi

c - 图片 18F4550 : local variable problem,

转载 作者:太空宇宙 更新时间:2023-11-04 04:57:01 24 4
gpt4 key购买 nike

我将 pic18F4550 与 microchip v8.63 和 C 18 编译器一起使用。我正在使用一个 LDR 来检索 LED 的值(不在我的 picdem 板上)(红色、绿色和蓝色),这些值在每次转换后都存储在一个变量中。之后,当我按下按钮 S2 时,我进入了 ISR 方法:这部分有效。

但现在:我尝试比较 if 中的变量红色、绿色和蓝色:但我认为这不会发生,他只是转到我的“其他”(导致我的 picdem 板上的 RB3 烧毁)。

#include <p18f4550.h>  

/** V E C T O R R E M A P P I N G *******************************************/

extern void _startup (void); // See c018i.c in your C18 compiler dir
#pragma code _RESET_INTERRUPT_VECTOR = 0x001000
void _reset (void)
{
_asm goto _startup _endasm
}
#pragma code
void ISR (void);
#pragma code _HIGH_INTERRUPT_VECTOR = 0x001008
void _high_ISR (void)
{
_asm goto ISR _endasm
}

#pragma code _LOW_INTERRUPT_VECTOR = 0x001018
void _low_ISR (void)
{
;
}
#pragma code
/******************************************************************************/

// global variable, value off LDR.
unsigned int var1ADRESH = 0x00;
unsigned int color_red = 0;
unsigned int color_green = 0;
unsigned int color_blue = 0;

void main (void)
{
TRISD = 0x00; // PORTD als uitgang

RCONbits.IPEN = 0; // prioriteit uit
INTCONbits.GIE = 1; // enable interrupt
INTCONbits.RBIE = 1; // interrupt portB aan

//= set up port =
TRISAbits.TRISA0 = 1; // Set RA0/AN0 to input
//leds
TRISAbits.TRISA3 = 0;
TRISAbits.TRISA4 = 0;
TRISAbits.TRISA5 = 0;
LATAbits.LATA3 = 1;
LATAbits.LATA4 = 1;
LATAbits.LATA5 = 1;

ADCON0 = 0b00000000; // Set channel select to AN0
ADCON1 = 0b00001110; // Configure RA0/AN0 as analogue
ADCON2 = 0b10101010; // Right justified result
// TAD 12 and FOSC 32 - may need to adjust this
// depending on your clock frequency (see datasheet)

while(1)
{
_asm sleep _endasm
}
}

#pragma interrupt ISR
void ISR (void)
{
if (INTCONbits.RBIF==1) {

//conversie blauw
LATAbits.LATA3 = 0;
ADCON0bits.ADON = 1; // Enable ADC
// read LDR value.
ADCON0bits.GO = 1; // Set the GO bit of the ADCON0 register to start the conversion.
while (ADCON0bits.GO); // Wait until the conversion is complete.
ADCON2bits.ADFM = 0; // read result as 8-bit. (dus data in ADRESH) !
//= read data in ADRESH =
var1ADRESH = ADRESH; // reading value from LDR

color_blue = ADRESH; //waarde in blauw

//conversie rood
LATAbits.LATA3 = 1;
LATAbits.LATA4 = 0;

ADCON0bits.ADON = 1; // Enable ADC
// read LDR value.
ADCON0bits.GO = 1; // Set the GO bit of the ADCON0 register to start the conversion.
while (ADCON0bits.GO); // Wait until the conversion is complete.
ADCON2bits.ADFM = 0; // read result as 8-bit. (dus data in ADRESH) !
//= read data in ADRESH =
var1ADRESH = ADRESH; // reading value from LDR

color_red = ADRESH; //waarde in blauwe steken

//conversie groen
LATAbits.LATA4 = 1;
LATAbits.LATA5 = 0;

ADCON0bits.ADON = 1; // Enable ADC
// read LDR value.
ADCON0bits.GO = 1; // Set the GO bit of the ADCON0 register to start the conversion.
while (ADCON0bits.GO); // Wait until the conversion is complete.
ADCON2bits.ADFM = 0; // read result as 8-bit. (dus data in ADRESH) !
//= read data in ADRESH =
var1ADRESH = ADRESH; // reading value from LDR

color_green = ADRESH; //waarde in blauwe steken

// alles uitzetten
//PORTB = 0b1111111;
LATAbits.LATA5 = 1;

if(color_blue > color_red && color_blue > color_green){
//blauw
LATDbits.LATD0 = 1;
}
if(color_red > color_blue && color_red > color_green){
//rood
LATDbits.LATD1 = 1;
}
if(color_green > color_red && color_green > color_blue){
//groen
LATDbits.LATD2 = 1;
}
else {
LATDbits.LATD3 = 1;
}
}
INTCONbits.RBIF = 0;
}

最佳答案

  • 尝试在每次读取前设置 LATA3、4、5 中的所有三个?
  • 为 D0..2 设置 TRIS

关于c - 图片 18F4550 : local variable problem,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5744290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com