你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

帮忙看一下这段代码为啥不能点亮LED,求大神指导  

[复制链接]
从天而降小太阳 提问时间:2016-10-25 20:41 /
这是main程序
#include "stm32f10x.h"
#include "bsp_usart1.h"
#include "bsp_led.h"


int main(void)
{
        LED_GPIO_Config();
        USART1_Config();
        NVIC_Configuration();
  while(1)
        {
                char ch;
                if(ch=='A')
                {
                 LED1(ON);
    }
                else if(ch=='B')
                {
                 LED1(OFF);
                }

        }
                                       
}                        

这是bsp.usart.c程序
#include "bsp_usart1.h"

/**
  * @brief  USART1 GPIO ÅäÖÃ,¹¤×÷ģʽÅäÖá£9600 8-N-1
  * @param  ÎÞ
  * @retval ÎÞ
  */
     
void USART1_Config(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
        
        /* config USART1 clock */
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
        
        /* USART1 GPIO config */
        /* Configure USART1 Tx (PA.09) as alternate function push-pull */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);   
        /* Configure USART1 Rx (PA.10) as input floating */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
        
        /* USART1 mode config */
        USART_InitStructure.USART_BaudRate = 115200;
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;
        USART_InitStructure.USART_StopBits = USART_StopBits_1;
        USART_InitStructure.USART_Parity = USART_Parity_No ;
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
        USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
        USART_Init(USART1, &USART_InitStructure);
        
        /* ʹÄÜ´®¿Ú1½ÓÊÕÖÐ¶Ï */
        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
//        USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
        USART_Cmd(USART1, ENABLE);
}

/// ÅäÖÃUSART1½ÓÊÕÖжÏ
void NVIC_Configuration(void)
{
        NVIC_InitTypeDef NVIC_InitStructure;
        /* Configure the NVIC Preemption Priority Bits */  
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
        
        /* Enable the USARTy Interrupt */
        NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;         
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);
}
/******************
ÖжϷþÎñ³ÌÐò
*****************/

void USART1_IRQHandler(void)
{
        uint8_t ch;
        if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
        {         
   
                  ch = USART_ReceiveData(USART1);
                  printf( "\r\n%02x\r\n", ch );    //printf·¢ËÍ·½Ê½
               
                  USART_SendData(USART1,ch);         //32¿âº¯Êý·¢ËÍ·½Ê½
                  while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
                  USART_ClearFlag(USART1,USART_FLAG_TC);
        }
         
}

/******************
    ¿ÉÒÔ·¢ËÍÒ»¸ö×Ö·û´®
*******************/
void USART1_Send_s(u8 *ch)   
{
        while(*ch)
        {
                USART_SendData(USART1,*ch);
                while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
                USART_ClearFlag(USART1,USART_FLAG_TC);
        }
}


/// ÖØ¶¨Ïòc¿âº¯Êýprintfµ½USART1
int fputc(int ch, FILE *f)
{
                /* ·¢ËÍÒ»¸ö×Ö½ÚÊý¾Ýµ½USART1 */
                USART_SendData(USART1, (uint8_t) ch);
               
                /* µÈ´ý·¢ËÍÍê±Ï */
                while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);               
        
                return (ch);
}

/// ÖØ¶¨Ïòc¿âº¯Êýscanfµ½USART1
int fgetc(FILE *f)
{
                /* µÈ´ý´®¿Ú1ÊäÈëÊý¾Ý */
                while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);

                return (int)USART_ReceiveData(USART1);
}
本人菜鸟,求大神指导
收藏 1 评论34 发布时间:2016-10-25 20:41

举报

34个回答
jinglixixi 最优答案 回答时间:2016-10-26 08:19:56
先在主函数分别测一下
LED1(ON);

LED1(OFF);
如果有效证明问题可能在通讯方面,否则有可能是引脚定义与LED的位置不符。

评分

参与人数 1ST金币 +2 收起 理由
zero99 + 2

查看全部评分

guiren 回答时间:2016-10-25 23:04:45
学习一些
anywill 回答时间:2016-10-26 07:16:36
没看到LED1()定义;
sunnydevil 回答时间:2016-10-26 08:15:46
题主确定程序成功进中断了么 如果发数据的话
高二毛 回答时间:2016-10-26 08:49:35
楼上正解。另外看看系统时钟起来了嘛?
没事逛荡 回答时间:2016-10-26 09:15:11
1,把NVIC_Configuration();放到初始化最前面。
2,没看到LED_GPIO_Config();,想来应该不会有问题;
3,把printf( "\r\n%02x\r\n", ch ); 放到LED(ON);和LED(OFF);至少可以确定通讯是否有问题。

评分

参与人数 1ST金币 +2 收起 理由
zero99 + 2

查看全部评分

回答时间:2016-10-26 09:46:05
ch临时变量,改为全局变量。
anobodykey 回答时间:2016-10-26 10:05:30
这能点亮才怪,主循环中ch又没有赋值,按你的思路楼上的回答是对的
zfz9232 回答时间:2016-10-26 13:16:07
我猜想,你是要根据串口接收到的数据来点亮LED灯,
那么第一,你应该先把串口的收和发的功能都实现;
第二步才来实现根据接收的数据实现对应的功能。
1234下一页

所属标签

相似问题

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
微信公众号二维码 微信公众号
手机版二维码 手机版