617036759 发表于 2012-7-23 10:12:55

新手求救___rt-thread按键输入问题

本帖最后由 617036759 于 2012-7-23 10:14 编辑

我在rt-thread0.3.0上添加如下代码:结果把程序烧在板子上之后,四个按键(PB1、2、3、4),按键2、3、4都正常,按键1无论按下与否(按下为低电平),芯片收到的永远是低电平。前提是,我在裸奔情况下,板子完全正常,请各位高手帮忙看一下。
/*
* File      : led.c
*/
#include <rtthread.h>
#include <stm32f10x.h>

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


#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);
        }       
}

tanghong668 发表于 2012-7-23 10:29:22

加上这个试一试吧/*
* This function will disable the JTAG port
*/
void rt_hw_disable_JTAG(void)
{
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);      // 打开AFIO时钟
        GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE);   // JTAG-DP禁用,SW-DP使能
}

你好好检查一下裸奔下是否设置了禁用JTAG

aozima 发表于 2012-7-23 10:33:05

同样类型的贴子,请不要一贴多发。
页: [1]
查看完整版本: 新手求救___rt-thread按键输入问题