Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include "stm32f30x.h"
- float risultato;
- float LUT[100];
- float dato[100];
- void salvaSuFile() {
- // per usare i file, bisogna configurare il progetto: Options >> General options >> library configuration >> seleziona Full
- FILE* file;
- file = fopen("prova.txt", "a");
- for(int i = 0; i < 100; i++) {
- fprintf(file, "%.6f\n", dato[i]);
- }
- fclose(file);
- }
- void popolaLUT() {
- /*
- per generare una sinus. di un dato periodo:
- 1. calcoliamo delta T (= tempo di generazione = tempo tra un campione e il successivo)
- = periodo / numero di campioni
- 2. TIM->ARR = T * fck (fck == 8 M oppure 72 M a seconda)
- 3. DHR12R1 = LUT[indiceLUT++ % 100];
- 4. ADSTART = 1;
- */
- float dx, x;
- dx = 2 * 3.1415 / 100;
- for(int i = 0; i < 100; i++) {
- // è 1.5 perché così il valore sta a metà strada rispetto al F.S.
- LUT[i] = (1.5 + 1.5 * sin(x)) * 4095 / 3.0;
- x += dx;
- }
- }
- void setup_DAC() {
- RCC->APB1ENR |= RCC_APB1ENR_DACEN;
- DAC->CR |= DAC_CR_EN1;
- int volt = 2;
- int codice = 4095 * volt / 3;
- DAC->DHR12R1 = codice;
- for(int i = 0; i < 16; i++);
- }
- void setup_ADC() {
- RCC->AHBENR |= RCC_AHBENR_ADC12EN;
- ADC1_2->CCR |= ADC12_CCR_CKMODE_0;
- ADC1->CR &= ~ADC_CR_ADVREGEN;
- ADC1->CR &= ~ADC_CR_ADVREGEN;
- for(int i = 0; i < 80000; i++);
- ADC1->CR |= ADC_CR_ADCAL;
- while((ADC1->CR & ADC_CR_ADCAL) == ADC_CR_ADCAL);
- ADC1->CR |= ADC_CR_ADEN;
- while((ADC1->ISR & ADC_ISR_ADRD) != ADC_ISR_ADRD);
- ADC1->CFGR &= ~ADC_CFGR_CONT;
- ADC1->SQR1 |= ADC_SQR1_SQ1_0 | ADC_SQR1_SQ1_1;
- ADC1->SQR1 &= ~ADC_SQR1_L;
- ADC1->SMPR1 |= ADC_SMPR1_SMP3;
- }
- void setup_TIM() {
- RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
- TIM2->ARR = 8000;
- TIM2->CR2 = TIM_CR2_MMS_1;
- }
- void main (void){
- RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
- GPIOA->MODER |= GPIO_MODER_MODER4 | GPIO_MODER_MODER2;
- setup_DAC();
- setup_ADC();
- setup_TIM();
- popolaLUT();
- TIM2->CNT = 0;
- TIM2->CR1 = TIM_CR1_CEN;
- for(int i = 0; i < 100; i++) {
- DAC->DHR12R1 = LUT[i];
- while((TIM2->SR & TIM_SR_UIF) != TIM_SR_UIF);
- TIM2->SR &= ~TIM_SR_UIF;
- for(int j = 0; j < 1000; j++);
- ADC1->CR |= ADC_CR_ADSTART;
- while((ADC1->ISR & ADC_ISR_EOC) != ADC_ISR_EOC);
- dato[i] = ADC1->DR;
- }
- salvaSuFile();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement