当前位置:首页 > 单片机 > 单片机
[导读]//代码仅仅是配置使能串口和相应的中断处理函数,具体功能需要自己定义//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

//代码仅仅是配置使能串口和相应的中断处理函数,具体功能需要自己定义

//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); //清除标志位 否则第一位数据丢失
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); //清除标志位 否则第一位数据丢失
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);
//}
}

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

中断机制在单片机及嵌入式系统中是重中之重,我们必须深入理解。首先我们要明白一点:CPU执行指令代码,并非一直顺序地逐条执行,而是可能突然跳到某段代码上去的。因为这段代码的优先级更高,或者说它更加紧迫,CPU必须暂时放下手...

关键字: 中断 单片机 嵌入式系统

PIC单片机是基于RISC系统结构的单片机,最初的设计是支持PDP(编程数据处理器)计算机。大量的操作可以用来控制外围设备。PIC单片机比微控制器具有更快的程序执行能力。它是由微芯片技术公司于1889年发明的,是一种8位...

关键字: PIC单片机 定时器 中断

STC单片机是一款增强型51单片机,完全兼容MCS-51,还增加了新的功能,比如新增两级中断优先级,多一个外中断,内置EEPROM,硬件看门狗,具有掉电模式,512B内存等。还支持ISP下载,不用编程器,只要一个MAX2...

关键字: 单片机 看门狗 中断

什么是PIC中断程序呢?形象的生活比喻就比如你现在这在看我的文章,突然你的朋友喊你一起去烤地瓜,这时候你就中断了看文章和朋友烤地瓜去了,烤完地瓜之后你又回来看文章。烤地瓜这件事就好比中断程序,他中断了你看文章这件事。在程...

关键字: PIC 中断 标志位

MCS—51系列单片机内部只需两个外部接中断连源输入端,当外部接连源多于两个时,就必须进行拓宽,下面介绍几种简略的拓宽办法:

关键字: 单片机 软件 中断

最近在浏览星球主题的时候,发现一位球友打卡中断了,就去关心了一下他(瞧我这该死的责任心❤️),结果被他反手就问了下面这个问题。

关键字: 中断 Spring Boot 实战项目

实质上是加一的计数器。外部脉冲让他不断加一,当计数器溢出时,计数器的溢出相应的中断标志位置1,并向CPU发出中断请求。如果定时器/计数器工作为定时模式,则表示时间以到;如果工作于计数模式,则表示计数器、则表示计数值已满。

关键字: 周期 中断

中断的分类和区别

关键字: 轮询 中断

在这篇文章中,小编将为大家带来微控制器的相关报道。

关键字: 微控制器 中断 控制器

TMOD : 控制定时器的工作方式。8个bit,高四位 bit 控制 T1,、低四位 bit 控制 T0。因为定时器有4种工作方式;TMOD = 0x00(工作方式0),TMOD = 0x01(工作方式0),TMOD =...

关键字: 51单片机 定时器 中断
关闭
关闭