Advertisement
Guest User

stm

a guest
Feb 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.63 KB | None | 0 0
  1. #include "stm32f0xx.h"
  2. #include "stm32f0xx_gpio.h"
  3. #include "stm32f0xx_rcc.h"
  4. #include "stm32f0xx_usart.h"
  5. #include "stm32f0_discovery.h"
  6.  
  7. void GPIO_Config(void);
  8. void RCC_Config(void);
  9. void NVIC_Config(void);
  10. void TIM_Config(void);
  11. void initUSART();
  12.  
  13. int wypelnienie = 0;
  14. int czestotliwosc = 0;
  15.  
  16.  
  17. char BuforRX[64]; // Bufor odczytu
  18. int brRead = 0; // pierwszy wolny indeks do odczytu
  19. int brWrite = 0; // ostatni zajety indeks odczytu
  20.  
  21. char BuforTX[64]; // Bufor wyslania
  22. int btRead = 0; // pierwszy wolny indeks do wyslania
  23. int btWrite = 0; // ostatni zajety indeks wyslania
  24.  
  25. char BuforTmp[64];
  26. uint8_t BuforIndex = 0;
  27.  
  28. int main(void)
  29. {
  30. volatile unsigned long int i;
  31.  
  32. RCC_Config();
  33. GPIO_Config();
  34. NVIC_Config();
  35. TIM_Config();
  36. USART2_Init();
  37. USART_print("Wlaczono STM32\r\n");
  38.  
  39.  
  40. while (1) {
  41. USART_received();
  42.  
  43.  
  44. //zapalanie diody niebieskiej
  45. if (!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8)){
  46. if (TIM3->CCR3<=(0xFFFF-250)){
  47. TIM3->CCR3 = TIM3->CCR3-250;
  48. }
  49. }
  50.  
  51. //przygaszenie diody zielonej
  52. if (!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_9)){
  53. if (TIM3->CCR4>=250){
  54. TIM3->CCR4 = TIM3->CCR4-250;
  55. }
  56. }
  57. for (i=0;i<400000;i++);
  58. };
  59. return 0;
  60. }
  61.  
  62. void RCC_Config(void)
  63. //konfigurowanie sygnalow taktujacych
  64. {
  65. RCC_DeInit();
  66.  
  67. //wlacz taktowanie portu GPIOA oraz GPIOC
  68. RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOCEN, ENABLE);
  69. //wlacz taktowanie licznika TIM3
  70. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  71.  
  72.  
  73. }
  74.  
  75. void NVIC_Config(void) {
  76.  
  77. //Konfigurowanie kontrolera przerwan NVIC
  78. NVIC_InitTypeDef NVIC_InitStructure;
  79.  
  80. //wybor odpowiedniego kanalu
  81. NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  82. NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  83. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  84. NVIC_Init(&NVIC_InitStructure);
  85.  
  86. }
  87.  
  88. void GPIO_Config(void) {
  89. //konfigurowanie portow GPIO
  90. GPIO_InitTypeDef gpio;
  91.  
  92. //Pin_0 - przycisk
  93. gpio.GPIO_Pin = GPIO_Pin_0;
  94. gpio.GPIO_Mode = GPIO_Mode_IN;
  95. gpio.GPIO_PuPd = GPIO_PuPd_NOPULL;
  96. gpio.GPIO_OType = GPIO_OType_PP;
  97. gpio.GPIO_Speed = GPIO_Speed_50MHz;
  98. GPIO_Init(GPIOA, &gpio);
  99.  
  100. //Pin_8 - led niebieski
  101. //Pin_9 - led zielony
  102. gpio.GPIO_Pin = GPIO_Pin_8 |GPIO_Pin_9;
  103. gpio.GPIO_Mode = GPIO_Mode_AF;
  104. gpio.GPIO_PuPd = GPIO_PuPd_NOPULL;
  105. gpio.GPIO_OType = GPIO_OType_PP;
  106. gpio.GPIO_Speed = GPIO_Speed_50MHz;
  107. GPIO_Init(GPIOC, &gpio);
  108.  
  109. }
  110.  
  111. void TIM_Config(void) {
  112.  
  113. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  114. TIM_OCInitTypeDef TIM_OCInitStructure;
  115.  
  116. // Konfiguracja TIM3
  117. // Ustawienia ukladu podstawy czasu
  118. TIM_TimeBaseStructure.TIM_Prescaler = 0; //bez prescelaera (prescaler=1)
  119. TIM_TimeBaseStructure.TIM_Period = 49999ul; //czestotliwosc PWM = 1440 Hz
  120. TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  121. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  122.  
  123. TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  124.  
  125.  
  126. // Konfiguracja kanalu 3 - uzywamy kanalu 3 poniewaz jego wyjscie jest na GPIOC8 - gdzie jest LED NIEBIESKI
  127. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  128. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  129. TIM_OCInitStructure.TIM_Pulse = 50ul; //wypelnienie poczatkowe = 50000/100=0.2%
  130. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  131. TIM_OC3Init(TIM3, &TIM_OCInitStructure);
  132.  
  133. TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
  134.  
  135. // Konfiguracja kanalu 4 - uzywamy kanalu 4 poniewaz jego wyjscie jest na GPIOC9 - gdzie jest LED ZIELONY
  136. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  137. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  138. TIM_OCInitStructure.TIM_Pulse = 25000ul; //wypelnienie = 50000/25000=50%
  139. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  140. TIM_OC4Init(TIM3, &TIM_OCInitStructure);
  141.  
  142. TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
  143.  
  144. TIM_ARRPreloadConfig(TIM3, ENABLE);
  145.  
  146. // Wlaczenie timera
  147. TIM_Cmd(TIM3, ENABLE);
  148. }
  149.  
  150. void TIM3_IRQHandler(void) {
  151. RCC_ClocksTypeDef RCC_Clocks;
  152. RCC_GetClocksFreq(&RCC_Clocks);
  153.  
  154. TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
  155.  
  156. //pobranie wartosci wejscia
  157. int timer2 = TIM_GetCapture2(TIM3);
  158.  
  159. if (timer2 != 0) {
  160. //obliczenia cyklu roboczego
  161. int timer1 = TIM_GetCapture1(TIM3);
  162.  
  163. //obliczenia czestotliwosci
  164. //TIM3 counter clock = 800;
  165. wypelnienie = timer1 * 100 / timer2;
  166. czestotliwosc = RCC_Clocks.HCLK_Frequency / timer2;
  167.  
  168. } else {
  169. wypelnienie = 0;
  170. czestotliwosc = 0;
  171. }
  172. }
  173.  
  174. void USART_print(char tmp[]) {
  175. volatile int index = btWrite; // volatile - zmienna zmieniana w przerwaniach (żeby nie została usunięta)
  176. for (int i = 0; i < strlen(tmp); i++) {
  177. BuforTX[index] = tmp[i];
  178. index++;
  179. if (index >= 64)
  180. index = 0;
  181. }
  182. __disable_irq(); // wylaczenie przerwania
  183. if ((btRead == btWrite) && (USART_GetFlagStatus(USART2, USART_FLAG_TXE))== SET) {
  184. btWrite = index;
  185. USART_SendData(USART2, BuforTX[btRead]); //pierwszy znak
  186. btRead++;
  187. if (btRead >= 64)
  188. btRead = 0;
  189. USART_ITConfig(USART2, USART_IT_TXE, ENABLE); //załącza przerwanie
  190. } else {
  191. btWrite = index;
  192. }
  193. __enable_irq(); // wlaczenie przerwania
  194. }
  195.  
  196. void USART2_Init(void) {
  197. USART_InitTypeDef usart;
  198. GPIO_InitTypeDef gpio;
  199. NVIC_InitTypeDef nvic;
  200.  
  201. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  202. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  203. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
  204.  
  205. GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
  206. GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
  207.  
  208. gpio.GPIO_Pin = GPIO_Pin_8; //LED
  209. gpio.GPIO_OType = GPIO_OType_PP;
  210. gpio.GPIO_Mode = GPIO_Mode_OUT;
  211. gpio.GPIO_Speed = GPIO_Speed_50MHz;
  212. GPIO_Init(GPIOC, &gpio);
  213.  
  214. gpio.GPIO_Pin = GPIO_Pin_9; //LED
  215. gpio.GPIO_OType = GPIO_OType_PP;
  216. gpio.GPIO_Mode = GPIO_Mode_OUT;
  217. gpio.GPIO_Speed = GPIO_Speed_50MHz;
  218. GPIO_Init(GPIOC, &gpio);
  219.  
  220. gpio.GPIO_Pin = GPIO_Pin_2; //TX
  221. gpio.GPIO_Speed = GPIO_Speed_50MHz;
  222. gpio.GPIO_Mode = GPIO_Mode_AF;
  223. gpio.GPIO_OType = GPIO_OType_PP;
  224. gpio.GPIO_PuPd = GPIO_PuPd_UP;
  225. GPIO_Init(GPIOA, &gpio);
  226.  
  227. gpio.GPIO_Pin = GPIO_Pin_3; //RD
  228. gpio.GPIO_Speed = GPIO_Speed_50MHz;
  229. gpio.GPIO_Mode = GPIO_Mode_AF;
  230. gpio.GPIO_OType = GPIO_OType_PP;
  231. gpio.GPIO_PuPd = GPIO_PuPd_UP;
  232. GPIO_Init(GPIOA, &gpio);
  233.  
  234. nvic.NVIC_IRQChannel = USART2_IRQn;
  235. nvic.NVIC_IRQChannelPriority = 0;
  236. nvic.NVIC_IRQChannelCmd = ENABLE;
  237. NVIC_Init(&nvic);
  238.  
  239. usart.USART_BaudRate = 9600;
  240. usart.USART_WordLength = USART_WordLength_8b;
  241. usart.USART_StopBits = USART_StopBits_1;
  242. usart.USART_Parity = USART_Parity_No;
  243. usart.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  244. usart.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  245. USART_Init(USART2, &usart);
  246. USART_Cmd(USART2, ENABLE);
  247. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//zezwolenie na przerwanie odbioru
  248.  
  249. }
  250.  
  251. void USART2_IRQHandler(void) { //obsługa przerwań
  252.  
  253. if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) { //obsługa przerwania odebrania
  254. BuforRX[brWrite] = USART_ReceiveData(USART2); //przepisanie odebranego bajtu z rejestru do buforu odbioru
  255. brWrite++;
  256. if (brWrite >= 64)
  257. brWrite = 0;
  258. }
  259.  
  260. if (USART_GetITStatus(USART2, USART_IT_TXE) != RESET) { //obsługa przerwania wysłania
  261. if (btRead != btWrite) {
  262. USART_SendData(USART2, BuforTX[btRead]);
  263. btRead++;
  264. if (btRead >= 64)
  265. btRead = 0;
  266. } else
  267. USART_ITConfig(USART2, USART_IT_TXE, DISABLE); //zablokowanie przerwania transmisji - koniec transmisji
  268. }
  269.  
  270. }
  271.  
  272. void parse(char *inputText) {
  273. if (strncmp(inputText, "name", 4) == 0) {
  274. USART_print("STM32\r\n");
  275. } else if (strncmp(inputText, "led on b", 8) == 0) {
  276. //zapalanie diody niebieskiej
  277. GPIO_SetBits(GPIOC, GPIO_Pin_8);
  278. USART_print("Wlaczono diode niebieska\r\n");
  279. } else if (strncmp(inputText, "led off b", 8) == 0) {
  280. GPIO_ResetBits(GPIOC, GPIO_Pin_8); //zresetowanie diody
  281. USART_print("Wylaczono diode niebieska\r\n");
  282. } else if (strncmp(inputText, "led on g", 8) == 0) {
  283. //zapalanie diody zielonej
  284. GPIO_SetBits(GPIOC, GPIO_Pin_9);
  285. USART_print("Wylaczono diode zielona\r\n");
  286. } else {
  287. USART_print("Zla komenda\r\n");
  288. }
  289. memset(inputText, 0, strlen(inputText)); // wyczyszczenie zawartosci tablicy
  290. }
  291.  
  292. void USART_received() {
  293. if (brRead != brWrite) {
  294. while (brRead != brWrite) {
  295. if (BuforRX[brRead] != '\n'&& BuforRX[brRead] != '\r') {
  296. if (BuforRX[brRead] != ';') {
  297. BuforTmp[BuforIndex] = BuforRX[brRead];
  298. BuforIndex++;
  299. if (BuforIndex >= 64)
  300. BuforIndex = 0;
  301. } else {
  302. parse(BuforTmp);
  303. BuforIndex = 0;
  304. }
  305. }
  306. brRead++;
  307. if (brRead >= 64)
  308. brRead = 0;
  309. }
  310. }
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement