#include <stm32f10x.h>
#include <stm32f10x_usart.h>
#include <stm32f10x_iwdg.h>
#include <stdlib.h>
#include <stdio.h>
#include "RCC.h"
#include "gpio.h"
#include "usart.h"
#include "modbus.h"
#include "global.h"

#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

/*无锡北微传感科技有限公司*/

u8 readX[8] = {0x01, 0x03, 0x00, 0x01, 0x00, 0x01, 0xD5, 0xCA}; //读Pitch 
//u8 readY[8] = {0x01, 0x03, 0x00, 0x02, 0x00, 0x01, 0x25, 0xCA}; //读ROLL 
//u8 readZ[8] = {0x01, 0x03, 0x00, 0x03, 0x00, 0x01, 0x74, 0x0A}; //读YAW
//u8 readXYZ[8] = {0x01, 0x03, 0x00, 0x01, 0x00, 0x03, 0x54, 0x0B}; //读三轴

int main(void)
{
	u16 x_value = 0;
//	u16 y_value = 0;
	/*使能看门狗*/
	IWDG_Init(4,2500);
	
	//时钟配置
	RCC_Configuration();

	/*system clock配置*/
	SysTick_Configuration();
	
	/*管脚初始化*/
	GPIO_Configuration();
	
	/*中断配置*/
	NVIC_Configuration();
	
	/*485接收使能*/
	ReceiveMode_485(USART3);
	
	
	TIMER2_Init();
	ModInit(0x01);//默认地址01
	USARTInit(USART1, 0x04);//波特率115200
	USARTInit(USART3, 0x04);
	USART_ITConfig(USART3, USART_IT_TC,DISABLE);//Mod	
	
	while(1)
	{
		if(Com_50Hz == 1) //50Hz频率发送读取命令
		{
			Com_50Hz = 0;
			USARTSendString(USART3, readX, 8); //发送读取命令

			x_value = X_Data[1]; //读取数据16进制转十进制，X_Data在modbus.h中ReadHoldingReg函数中赋值
			x_value = (x_value<<8)| X_Data[0];
		
			X_Pitch = (float)(x_value - 20000) / 100.0; //计算公式见数据手册
			printf("X_Pitch = %.2f\r\n", X_Pitch);	
		}
		IWDG_ReloadCounter();
	}
	
}

PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(USART1, (uint8_t) ch);

  /* 循环等待直到发送结束*/
  while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  {}

  return ch;
}
