我正在使用PIC18F26K83,并且正在尝试读取温度值。我使用LED作为调试工具。但是,它的行为很奇怪。我有2个数组1用于比较ADC值,另一个用于从ADC值数组中找出温度。这是应该起作用的简化代码:
int i;
int k;
int temperature;
unsigned int temp_data;
int temp_ADC_array[34]= { 259,293,332,377,428,487,555,632,720,821,934,1062,1203,1360,1531,1715,1910,2113,2320,2528,2731,2926,3108,3274,3422,3552,3663,3756,3833,3895,3945,3983,4013,4036};
int temp_array[34]= {125,120,115,110,105,100,95,90,85,80,75,70,65,60,55,50,45,40,35,30,25,20,15,10,5,0,-5,-10,-15,-20,-25,-30,-35,-40 };
void main(){
//Set the pins of the MCU
TRISA.B2=0; //LED
LATA.B2=0;
while(1){
temp_data=2000;
for(i =0;i<34; i++){
if(temp_data>temp_ADC_array[33-i]) {
temperature = temp_array[33-i];
break;}}
if(temperature>50){
led=1;}
else{
led=0;}
delay_ms(5000);
}
}
在上面的代码中,由于temp_data为2000,因此for循环的温度必须为45。在调试器中,它显示为45。这意味着LED必须关闭,因为温度低于50,并且在调试器中我也可以看到LED引脚始终为零。但是,当我尝试将其电路LED点亮时。在其他情况下,当我测试LED时,效果很好。 for循环中的问题可能在哪里?还是问题出在LED上?预先感谢。
编辑:我也尝试过:
if(temperature<50){
led=1;}
else{
led=0;}
因此,当温度 50时,LED点亮。怎么可能呢?
请注意,我使用MicroC。
编辑:如果有人感兴趣,这是完整的代码:
#include <stdint.h>
int i;
int k;
int temperature;
unsigned int temp_data;
short transmit_data1;
short transmit_data2;
uint16_t data_transmit;
int temp_ADC_array[34]= { 259,293,332,377,428,487,555,632,720,821,934,1062,1203,1360,1531,1715,1910,2113,2320,2528,2731,2926,3108,3274,3422,3552,3663,3756,3833,3895,3945,3983,4013,4036};
int temp_array[34]= {125,120,115,110,105,100,95,90,85,80,75,70,65,60,55,50,45,40,35,30,25,20,15,10,5,0,-5,-10,-15,-20,-25,-30,-35,-40 };
#define led (LATA.B2)
void Clk_62kHz (){
NOSC2_BIT =1;
NOSC1_BIT=1; //HFINTOSC
NOSC0_BIT=0;
FRQ3_BIT =0;
FRQ2_BIT=0; //1 MHz
FRQ1_BIT=0;
FRQ0_BIT=0;
NDIV3_BIT =0;
NDIV2_BIT=1;
NDIV1_BIT=0; //Divide 16 =62.5 kHz.
NDIV0_BIT=0;
}
void main(){
//Set the pins of the MCU
TRISA.B2=0; //LED
LATA.B2=0;
TRISA.B3=1; //Case Temp A
ANSELA.B3=1;
TRISC.B5=0; //SCLK
TRISC.B6=0; //CS
LATC.B6=1; //Deselect slave
TRISC.B7=0; //DIN
ANSELC.B5=0;
ANSELC.B6=0;
ANSELC.B7=0;
temperature=0;
Clk_62kHz();
// Clk_8Mhz() ;
//PPS Mapping
RC7PPS= 0b00011111 ; //DIN, RC7 = SDIPPS
RC6PPS= 0b00100000; //CS, RC6= SSPPS
RC5PPS= 0b00011110; //SCLK, RC5=SCKPPS
//transmit_data= 58112;
transmit_data1=0b11100011;
//buradan assagisi while loopun icindeydi!!!!!
LATC.B6=0; //Select the slave
transmit_data2=0b01111111;
LATC.B6=1; //deselect the slave && update the data
while(1)
{
temp_data=2000;
for(i =0;i<34; i++)
{
if (temp_data>temp_ADC_array[33-i])
{
temperature = temp_array[33-i];
if (temperature>50)
{
led=1;
}
else
{
led=0;
}
}
}
delay_ms(5000);
}
}
最佳答案
主循环看起来很疯狂...试试这个
while(1)
{
temp_data=2000;
for(i =0;i<34; i++)
{
if (temp_data>temp_ADC_array[33-i])
{
temperature = temp_array[33-i];
if (temperature>50)
{
led=1;
}
else
{
led=0;
}
}
delay_ms(5000);
}
}
并希望您定义
led
关于c - PIC18F26K83 LED根据调试器不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57359073/