Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Includes ------------------------------------------------------------------*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "stm32f10x.h"
- #include "stm32f10x_conf.h"
- #include "stm32f10x_rcc.h"
- #include<stm32f10x_adc.h>
- #include "stm32f10x_tim.h"
- #include "stm32f10x_gpio.h"
- #include "stm32f10x_usart.h"
- /* Declare function prototype ------------------------------------------------*/
- void GPIO_init(void);
- void Delay(__IO uint32_t nCount);
- void USART_puts(USART_TypeDef* USARTx, volatile char *c);
- void USART_init(uint32_t baudrate);
- void Timer_init(void);
- int main(void)
- {
- /* Configure General-Purpose Input/Output */
- GPIO_init();
- /* Configure Timer 100Hz 1-tick is 10ms*/
- Timer_init();
- /* Configure UART2 Baudrate 115200, */
- USART_init(115200);
- USART_puts(USART1,"Program started\r\n");
- while(1)
- {
- int value1 = ADC_simple();
- }
- }
- void Delay(__IO uint32_t nCount)
- {
- while(nCount--);
- }
- void USART_init(uint32_t baudrate)
- {
- /* Enable the UART1 Clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1 |
- RCC_APB2Periph_GPIOA, ENABLE);
- USART_InitTypeDef USART_InitStructure;
- USART_InitStructure.USART_BaudRate = baudrate;
- 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_Tx;
- USART_Init(USART1, &USART_InitStructure);
- /* Enable USART1 */
- USART_Cmd(USART1, ENABLE);
- }
- void USART_puts(USART_TypeDef* USARTx, volatile char *c)
- {
- while(*c){
- // wait until data register is empty
- while( !(USARTx->SR & 0x00000040) );
- USART_SendData(USARTx, *c);
- *c++;
- }
- }
- void GPIO_init(void)
- {
- /* Enable the GPIO Clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO , ENABLE);
- GPIO_InitTypeDef GPIO_InitStructure;
- /* Configure USART1 Tx (PA.02) 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);
- /* Initial Input GPIO A pin 4 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- }
- uint32_t micro(void) {
- /* return counter value form timer 3 */
- return (u32)TIM_GetCounter(TIM3);
- }
- void Timer_init(void) {
- /* Enable the Timer3 Clock */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
- /* Initial Timer 3,
- * frequency counter 72MHz / 720 = 100 KHz,
- * set Autoreload value(ARR) = 0xFFFF(65535)
- */
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_TimeBaseStructure.TIM_Prescaler = 720 - 1 ;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseStructure.TIM_ClockDivision = 0;
- TIM_TimeBaseStructure.TIM_Period = 0xFFFF - 1;
- TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
- /* TIM3 enable counter */
- TIM_Cmd(TIM3, ENABLE);
- }
- GPIO_InitTypeDef GPIO_InitStructure;
- ADC_InitTypeDef ADC_InitStructure;
- int i,j;
- /* Blink a LED, blink speed is set by ADC value */
- void ADC_simple(void)
- {
- // init for GPIO (LED)
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 ; // two LED (guess on what pin!!)
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- // input of ADC (it doesn't seem to be needed, as default GPIO state is floating input)
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ; // that's ADC1 (PA1 on STM32)
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- //clock for ADC (max 14MHz --> 72/6=12MHz)
- RCC_ADCCLKConfig (RCC_PCLK2_Div6);
- // enable ADC system clock
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
- // define ADC config
- ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
- ADC_InitStructure.ADC_ScanConvMode = DISABLE;
- ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; // we work in continuous sampling mode
- ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
- ADC_InitStructure.ADC_NbrOfChannel = 1;
- ADC_RegularChannelConfig(ADC1,ADC_Channel_1, 1,ADC_SampleTime_28Cycles5); // define regular conversion config
- ADC_Init ( ADC1, &ADC_InitStructure); //set config of ADC1
- // enable ADC
- ADC_Cmd (ADC1,ENABLE); //enable ADC1
- // ADC calibration (optional, but recommended at power on)
- ADC_ResetCalibration(ADC1); // Reset previous calibration
- while(ADC_GetResetCalibrationStatus(ADC1));
- ADC_StartCalibration(ADC1); // Start new calibration (ADC must be off at that time)
- while(ADC_GetCalibrationStatus(ADC1));
- // start conversion
- ADC_Cmd (ADC1,ENABLE); //enable ADC1
- ADC_SoftwareStartConvCmd(ADC1, ENABLE); // start conversion (will be endless as we are in continuous mode)
- // debug information
- RCC_ClocksTypeDef forTestOnly;
- RCC_GetClocksFreq(&forTestOnly); //this could be used with debug to check to real speed of ADC clock
- j= 50000;
- while(1)
- {
- /*
- GPIO_WriteBit(GPIOB,GPIO_Pin_8,Bit_RESET);
- GPIO_WriteBit(GPIOB,GPIO_Pin_9,Bit_SET);
- for (i=0;i<j;i++);
- GPIO_WriteBit(GPIOB,GPIO_Pin_9,Bit_RESET);
- GPIO_WriteBit(GPIOB,GPIO_Pin_8,Bit_SET);
- */
- // adc is in free run, and we get the value asynchronously, this is not a really nice way of doing, but it work!
- j = ADC_GetConversionValue(ADC1) * 1000 ; // value from 0 to 4095000
- for (i=0;i<j;i++);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement