0% found this document useful (0 votes)
73 views4 pages

Programme

This document contains C code for configuring an STM32 microcontroller to measure RPM using a hall effect sensor. It includes functions to initialize the system clock at 8MHz from the HSE, configure PWM on pin PA6 using Timer 3, read analog values from pin PA2 using ADC1, transmit/receive data over USART3 on pins PB10 and PB11 at 9600 baud, and measure the time between hall sensor pulses on pin PA0 using Timer 2 to calculate RPM. The main loop performs analog to digital conversion, calculates RPM using the pulse timing, and transmits the RPM value over USART.

Uploaded by

linkskil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views4 pages

Programme

This document contains C code for configuring an STM32 microcontroller to measure RPM using a hall effect sensor. It includes functions to initialize the system clock at 8MHz from the HSE, configure PWM on pin PA6 using Timer 3, read analog values from pin PA2 using ADC1, transmit/receive data over USART3 on pins PB10 and PB11 at 9600 baud, and measure the time between hall sensor pulses on pin PA0 using Timer 2 to calculate RPM. The main loop performs analog to digital conversion, calculates RPM using the pulse timing, and transmits the RPM value over USART.

Uploaded by

linkskil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include "STM32F4xx.

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

GPIOA->MODER |= 0x2000;// pa6 alternate function


GPIOA->AFR[0] |= 0x2000000; //set pa6 to AF2 "tim3"

TIM3->CCMR1 |= 0x60;//PWM mode 1 - In channel 1 is active as long as


TIMx_CNT<TIMx_CCR1 ;|-|_ not _|-| signal begin with high then get low
TIM3->PSC = 3999; // psc+1=3999+1=4000 => freq =8mhz/4000= 2000hz periode
0.0005s
TIM3->ARR = 255;
TIM3->CCR1 = 0;
TIM3->CCER|=0x01; // signal pwm yabda mi zero
TIM3->CR1 |= 0x1; //enable timer 3
}

///////////////////////////////////////////////////////////////////////////////////
/
//////// ADC PA2
///////////////////////////////////////////////////////////////////////////////////
/

void Config_ADC(void){
RCC->APB2ENR |= 0x00000100; //ENable clock ADC1
RCC->AHB1ENR |= 0x00000001;//Enable Clock for GpioA pa2

GPIOA->MODER |= 0x00000030; // MODE Register -> PA2 are analog inputs

ADC->CCR = 0x00000006; // No DMA, Regular simultaneous mode only


ADC1->CR2 = 0x00000001;// ADC1 On
ADC1->SQR3 = 0x00000002; // regular SeQuence Register 3

///////////////////////////////////////////////////////////////////////////////////
/
//////// USART3 PB10 PB11
///////////////////////////////////////////////////////////////////////////////////
/

void Config_USART3(void){

RCC->AHB1ENR |= 0x00000002; // Enable clock for GPIOB


RCC->APB1ENR|=0x00040000; // Enable clock for USART3
GPIOB->AFR[1]=0x00007700; // //enable USART3_TX to PB10 and USART3_RX to PB11
GPIOB->MODER|=0x2AA00000; // configuring the USART3 ALTERNATE function to
PB10 and PB11

USART3->BRR|=0X0341;// 9600 Baud BRR=8Mhz/9600


USART3->CR1|=0x0000202C; // USART3 enable
}

///////////////////////////////////////////////////////////////////////////////////
/
//////// 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);

}}

You might also like