Source code implementation based on stm32 8-channel ADC sampling for future design reference

Source: Internet
Author: User

# Include "stm32f10x_lib.h"
# Include <stdio. h>

Extern void board_configuration (void );
Extern unsigned short adc_convertedvalue [8];

Int main (void)
{
Unsigned int I = "0 ";
Unsigned short ad_scaled_ex [8];
Unsigned short ad_scaled [8];

Board_configuration ();
While (1)
{
For (I = 0; I <8; I ++)
{
Ad_scaled [I] = adc_convertedvalue [I];
If (ad_scaled [I]! = Ad_scaled_ex [I])
{
Printf ("ad % d value = % d \ r \ n", I, ad_scaled [I]);
Ad_scaled_ex [I] = ad_scaled [I];
}

}
For (I = 0; I <60000000; I ++ );
}
}

 

 

/* Includes ------------------------------------------------------------------*/
# Include "stm32f10x_lib.h"
# Include <stdio. h>

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
# Define adc1_dr_address (u32) 0x4001244c)

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
Adc_inittypedef adc_initstructure;
Dma_inittypedef dma_initstructure;
Unsigned short adc_convertedvalue [8];
Errorstatus hsestartupstatus;

/* Private function prototypes -----------------------------------------------*/
Void rcc_configuration (void );
Void nvic_configuration (void );
Void gpio_configuration (void );
Void usart_configuration (void );
Void dma_configuration (void );
Void adc_configuration (void );

/* Private functions ---------------------------------------------------------*/

/*************************************** ****************************************
* Function name: Main
* Description: main program.
* Input: None
* Output: None
* Return: None
**************************************** ***************************************/
Void board_configuration (void)
{
# Ifdef debug
Debug ();
# Endif

/* Configure the system clocks */
Rcc_configuration ();

/* Configure C configuration */
Nvic_configuration ();

/* Configure the gpios */
Gpio_configuration ();

/* Configure the usart1 */
Usart_configuration ();
/* Configure the DMA */
Dma_configuration ();
/* Configure the DMA */
Adc_configuration ();

}

# Ifdef debug
/*************************************** ****************************************
* Function name: assert_failed
* Description: reports the name of the source file and the source line number
* Where the assert error has occurred.
* Input:-file: pointer to the source file name
*-Line: assert error line source number
* Output: None
* Return: None
**************************************** ***************************************/
Void assert_failed (u8 * file, u32 line)
{
/* User can add his own implementation to report the file name and line number */

Printf ("\ n \ r wrong parameter value detected on \ r \ n ");
Printf ("file % s \ r \ n", file );
Printf ("line % d \ r \ n", line );

/* Infinite loop */
/* While (1)
{
}*/
}
# Endif
/*************************************** ****************************************
* Function name: dma_configuration
* Description: configures the different system clocks.
* Input: None
* Output: None
* Return: None
**************************************** ***************************************/
Void dma_configuration (void)
{
Dma_deinit (dma_channel1 );
Dma_initstructure.dma_peripheralbaseaddr = adc1_dr_address;
Dma_initstructure.dma_memorybaseaddr = (u32) & adc_convertedvalue;
Dma_initstructure.dma_dir = dma_dir_peripheralsrc;
Dma_initstructure.dma_buffersize = 8;
Dma_initstructure.dma_peripheralinc = dma_peripheralinc_disable;
Dma_initstructure.dma_memoryinc = dma_memoryinc_enable;
Dma_initstructure.dma_peripheraldatasize = dma_peripheraldatasize_halfword;
Dma_initstructure.dma_memorydatasize = dma_memorydatasize_halfword;
Dma_initstructure.dma_mode = dma_mode_circular;
Dma_initstructure.dma_priority = dma_priority_high;
Dma_initstructure.dma_m2m = dma_m2m_disable;
Dma_init (dma_channel1, & dma_initstructure );

/* Enable DMA channel1 */
Dma_cmd (dma_channel1, enable );
}
/*************************************** ****************************************
* Function name: adc_configuration
* Description: configures the different system clocks.
* Input: None
* Output: None
* Return: None
**************************************** ***************************************/
Void adc_configuration (void)
{
/* Adc1 configuration ------------------------------------------------------*/
Adc_initstructure.adc_mode = adc_mode_independent;
Adc_initstructure.adc_scanconvmode = Enable;
Adc_initstructure.adc_continuousconvmode = Enable;
Adc_initstructure.adc_externaltrigconv = adc_externaltrigconv_none;
Adc_initstructure.adc_dataalign = adc_dataalign_right;
Adc_initstructure.adc_nbrofchannel = 8;
Adc_init (adc1, & adc_initstructure );

/* Adc1 regular channel8 configuration */
Adc_regularchannelconfig (adc1, adc_channel_8, 1, adc_sampletime_55cycles5 );
/* Adc1 regular channel9 configuration */
Adc_regularchannelconfig (adc1, adc_channel_9, 2, adc_sampletime_55cycles5 );
/* Adc1 regular channel10 configuration */
Adc_regularchannelconfig (adc1, adc_channel_10, 3, adc_sampletime_55cycles5 );
/* Adc1 regular channel11 configuration */
Adc_regularchannelconfig (adc1, adc_channel_11, 4, adc_sampletime_55cycles5 );
/* Adc1 regular channel12 configuration */
Adc_regularchannelconfig (adc1, adc_channel_12, 5, adc_sampletime_55cycles5 );
/* Adc1 regular channel13 configuration */
Adc_regularchannelconfig (adc1, adc_channel_13, 6, adc_sampletime_55cycles5 );
/* Adc1 regular channel14 configuration */
Adc_regularchannelconfig (adc1, adc_channel_14, 7, adc_sampletime_55cycles5 );
/* Adc1 regular channel15 configuration */
Adc_regularchannelconfig (adc1, adc_channel_15, 8, adc_sampletime_55cycles5 );

/* Enable adc1 DMA */
Adc_dmacmd (adc1, enable );

/* Enable adc1 */
Adc_cmd (adc1, enable );

/* Enable adc1 reset calibaration register */
Adc_resetcalibration (adc1 );
/* Check the end of adc1 reset calibration register */
While (adc_getresetcalibrationstatus (adc1 ));

/* Start adc1 calibaration */
Adc_startcalibration (adc1 );
/* Check the end of adc1 calibration */
While (adc_getcalibrationstatus (adc1 ));

/* Start adc1 software conversion */
Adc_softwarestartconvcmd (adc1, enable );
}

