617036759 发表于 2012-7-21 15:51:33

新手求救_RT-Thread 1.0.0 PB3作为输入不正常的问题

本帖最后由 617036759 于 2012-7-21 15:53 编辑

void Key_Init(void)//PB3、PB4、PB5、PB6配置成输入,作为按键输入,按键按下为低电平。
{
        GPIO_InitTypeDef GPIO_InitStructure;
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU ;// 上拉输入
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

        GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_3;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
        GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_4;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
        GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_5;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
        GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_6;
                GPIO_Init(GPIOB, &GPIO_InitStructure);
       
        GPIO_SetBits(GPIOB, GPIO_Pin_3);
        GPIO_SetBits(GPIOB, GPIO_Pin_4);
        GPIO_SetBits(GPIOB, GPIO_Pin_5);
        GPIO_SetBits(GPIOB, GPIO_Pin_6);
}

void yd_key_thread_entry(void)
{                                                               
        while(1)
        {
                if(!KEY1)//不管按键是否按下,都进入这里。用
// ReadValue= GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_3);恒为0;
                {//只要复位就进入这里。
                        YDRED = 0;
                        YDGREEN = 1;
                        YDBLUE = 1;
                }
                if(!KEY2)
                {
                        YDRED = 1;
                        YDGREEN = 0;                                       
                        YDBLUE = 1;
                }
                if(!KEY3)
                {                       
                        YDRED = 1;
                        YDGREEN = 1;
                        YDBLUE = 0;
                }
                if(!KEY4)
                {
                        YDRED = 1;
                        YDGREEN = 1;
                        YDBLUE = 1;
                }        
        }
}

上述问题说明:电路板肯定没问题,裸奔的情况下,工作完全正常,一旦使用rt-thread,就为出现上述问题,PB3的按键不管是否按下,芯片接收到的都是低电平,初步怀疑是re-thread 1.0.0系统占用了PB3,但是没法证实,查找程序后,并没有发现,哪里占用了PB3,郁闷死了。请各位高手兄弟帮帮忙,看能否解决,我的QQ号:617036759,有兴趣的朋友可以加一下。共同学习。

aozima 发表于 2012-7-21 16:21:50

1. KEY线程会占用100%CPU。
2. RT-Thread沒有也不会占用I0〈可以检查一下是否有驱动占用〉

tiancaigao7 发表于 2012-7-21 17:29:20

如果仅仅是读取IO口的电平,那么同样一块板子,裸奔的情况下可以,加上了OS肯定也可以。因为硬件没有改变。另外,确实,楼上的线程没有主动释放CPU,这样低优先级的线程无法执行。
你最好开始给出全部的程序。另外,除了key1之外,其他的key都正常?

617036759 发表于 2012-7-23 08:48:43

首先谢谢您了,哈哈,除了按键1,其他三个键完全正常,您可以留下QQ号什么的吗,我好把整个工程发给您。

617036759 发表于 2012-7-23 08:52:24

楼上几位能留下QQ或者邮箱吗,我把程序发给您们,希望您们能抽时间帮小弟看看。

617036759 发表于 2012-7-23 09:58:20

/*
* File      : led.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2009, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date         Author       Notes
* 2009-01-05   Bernard      the first version
*/
#include <rtthread.h>
#include <stm32f10x.h>

/*#ifdef STM32_SIMULATOR
#define led1_rcc                  RCC_APB2Periph_GPIOA
#define led1_gpio                   GPIOA
#define led1_pin                  (GPIO_Pin_5)

#define led2_rcc                  RCC_APB2Periph_GPIOA
#define led2_gpio                   GPIOA
#define led2_pin                  (GPIO_Pin_6)
#else
#define led1_rcc                  RCC_APB2Periph_GPIOF
#define led1_gpio                   GPIOF
#define led1_pin                  (GPIO_Pin_6 | GPIO_Pin_7)

#define led2_rcc                  RCC_APB2Periph_GPIOF
#define led2_gpio                   GPIOF
#define led2_pin                  (GPIO_Pin_8)
#endif        */
void rt_hw_led_init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);

    GPIO_InitStructure.GPIO_Mode= GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_InitStructure.GPIO_Pin   = (GPIO_Pin_7) | (GPIO_Pin_8) | (GPIO_Pin_9);
    GPIO_Init(GPIOB, &GPIO_InitStructure);
}

void rt_hw_led_off(rt_uint32_t n)
{
    switch (n)
    {
    case 0:
      GPIO_SetBits(GPIOB, GPIO_Pin_7);
      break;
    case 1:
      GPIO_SetBits(GPIOB, GPIO_Pin_8);
      break;
        case 2:
            GPIO_SetBits(GPIOB, GPIO_Pin_9);
                   break;
    default:
      break;
    }
}

