/**********************************************************************
* Filename:             main.c
* Project:              DefaultProject
* Version: 		        2.0.0
* Chip type:            STM32F107VC-CortexM3
* PLL freq:		        
* CPU freq:		        
* Author: 	            Mostafa Tavallodi Sharif - 09391671368
* Company: 		        
* Creation Date:        
* Creation Time:        
* Description:          
**********************************************************************/

#include "stm32f10x.h"
#include "rtl.h"
#include "stdio.h"
#include "string.h"

/* Private function prototypes */
GPIO_InitTypeDef GPIO_Structure;
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
int i;
int sumadc;
int flagsliper=0;
/**
  * @brief  main function
  */
	unsigned long mass;
char lcd[16];
uint32_t TimingDelay;
int tim;
int flag;
int flag1;
int kalib;
int flags;
volatile char recBuff[2];
int countRec;
char RecFlag;
void myprintf(char *data,char length)
{	
	int i;
	for (i=0;i<length;i++)
		{
			while(!USART_GetFlagStatus(USART1,USART_FLAG_TC));
			USART_SendData(USART1,data[i]);
		}
}

void USART1_IRQHandler(){
	/*Checking for recieving data throuhg usart2*/
	if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==1){
     
			 
recBuff[0]=USART_ReceiveData(USART1);
if (recBuff[0]=='1')
	flags=1;
if (recBuff[0]=='2')
	flags=2;
if (recBuff[0]=='3')
	flags=3;
			 	USART_ClearFlag(USART1,USART_FLAG_RXNE);


		 }
	}
	



void SysTick_Handler(void)
{
	if ( TimingDelay != 0 ) TimingDelay--;
	tim++;
	
}
 void delay_ms(__IO uint32_t nTime)
{ 
  TimingDelay = nTime;
  while(TimingDelay != 0);
}


char adcvalue[2];
char adcval[10];
int iiii;
int countadc=0;
char str[100];
char readBuff[100];   
int ADC_read;
int mmm;
int flag11;
FILE *file1;
FILE *file2;
FINFO info;   
void ADC1_2_IRQHandler(void){

	if(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)==1){
		if(countadc<=2000)
		

	  adcvalue[countadc++]=ADC_GetConversionValue(ADC1);
	}
	else{
		
		countadc=0;
	}
//	ADC_ClearITPendingBit(ADC1,ADC_IT_EOC);
//	ADC_ClearFlag(ADC1,ADC_FLAG_EOC);
}


void configADC(void){
	  GPIO_InitTypeDef GPIO_ADCConfig;
	  ADC_InitTypeDef ADC_Config;
	/*Step1:Configures RCC*/
	  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
	  RCC_ADCCLKConfig(RCC_PCLK2_Div8);
	
	/*Step2:Configures GPIO For ADC1 Channel8*/
//	  GPIO_StructInit(&GPIO_ADCConfig);
		GPIO_ADCConfig.GPIO_Pin  = GPIO_Pin_0|GPIO_Pin_1;
		GPIO_ADCConfig.GPIO_Speed = GPIO_Speed_2MHz;
		GPIO_ADCConfig.GPIO_Mode = GPIO_Mode_AIN;
  	GPIO_Init(GPIOA,&GPIO_ADCConfig);
	
	/*Step3:Configures  ADC1 Channel8*/
	  ADC_Config.ADC_Mode=ADC_Mode_Independent;
	  ADC_Config.ADC_NbrOfChannel=1;//2
	  ADC_Config.ADC_ScanConvMode=DISABLE;//ENABLE
	  ADC_Config.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;
	  ADC_Config.ADC_DataAlign=ADC_DataAlign_Right;
	  ADC_Config.ADC_ContinuousConvMode=ENABLE;
	  ADC_Init(ADC1,&ADC_Config);

	  /*Configures channel8 for reading adc values*/
//	  ADC_RegularChannelConfig(ADC1,ADC_Channel_8,1,ADC_SampleTime_1Cycles5);
	  ADC_RegularChannelConfig(ADC1,ADC_Channel_1,1,ADC_SampleTime_239Cycles5);
		ADC_ExternalTrigConvCmd(ADC1,ENABLE);
			  ADC_Cmd(ADC1,ENABLE);

		ADC_ResetCalibration(ADC1);
						while(ADC_GetResetCalibrationStatus(ADC1));

		ADC_StartCalibration(ADC1);
				while(ADC_GetCalibrationStatus(ADC1));


		ADC_SoftwareStartConvCmd(ADC1,ENABLE);
	  /*enable adc interrupt*/
//	  ADC_ITConfig(ADC1,ADC_IT_EOC,ENABLE);
//	  NVIC_EnableIRQ(ADC1_2_IRQn);
//		
//    /*Enable adc1*/
//		/*Start conversion */		
}

