根据论坛上和网上的一些资料 实现了串口发送数据
代码:
//Header:stm32f10x_conf.h
//FIle Name: main.c
//Author:
//Data:
#include "stm32f10x_conf.h"
#include "stm32f10x.h"
#include <stdio.h>
int fputc(int ch, FILE *f);
void Uart1_PutChar(u8 ch);
/*************************************************
函数: void RCC_Configuration(void)
功能: 复位和时钟控制 配置
参数: 无
返回: 无
**************************************************/
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus; //定义外部高速晶体启动状态枚举变量
RCC_DeInit(); //复位RCC外部设备寄存器到默认值
RCC_HSEConfig(RCC_HSE_ON); //打开外部高速晶振
HSEStartUpStatus = RCC_WaitForHSEStartUp(); //等待外部高速时钟准备好
if(HSEStartUpStatus == SUCCESS) //外部高速时钟已经准别好
{
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //开启FLASH预读缓冲功能,加速FLASH的读取。所有程序中必须的用法.位置:RCC初始化子函数里面,时钟起振之后
FLASH_SetLatency(FLASH_Latency_2); //flash操作的延时
RCC_HCLKConfig(RCC_SYSCLK_Div1); //配置AHB(HCLK)时钟等于==SYSCLK
RCC_PCLK2Config(RCC_HCLK_Div1); //配置APB2(PCLK2)钟==AHB时钟
RCC_PCLK1Config(RCC_HCLK_Div2); //配置APB1(PCLK1)钟==AHB1/2时钟
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); //配置PLL时钟 == 外部高速晶体时钟 * 9 = 72MHz
RCC_PLLCmd(ENABLE); //使能PLL时钟
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) //等待PLL时钟就绪
{
}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //配置系统时钟 = PLL时钟
while(RCC_GetSYSCLKSource() != 0x08) //检查PLL时钟是否作为系统时钟
{
}
}
}
/*******************************************************************************
函数名:USART1_Configuration
输 入:
输 出:
功能说明:
初始化串口硬件设备,启用中断
配置步骤:
(1)打开GPIO和USART1的时钟
(2)设置USART1两个管脚GPIO模式
(3)配置USART1数据格式、波特率等参数
(4)使能USART1接收中断功能
(5)最后使能USART1功能
*/
void USART1_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* 第1步:打开GPIO和USART部件的时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/* 第2步:将USART Tx的GPIO配置为推挽复用模式 */
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);
/* 第3步:将USART Rx的GPIO配置为浮空输入模式
由于CPU复位后,GPIO缺省都是浮空输入模式,因此下面这个步骤不是必须的
但是,我还是建议加上便于阅读,并且防止其它地方修改了这个口线的设置参数
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* 第4步:配置USART1参数
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = 9600;
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);
/* 第5步:使能 USART1, 配置完毕 */
USART_Cmd(USART1, ENABLE);
/* 如下语句解决第1个字节无法正确发送出去的问题 */
USART_ClearFlag(USART1, USART_FLAG_TC); // 清标志
}
int main(void)
{
RCC_Configuration();
// NVIC_Configuration();
USART1_Configuration();
printf("\n\rEEPW ARMDIY WW 串口1测试程序\n");
while(1)
{
}
return 1;
}
int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (uint8_t) ch); /*发送一个字符函数*/
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)/*等待发送完成*/
{
}
return ch;
}
void Uart1_PutChar(u8 ch)
{
USART_SendData(USART1, (u8) ch);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
jlink下载
compiling main.c...
main.c(131): warning: #111-D: statement is unreachable
main.c(154): warning: #1-D: last line of file ends without a newline
linking...
Program Size: Code=5808 RO-data=268 RW-data=24 ZI-data=512
FromELF: creating hex file...
".\obj\My_Prj_Template.axf" - 0 Error(s), 2 Warning(s).
Load "D:\\STM32Proj\\My_Stm_Com\\obj\\My_Prj_Template.AXF"
Set JLink Project File to "D:\STM32Proj\My_Stm_Com\JLinkSettings.ini"
JLink info:
------------
DLL: V4.24b, compiled Feb 22 2011 20:47:17
Firmware: J-Link ARM V8 compiled Jan 31 2011 18:34:52
Hardware: V8.00
S/N : 20100214
Feature(s) : RDI,FlashDL,FlashBP,JFlash,GDBFull
* JLink Info: TotalIRLen = 9, IRPrint = 0x0011
* JLink Info: Found Cortex-M3 r1p1, Little endian.
* JLink Info: TPIU fitted.
* JLink Info: ETM fitted.
* JLink Info: FPUnit: 6 code (BP) slots and 2 literal slots
ROMTableAddr = 0xE00FF003
* JLink Info: TotalIRLen = 9, IRPrint = 0x0011
* JLink Info: Found Cortex-M3 r1p1, Little endian.
* JLink Info: TPIU fitted.
* JLink Info: ETM fitted.
* JLink Info: FPUnit: 6 code (BP) slots and 2 literal slots
* JLink Info: TotalIRLen = 9, IRPrint = 0x0011
* JLink Info: Found Cortex-M3 r1p1, Little endian.
* JLink Info: TPIU fitted.
* JLink Info: ETM fitted.
* JLink Info: FPUnit: 6 code (BP) slots and 2 literal slots
Target info:
------------
Device: STM32F103ZE
VTarget = 3.617V
State of Pins:
TCK: 1, TDI: 0, TDO: 1, TMS: 0, TRES: 1, TRST: 1
Hardware-Breakpoints: 6
Software-Breakpoints: 2048
Watchpoints: 4
JTAG speed: 2000 kHz
Erase Done.
Programming Done.
Verify OK.
截图:
下一步准备实现串口的接收功能
另外我发现我们的DIY的板子和冰凌科技的STM32开发版在硬件和一些软件代码上都挺像的,就找了些冰凌科技的资料