高级会员
- 积分
- 541
- 金钱
- 541
- 注册时间
- 2011-10-18
- 在线时间
- 9 小时
|
发表于 2011-10-18 11:04:04
|
显示全部楼层
#include "stm32f10x.h"
#include "SysTickDelay.h"
u8 *a = "suqingxiao";
void Uart1_Init(void);
void Uart1_PutChar(u8 ch);
void Uart1_PutString(u8 *buf , u8 len);
void USART1_Configuration(long Baud)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA |
RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
//TX
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);
//RX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = Baud;
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;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // 指定抢占式优先级别1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//发送一个字符
void Uart1_PutChar(u8 ch)
{
USART_SendData(USART1, ch);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
//发送一个字符串
void Uart1_PutString(u8 *buf , u8 len)
{
u8 i;
for (i = 0; i<len; i++)
{
Uart1_PutChar(*buf++);
}
}
int main(void)
{
SystemInit();
SysTick_Initaize();
USART1_Configuration(9600);
while (1)
{
Uart1_PutString(a,10);
delay_ms(500);
}
}
void USART1_IRQHandler(void)
{
u8 tab[8];
u8 i = 0;
u8 *p_uart1;
p_uart1 = tab;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)//接收中断
{
USART_ClearITPendingBit(USART1, USART_IT_RXNE); //清中断
tab = USART_ReceiveData(USART1); //接收数据
//Uart1_PutChar(tab);
Uart1_PutString(p_uart1 , 1);
i++;
p_uart1++;
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)//等待接收完成标志位
{
}
}
}
> |
|