完成:从终端输入选项,完成点灯关灯,打开风扇关闭风扇等操作
#include "gpio.h"
int main()
{char a;//char buf[128];uart4_config();gpio_config();while(1){//接收一个字符数据a = getchar();//发送接收的字符putchar(a);switch(a){case '1':{light1_on();}break;case '2':{light2_on();}break;case '3':{light3_on();}break;case '4':{fans_on();}break;case '0':{light_off();}break;}}return 0;
}
#include "gpio.h"void uart4_config(){//使能GPIOB\GPIOG\UART4外设时钟RCC->MP_AHB4ENSETR |= (0x1<<6); //gpioBRCC->MP_AHB4ENSETR |= (0x1<<1); //gpioGRCC->MP_APB4ENSETR |= (0x1<<16); //uart4//设置PB2、PG11用于UART4管脚复用GPIOB->MODER &=~(0x3<<4);GPIOB->MODER |= (0x2<<4);GPIOB->AFRL &=~(0xf<<8);GPIOB->AFRL |= (0x8<<8);GPIOG->MODER &= ~(0x3<<22);GPIOG->MODER |= (0x2<<22);GPIOG->AFRH &=~(0xf<<12);GPIOG->AFRH |= (0x6<<12);//设置串口禁用USART4->CR1 &= ~(0x1);//设置数据位宽为8位USART4->CR1 &=(~(0x1<<12));USART4->CR1 &=(~(0x1<<28));//设置无奇偶校验位USART4->CR1 &=(~(0x1<<10));//设置16倍过采样USART4->CR1 &=(~(0x1<<15));//设置1位停止位USART4->CR2 &=(~(0x3<<12));//设置不分频USART4->PRESC &= (~0xf);//设置波特率为115200USART4->BRR = 0x22B;//使能发送器USART4->CR1 |= (0x1<<3);//使能接收器USART4->CR1 |= (0x1<<2);//使能串口USART4->CR1 |= (0x1);}void putchar(char a)
{while(!(USART4->ISR &(0x1<<7)));USART4->TDR=a;while(!(USART4->ISR &(0x1<<6)));
}char getchar()
{char a;while(!(USART4->ISR &(0x1<<5)));a=USART4->RDR;return a;
}void puts(char *s)
{while(*s){putchar(*s);s++;}putchar('\r');putchar('\n');
}void gets(char *s)
{while(1){*s = getchar();putchar(*s);if(*s == 'r')break;s++;}*s = '\0';putchar('\n');
}void gpio_config()
{RCC_GPIO |=(0x3<<4);GPIOE->MODER &=(~(0x3<<20));GPIOE->MODER |=(0x1<<20);GPIOE->OTYPER &=(~(0x1<<10));GPIOE->OSPEEDR &=(~(0x3<<20));GPIOE->PUPDR &=(~(0x3<<20));GPIOF->MODER &=(~(0x3<<20));GPIOF->MODER |=(0x1<<20);GPIOF->OTYPER &=(~(0x1<<10));GPIOF->OSPEEDR &=(~(0x3<<20));GPIOF->PUPDR &=(~(0x3<<20));GPIOE->MODER &=(~(0x3<<16));GPIOE->MODER |=(0x1<<16);GPIOE->OTYPER &=(~(0x1<<8));GPIOE->OSPEEDR &=(~(0x3<<16));GPIOE->PUPDR &=(~(0x3<<16));GPIOE->MODER &=(~(0x3<<18));GPIOE->MODER |=(0x1<<18);GPIOE->OTYPER &=(~(0x1<<9));GPIOE->OSPEEDR &=(~(0x3<<18));GPIOE->PUPDR &=(~(0x3<<18));}void light1_on()
{GPIOE->ODR |= (0x1<<10);
}void light2_on()
{GPIOF->ODR |= (0x1<<10);
}void light3_on()
{GPIOE->ODR |= (0x1<<8);
}void fans_on()
{GPIOE->ODR |= (0x1<<9);
}void light_off()
{GPIOE->ODR &= (~(0x1<<10));GPIOF->ODR &= (~(0x1<<10));GPIOE->ODR &= (~(0x1<<8));GPIOE->ODR &= (~(0x1<<9));
}
#include "gpio.h"void uart4_config(){//使能GPIOB\GPIOG\UART4外设时钟RCC->MP_AHB4ENSETR |= (0x1<<6); //gpioBRCC->MP_AHB4ENSETR |= (0x1<<1); //gpioGRCC->MP_APB4ENSETR |= (0x1<<16); //uart4//设置PB2、PG11用于UART4管脚复用GPIOB->MODER &=~(0x3<<4);GPIOB->MODER |= (0x2<<4);GPIOB->AFRL &=~(0xf<<8);GPIOB->AFRL |= (0x8<<8);GPIOG->MODER &= ~(0x3<<22);GPIOG->MODER |= (0x2<<22);GPIOG->AFRH &=~(0xf<<12);GPIOG->AFRH |= (0x6<<12);//设置串口禁用USART4->CR1 &= ~(0x1);//设置数据位宽为8位USART4->CR1 &=(~(0x1<<12));USART4->CR1 &=(~(0x1<<28));//设置无奇偶校验位USART4->CR1 &=(~(0x1<<10));//设置16倍过采样USART4->CR1 &=(~(0x1<<15));//设置1位停止位USART4->CR2 &=(~(0x3<<12));//设置不分频USART4->PRESC &= (~0xf);//设置波特率为115200USART4->BRR = 0x22B;//使能发送器USART4->CR1 |= (0x1<<3);//使能接收器USART4->CR1 |= (0x1<<2);//使能串口USART4->CR1 |= (0x1);}void putchar(char a)
{while(!(USART4->ISR &(0x1<<7)));USART4->TDR=a;while(!(USART4->ISR &(0x1<<6)));
}char getchar()
{char a;while(!(USART4->ISR &(0x1<<5)));a=USART4->RDR;return a;
}void gpio_config()
{RCC_GPIO |=(0x3<<4);GPIOE->MODER &=(~(0x3<<20));GPIOE->MODER |=(0x1<<20);GPIOE->OTYPER &=(~(0x1<<10));GPIOE->OSPEEDR &=(~(0x3<<20));GPIOE->PUPDR &=(~(0x3<<20));GPIOF->MODER &=(~(0x3<<20));GPIOF->MODER |=(0x1<<20);GPIOF->OTYPER &=(~(0x1<<10));GPIOF->OSPEEDR &=(~(0x3<<20));GPIOF->PUPDR &=(~(0x3<<20));GPIOE->MODER &=(~(0x3<<16));GPIOE->MODER |=(0x1<<16);GPIOE->OTYPER &=(~(0x1<<8));GPIOE->OSPEEDR &=(~(0x3<<16));GPIOE->PUPDR &=(~(0x3<<16));GPIOE->MODER &=(~(0x3<<18));GPIOE->MODER |=(0x1<<18);GPIOE->OTYPER &=(~(0x1<<9));GPIOE->OSPEEDR &=(~(0x3<<18));GPIOE->PUPDR &=(~(0x3<<18));}void light1_on()
{GPIOE->ODR |= (0x1<<10);
}void light2_on()
{GPIOF->ODR |= (0x1<<10);
}void light3_on()
{GPIOE->ODR |= (0x1<<8);
}void fans_on()
{GPIOE->ODR |= (0x1<<9);
}void light_off()
{GPIOE->ODR &= (~(0x1<<10));GPIOF->ODR &= (~(0x1<<10));GPIOE->ODR &= (~(0x1<<8));GPIOE->ODR &= (~(0x1<<9));
}