Advertisement
pongfactory

Lab 4-2 v.2 Structure

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