當(dāng)前位置:首頁 > 單片機(jī) > 單片機(jī)
[導(dǎo)讀]//代碼僅僅是配置使能串口和相應(yīng)的中斷處理函數(shù),具體功能需要自己定義//IAR 7.1#include "stm32_uart.h"uint8_t card_ture=0;uint16_t add_count=0;uint8_t Key_Data[1]={0};uint8_t Media_Flag=0;uint8_t card_cod

//代碼僅僅是配置使能串口和相應(yīng)的中斷處理函數(shù),具體功能需要自己定義

//IAR 7.1

#include "stm32_uart.h"

uint8_t card_ture=0;
uint16_t add_count=0;
uint8_t Key_Data[1]={0};
uint8_t Media_Flag=0;

uint8_t card_code[4]; //[5:1]save the card SN, [0]valid or invalid
uint8_t Temp_Card_Num[4];
uint8_t cardcode_rx_complete = 0;

uint32_t uart_config_record_map[4] = {
0,
UART_CONFIG_UART_MEMORY_ADDR,
FIRMWARE_VERSION_MEMORY_ADDR,
SCHEME_SPECIFIC_MEMORY_ADDR
};

UartConfig_TypeDef the_uart_config;

void stm32_uart_init(uint8_t uart_port, uint32_t BaudRate)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

/*Fill the structure of uart init*/
if(uart_port == UART2)
{
/*Fill the structure of uart init*/
USART_InitStructure.USART_BaudRate = BaudRate;
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;
/*Enable priph clock*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);


/*Config gpio as uart pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);

/*Config gpio as uart pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);

/*Init uart*/
USART_Init(USART2, &USART_InitStructure);
/*Enable uart*/
/*Clear flags*/
USART_ClearFlag(USART2, USART_FLAG_TC);

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
}
else if(uart_port == UART1)
{
/*Enable priph clock*/

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);

/*Fill the structure of uart init*/
USART_InitStructure.USART_BaudRate = BaudRate;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
if(the_uart_config.checksum==Parity_No)
{
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //USART_WordLength_8b
USART_InitStructure.USART_Parity = USART_Parity_No; //USART_Parity_No
}
else if(the_uart_config.checksum==Parity_Odd)
{
USART_InitStructure.USART_WordLength = USART_WordLength_9b; //USART_WordLength_9b
USART_InitStructure.USART_Parity = USART_Parity_Odd; //USART_Parity_Odd
}
else if(the_uart_config.checksum==Parity_Even)
{
USART_InitStructure.USART_WordLength = USART_WordLength_9b; //USART_WordLength_9b
USART_InitStructure.USART_Parity = USART_Parity_Even; //USART_Parity_Even
}
else
{

}
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

/*Config gpio as uart pin */
GPIO_InitStructure.GPIO_Pin = UART1_TX;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);

/*Init uart*/
USART_Init(USART1, &USART_InitStructure);
/*Enable uart*/
//USART_Cmd(USART1, ENABLE);
/*Clear flags*/
USART_ClearFlag(USART1, USART_FLAG_TC);
#if 0
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//USART_ITConfig(USART1, USART_IT_RXNE | USART_IT_TXE, ENABLE);
#endif
USART_Cmd(USART1, ENABLE);
}
}


static void stm32_uart_senddatas(uint8_t uart_port, const uint8_t* data, uint16_t len)
{
uint16_t count;
if(uart_port == UART1)
{
for(count = 0; count < len; count++)
{
USART_SendData(USART1, data[count]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
}
else if(uart_port == UART2)
{
for(count = 0; count < len; count++)
{
USART_SendData(USART2, data[count]);
while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
}
}
}

static uint8_t* uart1_tx_buffer;
static uint8_t uart1_tx_buffer_len;
static uint8_t uart1_tx_buffer_index = 0;
void stm32_uart_pro_poweron(ProComm_TypeDef comm)
{
USART_ClearFlag(USART1,USART_FLAG_TC); //清除標(biāo)志位 否則第一位數(shù)據(jù)丟失
switch(comm)

{

}
//USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
//USART_Cmd(USART1, ENABLE);
}

void stm32_uart2_senddata(uint8_t uart_port, const uint8_t* data, uint16_t len)
{
USART_ClearFlag(USART2,USART_FLAG_TC); //清除標(biāo)志位 否則第一位數(shù)據(jù)丟失
stm32_uart_senddatas(uart_port, data,len);
}

void USART1_IRQHandler(void)
{

if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
{
USART_ClearITPendingBit(USART1, USART_IT_TXE);
/* Write one byte to the transmit data register */
USART_SendData(USART1, uart1_tx_buffer[uart1_tx_buffer_index++]);

if(uart1_tx_buffer_index == uart1_tx_buffer_len)
{
/* Disable the USARTz Transmit interrupt */
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
uart1_tx_buffer_index = 0;
}
}
}
/*
void USART2_IRQHandler(void)
{
static uint8_t index = 1;
static uint8_t checksum = 0;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
card_code[index] = USART_ReceiveData(USART2);
if(index < 5)
{
checksum ^= card_code[index++];
}
else
{
if(checksum == card_code[5])
{

}
else
{
}
cardcode_rx_complete = 1;
checksum = 0;
}
}
}
*/

void USART2_IRQHandler(void)
{
uint8_t index=0;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
Key_Data[index] = USART_ReceiveData(USART2);
switch(Key_Data[0])
{

}
}
//if(USART_GetITStatus(USART2, USART_IT_TXE) != RESET)
//{
// USART_ClearITPendingBit(USART2, USART_IT_TXE);
//}
}

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或?qū)⒋呱龈蟮莫?dú)角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

倫敦2024年8月29日 /美通社/ -- 英國汽車技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認(rèn)證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時(shí)1.5...

關(guān)鍵字: 汽車 人工智能 智能驅(qū)動(dòng) BSP

北京2024年8月28日 /美通社/ -- 越來越多用戶希望企業(yè)業(yè)務(wù)能7×24不間斷運(yùn)行,同時(shí)企業(yè)卻面臨越來越多業(yè)務(wù)中斷的風(fēng)險(xiǎn),如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報(bào)道,騰訊和網(wǎng)易近期正在縮減他們對(duì)日本游戲市場(chǎng)的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)開幕式在貴陽舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機(jī) 衛(wèi)星通信

要點(diǎn): 有效應(yīng)對(duì)環(huán)境變化,經(jīng)營業(yè)績穩(wěn)中有升 落實(shí)提質(zhì)增效舉措,毛利潤率延續(xù)升勢(shì) 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競(jìng)爭力 堅(jiān)持高質(zhì)量發(fā)展策略,塑強(qiáng)核心競(jìng)爭優(yōu)勢(shì)...

關(guān)鍵字: 通信 BSP 電信運(yùn)營商 數(shù)字經(jīng)濟(jì)

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺(tái)與中國電影電視技術(shù)學(xué)會(huì)聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會(huì)上宣布正式成立。 活動(dòng)現(xiàn)場(chǎng) NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會(huì)上,軟通動(dòng)力信息技術(shù)(集團(tuán))股份有限公司(以下簡稱"軟通動(dòng)力")與長三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