stm32f103串口1串口2收发程序
来源:互联网 发布:c语言对数函数怎么表示 编辑:程序博客网 时间:2024/05/17 07:20
#include "stm32f10x.h"
#include "string.h"
#include "stdio.h"
void delay(void);
void GPIO_Configuration(void);
void uart_init();
extern void USART_OUT(USART_TypeDef* USARTx, uint16_t *Data,...);
void RCC_Configuration(void)
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|
RCC_APB2Periph_GPIOA|
RCC_APB2Periph_GPIOB|
RCC_APB2Periph_GPIOD |
RCC_APB2Periph_AFIO|
RCC_APB2Periph_GPIOC,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn|USART2_IRQn; //ÉèÖô®¿Ú1ÖжÏ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //ÇÀÕ¼ÓÅÏȼ¶ 0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //×ÓÓÅÏȼ¶Îª0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜ
NVIC_Init(&NVIC_InitStructure);
}
void UART_PutChar(USART_TypeDef* USARTx, uint8_t Data)
{
USART_SendData(USARTx, Data);
while(USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET){}
}
void UART_PutStr (USART_TypeDef* USARTx, uint8_t *str)
{
while (0 != *str)
{
UART_PutChar(USARTx, *str);
str++;
}
}
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
NVIC_Configuration();
uart_init();
while(1)
{
GPIO_ResetBits(GPIOC,GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_13);
delay();
GPIO_SetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_8|GPIO_Pin_13);
delay();
GPIO_ResetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_8|GPIO_Pin_13);
delay();
GPIO_SetBits(GPIOC,GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_13);
delay();
//USART_SendData(USART1, '1');
UART_PutStr(USART1,(uint8_t*)("234"));
UART_PutStr(USART2,(uint8_t*)("234"));
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_2; //USART1 TX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_Init(GPIOA, &GPIO_InitStructure); //A¶Ë¿Ú
/* Configure USART2 Rx (PA.03) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_3; //USART1 RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //¸´ÓÿªÂ©ÊäÈë
GPIO_Init(GPIOA, &GPIO_InitStructure); //A¶Ë¿Ú
}
void uart_init()
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200;
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_Tx | USART_Mode_Rx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //ʹÄܽÓÊÕÖжÏ
USART_ITConfig(USART1, USART_IT_TXE, ENABLE); //ʹÄÜ·¢ËÍ»º³å¿ÕÖжÏ
USART_Init(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //ʹÄܽÓÊÕÖжÏ
USART_ITConfig(USART2, USART_IT_TXE, ENABLE); //ʹÄÜ·¢ËÍ»º³å¿ÕÖжÏ
USART_Cmd(USART1, ENABLE);
USART_Cmd(USART2, ENABLE);
}
void USART1_IRQHandler(void) //´®¿Ú1 ÖжϷþÎñ³ÌÐò
{
static u8 RX_dat[256]={0};static int i;
if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)//USART_IT_RXNE£º½ÓÊÕÖжÏ
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
RX_dat[i++]=USART_ReceiveData(USART1);
}
if(RX_dat[0]=='{' && RX_dat[i-1]=='}')
{
//UART_PutStr(USART2,"fdg");
}
}
void USART2_IRQHandler(void) //´®¿Ú2 ÖжϷþÎñ³ÌÐò
{
unsigned int i;
}
0 0
- stm32f103串口1串口2收发程序
- stm32f103串口程序
- linux串口收发程序
- 串口收发程序
- 2串口收发数据
- stm32串口1收发
- 串口收发数据1
- STM32F103程序设计-9-USB转TTL串口(收发)
- MSP430F149串口收发程序详解
- STM32F103程序串口调用printf打印数据
- STM32F103串口1和串口2不同波特率之间交换数据问题
- stm32f103的串口调试
- stm32f103串口IAP实验
- Delphi+MSComm控件串口收发程序
- 51单片机(AT89C52)串口收发程序
- STM32F030学习之串口收发程序
- [CSR8] 分享一个CSR8670串口收发程序
- STM32F103学习1:由开发软件MDK4.12生成HEX文件与程序下载(串口)
- Ajax传递json数据
- 经典查找算法
- JAVA实现特定范围的完数输出
- 一些感悟
- 单例设计模式的练习
- stm32f103串口1串口2收发程序
- 获取图像像素方法汇总
- strurts2总结
- vim 保存utf-8文件为gbk
- Mybatis结果集自动映射
- uva536 -- Tree Recovery
- js运动功能的封装
- 线性图形混合
- 题目1028:继续畅通工程