在线时间6 小时
UID101663
ST金币0
蝴蝶豆0
注册时间2008-6-28
初级会员
- 最后登录
- 1970-1-1
|
楼主 |
发表于 2012-6-26 11:10:26
|
显示全部楼层
a0a.1 0b0c
回复:【ST学习小组】STM32F103的CAN 通信之主控器
usart.c
#include "usart.h"
USART_InitTypeDef USART_InitStruct;
USART_ClockInitTypeDef USART_ClockInitStruct;
void USART1_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA , ENABLE);
//USART1 TX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure); //A端口
//USART1 RX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //复用开漏输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void USART1_NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //设置串口中断
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART_Config(USART_TypeDef* USARTx)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200; //速率115200bps
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; //收发模式
/* Configure USART1 */
USART_Init(USARTx, &USART_InitStructure); //配置串口参数函数
/* Enable USART1 Receive and Transmit interrupts */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //使能接收中断
//USART_ITConfig(USART1, USART_IT_TXE, ENABLE); //使能发送缓冲空中断
//USART_ITConfig(USART1, USART_IT_TC, ENABLE); //使能发送完成中断
/* Enable the USART1 */
USART_Cmd(USART1, ENABLE);
}
void USART_Init_Config(void)
{
USART1_GPIO_Config(); //端口配置
USART_Config(USART1); //数据格式配置
USART1_NVIC_Configuration(); //中断配置
}
void Usart_transmitByte(uint8_t Usart_TransmitDatebuf)
{
USART1->DR=Usart_TransmitDatebuf;
while((USART1->SR&0X40)==0);//wait
}
void Usart_TransmitData(u16 Usart_TransmitDataLength,uint8_t * Usart_TransmitDatebuf)
{
u16 Usart_Transmitindex;
//_____________________send the start byte_______________________
Usart_transmitByte(0xFF);
Usart_transmitByte(0x00);
// _____________________send the end byte________________________
for(Usart_Transmitindex=0;Usart_Transmitindex |
|