void rt_hw_led_on(rt_uint32_t n)
{
    switch (n)
    {
    case 0:
      GPIO_ResetBits(GPIOB, GPIO_Pin_7);
      break;
    case 1:
      GPIO_ResetBits(GPIOB, GPIO_Pin_8);
      break;
        case 2:
      GPIO_ResetBits(GPIOB, GPIO_Pin_9);
      break;
    default:
      break;
    }
}

#ifdef RT_USING_FINSH
#include <finsh.h>
static rt_uint8_t led_inited = 0;
void led(rt_uint32_t led, rt_uint32_t value)
{
    /* init led configuration if it's not inited. */
    if (!led_inited)
    {
      rt_hw_led_init();
      led_inited = 1;
    }

    if ( led == 0 )
    {
      /* set led status */
      switch (value)
      {
      case 1:
            rt_hw_led_off(0);
            break;
      case 0:
            rt_hw_led_on(0);
            break;               
      default:
            break;
      }
    }

    if ( led == 1 )
    {
      /* set led status */
      switch (value)
      {
      case 1:
            rt_hw_led_off(1);
            break;
      case 0:
            rt_hw_led_on(1);
            break;
      default:
            break;
      }
    }
        if ( led == 2 )
    {
      /* set led status */
      switch (value)
      {
      case 1:
            rt_hw_led_off(2);
            break;
      case 0:
            rt_hw_led_on(2);
            break;
      default:
            break;
      }
    }
}
FINSH_FUNCTION_EXPORT(led, set led on or off.)
#endif

617036759 发表于 2012-7-23 09:58:36

#include "stm32f10x.h"
#include "rtthread.h"       
#include "key.h"
#include "led.h"
void Key_Init(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU ;// 上拉输入
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

        GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6;
        GPIO_Init(GPIOB, &GPIO_InitStructure);       
        GPIO_SetBits(GPIOB, GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6);       
}

void yd_key_thread_entry(void* parameter)
{
        rt_hw_led_init();
        Key_Init();
        rt_kprintf("进入key\n");                                                               
        while(1)
        {
                if(!GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_3))
                {
                        GPIO_ResetBits(GPIOB, GPIO_Pin_7);
                        GPIO_SetBits(GPIOB, GPIO_Pin_8 | GPIO_Pin_9);
                }
                if(!GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4))
                {
                        GPIO_ResetBits(GPIOB, GPIO_Pin_8);
                        GPIO_SetBits(GPIOB, GPIO_Pin_7 | GPIO_Pin_9);
                }
                if(!GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5))
                {                       
                        GPIO_ResetBits(GPIOB, GPIO_Pin_9);
                        GPIO_SetBits(GPIOB, GPIO_Pin_7 | GPIO_Pin_8);
                }
                if(!GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_6))
                {
                        GPIO_SetBits(GPIOB, GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9);
                }        
        }
}



//ALIGN(RT_ALIGN_SIZE)
static char key_stack[ 512 ];
static struct rt_thread key_thread;
void yd_Key_thread(void)
{
        rt_err_t result;
    // init key thread
        result = rt_thread_init(&key_thread, "key",        yd_key_thread_entry, RT_NULL, (rt_uint8_t*)&key_stack, sizeof(key_stack), 20, 5);
        if (result == RT_EOK) //线程建立成功,启动线程
        {
      rt_thread_startup(&key_thread);
        }       
}

617036759 发表于 2012-7-23 09:59:10

我在版本0.3.0里面,制作了如上更改,但是还是那个问题。

lixun00 发表于 2012-7-23 10:16:55

这是属于调试端口。
要打开这些端口有特殊的限制:
1.端口要重新映射
2.重新映射之前要打开复用功能
3.调试时此端口不能用

617036759 发表于 2012-7-23 10:22:41

TO:linxun00:
   大哥,能说的具体点吗,我新手一个。现在比较急,需要急救。

tanghong668 发表于 2012-7-23 10:25:51

楼上的正解,STM32的手册还是需要好好看的

617036759 发表于 2012-7-23 10:45:01

各位兄弟姐妹,谢谢大家了,问题已经解决了,又学到了一点,哈哈,原因是JTAG接口,没有关闭,我用的是SW调试,由于没有关掉JTAG,而JTAG的其中一个接口正好和PB3复用了,我关掉了JTAG就好了。{:lol:}{:lol:},真是高手在民间呀,谢谢各位了。

zchong 发表于 2012-7-23 10:57:25

高手在文档中
页: [1]
查看完整版本: 新手求救_RT-Thread 1.0.0 PB3作为输入不正常的问题