Advertisement
pongfactory

Lab 4-2 v.1 DEMO

Jan 28th, 2014
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.62 KB | None | 0 0
  1. /* Includes ------------------------------------------------------------------*/
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "stm32f10x.h"
  6. #include "stm32f10x_conf.h"
  7. #include "stm32f10x_rcc.h"
  8. #include<stm32f10x_adc.h>
  9. #include "stm32f10x_tim.h"
  10. #include "stm32f10x_gpio.h"
  11. #include "stm32f10x_usart.h"
  12.  
  13. /* Declare function prototype ------------------------------------------------*/
  14. void GPIO_init(void);
  15. void Delay(__IO uint32_t nCount);
  16. void USART_puts(USART_TypeDef* USARTx, volatile char *c);
  17. void USART_init(uint32_t baudrate);
  18. void Timer_init(void);
  19.  
  20. int main(void)
  21. {
  22.  
  23.         /* Configure General-Purpose Input/Output */
  24.         GPIO_init();
  25.         /* Configure Timer 100Hz 1-tick is 10ms*/
  26.         Timer_init();
  27.         /* Configure UART2 Baudrate 115200, */
  28.         USART_init(115200);
  29.  
  30.         USART_puts(USART1,"Program started\r\n");
  31.     while(1)
  32.     {
  33.         int value1 = ADC_simple();
  34.        
  35.  
  36.     }
  37. }
  38.  
  39. void Delay(__IO uint32_t nCount)
  40. {
  41.   while(nCount--);
  42. }
  43.  
  44.  
  45. void USART_init(uint32_t baudrate)
  46. {
  47.         /* Enable the UART1 Clock */
  48.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1 |
  49.                                 RCC_APB2Periph_GPIOA, ENABLE);
  50.         USART_InitTypeDef USART_InitStructure;
  51.         USART_InitStructure.USART_BaudRate                              = baudrate;
  52.         USART_InitStructure.USART_WordLength                    = USART_WordLength_8b;
  53.         USART_InitStructure.USART_StopBits                              = USART_StopBits_1;
  54.         USART_InitStructure.USART_Parity                                = USART_Parity_No;
  55.         USART_InitStructure.USART_HardwareFlowControl   = USART_HardwareFlowControl_None;
  56.         USART_InitStructure.USART_Mode                                  = USART_Mode_Tx;
  57.         USART_Init(USART1, &USART_InitStructure);
  58.  
  59.         /* Enable USART1 */
  60.         USART_Cmd(USART1, ENABLE);
  61. }
  62.  
  63. void USART_puts(USART_TypeDef* USARTx, volatile char *c)
  64. {
  65.         while(*c){
  66.                 // wait until data register is empty
  67.                 while( !(USARTx->SR & 0x00000040) );
  68.                 USART_SendData(USARTx, *c);
  69.                 *c++;
  70.         }
  71. }
  72.  
  73. void GPIO_init(void)
  74. {
  75.         /* Enable the GPIO Clock */
  76.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO , ENABLE);
  77.  
  78.         GPIO_InitTypeDef GPIO_InitStructure;
  79.         /* Configure USART1 Tx (PA.02) as alternate function push-pull */
  80.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  81.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  82.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  83.         GPIO_Init(GPIOA, &GPIO_InitStructure);
  84.  
  85.     /* Initial Input GPIO A pin 4 */
  86.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  87.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  88.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  89.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  90.  
  91. }
  92.  
  93. uint32_t micro(void) {
  94.  
  95.         /* return counter value form timer 3 */
  96.         return (u32)TIM_GetCounter(TIM3);
  97. }
  98.  
  99. void Timer_init(void) {
  100.  
  101.         /* Enable the Timer3 Clock */
  102.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  103.  
  104.         /* Initial Timer 3,
  105.          * frequency counter 72MHz / 720 =  100 KHz,
  106.          * set Autoreload value(ARR) = 0xFFFF(65535)
  107.          */
  108.         TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  109.         TIM_TimeBaseStructure.TIM_Prescaler = 720 - 1 ;
  110.         TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  111.         TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  112.         TIM_TimeBaseStructure.TIM_Period = 0xFFFF - 1;
  113.         TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  114.  
  115.         /* TIM3 enable counter */
  116.         TIM_Cmd(TIM3, ENABLE);
  117.  
  118. }
  119.  
  120. GPIO_InitTypeDef GPIO_InitStructure;
  121. ADC_InitTypeDef ADC_InitStructure;
  122. int i,j;
  123.  
  124. /* Blink a LED, blink speed is set by ADC value */
  125. void ADC_simple(void)
  126. {
  127. // init for GPIO (LED)
  128.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  129.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  130.         GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
  131.         GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_8 | GPIO_Pin_9 ;       // two LED (guess on what pin!!)
  132.         GPIO_Init(GPIOB, &GPIO_InitStructure);
  133.  
  134. // input of ADC (it doesn't seem to be needed, as default GPIO state is floating input)
  135.         GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AIN;
  136.         GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_1 ;            // that's ADC1 (PA1 on STM32)
  137.         GPIO_Init(GPIOA, &GPIO_InitStructure);
  138.  
  139. //clock for ADC (max 14MHz --> 72/6=12MHz)
  140.         RCC_ADCCLKConfig (RCC_PCLK2_Div6);
  141. // enable ADC system clock
  142.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  143.  
  144. // define ADC config
  145.         ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  146.         ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  147.         ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;      // we work in continuous sampling mode
  148.         ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  149.         ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  150.         ADC_InitStructure.ADC_NbrOfChannel = 1;
  151.  
  152.         ADC_RegularChannelConfig(ADC1,ADC_Channel_1, 1,ADC_SampleTime_28Cycles5); // define regular conversion config
  153.         ADC_Init ( ADC1, &ADC_InitStructure);   //set config of ADC1
  154.  
  155. // enable ADC
  156.         ADC_Cmd (ADC1,ENABLE);  //enable ADC1
  157.  
  158. //      ADC calibration (optional, but recommended at power on)
  159.         ADC_ResetCalibration(ADC1);     // Reset previous calibration
  160.         while(ADC_GetResetCalibrationStatus(ADC1));
  161.         ADC_StartCalibration(ADC1);     // Start new calibration (ADC must be off at that time)
  162.         while(ADC_GetCalibrationStatus(ADC1));
  163.  
  164. // start conversion
  165.         ADC_Cmd (ADC1,ENABLE);  //enable ADC1
  166.         ADC_SoftwareStartConvCmd(ADC1, ENABLE); // start conversion (will be endless as we are in continuous mode)
  167.  
  168. // debug information
  169.         RCC_ClocksTypeDef forTestOnly;
  170.         RCC_GetClocksFreq(&forTestOnly);        //this could be used with debug to check to real speed of ADC clock
  171.  
  172.         j= 50000;
  173.     while(1)
  174.     {
  175.         /*
  176.         GPIO_WriteBit(GPIOB,GPIO_Pin_8,Bit_RESET);
  177.                 GPIO_WriteBit(GPIOB,GPIO_Pin_9,Bit_SET);
  178.                 for (i=0;i<j;i++);
  179.                 GPIO_WriteBit(GPIOB,GPIO_Pin_9,Bit_RESET);
  180.                 GPIO_WriteBit(GPIOB,GPIO_Pin_8,Bit_SET);
  181.         */
  182.                 // adc is in free run, and we get the value asynchronously, this is not a really nice way of doing, but it work!
  183.                 j = ADC_GetConversionValue(ADC1) * 1000 ; // value from 0 to 4095000
  184.                 for (i=0;i<j;i++);
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement