久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 723|回復: 5
打印 上一主題 下一主題
收起左側

請教前輩,為什么我換了幾個變量名這段代碼就能正常運行了

[復制鏈接]
跳轉到指定樓層
樓主
ID:1133932 發表于 2024-11-10 23:15 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
這是之前不能運行的代碼:
#include "stm32f10x.h"                  // Device header
#include "Delay.h"

uint8_t IrControl_GetFlag;
uint32_t IrControl_Data;

/*紅外遙控函數初始化*/
void IrControl_Init(void)
{
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);
       
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
        GPIO_Init(GPIOA,&GPIO_InitStructure);
       
        GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource8);
        EXTI_ClearITPendingBit(EXTI_Line8);
       
        EXTI_InitTypeDef EXTI_InitStructure;
        EXTI_InitStructure.EXTI_Line=EXTI_Line8;
        EXTI_InitStructure.EXTI_LineCmd=ENABLE;
        EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
        EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
        EXTI_Init(&EXTI_InitStructure);
       
        NVIC_InitTypeDef NVIC_InitStructure;
        NVIC_InitStructure.NVIC_IRQChannel=EXTI9_5_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
        NVIC_Init(&NVIC_InitStructure);
}


/*獲取高電平的持續時間*/
uint8_t IrControl_GetTime(void)
{
        uint8_t time=0;
        while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8) == 1)
        {
                time++;
                Delay_us(20);
                if(time>=250) return time;       
        }
        return time;
}

void EXTI9_5_IRQHandler(void)
{
        uint8_t Time=0,Data,Num=0,read=0;
        while(1)
        {
                if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8) == 1)
                {
                        Time=IrControl_GetTime();
                        if(Time>=250) break;
                        if(Time>=200 && Time<250) //高電平持續4500us左右,接收到碼頭
                        {
                                read=1;
                        }
                        else if(Time>=10 && Time<50) //高電平持續560us左右 輸出為0
                        {
                                Data=0;
                        }
                        else if(Time>=60 && Time<90) //高電平持續1690us左右 輸出為1
                        {
                                Data=1;
                        }
                       
                        if(read == 1)
                        {
                                IrControl_Data<<=1;
                                IrControl_Data+=Data;
                               
                                if(Num>=32)
                                {
                                IrControl_GetFlag=1;
                                break;
                                }
                        }       
                                Num++;       
                       
                }
        }
        EXTI_ClearITPendingBit(EXTI_Line8);
}



這是修改之后可以運行的代碼:
#include "stm32f10x.h"                  // Device header
#include "Delay.h"

#define IRED_PORT                         GPIOA  
#define IRED_PIN                           GPIO_Pin_8
#define IRED_PORT_RCC                RCC_APB2Periph_GPIOA

uint8_t IR_Receiveflag;
uint32_t IR_Receivecode;

/*紅外遙控函數初始化*/
void IRremote_Init(void)
{
        RCC_APB2PeriphClockCmd(IRED_PORT_RCC|RCC_APB2Periph_AFIO,ENABLE);
       
        GPIO_InitTypeDef GPIO_InitStructure;
        EXTI_InitTypeDef EXTI_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
       
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
        GPIO_InitStructure.GPIO_Pin=IRED_PIN;
        GPIO_Init(GPIOA,&GPIO_InitStructure);
       
        GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource8);
        EXTI_ClearITPendingBit(EXTI_Line8);
       
        EXTI_InitStructure.EXTI_Line=EXTI_Line8;
        EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
        EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
        EXTI_InitStructure.EXTI_LineCmd=ENABLE;
        EXTI_Init(&EXTI_InitStructure);
       
        NVIC_InitStructure.NVIC_IRQChannel=EXTI9_5_IRQn;       
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
        NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
        NVIC_Init(&NVIC_InitStructure);
}


/*獲取高電平的持續時間*/
uint8_t IRremote_Counttime(void)
{
        u8 t=0;
        while(GPIO_ReadInputDataBit(GPIOA,IRED_PIN) == 1)
        {
                t++;
                Delay_us(20);
                if(t>=250) return t;       
        }
        return t;
}

void EXTI9_5_IRQHandler(void)
{
        uint8_t Time=0,Data,Num=0,read=0;
        while(1)
        {
                if(GPIO_ReadInputDataBit(GPIOA,IRED_PIN) == 1)
                {
                        Time=IRremote_Counttime();
                        if(Time>=250) break;
                        if(Time>=200 && Time<250) //高電平持續4500us左右,接收到碼頭
                        {
                                read=1;
                        }
                        else if(Time>=60 && Time<90) //高電平持續1690us左右 輸出為1
                        {
                                Data=1;
                        }
                        else if(Time>=10 && Time<50) //高電平持續560us左右 輸出為0
                        {
                                Data=0;
                        }
                       
                        if(read == 1)
                        {
                                IR_Receivecode<<=1;
                                IR_Receivecode+=Data;
                               
                                if(Num>=32)
                                {
                                   IR_Receiveflag=1;
                                break;
                                }
                        }
                        Num++;                       
                }
        }
        EXTI_ClearITPendingBit(EXTI_Line8);
}
請問為什么只是變了幾個變量名字就可以運行了,我找bug找了特別久沒想到這樣解決了

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

沙發
ID:1133932 發表于 2024-11-10 23:22 | 只看該作者
修改的地方有三個宏定義,全局變量標志位和數據位的名字,初始化和獲取高低電平的函數名。邏輯上兩段代碼應該是沒有區別的
回復

使用道具 舉報

板凳
ID:23640 發表于 2024-11-11 10:26 | 只看該作者
請問運行部了的原因是什么,這種芯片可以在線調試的呀
回復

使用道具 舉報

地板
ID:21455 發表于 2024-11-11 15:23 | 只看該作者
void IrControl_Init(void)  -> void IRremote_Init(void) 改之前,你的程序調用這個 IrControl_Init() 初始化函數了么?
回復

使用道具 舉報

5#
ID:1133932 發表于 2024-11-11 16:53 | 只看該作者
發表于 2024-11-11 15:23
void IrControl_Init(void)  -> void IRremote_Init(void) 改之前,你的程序調用這個 IrControl_Init() 初 ...

這個是肯定調用了的,
這是我修改之前的main.c:
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "robot.h"
#include "PWM.h"
#include "Nixie.h"
#include "Ircontrol.h"

int main(void)
{
        uint8_t buf[2];
        uint8_t DatA=0;
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
        Robot_Init();
        IrControl_Init();
        Nixie_Init();
        Display(0);
        while(1)
        {                                
                if(IrControl_GetFlag == 1)
                {
                        IrControl_GetFlag=0;
                        DatA=IrControl_Data>>8;
                        IrControl_Data=0;
                        buf[0]=DatA/16;
                        buf[1]=DatA%16;
                }
                if(buf[0] == 6 && buf[1] == 2)
                {
                        Robot_GoStraight(70,0);
                        Display(1);
                }
                else if(buf[0] == 2 && buf[1] == 2)
                {
                        Robot_TurnLeft(70,0);
                        Display(2);
                }
                else if(buf[0] == 12 && buf[1] == 3)
                {
                        Robot_TurnRight(70,0);
                        Display(3);
                }
                else if(buf[0] == 10 && buf[1] == 8)
                {
                        Robot_DrawBack(70,0);
                        Display(4);
                }
                else if(buf[0] == 0 && buf[1] == 2)
                {
                        Robot_Stop(100);
                        Display(4);
                }
                else
                {
                        Robot_Stop(100);
                }
        }
}
這是修改之后的:
#include "stm32f10x.h"                  // Device header
#include "LEDSEG.h"
#include "Delay.h"
#include "robot.h"
#include "Incontrol.h"
#include "Key.h"
#include "Serial.h"

int main(void)
{
        uint8_t buf[2];
        uint8_t data_code=0;
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);  //中斷優先級分組分2組
        IRremote_Init();           // 紅外遙控器初始化
        LEDSEG_Init();
        Serial_Init();             // 串口初始化
        robot_Init();              // 機器人初始化
        //Digital_Display(0);
        while (1)
        {
          if(IR_Receiveflag == 1) //如果紅外接收到
                {
                        IR_Receiveflag = 0; //清零
                        printf("紅外接收碼 %0.8X\r\n",IR_Receivecode);        //打印
                        data_code=IR_Receivecode>>8;
                        IR_Receivecode = 0; //接收碼清零
                        
                        buf[0] = data_code/16;
                        buf[1] = data_code%16;
                        
                  printf("buf[0]:%d\r\n",buf[0]);
                  printf("buf[1]:%d\r\n",buf[1]);
                }
    if(buf[0] == 6 && buf[1] == 2)
                {
                        makerobo_run(70,0);  // 前進2s
                        Digital_Display(0);
                }
                else if(buf[0] == 10 && buf[1] == 8)
                {
                        makerobo_back(70,0); // 后退2s
                        Digital_Display(1);
                }
                else if(buf[0] == 2 && buf[1] == 2)
                {
                        makerobo_Spin_Left(70,0); //左轉
                        Digital_Display(2);
                }
                else if(buf[0] == 12 && buf[1] == 2)
                {
                        makerobo_Spin_Right(70,0); // 右轉
                        Digital_Display(3);
                }
                else if(buf[0] == 0 && buf[1] == 2)
                {
                        makerobo_brake(0); // 停止
                        Digital_Display(4);
                }
                else
                {
                        makerobo_brake(0); // 停止
                }
        }
}
修改的也只有函數名,邏輯上修改前后沒什么變化
回復

使用道具 舉報

6#
ID:1133932 發表于 2024-11-11 17:00 | 只看該作者
yaosongjin 發表于 2024-11-11 10:26
請問運行部了的原因是什么,這種芯片可以在線調試的呀

運行不了的原因我也不知道是什么,我找bug找了特別久找不出來所以就試試偏方,把幾個函數名改變了一下就可以運行了不知道是什么原因。沒有修改之前的話只能達到數碼管顯示0的功能,也就是Display(0);while里面的都失效,主函數在我下面的回復里,麻煩前輩幫我看看
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 免费久久网 | 一区二区三区av | 久久99精品久久久久久国产越南 | 欧美成人精品欧美一级 | 伊人91在线 | 毛片站| 亚洲精品大片 | 麻豆视频国产在线观看 | 中文在线一区 | 伊人91在线 | 97av视频| 一级二级三级在线观看 | 久久精品国产清自在天天线 | 中文在线视频 | 国产精品一区二区三区在线播放 | 亚洲人成人一区二区在线观看 | 免费久久久 | av入口| 久久久免费精品 | 欧美精品成人一区二区三区四区 | 日韩视频成人 | 国产电影一区 | 亚洲精品一区二区三区蜜桃久 | 美女艹b | 国产精品久久久久久久久久了 | 国产美女自拍视频 | 日韩精品 电影一区 亚洲 | 男女啪啪高潮无遮挡免费动态 | 网黄在线| 黄色日本视频 | 日韩成人影院 | 日本不卡一区二区三区在线观看 | 成人免费网站在线 | www午夜视频 | 精品欧美 | 国产高清美女一级a毛片久久w | 亚洲国产精品91 | 亚洲欧美日韩精品久久亚洲区 | 国产在线精品一区二区三区 | 免费九九视频 | 国产一区二区自拍 |