找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2659|回复: 2
打印 上一主题 下一主题
收起左侧

STM32无法循环点亮,求个大神帮帮忙

[复制链接]
跳转到指定楼层
楼主
#include "stm32f10x.h"
uint16_t temp,i;
void Delay(unsigned int count)
{
        unsigned int i;
        for(;count!=0;count--)
        {
                i=5000;
                while(i--);
        }
}
void main(void)
{
        
        GPIO_InitTypeDef GPIO_InitStructure;
        
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
        
        
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOB,&GPIO_InitStructure);
        
        while(1)
        {
               
                if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==0)
                {
                        Delay(500);
                        if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==0)
                        {
                                temp=0x0100;
                    for(i=0;i<8;i++)
                    {
                                 
                            GPIO_Write(GPIOB,~temp);
                             Delay(100);
                            temp=temp<<1;
                                        while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==1);
                    }
                        }
           }else if (GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==1){
                        
                         temp=0x8000;
                         for(i=7;i>=0;i--)
                    {
                                 
                            GPIO_Write(GPIOB,~temp);
                             Delay(100);
                            temp=temp>>1;
                                        while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==1);
                    }
                 }

                        
         }
        
}

1.PNG (41.31 KB, 下载次数: 72)

1.PNG
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:277948 发表于 2020-6-21 18:19 | 只看该作者
我把程序改为 按键按下灯取反四次

#include "stm32f10x.h"
#include "sys.h"
//#include "intrins.h"
#define LED1 PDout(2)        // PD2       
uint16_t temp,i;
void Delay(unsigned int count)
{
        unsigned int i;
        for(;count!=0;count--)
        {
                i=5000;
                while(i--);
        }
}
int main(void)
{

        GPIO_InitTypeDef GPIO_InitStructure;

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;//注意GPIO_Mode_IPD不是GPIO_Mode_IPU
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOD,&GPIO_InitStructure);
                                temp=0x0001;//注要放在循环前边
while(1)
{
        if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==1)     //没按下按键为0 按下按键为1
        {
                Delay(200);
                if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==1)
                {
                        for(i=0;i<8;i++)
                        {
//                                GPIO_Write(GPIOD,temp);
                                LED1=!LED1;
                                Delay(1000);
                        }
                }
   }while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==0); //按键释放应该放在按键判断之外
}        
}

回复

使用道具 举报

板凳
ID:355173 发表于 2020-6-22 14:47 | 只看该作者
是IPU(上拉输入),而IPD是(下拉),很明显PA0接了个电阻到VDD,而当有按键按下时,电路接通到地,所以PA0处读到的电压是0,当释放时,与VDD想接,是高电平为1
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表