Facebook
From Tinct Marmoset, 5 Years ago, written in Plain Text.
This paste is a reply to Untitled from Rude Curlew - view diff
Embed
Download Paste or View Raw
Hits: 326
  1. /************************* (C) COPYRIGHT 2010 ROBOTIS **************************
  2. * File Name          : main.c
  3. * Author             : danceww
  4. * Version            : V0.0.1
  5. * Date               : 08/23/2010
  6. * Description        : Main program body
  7. *******************************************************************************/
  8.  
  9. /* Includes ------------------------------------------------------------------*/
  10. #include "stm32f10x_lib.h"
  11.  
  12. /* Private typedef -----------------------------------------------------------*/
  13. /* Private define ------------------------------------------------------------*/
  14. #define PORT_SW_UP                              GPIOC
  15. #define PORT_SW_DOWN                    GPIOC
  16. #define PORT_SW_RIGHT                   GPIOA
  17. #define PORT_SW_LEFT                    GPIOA
  18. #define PORT_SW_START                   GPIOB
  19.  
  20. #define PORT_LED_AUX                    GPIOB
  21. #define PORT_LED_MANAGE                 GPIOB
  22. #define PORT_LED_PROGRAM                GPIOB
  23. #define PORT_LED_PLAY                   GPIOB
  24. #define PORT_LED_POWER                  GPIOC
  25. #define PORT_LED_TX                             GPIOC
  26. #define PORT_LED_RX                             GPIOC
  27.  
  28. #define PIN_SW_UP                               GPIO_Pin_11
  29. #define PIN_SW_DOWN                             GPIO_Pin_10
  30. #define PIN_SW_RIGHT                    GPIO_Pin_14
  31. #define PIN_SW_LEFT                             GPIO_Pin_15
  32. #define PIN_SW_START                    GPIO_Pin_3
  33.  
  34. #define PIN_LED_AUX                             GPIO_Pin_12
  35. #define PIN_LED_MANAGE                  GPIO_Pin_13
  36. #define PIN_LED_PROGRAM                 GPIO_Pin_14
  37. #define PIN_LED_PLAY                    GPIO_Pin_15
  38. #define PIN_LED_POWER                   GPIO_Pin_13
  39. #define PIN_LED_TX                              GPIO_Pin_14
  40. #define PIN_LED_RX                              GPIO_Pin_15
  41.  
  42. /* Private macro -------------------------------------------------------------*/
  43. /* Private variables ---------------------------------------------------------*/
  44. /* Private function prototypes -----------------------------------------------*/
  45. /* Private functions ---------------------------------------------------------*/
  46. void RCC_Configuration(void); //konfiguracja zegarów
  47. void NVIC_Configuration(void); //obsluga przerwan
  48. void GPIO_Configuration(void); //konfiguracja portow
  49.  
  50. /*******************************************************************************
  51. * Function Name  : main
  52. * Description    : Main program
  53. * Input          : None
  54. * Output         : None
  55. * Return         : None
  56. *******************************************************************************/
  57. int main(void)
  58. {
  59.         /* System Clocks Configuration */
  60.         RCC_Configuration();
  61.  
  62.         /* NVIC configuration */
  63.         GPIO_Configuration();
  64.  
  65.         /* Configure the GPIO ports */
  66.         NVIC_Configuration();
  67.  
  68.         GPIO_ResetBits(PORT_LED_POWER, PIN_LED_POWER);
  69.  
  70.         while(1)
  71.         {
  72.                 if( GPIO_ReadInputDataBit(PORT_SW_UP, PIN_SW_UP) != SET )
  73.                         {
  74.                         GPIO_ResetBits(GPIOB, PIN_LED_AUX);
  75.                         GPIO_ResetBits(GPIOB, PIN_LED_MANAGE);
  76.                         }
  77.                 else if( GPIO_ReadInputDataBit(PORT_SW_DOWN, PIN_SW_DOWN) != SET )
  78.                         GPIO_ResetBits(GPIOB, PIN_LED_MANAGE);
  79.  
  80.                 else if( GPIO_ReadInputDataBit(PORT_SW_LEFT, PIN_SW_LEFT) != SET )
  81.                         GPIO_ResetBits(PORT_LED_PROGRAM, PIN_LED_PROGRAM);
  82.  
  83.                 else if( GPIO_ReadInputDataBit(PORT_SW_RIGHT, PIN_SW_RIGHT) != SET )
  84.                         GPIO_ResetBits(PORT_LED_PLAY, PIN_LED_PLAY);
  85.  
  86.                 else if( GPIO_ReadInputDataBit(PORT_SW_START, PIN_SW_START) != SET )
  87.                 {
  88.                         GPIO_ResetBits(PORT_LED_MANAGE, PIN_LED_MANAGE);
  89.                         GPIO_ResetBits(PORT_LED_PROGRAM, PIN_LED_PROGRAM);
  90.                         GPIO_ResetBits(PORT_LED_PLAY, PIN_LED_PLAY);
  91.                         GPIO_ResetBits(PORT_LED_TX, PIN_LED_TX);
  92.                         GPIO_ResetBits(PORT_LED_RX, PIN_LED_RX);
  93.                         GPIO_ResetBits(PORT_LED_AUX, PIN_LED_AUX);
  94.                 }
  95.  
  96.                 else
  97.                 {
  98.                         GPIO_SetBits(PORT_LED_MANAGE, PIN_LED_MANAGE);
  99.                         GPIO_SetBits(PORT_LED_PROGRAM, PIN_LED_PROGRAM);
  100.                         GPIO_SetBits(PORT_LED_PLAY, PIN_LED_PLAY);
  101.                         GPIO_SetBits(PORT_LED_TX, PIN_LED_TX);
  102.                         GPIO_SetBits(PORT_LED_RX, PIN_LED_RX);
  103.                         GPIO_SetBits(PORT_LED_AUX, PIN_LED_AUX);
  104.                 };
  105.         }
  106.         return 0;
  107. }
  108.  
  109. void RCC_Configuration(void)
  110. {
  111.         ErrorStatus HSEStartUpStatus;
  112.         /* RCC system reset(for debug purpose) */
  113.         RCC_DeInit();
  114.  
  115.         /* Enable HSE */
  116.         RCC_HSEConfig(RCC_HSE_ON);
  117.  
  118.         /* Wait till HSE is ready */
  119.         HSEStartUpStatus = RCC_WaitForHSEStartUp();
  120.  
  121.         if(HSEStartUpStatus == SUCCESS)
  122.         {
  123.                 /* Enable Prefetch Buffer */
  124.                 FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
  125.  
  126.                 /* Flash 2 wait state */
  127.                 FLASH_SetLatency(FLASH_Latency_2);
  128.  
  129.                 /* HCLK = SYSCLK */
  130.                 RCC_HCLKConfig(RCC_SYSCLK_Div1);
  131.  
  132.                 /* PCLK2 = HCLK */
  133.                 RCC_PCLK2Config(RCC_HCLK_Div1);
  134.  
  135.                 /* PCLK1 = HCLK/2 */
  136.                 RCC_PCLK1Config(RCC_HCLK_Div2);
  137.  
  138.                 /* PLLCLK = 8MHz * 9 = 72 MHz */
  139.                 RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
  140.  
  141.                 /* Enable PLL */
  142.                 RCC_PLLCmd(ENABLE);
  143.  
  144.                 /* Wait till PLL is ready */
  145.                 while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  146.                 {
  147.                 }
  148.  
  149.                 /* Select PLL as system clock source */
  150.                 RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
  151.  
  152.                 /* Wait till PLL is used as system clock source */
  153.                 while(RCC_GetSYSCLKSource() != 0x08)
  154.                 {
  155.                 }
  156.         }
  157.  
  158.         /* Enable peripheral clocks --------------------------------------------------*/
  159.  
  160.         /* Enable GPIOA, GPIOB, and GPIOC clocks */
  161.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE);
  162.  
  163.         PWR_BackupAccessCmd(ENABLE);
  164. }
  165.  
  166. /*******************************************************************************
  167. * Function Name  : NVIC_Configuration
  168. * Description    : Configures Vector Table base location.
  169. * Input          : None
  170. * Output         : None
  171. * Return         : None
  172. *******************************************************************************/
  173. void NVIC_Configuration(void)
  174. {
  175.     #ifdef  VECT_TAB_RAM
  176.                 // Set the Vector Table base location at 0x20000000
  177.                 NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  178.         #else  // VECT_TAB_FLASH
  179.                 // Set the Vector Table base location at 0x08003000
  180.                 NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x3000);
  181.         #endif
  182. }
  183.  
  184. /*******************************************************************************
  185. * Function Name  : GPIO_Configuration
  186. * Description    : Configures the different GPIO ports.
  187. * Input          : None
  188. * Output         : None
  189. * Return         : None
  190. *******************************************************************************/
  191. void GPIO_Configuration(void)
  192. {
  193.         GPIO_InitTypeDef GPIO_InitStructure;
  194.         GPIO_StructInit(&GPIO_InitStructure);
  195.  
  196.         // PORTA CONFIG
  197.         GPIO_InitStructure.GPIO_Pin =    PIN_SW_RIGHT | PIN_SW_LEFT;
  198.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  199.         GPIO_Init(GPIOA, &GPIO_InitStructure);
  200.  
  201.         // PORTB CONFIG
  202.         GPIO_InitStructure.GPIO_Pin =    PIN_SW_START;
  203.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  204.         GPIO_Init(GPIOB, &GPIO_InitStructure);
  205.  
  206.         GPIO_InitStructure.GPIO_Pin =    PIN_LED_AUX | PIN_LED_MANAGE | PIN_LED_PROGRAM | PIN_LED_PLAY;
  207.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  208.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  209.         GPIO_Init(GPIOB, &GPIO_InitStructure);
  210.  
  211.         // PORTC CONFIG
  212.         GPIO_InitStructure.GPIO_Pin =    PIN_SW_UP | PIN_SW_DOWN;
  213.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  214.         GPIO_Init(GPIOC, &GPIO_InitStructure);
  215.  
  216.         GPIO_InitStructure.GPIO_Pin =    PIN_LED_POWER | PIN_LED_TX | PIN_LED_RX;
  217.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  218.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  219.         GPIO_Init(GPIOC, &GPIO_InitStructure);
  220. }
  221.