Advertisement
Guest User

guziki

a guest
Feb 28th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. //******************************************************************************
  2. // THE SOFTWARE INCLUDED IN THIS FILE IS FOR GUIDANCE ONLY.
  3. // AUTHOR SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT
  4. // OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  5. // FROM USE OF THIS SOFTWARE.
  6. //
  7. // PROGRAM ZAWARTY W TYM PLIKU PRZEZNACZONY JEST WYLACZNIE
  8. // DO CELOW SZKOLENIOWYCH. AUTOR NIE PONOSI ODPOWIEDZIALNOSCI
  9. // ZA ZADNE EWENTUALNE, BEZPOSREDNIE I POSREDNIE SZKODY
  10. // WYNIKLE Z JEGO WYKORZYSTANIA.
  11. //******************************************************************************
  12.  
  13. #include "stm32f10x.h"
  14.  
  15. void GPIO_Config(void);
  16. void RCC_Config(void);
  17.  
  18. int main(void)
  19. {
  20. volatile unsigned long int i;
  21. uint8_t button_state=0xFF, temp=0, port_data ;
  22.  
  23. //konfiguracja systemu
  24. RCC_Config();
  25. GPIO_Config();
  26. /*Tu nalezy umiescic ewentualne dalsze funkcje konfigurujace system*/
  27. GPIO_ResetBits(GPIOA, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 );
  28. GPIO_ResetBits(GPIOB, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7);
  29.  
  30. while (1) {
  31. /*Tu nalezy umiescic glowny kod programu*/
  32. port_data = GPIO_ReadInputData(GPIOA); //czytaj port GPIOA
  33. temp = port_data ^ button_state; // czy stan przycisków sie zmienil?
  34. temp &= button_state; // czy to byla zmiana z 1 na 0?
  35. button_state = port_data; // zapamietaj nowy stan
  36.  
  37. if (temp & 0x01) // czy to przycisk 1?
  38. // zmien stan LED na przeciwny
  39. GPIO_WriteBit(GPIOB, GPIO_Pin_0, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_0)));
  40. if (temp & 0x02) // czy to przycisk 2?
  41. // zmien stan LED na przeciwny
  42. GPIO_WriteBit(GPIOB, GPIO_Pin_1, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_1)));
  43. if (temp & 0x04) // czy to przycisk 3?
  44. // zmien stan LED na przeciwny
  45. GPIO_WriteBit(GPIOB, GPIO_Pin_2, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_2)));
  46. if (temp & 0x08) // czy to przycisk 4?
  47. // zmien stan LED na przeciwny
  48. GPIO_WriteBit(GPIOB, GPIO_Pin_3, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_3)));
  49. };
  50. return 0;
  51. }
  52.  
  53.  
  54. void RCC_Config(void)
  55. //konfigurowanie sygnalow taktujacych
  56. {
  57. ErrorStatus HSEStartUpStatus; //zmienna opisujaca rezultat uruchomienia HSE
  58.  
  59. RCC_DeInit(); //Reset ustawien RCC
  60. RCC_HSEConfig(RCC_HSE_ON); //Wlaczenie HSE
  61. HSEStartUpStatus = RCC_WaitForHSEStartUp(); //Odczekaj az HSE bedzie gotowy
  62. if(HSEStartUpStatus == SUCCESS)
  63. {
  64. FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//
  65. FLASH_SetLatency(FLASH_Latency_2); //ustaw zwloke dla pamieci Flash; zaleznie od taktowania rdzenia
  66. //0:<24MHz; 1:24~48MHz; 2:>48MHz
  67. RCC_HCLKConfig(RCC_SYSCLK_Div1); //ustaw HCLK=SYSCLK
  68. RCC_PCLK2Config(RCC_HCLK_Div1); //ustaw PCLK2=HCLK
  69. RCC_PCLK1Config(RCC_HCLK_Div2); //ustaw PCLK1=HCLK/2
  70. RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); //ustaw PLLCLK = HSE*9 czyli 8MHz * 9 = 72 MHz
  71. RCC_PLLCmd(ENABLE); //wlacz PLL
  72. while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); //odczekaj na poprawne uruchomienie PLL
  73. RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //ustaw PLL jako zrodlo sygnalu zegarowego
  74. while(RCC_GetSYSCLKSource() != 0x08); //odczekaj az PLL bedzie sygnalem zegarowym systemu
  75.  
  76. /*Tu nalezy umiescic kod zwiazany z konfiguracja sygnalow zegarowych potrzebnych w programie peryferiow*/
  77. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);//wlacz taktowanie portu GPIO A
  78. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
  79.  
  80. } else {
  81. }
  82. }
  83.  
  84.  
  85.  
  86. void GPIO_Config(void)
  87. {
  88. //konfigurowanie portow GPIO
  89. GPIO_InitTypeDef GPIO_InitStructure;
  90. GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
  91. /*Tu nalezy umiescic kod zwiazany z konfiguracja poszczegolnych portow GPIO potrzebnych w programie*/
  92. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  93. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  94. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //wyjscie push-pull
  95. GPIO_Init(GPIOB, &GPIO_InitStructure);
  96.  
  97. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  98. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  99. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //wejscie bez podciagania
  100. GPIO_Init(GPIOA, &GPIO_InitStructure);
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement