m0_51254462 2021-11-11 09:43 采纳率: 66.7%
浏览 117
已结题

编写可以通电时中断的stm32流水灯程序

设STM32芯片管脚A0到A7各接一个发光二极管,发光二极管负极接地;管脚B0为一输入管脚,内部接下拉电阻。当管脚B0接3.3V电压管脚时,8个发光二极管流水灯显示;当管脚B0浮空时,流水灯暂停。编写程序完成上述功能,汇编,C均可。(芯片型号f103 c8t6)请提供代码,最好带注释

  • 写回答

1条回答 默认 最新

  • soar3033 2021-11-11 12:14
    关注
    
    #include"stm32f10x.h"
    
    
    void GPIO_Init()
    {
        GPIO_InitTypeDef GPIO_InitStruct;
        //GPIOA时钟使能
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
        //GPIOB时钟使能
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
        //初始化PA0
        GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
        GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;//输出
        GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;//推挽模式
        GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;//没有上拉下拉
        GPIO_Init(GPIOA, &GPIO_InitStruct);
        //初始化PB0
        GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
        GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;//输入
        GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_DOWN;//下拉(也就是PB0不接3.3V的时候是0)
        GPIO_Init(GPIOB, &GPIO_InitStruct);
    }
    
    
    
    int main()
    {
        u8 b = 0x0f;//流水灯初始为000011110为灭,1为亮)
        GPIO_Init();
        while (1) {
            if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0) == 1) //读取PB0,如果为1(即接了3.3V)
            {
                if (b&0x80)//如果b的最高位为1
                {
                    b <<= 1;//b的各位左移1位
                    b |= 0x01;//b的最低位置1(举例:当b为11110000,则b先变为11100000,然后变为11100001.实现循环)
                }
                else 
                {
                    b <<= 1;
                }
                if (b&0x80)//如果移位后最高位为1
                {
                    GPIO_SetBits(GPIOA, GPIO_Pin_7);//PA7亮灯
                }
                else//否则
                {
                    GPIO_ResetBits(GPIOA, GPIO_Pin_7);//PA7灭灯
                }
                if (b & 0x40)//如果移位后第6位为1
                {
                    GPIO_SetBits(GPIOA, GPIO_Pin_6);//PA6亮灯
                }
                else//否则
                {
                    GPIO_ResetBits(GPIOA, GPIO_Pin_6);//PA6灭灯
                }
                if (b & 0x20)//如果移位后第5位为1
                {
                    GPIO_SetBits(GPIOA, GPIO_Pin_5);//PA5亮灯
                }
                else//否则
                {
                    GPIO_ResetBits(GPIOA, GPIO_Pin_5);//PA5灭灯
                }
                if (b & 0x10)//如果移位后第4位为1
                {
                    GPIO_SetBits(GPIOA, GPIO_Pin_4);//PA4亮灯
                }
                else//否则
                {
                    GPIO_ResetBits(GPIOA, GPIO_Pin_4);//PA4灭灯
                }
                if (b & 0x08)//如果移位后第3位为1
                {
                    GPIO_SetBits(GPIOA, GPIO_Pin_3);//PA3亮灯
                }
                else//否则
                {
                    GPIO_ResetBits(GPIOA, GPIO_Pin_3);//PA3灭灯
                }
                if (b & 0x04)//如果移位后第2位为1
                {
                    GPIO_SetBits(GPIOA, GPIO_Pin_2);//PA2亮灯
                }
                else//否则
                {
                    GPIO_ResetBits(GPIOA, GPIO_Pin_2);//PA2灭灯
                }
                if (b & 0x02)//如果移位后第1位为1
                {
                    GPIO_SetBits(GPIOA, GPIO_Pin_1);//PA1亮灯
                }
                else//否则
                {
                    GPIO_ResetBits(GPIOA, GPIO_Pin_1);//PA1灭灯
                }
                if (b & 0x01)//如果移位后第0位为1
                {
                    GPIO_SetBits(GPIOA, GPIO_Pin_0);//PA0亮灯
                }
                else//否则
                {
                    GPIO_ResetBits(GPIOA, GPIO_Pin_0);//PA0灭灯
                }
            }
            for (int i = 0; i < 60000; i++)//延时
            {
                for (int j = 0; j < 200; j++);
            }
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月20日
  • 已采纳回答 11月12日
  • 赞助了问题酬金 11月12日
  • 创建了问题 11月11日

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line