/*************************************** ****************************************
* Function name: rcc_configuration
* Description: configures the different system clocks.
* Input: None
* Output: None
* Return: None
**************************************** ***************************************/
Void rcc_configuration (void)
{
/* RCC system reset (for debug purpose )*/
Rcc_deinit ();

/* Enable HSE */
Rcc_hseconfig (rcc_hse_on );

/* Wait till HSE is ready */
Hsestartupstatus = rcc_waitforhsestartup ();

If (hsestartupstatus = success)
{
/* Hclk = sysclk */
Rcc_hclkconfig (rcc_sysclk_div1 );

/* Pclk2 = hclk */
Rcc_pclk2config (rcc_hclk_div1 );

/* Pclk1 = hclk/2 */
Rcc_pclk1config (rcc_hclk_div2 );
/* Adcclk = pclk2/4 */
Rcc_adcclkconfig (rcc_pclk2_div4 );

/* Flash 2 wait state */
Flash_setlatency (flash_latency_2 );
/* Enable prefetch buffer */
Flash_prefetchbuffercmd (flash_prefetchbuffer_enable );

/* Pllclk = 8 MHz * 9 = 72 MHz */
Rcc_pllconfig (rcc_pllsource_hse_div1, rcc_pllmul_9 );

/* Enable PLL */
Rcc_pllcmd (enable );

/* Wait till PLL is ready */
While (rcc_getflagstatus (rcc_flag_pllrdy) = reset)
{
}

/* Select PLL as system clock source */
Rcc_sysclkconfig (rcc_sysclksource_pllclk );

/* Wait till PLL is used as system clock source */
While (rcc_getsysclksource ()! = 0x08)
{
}
}

/* Enable usart1 and gpioa clock */
Rcc_apb2periphclockcmd (rcc_apb2periph_usart1 | rcc_apb2periph_gpioa, enable );
/* Enable DMA clock */
Rcc_ahbperiphclockcmd (rcc_ahbperiph_dma, enable );
/* Enable adc1 and gpioc clock */
Rcc_apb2periphclockcmd (rcc_apb2periph_adc1 | rcc_apb2periph_gpioc |
Rcc_apb2periph_gpiob, enable );

}

