Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

STM32 and HC-SR04 timer problem

Status
Not open for further replies.

bonoko

Newbie level 2
Newbie level 2
Joined
Oct 30, 2011
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,349
Hello everybody,

I have a problem with STM32F3-Discovery and module HC-SR04.
My code:

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "main.h"
 
#define TIM_Period_init 40959
#define TRIGGER1_ON  GPIO_SetBits(GPIOC,GPIO_Pin_0);
#define TRIGGER1_OFF GPIO_ResetBits(GPIOC,GPIO_Pin_0);
 
RCC_ClocksTypeDef RCC_Clocks;
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure; 
    
int ograniczenie_czasowe = 0;
int i = 0;
 
volatile uint16_t TIM_Period = TIM_Period_init;
volatile uint16_t START_HCSR1 = 0;
volatile uint16_t pomiar_HCSR1 = 0;
volatile uint16_t pomiar_ULTRA_1 = 100;
volatile int i_t = 0;
 
char tab[10];
 
int main(void)
{                           
    RCC_Configuration();    
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);         
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);       
    
    TIM2_Configuration();           
    GPIO_Configuration();       
    
    USART2_Configuration(); 
    HCSR04_Configuration(); 
            
    while (1)
    {                                           
        ograniczenie_czasowe++;                             
        if (ograniczenie_czasowe > 5000000) 
        {               
                ograniczenie_czasowe =  0;          
                TRIGGER1_ON;
                for(i=0;i<0x4;i++);
                TRIGGER1_OFF; 
                START_HCSR1=1;
            
                sprintf(tab, "%d", i_t);
                send_string(tab);
                send_string(" END\r\n");    
        }       
    }   
}
 
 
void RCC_Configuration(void)
{   
    /* Before this function calling system is working on internal HSI 8MHz */
    /* Flash 1 wait state */
    FLASH_SetLatency(FLASH_Latency_1);
    /* HCLK = SYSCLK = 48MHz*/
    RCC_HCLKConfig(RCC_SYSCLK_Div1);    
    /* PCLK = HCLK = 48MHz*/
    RCC_PCLK1Config(RCC_HCLK_Div1);     
    /* Configure PLL ------------------------------------------------------*/
    /* PLL1 configuration 8MHz/2 * 12 = 48MHz */
    RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_12);               
    /* Enable PLL1 */
    RCC_PLLCmd (ENABLE);    
    /* Wait till PLL1 is ready */
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    } 
    /* Select PLL as system clock source (from HSI to HSI+PLL) */
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); 
    /* Wait till PLL is used as system clock source */
    while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
    {
    }   
}
 
void send_string(const char *str)
{
    while (*str)
    {
        while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
        USART_SendData(USART1, *str++);
    }
}
 
void USART2_Configuration(void)
{
    USART_InitTypeDef USART_InitStructure;
 
  USART_InitStructure.USART_BaudRate = 115200;
  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(USART1, &USART_InitStructure);
    USART_ITConfig (USART1, USART_IT_RXNE, ENABLE); 
  USART_Cmd(USART1, ENABLE);
}
 
 
void GPIO_Configuration(void)
{
    GPIO_InitTypeDef GPIO_InitStructure; 
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7); // TX 
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7); // RX   
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
   
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_Init(GPIOA, &GPIO_InitStructure);    
}
 
 
 
void HCSR04_Configuration(void)
{
    GPIO_InitTypeDef GPIO_InitStructure; 
    EXTI_InitTypeDef EXTI_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    
        // PC1 -- ECHO 1
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
  // PC1 EXT1
  SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource1);
  /* Configure EXTI1 line */
  EXTI_InitStructure.EXTI_Line = EXTI_Line1;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;  
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_InitStructure);
  /* Enable and set EXTI1 Interrupt */
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure); 
    
    
    // Ustawienie wyjsc Triggering
    
    // Trig1 - PC0
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
 
}
void TIM2_Configuration(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
 
    // 48 prescaleker
    #define TIM2_Prescaler 0x30
    #define TIM2_Period 0x0001
 
  /* TIM2 Periph clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  /* Time base configuration */
  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); 
  TIM_TimeBaseStructure.TIM_Period = TIM2_Period;          
  TIM_TimeBaseStructure.TIM_Prescaler = TIM2_Prescaler;       
  TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;    
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
 
  TIM_ARRPreloadConfig(TIM2, ENABLE);   
 
  /* TIM2 TRGO selection: trigger for DAC and ADC conversions */
  TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
  TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
  
 
 
  /* Enable and set TIM2_Update Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;  
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
    
     /* TIM2 enable counter */
  TIM_Cmd(TIM2, ENABLE);
    
}
 
void EXTI0_IRQHandler(void)
{               
  if(EXTI_GetITStatus(EXTI_Line1) != RESET)
  { 
        i_t = 1;
        if(START_HCSR1==2)
        {
            pomiar_ULTRA_1=pomiar_HCSR1/58;
            pomiar_HCSR1=0;
            START_HCSR1=0;
 
        }       
        if(START_HCSR1==1)
        {
            pomiar_HCSR1=0;
            START_HCSR1=2;          
        }       
    EXTI_ClearITPendingBit(EXTI_Line1);
  }
} 
 
void TIM2_IRQHandler(void)
{   
    pomiar_HCSR1++; 
    TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
 
/**
  * @}
  */




My variable i_t isn't never i_t = 1! so program is never in the interruption.
Why?

USART result:
0 END
0 END
0 END
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top