刚开始学,好多不懂的。
串口用的uart2,spi用的是spi2.
spi的mosi没有数据输出。想知道为什么,或者这个程序有什么毛病。
下面是程序:
#include "stm32f10x.h"
#include "SysTick.h"
#include "stm32f10x_it.h"
void RCC_Configuration(void);
void USART_Config(void);
void NVIC_Configuration(void);
void SPI_Config(void);
void GeneralIO_Config(void);
//* main
int main(void)
{
/* GPIO Setting */
GeneralIO_Config();
/* Clock Setting */
RCC_Configuration();
/* SPI Setting */
SPI_Config();
/* Usart Setting */
USART_Config();
/* Interrupt Setting */
NVIC_Configuration();
/* System Tick Generation */
SysTick_Init();
for(;;) {
}
}
void USART2_IRQHandler(void)
{
u8 txdate;
if(USART_GetITStatus(USART2,USART_IT_RXNE))
{
txdate= USART_ReceiveData(USART2);
SPI_I2S_SendData(SPI2,txdate);
}
}
void SPI2_IRQHandler(void)
{
u8 rxdate;
if(SPI_I2S_GetITStatus(SPI2,SPI_I2S_FLAG_RXNE))
{
rxdate=SPI_I2S_ReceiveData(SPI2);
USART_SendData(USART2,rxdate);
}
}
void RCC_Configuration(void)
{
//ErrorStatus HSEStartUpStatus; //定义枚举类型变量HSEStartUpStatus
//RCC_DeInit(); //复位系统时钟
//RCC_HSEConfig(RCC_HSE_ON); //开启HSE
//HSEStartUpStatus=RCC_WaitForHSEStartUp(); //等待HSE稳定起振
//
//if(HSEStartUpStatus==SUCCESS) //如果HSE稳定起振
//{
// RCC_HCLKConfig(RCC_SYSCLK_Div1); //选择HCLK(AHB)时钟源为SYSCLK 1分频
// RCC_PCLK2Config(RCC_HCLK_Div1); //选择PCLK2时钟源为HCLK(AHB) 1分频
// RCC_PCLK1Config(RCC_HCLK_Div2); //选择PLCK1时钟源为HCLK(AHB) 2分频
// FLASH_SetLatency(FLASH_Latency_2); //设置FLASH延时周期数为2
// FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //使能FLASH预取缓存
// RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_6); //选择PLL时钟源为HSE 1分频,倍频数为6,则PLL=11.0592MHz*6=66.3552MHz
// RCC_PLLCmd(ENABLE); //使能PLL
// while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET); //等待PLL输出稳定
// RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //以PLL作为系统时钟
// while(RCC_GetSYSCLKSource()!=0x08); //等待PLL成为有效系统时钟源
//}
RCC_DeInit(); //复位系统时钟
RCC_HSICmd(ENABLE); //开启HSI
while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET) //等待HSI准备好
{
}
if(1)
{
RCC_HCLKConfig(RCC_SYSCLK_Div1); //选择HCLK(AHB)时钟源为SYSCLK 1分频
RCC_PCLK2Config(RCC_HCLK_Div1); //选择PCLK2时钟源为HCLK(AHB) 1分频
RCC_PCLK1Config(RCC_HCLK_Div2); //选择PLCK1时钟源为HCLK(AHB) 2分频
FLASH_SetLatency(FLASH_Latency_2); //设置FLASH延时周期数为2
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //使能FLASH预取缓存
RCC_PLLConfig(RCC_PLLSource_HSI_Div2,RCC_PLLMul_10); //选择PLL时钟源为HSI 1分频,倍频数为5,则PLL=8MHz/2*10=40MHz
RCC_PLLCmd(ENABLE); //使能PLL
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET); //等待PLL输出稳定
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //以PLL作为系统时钟
while(RCC_GetSYSCLKSource()!=0x08); //等待PLL成为有效系统时钟源
}
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
/* Enable the USARTy Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable the SPIy Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void GeneralIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
/* Cradle In/Out/Fix limit switch IO setting */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* gpio setting */
GPIO_ResetBits(GPIOA,GPIO_Pin_1);
GPIO_SetBits(GPIOA,GPIO_Pin_11);
}
void USART_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* config USART2 & general io clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
/* ----------------USART1 GPIO config-------------------------- */
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USART1 mode config */
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_Parity_No
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
/* ----------------USART1 GPIO config-------------------------- */
}
void SPI_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
/* config SPI2 & general io clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
/* ----------------SPI2 GPIO config-------------------------- */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* SPI2 mode config */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_Init(SPI2, &SPI_InitStructure);
SPI_Cmd(SPI2, ENABLE);
SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE);
/* ----------------SPI2 GPIO config-------------------------- */
}
自己感觉这两个中断函数怪怪的,这是自己模仿两个uart通信仿写的,虽然感觉怪,但是不知道到底错在哪,哪里的库函数调用错了。求赐教
此帖出自
小平头技术问答
一周热门 更多>