Programme
Programme
h"
#include <stdio.h>
char text[30];
int i,N1,f,ADC_Coversion;
long int value_CCR1=0,periode,j,nbres_tour_par_seconde,nbres_tour_par_MIN,x=0;
///////////////////////////////////////////////////////////////////////////////////
/
//////// SYSTEME CLOCK HSE 8MHZ
///////////////////////////////////////////////////////////////////////////////////
/
void System_Clock_HSE(void){
RCC->CFGR = 0x00000001; //select HSE as system clock a partirdes SW1 SW2
RCC->CR |= 1<<16; //make HSE ON
while (!(RCC->CR & (1<<17)));//While HSE is Ready Flag
}
///////////////////////////////////////////////////////////////////////////////////
/
//////// PWM PA6
///////////////////////////////////////////////////////////////////////////////////
/
void Config_PWM(void){
// set clock for tim3 and portA
RCC->APB1ENR |= 0x00000002;// Enable clock Timer3
RCC->AHB1ENR |= 0x00000001;// Enable clock GPIOA
///////////////////////////////////////////////////////////////////////////////////
/
//////// ADC PA2
///////////////////////////////////////////////////////////////////////////////////
/
void Config_ADC(void){
RCC->APB2ENR |= 0x00000100; //ENable clock ADC1
RCC->AHB1ENR |= 0x00000001;//Enable Clock for GpioA pa2
///////////////////////////////////////////////////////////////////////////////////
/
//////// USART3 PB10 PB11
///////////////////////////////////////////////////////////////////////////////////
/
void Config_USART3(void){
///////////////////////////////////////////////////////////////////////////////////
/
//////// RPM METER TIM2 PA0
///////////////////////////////////////////////////////////////////////////////////
/
void Config_TIM2_Capteur(void){
RCC->APB1ENR |=0x00000001;//enable clock for Timer_2
TIM2->CCMR1 = 0x0021;
//events are needed to validate a transition on the output:
TIM2->CCER = 0x0033; // falling edge //enable input capture
TIM2->SR=0;// mettre Flag a 0
TIM2->CR1= 0x0001;//enable Timer_2
/// Config GpioA pa0///////
RCC->AHB1ENR|= 0x00000001; // clock enabled for GPIOA
GPIOA->MODER|= 0x2; // PA0 is AF mode
GPIOA->AFR[0]|= 0x00000011; // AF is set TIM2
}
///////////////////////////////////////////////////////////////////////////////////
/
//////// FONCTIONS
///////////////////////////////////////////////////////////////////////////////////
/
/*******************************Delay_TIME ********************/
void Delay_TIME(__IO uint32_t nCount)
{
while(nCount--);
}
/*******************************Conversion ********************/
void Conversion(void){
ADC1->CR2 |= 0x40000000;//START CONVERSION ADC1 PA0
Delay_TIME(0x00ff);
ADC_Coversion=ADC1->DR; // ADC_Coversion variable 8 bits
TIM3->CCR1=0.063*ADC_Coversion;
if(TIM3->CCR1==TIM3->ARR)
{
TIM3->CCR1=0;
}
Delay_TIME(0x00ff);
}
/*******************************USART3__Send1_Char ********************/
void USART3__Send1_Char(char c)
{
while((USART3->SR & (0x0080))==0); // On attend � ce que TXBUFF soit vide (libere)
ou (FLAG TXNE=1) ou Le caractere est envoy�
//// wait for TXE to be 1 [1:Data tranfered to dhift_registre 0:NOT]
USART3->DR=c;
}
/*******************************USART3_send_String ********************/
void USART3_send_String(char *pt)
{
while(*pt)
{
USART3__Send1_Char(*pt);
pt++;
}
}
/*******************************USART3__Recive1_char ********************/
char USART3__Recive1_char(void){
while(((USART3->SR) & (0x20))==0)//when Data is Reeady to read RXNE=1 else
RXN3=0 'when data is not recived'
return((USART3->DR));
}
/*******************************USART3__Recive_String ********************/
char USART3__Recive_String(void){
char c,TAB[100];
while((USART3->SR) & (0x20==0)){////when Data is Reeady to read RXNE=1 else
RXN3=0 'when data is not recived'
c=USART3->DR;
TAB[i]=c;
i++;
}
return(TAB);
}
/*******************************CALCUL VITESSE ********************/
void Calcul_Vitesse(void){
if((TIM2->SR&0x0002)) // CC1IF =1 ?
{N1++;
TIM2->SR=0;
if(N1==70){
x=TIM2->CCR1-x;
periode=x/8;
nbres_tour_par_seconde=(10000000/periode);
nbres_tour_par_MIN=nbres_tour_par_seconde*60;
sprintf(text,"%d ",nbres_tour_par_MIN);
USART3_send_String(text);
N1=0;
TIM2->CCR1=0;
x=0;
TIM2->CNT=0;
}
}
}
/*******************************MAIN ********************/
void main()
{
System_Clock_HSE();
Config_PWM();
Config_ADC();
Config_USART3();
Config_TIM2_Capteur();
while(1)
{
//USART3_send_String("hi ");
Conversion();
Calcul_Vitesse();
Delay_TIME(0xffff);
}}