历史上的今天
今天是:2025年08月19日(星期二)
2021年08月19日 | stm32串口点灯
2021-08-19 来源:eefocus
串口程序:
hal.h
#ifndef HAL_H
#define HAL_H
//输出宏定义
//清零
#define LED1_OFF GPIO_ResetBits(GPIOA, GPIO_Pin_8)
//置一
#define LED1_ON GPIO_SetBits(GPIOA, GPIO_Pin_8)
#define LED2_OFF GPIO_ResetBits(GPIOA, GPIO_Pin_7)
#define LED2_ON GPIO_SetBits(GPIOA, GPIO_Pin_7)
#define LED3_OFF GPIO_ResetBits(GPIOC, GPIO_Pin_7)
#define LED3_ON GPIO_SetBits(GPIOC, GPIO_Pin_7)
#define LED4_OFF GPIO_ResetBits(GPIOC, GPIO_Pin_5)
#define LED4_ON GPIO_SetBits(GPIOC, GPIO_Pin_5)
#define LED5_OFF GPIO_ResetBits(GPIOB, GPIO_Pin_9)
#define LED5_ON GPIO_SetBits(GPIOB, GPIO_Pin_9)
#define LED6_OFF GPIO_ResetBits(GPIOB, GPIO_Pin_8)
#define LED6_ON GPIO_SetBits(GPIOB, GPIO_Pin_8)
#define LED7_OFF GPIO_ResetBits(GPIOB, GPIO_Pin_5)
#define LED7_ON GPIO_SetBits(GPIOB, GPIO_Pin_5)
#define LED8_OFF GPIO_ResetBits(GPIOB, GPIO_Pin_0)
#define LED8_ON GPIO_SetBits(GPIOB, GPIO_Pin_0)
//硬件初始化
extern void ChipHalInit(void);
extern void ChipOutHalInit(void);
//串口
extern void USART1_Putc(u8 c);
extern void USART1_Puts(char * str);
extern void USART2_Putc(u8 c);
extern void USART2_Puts(char * str);
extern u8 Uart1_Get_Flag;
extern u8 Uart1_Get_Data;
#endif
GPIO.c
#include "STM32Libstm32f10x.h"
/*******************************************************************************
* Function Name : GPIO_Configuration
* 设置PD3,PD4,PD5,PD6为键盘输入
* 设置PB0,5,8,9; PC5,7; PA7 ;PA8 为输出LED灯
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*允许总线CLOCK,在使用GPIO之前必须允许相应端的时钟.
从STM32的设计角度上说,没被允许的端将不接入时钟,也就不会耗能,
这是STM32节能的一种技巧,*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
/* PB0,5,8,9输出 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_5|GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //开漏输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M时钟速度
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* PC5,7输出*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //开漏输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M时钟速度
GPIO_Init(GPIOC, &GPIO_InitStructure);
/*PA7,输出*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //开漏输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M时钟速度
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*PA8,输出*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //开漏输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M时钟速度
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
USART.c
#include "STM32Libstm32f10x.h"
/**********************************************
**串口配置函数,这里使能了两个串口,其中串口2使用了中断接收模式
**
**********************************************/
u8 Uart1_Get_Flag; //串口1接收到数据标志
u8 Uart1_Get_Data; //串口1接收的数据
void USART_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
//使能串口1,PA,AFIO总线
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |
RCC_APB2Periph_AFIO |
RCC_APB2Periph_USART1 ,
ENABLE);
/* A9 USART1_Tx */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽输出-TX
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* A10 USART1_Rx */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入-RX
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
USART_ClockInit(USART1, &USART_ClockInitStructure);
USART_Init(USART1, &USART_InitStructure);
/* Enable the USARTx */
USART_Cmd(USART1, ENABLE);
//串口1使用接收中断
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
//使能串口2时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
// A2 做T2X
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// A3 做R2X
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
USART_ClockInit(USART2, &USART_ClockInitStructure);
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
//串口2使用接收中断
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
}
void USART1_Putc(unsigned char c)
{
USART_SendData(USART1, c);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET );
}
void USART1_Puts(char * str)
{
while(*str)
{
USART_SendData(USART1, *str++);
/* Loop until the end of transmission */
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
}
void USART2_Putc(unsigned char c)
{
USART_SendData(USART2, c);
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET );
}
void USART2_Puts(char * str)
{
while(*str)
{
USART_SendData(USART2, *str++);
/* Loop until the end of transmission */
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
}
}
RCC.c
#include "STM32Libstm32f10x.h"
RCC_ClocksTypeDef RCC_ClockFreq;
void RCC_Configuration(void)
{
SystemInit();//源自system_stm32f10x.c文件,只需要调用此函数,则可完成RCC的配置.具体请看2_RCC
/**************************************************
获取RCC的信息,调试用
请参考RCC_ClocksTypeDef结构体的内容,当时钟配置完成后,
里面变量的值就直接反映了器件各个部分的运行频率
***************************************************/
RCC_GetClocksFreq(&RCC_ClockFreq);
/* 这个配置可使外部晶振停振的时候,产生一个NMI中断,不需要用的可屏蔽掉*/
//RCC_ClockSecuritySystemCmd(ENABLE);
}
NVIC.c
#include "STM32Libstm32f10x.h"
//设置所有的中断允许
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/*UART1*/
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
main.c
/************************************************************
**实验名称:UART
**功能:串口实验,串口1配置为接收中断
通过串口发送数字给stm32点亮相应的LED灯
**注意事项:
**作者:电子白菜
*************************************************************/
#include "STM32Libstm32f10x.h"
#include "hal.h"
void delay(u32 z)
{
while(z--);
}
int main(void)
{
// u8 uart1_get_data;
ChipHalInit(); //片内硬件初始化
ChipOutHalInit(); //片外硬件初始化
for(;;)
{
//串口1使用中断方式置位标志
if(Uart1_Get_Flag)
{
Uart1_Get_Flag=0;
USART1_Puts("rn GET CMD:");
USART1_Putc(Uart1_Get_Data);
USART1_Puts("rn");
switch(Uart1_Get_Data)
{
case '1':{LED1_ON;LED2_OFF;LED3_OFF;LED4_OFF;LED5_OFF;LED6_OFF;LED7_OFF;LED8_OFF;} break;
case '2':{LED2_ON;LED1_OFF;LED3_OFF;LED4_OFF;LED5_OFF;LED6_OFF;LED7_OFF;LED8_OFF;} break;
case '3':{LED3_ON;LED1_OFF;LED2_OFF;LED4_OFF;LED5_OFF;LED6_OFF;LED7_OFF;LED8_OFF;} break;
case '4':{LED4_ON;LED1_OFF;LED2_OFF;LED3_OFF;LED5_OFF;LED6_OFF;LED7_OFF;LED8_OFF;} break;
case '5':{LED5_ON;LED1_OFF;LED2_OFF;LED3_OFF;LED4_OFF;LED6_OFF;LED7_OFF;LED8_OFF;} break;
case '6':{LED6_ON;LED1_OFF;LED2_OFF;LED3_OFF;LED4_OFF;LED5_OFF;LED7_OFF;LED8_OFF;} break;
case '7':{LED7_ON;LED1_OFF;LED2_OFF;LED3_OFF;LED4_OFF;LED5_OFF;LED6_OFF;LED8_OFF;} break;
case '8':{LED8_ON;LED1_OFF;LED2_OFF;LED3_OFF;LED4_OFF;LED5_OFF;LED6_OFF;LED7_OFF;} break;
case '0':{LED1_OFF;LED2_OFF;LED3_OFF;LED4_OFF;LED5_OFF;LED6_OFF;LED7_OFF;LED8_OFF;} break;
}
}
//USART1_Puts("Test Ok!rn");
//delay(3000000);
}
}
stm32f10x_it.c
/******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
* stm32f10x_it.c
* 所有中断响应函数,一般如果中断比较简易,则直接在此编写,否则
* 可调用HAL模块中各自对应的处理函数处理中断。
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "STM32Libstm32f10x.h"
/* Private typedef -----------------------------------------------------------*/
上一篇:stm32入门系列程序
史海拾趣
|
自动跟踪技术是在智能识别的基础上,对图像进行差分计算,自动识别视觉范围内目标的运动方向,并自动控制云台对移动目标进行跟踪目标在进入智能高速球的范围到离开的这段时间内,通过所配置的高清晰自动变焦镜头,使所有动作都被清晰地传送到监控中 ...… 查看全部问答> |
|
vs2005可以编写在wince下运行的程序吗?或求evc4的10位序列号 各位大虾好 本来是要用evc4做的,可现在evc4的序列号是10位的,不是25位的,所以根本安装不了 听说vs2005也可以?请大家帮助~~~… 查看全部问答> |
|
wince 5中 dialog-based的APP 怎样实现窗口最小化? wince control panel中的那些 applet都是 dialog based的app,但是可以通过tasbar来实现窗口隐藏 我自己通过EVC 的app wizard做的一个dialog based app,点击taskbar上的task,不能实现dialog隐藏 应该怎么做?… 查看全部问答> |
|
MTK6225 手机开发板(含源代码) 有诚意者请联系 Qq:296662705 硬件特性: 1、 CPU:mtk6225 2、 TFLASH 卡接口 3、 176X220 2.6寸TFT LCD,最高支持320*240*16的TFT LCD 4、 4线触摸屏接口 5、 双声道音频输出 6、 一个Camera 130万像素 ...… 查看全部问答> |