/*************************************** ****************************************
* Function name: nvic_configuration
* Description: configures vector table base location.
* Input: None
* Output: None
* Return: None
**************************************** ***************************************/
Void nvic_configuration (void)
{
# Ifdef vect_tab_ram
/* Set the vector table base location at 0x20000000 */
Nvic_setvectortable (nvic_vecttab_ram, 0x0 );
# Else/* vect_tab_flash */
/* Set the vector table base location at 0x08000000 */
Nvic_setvectortable (nvic_vecttab_flash, 0x0 );
# Endif
}

/*************************************** ****************************************
* Function name: gpio_configuration
* Description: configures the different gpio ports.
* Input: None
* Output: None
* Return: None
**************************************** ***************************************/
Void gpio_configuration (void)
{
Gpio_inittypedef gpio_initstructure;

/* Configure usart1 TX (pa.09) as alternate function push-pull */
Gpio_initstructure.gpio_pin = gpio_pin_9;
Gpio_initstructure.gpio_mode = gpio_mode_af_pp;
Gpio_initstructure.gpio_speed = gpio_speed_50mhz;
Gpio_init (gpioa, & gpio_initstructure );

/* Configure usart1 rx (pa.10) as input floating */
Gpio_initstructure.gpio_pin = gpio_pin_10;
Gpio_initstructure.gpio_mode = gpio_mode_in_floating;
Gpio_init (gpioa, & gpio_initstructure );

/* Configure pc.0 \ 1 \ 2 \ 3 \ 4 \ 5 (ADC channel10 \ 11 \ 12 \ 13 \ 14 \ 15) as analog input */
Gpio_initstructure.gpio_pin = gpio_pin_0 | gpio_pin_1 |
Gpio_pin_2 | gpio_pin_3 |
Gpio_pin_4 | gpio_pin_5;
Gpio_initstructure.gpio_mode = gpio_mode_ain;
Gpio_init (gpioc, & gpio_initstructure );

/* Configure pb.0 \ 1 (ADC channel8 \ 9) As analog input */
Gpio_initstructure.gpio_pin = gpio_pin_0 | gpio_pin_1;

Gpio_initstructure.gpio_mode = gpio_mode_ain;
Gpio_init (gpiob, & gpio_initstructure );

}

/*************************************** ****************************************
* Function name: usart_configuration
* Description: configures the usart1.
* Input: None
* Output: None
* Return: None
**************************************** ***************************************/
Void usart_configuration (void)
{
 
Usart_inittypedef usart_initstructure;
/* Usart1 configuration ------------------------------------------------------*/
/* Usart1 configured as follow:
-Bau drate = 115200 baud
-Word Length = 8 bits
-One Stop bit
-No parity
-Hardware Flow Control Disabled (RTS and CTS signals)
-Receive and transmit Enabled
-Usart clock disabled
-Usart cpol: clock is active low
-Usart cpha: data is captured on the middle
-Usart lastbit: the clock pulse of the last data bit is not output
The sclk pin
*/
Usart_initstructure.usart_baudrate = 115200;
Usart_initstructure.usart_wordlength = usart_wordlength_8b;
Usart_initstructure.usart_stopbits = usart_stopbits_1;
Usart_initstructure.usart_parity = usart_parity_no;
Usart_initstructure.usart_hardwareflowcontrol = usart_hardwareflowcontrol_none;
Usart_initstructure.usart_mode = usart_mode_rx | usart_mode_tx;
Usart_initstructure.usart_clock = usart_clock_disable;
Usart_initstructure.usart_cpol = usart_cpol_low;
Usart_initstructure.usart_cpha = usart_cpha_2edge;
Usart_initstructure.usart_lastbit = usart_lastbit_disable;

Usart_init (usart1, & usart_initstructure );

/* Enable usart1 */
Usart_cmd (usart1, enable );
}

/*************************************** ****************************************
* Function name: fputc
* Description: retargets the C library printf function to the usart.
* Input: None
* Output: None
* Return: None
**************************************** ***************************************/
Int fputc (int ch, file * F)
{
/* Place your Implementation of fputc here */
/* E.g. Write a character to the usart */
Usart_senddata (usart1, (u8) CH );

/* Loop until the end of transmission */
While (usart_getflagstatus (usart1, usart_flag_tc) = reset)
{
}

Return ch;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.