从天而降小太阳 发表于 2016-10-25 20:41:26

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

这是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"

/**
* @briefUSART1 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);
}
本人菜鸟,求大神指导

jinglixixi 发表于 2016-10-26 08:19:56

先在主函数分别测一下
LED1(ON);

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

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);至少可以确定通讯是否有问题。

发表于 2016-10-26 09:46:05

ch临时变量,改为全局变量。

anobodykey 发表于 2016-10-26 10:05:30

这能点亮才怪,主循环中ch又没有赋值,按你的思路楼上的回答是对的

zfz9232 发表于 2016-10-26 13:16:07

我猜想,你是要根据串口接收到的数据来点亮LED灯,
那么第一,你应该先把串口的收和发的功能都实现;
第二步才来实现根据接收的数据实现对应的功能。
页: [1] 2 3 4
查看完整版本: 帮忙看一下这段代码为啥不能点亮LED,求大神指导