stm32读取目标芯片_STM32F103C8T6实现串口通信和读取芯片
/**************************************** 文件名:usart1.c* 描述:配置USART1* 实验平台:MINI STM32开发板 基于STM32F103C8T6* 硬件连接:------------------------*| PA9- USART1(Tx)|*| PA10 - USA...
/***************************************
* 文件名 :usart1.c
* 描述 :配置USART1
* 实验平台:MINI STM32开发板 基于STM32F103C8T6
* 硬件连接:------------------------
* | PA9 - USART1(Tx) |
* | PA10 - USART1(Rx) |
* ------------------------
* 库版本 :ST3.0.0
**********************************************************************************/
#include "usart1.h"
#include "misc.h"
#include
void USART1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStrue;
/* 使能 USART1 时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
/* USART1 使用IO端口配置 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOA
/* USART1 工作模式配置 */
USART_InitStructure.USART_BaudRate = 115200;//波特率设置:115200
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//数据位数设置:8位
USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位设置: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_Init(USART1, &USART_InitStructure); //初始化USART1
USART_Cmd(USART1, ENABLE);// USART1使能
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//④开启接收中断!!!!
NVIC_InitStrue.NVIC_IRQChannel=USART1_IRQn;;//串口1中断
NVIC_InitStrue.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStrue.NVIC_IRQChannelPreemptionPriority=1;// 抢占优先级为1
NVIC_InitStrue.NVIC_IRQChannelSubPriority=1;//子优先级位1
NVIC_Init(&NVIC_InitStrue);//④中断初始化函数
}
// /*发送一个字节数据*/
// void UART1SendByte(unsigned char SendData)
//{
// USART_SendData(USART1,SendData);
// while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
//}
///*接收一个字节数据*/
//unsigned char UART1GetByte(unsigned char* GetData)
//{
// if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
// { return 0;//没有收到数据
//}
// *GetData = USART_ReceiveData(USART1);
// return 1;//收到数据
//}
///*接收一个数据,马上返回接收到的这个数据*/
//void UART1Test(void)
//{
// unsigned char i = 0;
// while(1)
// {
// while(UART1GetByte(&i))
// {
// USART_SendData(USART1,i);
// }
// }
//}
/* 描述 :重定向c库函数printf到USART1*/
int fputc(int ch, FILE *f)
{
/* 将Printf内容发往串口 */
USART_SendData(USART1, (unsigned char) ch);
while (!(USART1->SR & USART_FLAG_TXE));
return (ch);
}
u32 ChipUniqueID[3];
/* 获取芯片ID */
void Get_ChipID(void)
{
ChipUniqueID[0] = *(__IO u32 *)(0X1FFFF7F0); // 高字节
ChipUniqueID[1] = *(__IO u32 *)(0X1FFFF7EC); //
ChipUniqueID[2] = *(__IO u32 *)(0X1FFFF7E8); // 低字节
}
void USART1_IRQHandler(void)
{
u8 res;
if(USART_GetITStatus(USART1,USART_IT_RXNE))//判断是否有接受中断!
{
//串口数据收发函数⑦
res= USART_ReceiveData(USART1);
USART_SendData(USART1,res);
}
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)