中级会员
 
- 积分
- 276
- 金钱
- 276
- 注册时间
- 2010-12-14
- 在线时间
- 5 小时
|
发表于 2013-6-5 12:00:56
|
显示全部楼层
你先用软件仿真,跑一下这个代码,打印窗口是否能打印出OK
如果能打印出OK,剩下的就只能你自己解决了,我现在手里没有板子。
#include "stm32f10x.h"
#include<stdio.h>
#if 1
#pragma import(__use_no_semihosting)
//标准库需要的支持函数
struct __FILE
{
int handle;
};
FILE __stdout;
//定义_sys_exit()以避免使用半主机模式
_sys_exit(int x)
{
x = x;
}
//重定义fputc函数
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);//循环发送,直到发送完毕
USART1->DR = (u8) ch;
return ch;
}
#endif
void uart_init()
{
GPIO_InitTypeDef GPIO_InitStructure; //定义GPIO的结构体名称
USART_InitTypeDef USART_InitStructure; //定义UART的结构体名称
NVIC_InitTypeDef NVIC_InitStructure; //定义NVIC的结构体名称
/* 使能USART1的时钟和GPIO的时钟(串口的发送PA9\接收用的是PA10)*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);
/* 对GPIO进行初始化,包括GPIO的引脚、速度和工作模式*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* 对NVIC进行初始化,包括入口、优先级、以及使能中断*/
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //设置中断入口
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//抢占优先级为2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //抢占子优先级为2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器USART1
/* 对USART进行初始化,包括波特率、字长、停止位、奇偶校验位、*/
USART_InitStructure.USART_BaudRate = 9600; //一般设置为9600;
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(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启中断
USART_Cmd(USART1, ENABLE); //使能串口
}
/*void USART1_IRQHandler(void) //串口1中断服务程序
{
unsigned char Res;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾)
{
Res =USART_ReceiveData(USART1);//(USART1->DR); //读取接收到的数据
USART_SendData(USART1, Res);
//printf("OK\n");
}
} */
int main()
{
uart_init();
while(1)
{
printf("OK\n");
}
} |
|