Advertisement
Guest User

Untitled

a guest
Oct 20th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.92 KB | None | 0 0
  1. /*
  2. * cv5H.h
  3. *
  4. * Created on: 18. 10. 2016
  5. * Author: Peter
  6. */
  7.  
  8. #ifndef CV5H_H_
  9. #define CV5H_H_
  10.  
  11. void adc_init(void);
  12. void init_NVIC(void);
  13. void ADC1_IRQHandler(void);
  14.  
  15. void initUSART2(void); // usart 1
  16. void PutcUART2(char);// usart 1
  17.  
  18. void RegisterCallbackUART2(void *callback); // usart 1
  19. void USART2_IRQHandler(void); // usart 1
  20.  
  21. double prevod();
  22. void stav(uint16_t);
  23. void Put(char []);
  24.  
  25. #endif /* CV5H_H_ */
  26.  
  27.  
  28.  
  29.  
  30. /*
  31. * cv5C.c
  32. *
  33. * Created on: 18. 10. 2016
  34. * Author: Peter
  35. */
  36.  
  37. #include <stddef.h>
  38. #include "stm32l1xx.h"
  39. #include "cv5H.h"
  40. #include <stdio.h>
  41. #include <string.h>
  42.  
  43. void adc_init(void)
  44. {
  45.  
  46.  
  47.  
  48.  
  49. GPIO_InitTypeDef GPIO_InitStructure;
  50. ADC_InitTypeDef ADC_InitStructure;
  51. /* Enable GPIO clock */
  52. RCC_AHBPeriphClockCmd(GPIOB, ENABLE);//Opraviť a upraviť
  53. /* Configure ADCx Channel 2 as analog input */
  54. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
  55. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  56. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  57. GPIO_Init(GPIOA, &GPIO_InitStructure);
  58. /* Enable the HSI oscillator */
  59. RCC_HSICmd(ENABLE);
  60. /* Check that HSI oscillator is ready */
  61. while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
  62. /* Enable ADC clock */
  63. RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  64. /* Initialize ADC structure */
  65. ADC_StructInit(&ADC_InitStructure);
  66. /* ADC1 configuration */
  67. ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  68. ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  69. ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  70. ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  71. ADC_InitStructure.ADC_NbrOfConversion = 1;
  72. ADC_Init(ADC1, &ADC_InitStructure);
  73. /* ADCx regular channel8 configuration */
  74. ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_96Cycles);
  75.  
  76. /* Enable the ADC */
  77. init_NVIC();
  78. ADC_Cmd(ADC1, ENABLE);
  79.  
  80.  
  81. /* Wait until the ADC1 is ready */
  82. while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET)
  83. {
  84. }
  85. /* Start ADC Software Conversion */
  86. ADC_SoftwareStartConv(ADC1);
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93. }
  94. void init_NVIC(void){
  95. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
  96. NVIC_InitTypeDef NVIC_InitStructure;
  97. NVIC_InitStructure.NVIC_IRQChannel = ADC1_IRQn; //zoznam prerušení nájdete v súbore stm32l1xx.h
  98. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  99. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  100. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  101. NVIC_Init(&NVIC_InitStructure);
  102. ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);
  103.  
  104. }
  105.  
  106. uint16_t value = 0;
  107.  
  108. void ADC1_IRQHandler(void){
  109. if(ADC1->SR & ADC_SR_EOC){
  110.  
  111. value = ADC1->DR;
  112.  
  113. }
  114. }
  115.  
  116. void initUSART2(void) /// usart 1
  117. {
  118. USART_InitTypeDef USART_InitStructure;
  119. NVIC_InitTypeDef NVIC_InitStructure;
  120. GPIO_InitTypeDef GPIO_InitStructure;
  121.  
  122.  
  123. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  124.  
  125. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  126.  
  127. // RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART2, ENABLE); // usart 1
  128.  
  129. //choosing peripherals for selected pins
  130. GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); // GPIO_PinSource10, GPIO_AF_USART1
  131. GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2); // GPIO_PinSource9, GPIO_AF_USART1
  132.  
  133. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  134. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
  135. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  136. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  137. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; // GPIO_Pin_10 | GPIO_Pin_9;
  138. GPIO_Init(GPIOA, &GPIO_InitStructure);
  139. //usart configuration
  140.  
  141. USART_InitStructure.USART_BaudRate = 9600;
  142. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  143. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  144. USART_InitStructure.USART_Parity = USART_Parity_No;
  145. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  146. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  147. USART_Init(USART2, &USART_InitStructure); // usart 1
  148. //configuring interrupts
  149.  
  150. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
  151.  
  152.  
  153. NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; ///USART1_IRQn
  154. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  155. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  156. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  157. NVIC_Init(&NVIC_InitStructure);
  158. //choosing which event should cause interrupt
  159. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); // usart 1
  160. // Enable USART
  161. USART_Cmd(USART2, ENABLE); // usart 1
  162.  
  163. }
  164.  
  165. void PutcUART2(char ch){ //PutcUART1
  166. USART_SendData(USART2, (uint8_t) ch); // usart 1
  167. while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET); // usart 1
  168. }
  169. //char pole[sizeof(double)];
  170. char pole[];
  171. void Put(char pole[])
  172. {
  173. int i=0;
  174.  
  175. while(pole[i]!='\0')
  176. {
  177.  
  178. USART_SendData(USART2, pole[i]); // usart 1
  179. while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET); // usart 1
  180. i++;
  181. }
  182.  
  183. USART_SendData(USART2,' ');
  184. while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
  185.  
  186. }
  187.  
  188. void (* gCallback1)(unsigned char) = 0;
  189. void RegisterCallbackUART2(void *callback){ //usart 1
  190. gCallback1 = callback;
  191. }
  192.  
  193.  
  194. void USART2_IRQHandler(void) //USART1_IRQHandler
  195. {
  196. uint16_t pom = 0;
  197. if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) // usart 1
  198. {
  199. USART_ClearITPendingBit(USART2, USART_IT_RXNE); // usart 1
  200. pom = USART_ReceiveData(USART2); // usart 1
  201. if (gCallback1)
  202. {
  203. gCallback1(pom);
  204. }
  205. }
  206. }
  207.  
  208. double pom10=0;
  209.  
  210. double prevod()
  211. {
  212.  
  213.  
  214. pom10=value;
  215. return pom10*=0.000805;
  216.  
  217. }
  218.  
  219. int pom8=0;
  220. double pom9 = 0;
  221.  
  222. void stav(uint16_t hodnota){
  223.  
  224.  
  225.  
  226.  
  227. switch (hodnota){
  228. case 'm' :
  229. if(pom8==0)
  230. {
  231. pom9 = prevod();
  232.  
  233.  
  234.  
  235. uint8_t num1= (uint8_t)pom9;
  236. sprintf(pole,"%d.%dV", num1, (uint8_t)((pom9-num1)*100));
  237.  
  238.  
  239. Put(pole);
  240. pom8++;
  241. }
  242. else
  243. {
  244. //PutcUART2(value);
  245. sprintf(pole,"%d",value);
  246. Put(pole);
  247. pom8=0;
  248. }
  249.  
  250. break;
  251.  
  252. }
  253.  
  254. }
  255.  
  256.  
  257.  
  258.  
  259. /*
  260. ******************************************************************************
  261. File: main.c
  262. Info: Generated by Atollic TrueSTUDIO(R) 6.0.0 2016-10-18
  263.  
  264. The MIT License (MIT)
  265. Copyright (c) 2009-2016 Atollic AB
  266.  
  267. Permission is hereby granted, free of charge, to any person obtaining a copy
  268. of this software and associated documentation files (the "Software"), to deal
  269. in the Software without restriction, including without limitation the rights
  270. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  271. copies of the Software, and to permit persons to whom the Software is
  272. furnished to do so, subject to the following conditions:
  273.  
  274. The above copyright notice and this permission notice shall be included in all
  275. copies or substantial portions of the Software.
  276.  
  277. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  278. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  279. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  280. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  281. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  282. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  283. SOFTWARE.
  284. ******************************************************************************
  285. */
  286.  
  287. /* Includes */
  288. #include <stddef.h>
  289. #include "stm32l1xx.h"
  290. #include "cv5H.h"
  291.  
  292.  
  293. extern uint16_t value;
  294. /* Private typedef */
  295. /* Private define */
  296. /* Private macro */
  297. /* Private variables */
  298. /* Private function prototypes */
  299. /* Private functions */
  300.  
  301.  
  302. /**
  303. **===========================================================================
  304. **
  305. ** Abstract: main program
  306. **
  307. **===========================================================================
  308. */
  309. int main(void)
  310. {
  311.  
  312. uint16_t pom1;
  313. double pom11;
  314.  
  315. int i = 0;
  316. char ch ='X';
  317.  
  318. /**
  319. * IMPORTANT NOTE!
  320. * See the <system_*.c> file and how/if the SystemInit() function updates
  321. * SCB->VTOR register. Sometimes the symbol VECT_TAB_SRAM needs to be defined
  322. * when building the project if code has been located to RAM and interrupts
  323. * are used. Otherwise the interrupt table located in flash will be used.
  324. * E.g. SCB->VTOR = 0x20000000;
  325. */
  326.  
  327. /**
  328. * At this stage the microcontroller clock setting is already configured,
  329. * this is done through SystemInit() function which is called from startup
  330. * file (startup_stm32l1xx_hd.s) before to branch to application main.
  331. * To reconfigure the default setting of SystemInit() function, refer to
  332. * system_stm32l1xx.c file
  333. */
  334.  
  335. /* TODO - Add your application code here */
  336.  
  337. adc_init();
  338. initUSART2(); // usart 1
  339.  
  340.  
  341. // PutcUART2(ch); // usart 1
  342.  
  343. /* Infinite loop */
  344. while (1)
  345. {
  346.  
  347.  
  348. RegisterCallbackUART2(stav); // usart 1
  349.  
  350.  
  351. i++;
  352. }
  353. return 0;
  354. }
  355.  
  356. #ifdef USE_FULL_ASSERT
  357.  
  358. /**
  359. * @brief Reports the name of the source file and the source line number
  360. * where the assert_param error has occurred.
  361. * @param file: pointer to the source file name
  362. * @param line: assert_param error line source number
  363. * @retval None
  364. */
  365. void assert_failed(uint8_t* file, uint32_t line)
  366. {
  367. /* User can add his own implementation to report the file name and line number,
  368. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  369.  
  370. /* Infinite loop */
  371. while (1)
  372. {
  373. }
  374. }
  375. #endif
  376.  
  377. /*
  378. * Minimal __assert_func used by the assert() macro
  379. * */
  380. void __assert_func(const char *file, int line, const char *func, const char *failedexpr)
  381. {
  382. while(1)
  383. {}
  384. }
  385.  
  386. /*
  387. * Minimal __assert() uses __assert__func()
  388. * */
  389. void __assert(const char *file, int line, const char *failedexpr)
  390. {
  391. __assert_func (file, line, NULL, failedexpr);
  392. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement