#include "stm32f10x_lib.h"
#include"stdio.h"
#define SS1 PDout(8)// PD 8
#define SS2 PDout(9)// PD9
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void RCC_Configuration(void);
void EXTI_Configuration(void);
void spi_init(void);
void delay(void);
void delay_m(void);
u16 SPI_ReadWrite(u16 Txdat);
void USART1_Printf(char *pch);
void USART1_SendByte(unsigned char temp);
void Mlx90316Frame(void);
void UartTXdata();
unsigned int Frame_EA,Frame_EB;
char string1[20]="";
/*******************************************************************************
* Function Name : main
*******************************************************************************/
u16 a;
int main(void)
{
//NVIC_Configuration();
RCC_Configuration();
GPIO_Configuration();
spi_init();
while(1)
{
Mlx90316Frame();
delay();
UartTXdata();
delay();
delay();
}
}
/*******************************************************************************
* Function Name : GPIO_Configuration
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIOC clock */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE);
GPIO_DeInit(GPIOC);
GPIO_StructInit(&GPIO_InitStructure);
/* Configure PC.06, PC.07, PC.08 and PC.09 as Output push-pull for debugging
purposes */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
/*******************************************************************************
* Function Name : RCC_Configuration
*******************************************************************************/
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div4);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
}
/*******************************************************************************
* Function Name : NVIC_Configuration
*******************************************************************************/
void NVIC_Configuration(void)
{
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
}
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
void delay(void)
{
int i,j;
for(j=0;j<100;j++)
for(i=0;i<3000;i++);
}
void spi_init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
/* GPIO, TIM1,UART1 clocks enabling */
/* Enable GPIOA, GPIOC, GPIOE, AFIO clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA |
RCC_APB2Periph_GPIOC, ENABLE);
/* Enable UART1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
/* Enable SPI1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
/* UART1 pins configurations */
GPIO_StructInit(&GPIO_InitStructure);
/*Configure USART1 Rx (PA.10) as input floating*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_StructInit(&GPIO_InitStructure);
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
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_StructInit(&GPIO_InitStructure);
/*configure SPI NSS,SCK,MISO,MOSI*/
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_4; //NSS
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_WriteBit(GPIOA,GPIO_Pin_4,1);
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7; //PA.5 PA.6 PA.7 sck,ISO,OSI
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*SPI1 Peripheral Configuration*/
SPI_InitStructure.SPI_Direction=SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode=SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
SPI_InitStructure.SPI_CPOL=SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA=SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS=SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_256;
SPI_InitStructure.SPI_FirstBit=SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial=7;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1,ENABLE);
/*UART1 Peripheral Configuration*/
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_Cmd(USART1, ENABLE);
/* Configure one bit for preemption priority
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_StructInit(&NVIC_InitStructure);
/* Enable the TIM1 BRK Interrupt */
/*
NVIC_InitStructure.NVIC_IRQChannel = TIM1_BRK_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = BRK_PRE_EMPTION_PRIORITY;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = BRK_SUB_PRIORITY;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
*/
}
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
//Configure EXTI Line16 to generate an interrupt on rising edge
EXTI_DeInit();//将外设EXTI寄存器重设为缺省值
EXTI_StructInit(&EXTI_InitStructure);//把EXTI_InitStruct中的每一个参数按缺省值填入
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource6);//选择PC6用作外部中断线路
EXTI_ClearITPendingBit(EXT