Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * cv5H.h
- *
- * Created on: 18. 10. 2016
- * Author: Peter
- */
- #ifndef CV5H_H_
- #define CV5H_H_
- void adc_init(void);
- void init_NVIC(void);
- void ADC1_IRQHandler(void);
- void initUSART2(void); // usart 1
- void PutcUART2(char);// usart 1
- void RegisterCallbackUART2(void *callback); // usart 1
- void USART2_IRQHandler(void); // usart 1
- double prevod();
- void stav(uint16_t);
- void Put(char []);
- #endif /* CV5H_H_ */
- /*
- * cv5C.c
- *
- * Created on: 18. 10. 2016
- * Author: Peter
- */
- #include <stddef.h>
- #include "stm32l1xx.h"
- #include "cv5H.h"
- #include <stdio.h>
- #include <string.h>
- void adc_init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- ADC_InitTypeDef ADC_InitStructure;
- /* Enable GPIO clock */
- RCC_AHBPeriphClockCmd(GPIOB, ENABLE);//Opraviť a upraviť
- /* Configure ADCx Channel 2 as analog input */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /* Enable the HSI oscillator */
- RCC_HSICmd(ENABLE);
- /* Check that HSI oscillator is ready */
- while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
- /* Enable ADC clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
- /* Initialize ADC structure */
- ADC_StructInit(&ADC_InitStructure);
- /* ADC1 configuration */
- ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
- ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
- ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
- ADC_InitStructure.ADC_NbrOfConversion = 1;
- ADC_Init(ADC1, &ADC_InitStructure);
- /* ADCx regular channel8 configuration */
- ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_96Cycles);
- /* Enable the ADC */
- init_NVIC();
- ADC_Cmd(ADC1, ENABLE);
- /* Wait until the ADC1 is ready */
- while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET)
- {
- }
- /* Start ADC Software Conversion */
- ADC_SoftwareStartConv(ADC1);
- }
- void init_NVIC(void){
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_InitStructure.NVIC_IRQChannel = ADC1_IRQn; //zoznam prerušení nájdete v súbore stm32l1xx.h
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);
- }
- uint16_t value = 0;
- void ADC1_IRQHandler(void){
- if(ADC1->SR & ADC_SR_EOC){
- value = ADC1->DR;
- }
- }
- void initUSART2(void) /// usart 1
- {
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
- // RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART2, ENABLE); // usart 1
- //choosing peripherals for selected pins
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); // GPIO_PinSource10, GPIO_AF_USART1
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2); // GPIO_PinSource9, GPIO_AF_USART1
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; // GPIO_Pin_10 | GPIO_Pin_9;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- //usart configuration
- USART_InitStructure.USART_BaudRate = 9600;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(USART2, &USART_InitStructure); // usart 1
- //configuring interrupts
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
- NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; ///USART1_IRQn
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- //choosing which event should cause interrupt
- USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); // usart 1
- // Enable USART
- USART_Cmd(USART2, ENABLE); // usart 1
- }
- void PutcUART2(char ch){ //PutcUART1
- USART_SendData(USART2, (uint8_t) ch); // usart 1
- while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET); // usart 1
- }
- //char pole[sizeof(double)];
- char pole[];
- void Put(char pole[])
- {
- int i=0;
- while(pole[i]!='\0')
- {
- USART_SendData(USART2, pole[i]); // usart 1
- while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET); // usart 1
- i++;
- }
- USART_SendData(USART2,' ');
- while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
- }
- void (* gCallback1)(unsigned char) = 0;
- void RegisterCallbackUART2(void *callback){ //usart 1
- gCallback1 = callback;
- }
- void USART2_IRQHandler(void) //USART1_IRQHandler
- {
- uint16_t pom = 0;
- if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) // usart 1
- {
- USART_ClearITPendingBit(USART2, USART_IT_RXNE); // usart 1
- pom = USART_ReceiveData(USART2); // usart 1
- if (gCallback1)
- {
- gCallback1(pom);
- }
- }
- }
- double pom10=0;
- double prevod()
- {
- pom10=value;
- return pom10*=0.000805;
- }
- int pom8=0;
- double pom9 = 0;
- void stav(uint16_t hodnota){
- switch (hodnota){
- case 'm' :
- if(pom8==0)
- {
- pom9 = prevod();
- uint8_t num1= (uint8_t)pom9;
- sprintf(pole,"%d.%dV", num1, (uint8_t)((pom9-num1)*100));
- Put(pole);
- pom8++;
- }
- else
- {
- //PutcUART2(value);
- sprintf(pole,"%d",value);
- Put(pole);
- pom8=0;
- }
- break;
- }
- }
- /*
- ******************************************************************************
- File: main.c
- Info: Generated by Atollic TrueSTUDIO(R) 6.0.0 2016-10-18
- The MIT License (MIT)
- Copyright (c) 2009-2016 Atollic AB
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- ******************************************************************************
- */
- /* Includes */
- #include <stddef.h>
- #include "stm32l1xx.h"
- #include "cv5H.h"
- extern uint16_t value;
- /* Private typedef */
- /* Private define */
- /* Private macro */
- /* Private variables */
- /* Private function prototypes */
- /* Private functions */
- /**
- **===========================================================================
- **
- ** Abstract: main program
- **
- **===========================================================================
- */
- int main(void)
- {
- uint16_t pom1;
- double pom11;
- int i = 0;
- char ch ='X';
- /**
- * IMPORTANT NOTE!
- * See the <system_*.c> file and how/if the SystemInit() function updates
- * SCB->VTOR register. Sometimes the symbol VECT_TAB_SRAM needs to be defined
- * when building the project if code has been located to RAM and interrupts
- * are used. Otherwise the interrupt table located in flash will be used.
- * E.g. SCB->VTOR = 0x20000000;
- */
- /**
- * At this stage the microcontroller clock setting is already configured,
- * this is done through SystemInit() function which is called from startup
- * file (startup_stm32l1xx_hd.s) before to branch to application main.
- * To reconfigure the default setting of SystemInit() function, refer to
- * system_stm32l1xx.c file
- */
- /* TODO - Add your application code here */
- adc_init();
- initUSART2(); // usart 1
- // PutcUART2(ch); // usart 1
- /* Infinite loop */
- while (1)
- {
- RegisterCallbackUART2(stav); // usart 1
- i++;
- }
- return 0;
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
- /*
- * Minimal __assert_func used by the assert() macro
- * */
- void __assert_func(const char *file, int line, const char *func, const char *failedexpr)
- {
- while(1)
- {}
- }
- /*
- * Minimal __assert() uses __assert__func()
- * */
- void __assert(const char *file, int line, const char *failedexpr)
- {
- __assert_func (file, line, NULL, failedexpr);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement