#if 1
#pragma import(__use_no_semihosting)
//標準庫需要的支持函式
struct __FILE
{
int handle;
};
FILE __stdout;
//定義_sys_exit()以避免使用半主機模式
void _sys_exit(int x)
{
x = x;
}
//重定義fputc函式
int fputc(int ch, FILE *f)
{
while((USART6->SR&0X40)==0);//回圈發送,直到發送完畢
USART6->DR = (u8) ch;
return ch;
}
#endif
void uart6_init(u32 bound)
{
//GPIO埠設定
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG,ENABLE); //使能GPIOG時鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6,ENABLE);//使能USART6時鐘
//串口6對應引腳復用映射
GPIO_PinAFConfig(GPIOG,GPIO_PinSource9,GPIO_AF_USART6); //GPIOG9復用為USART6
GPIO_PinAFConfig(GPIOG,GPIO_PinSource14,GPIO_AF_USART6); //GPIOG14復用為USART6
//USART6埠配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_14; //GPIOG9與GPIOG14
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//復用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽復用輸出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOG,&GPIO_InitStructure); //初始化
//USART6 初始化設定
USART_InitStructure.USART_BaudRate = bound;//波特率設定
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字長為8位資料格式
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_Init(USART6, &USART_InitStructure); //初始化串口6
USART_Cmd(USART6, ENABLE); //使能串口6
USART_ClearFlag(USART6, USART_FLAG_TC);
USART_ITConfig(USART6, USART_IT_RXNE, ENABLE);//開啟相關中斷
//Usart6 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;//串口6中斷通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//搶占優先級3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =2; //子優先級2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根據指定的引數初始化NVIC暫存器
}
void USART6_IRQHandler(void) //串口6中斷服務程式
{
u8 Res;
if(USART_GetITStatus(USART6, USART_IT_RXNE) != RESET) //接收中斷
{
Res =USART_ReceiveData(USART6);//(USART1->DR); //讀取接收到的資料
}
}
uj5u.com熱心網友回復:
bound 多少,是否接錯引腳?如果有其他初始化的代碼,先屏蔽uj5u.com熱心網友回復:
謝謝,這個找到問題了,我接線接的串口1的
uj5u.com熱心網友回復:
有時候確認容易犯這種小錯轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/13950.html
標籤:單片機/工控
