使用的是
STM32F107vc
芯片。LED灯共接3.3V的
电源。程序如下:
#include"stm32f10x.h"
#include "stdio.h"
#define BitBand(addr,n) *(vola
tile unsigned long *)((addr & 0xf0000000)+0x2000000+(addr & 0xfffff)*32+n*2)
#define GPIOA_ODR_A (GPIOA_BASE+0x0c)
#define GPIOA_IDR_A (GPIOA_BASE+0x08)
#define GPIOB_ODR_A (GPIOB_BASE+0x0c)
#define GPIOB_IDR_A (GPIOB_BASE+0x08)
#define GPIOC_ODR_A (GPIOC_BASE+0x0c)
#define GPIOC_IDR_A (GPIOC_BASE+0x08)
#define GPIOD_ODR_A (GPIOD_BASE+0x0c)
#define GPIOD_IDR_A (GPIOD_BASE+0x08)
#define GPIOE_ODR_A (GPIOE_BASE+0x0c)
#define GPIOE_IDR_A (GPIOE_BASE+0x08)
#define PAout(n) BitBand(GPIOA_ODR_A,n)
#define PAin(n) BitBand(GPIOA_IDR_A,n)
#define PBout(n) BitBand(GPIOB_ODR_A,n)
#define PBin(n) BitBand(GPIOB_IDR_A,n)
#define PCout(n) BitBand(GPIOC_ODR_A,n)
#define PCin(n) BitBand(GPIOC_IDR_A,n)
#define PDout(n) BitBand(GPIOD_ODR_A,n)
#define PDin(n) BitBand(GPIOD_IDR_A,n)
#define PEout(n) BitBand(GPIOE_ODR_A,n)
#define PEin(n) BitBand(GPIOE_IDR_A,n)
#define LED1 PDout(2)
#define LED2 PDout(3)
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void USART_Configuration(void){ USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate =9600; USART_InitStructure.USART_HardwareFlowControl =USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx |USART_Mode_Rx; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_WordLength =USART_WordLength_8b; USART_Init(USART1,&USART_InitStructure); USART_Cmd(USART1,ENABLE); }
void delay(__IO uint32_t nCount)
{
for (; nCount != 0; nCount--);
}
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3; /////选中之后相应的LED灯就会亮了,不用写0就亮了
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
USART_Configuration();
LED_Init();
while(1)
{
LED1=0; /////////////因为是共电源,所以写0灯亮。
LED2=1; //但是写1,灯并不会灭
//但是,如果换成GPIO_SetBits(GPIOD,GPIO_Pin_2); 和 GPIO_ResetBits(GPIOD,GPIO_Pin_3);就能实现亮灭
delay(5000000);
LED1=1;
LED1=0;
delay(5000000);
}
}
出现以上情况是为什么啊?不应该写0led灯亮,写1led等灭吗?向管脚不管是写1还是写0都实现不了led灭。。。。
一周热门 更多>