int size = 0;
/*********************************************************************
* @brief
* @param[in]
* @return
*********************************************************************/
int main(void)
{
	 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
  /* Initialize all configured peripherals */
	//GPIO_Structure.GPIO_Pin =GPIO_Pin_13;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

	GPIO_Structure.GPIO_Pin =GPIO_Pin_15| GPIO_Pin_13 | GPIO_Pin_14| GPIO_Pin_2;
	GPIO_Structure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Structure.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_Init (GPIOA,&GPIO_Structure);
	
	GPIO_Structure.GPIO_Pin =GPIO_Pin_2;
	GPIO_Structure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Structure.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_Init (GPIOA,&GPIO_Structure);

		GPIO_Structure.GPIO_Pin =GPIO_Pin_3;
	GPIO_Structure.GPIO_Mode = GPIO_Mode_IPD;
	GPIO_Structure.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_Init (GPIOA,&GPIO_Structure);
	
	//
	  SysTick_Config( SystemCoreClock / 1000 );

    // USART peripheral initialization settings    

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
			configADC();

//		   //Configure : SCK(PA9)
//	  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 ;
//    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
//    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
//    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
//    GPIO_Init(GPIOA, &GPIO_InitStructure);
//	
//		//Configure : DO(PA10)
//    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 ;
//    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
//    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
//    GPIO_Init(GPIOA, &GPIO_InitStructure);
//      
	   //Configure : SCK(PA11)
	  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 ;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
	
		//Configure : DO(PA12)
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 ;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

    GPIO_Init(GPIOA, &GPIO_InitStructure);
		
		
		
			/*Configures TX*/	
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_9;
	GPIO_Init(GPIOA,&GPIO_InitStruct);

	/*Configures RX*/	
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_10;
	GPIO_Init(GPIOA,&GPIO_InitStruct);
    //Configure USART1 pins: Rx (PA2), Tx(PA3)
		


     
    //Configure USART1 setting: ----------------------------
	
	
   USART_InitStructure.USART_BaudRate=9600;
   USART_InitStructure.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
   USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
   USART_InitStructure.USART_Parity=USART_Parity_No;
   USART_InitStructure.USART_StopBits=USART_StopBits_1;
   USART_InitStructure.USART_WordLength=USART_WordLength_8b;
   USART_Init(USART1, &USART_InitStructure); // USART is disabled after calling the USART_Init function
   USART_Cmd(USART1, ENABLE);
	//myprintf("MASS SENSOR\n",5);
	 USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
	 
	 USART_Cmd(USART1,ENABLE);

   NVIC_EnableIRQ(USART1_IRQn);

 
    
    while( finit("M0:") != 0 );  
    file1 = fopen("tttttt.txt","w");
    if(file1 != NULL)
    {
        sprintf(str,"shah");
        fwrite(str,1,strlen(str),file1);
        fclose(file1);
    }   
    file2 = fopen("tttttt.txt","r");
	if(file2 != NULL)
	{ 
        if(ffind("M0:tttttt.TXT",&info) != 0)
            size = info.size;
        else
            size = 100;
       
        fread(&readBuff,1,10,file2);
		fclose(file2);
	}
    
    while(1)
	{
     		
   delay_ms(100);
		mmm++;

//		myprintf("MASS SENSOR\n",5);

//		for (iiii=1;iiii<=4;iiii++){
//			
ADC_read=ADC_GetConversionValue(ADC1);
//		adcval[iiii]=ADC_read;
//		sumadc=adcval[iiii]+sumadc;	
//			
//		}

		sprintf(lcd,"mass=%d \t\n",ADC_read);
		sumadc=0;
		myprintf(lcd,25);


   
	}
}
