Advertisement
Guest User

Untitled

a guest
Jun 30th, 2017
707
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. #include "stm32f10x.h"
  2. #include "stm32f10x_rcc.h"
  3. #include "stm32f10x_gpio.h"
  4. #include "stm32f10x_tim.h"
  5. #include "stm32f10x_exti.h"
  6. #include "delay.h"
  7. #include "lcd16x2.h"
  8. #include <stdio.h>
  9.  
  10. // LCD custom char
  11. uint8_t bar[][8] =
  12. {
  13. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  14. { 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10 },
  15. { 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18 },
  16. { 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C },
  17. { 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E },
  18. { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F }
  19. };
  20. /*
  21. volatile uint8_t last_enc_val_a_B; //broach
  22. volatile uint8_t last_enc_val_b_B; //broach
  23. volatile uint8_t last_enc_val_a_M; //motor
  24. volatile uint8_t last_enc_val_b_M; //motor
  25. volatile int enc_cnt_B; //broach
  26. volatile int enc_cnt_M; //motor
  27. char enc_cnt_buf_B[8]; //broach
  28. char enc_cnt_buf_M[8]; //motor
  29. */
  30. int CW = 0;
  31. int CCW = 500;
  32. volatile int valeur_moteur = 0;
  33. int pitch_100 = 0;
  34. int codeur = 0;
  35. char codeur_buf[8];
  36.  
  37. void init_lcd(void);
  38. void intro_LCD(void);
  39. void lcd_update(void);
  40. void init_motor(void);
  41. void test_motor_CW(void);
  42. void test_motor_CCW(void);
  43. void init_timer(void);
  44.  
  45.  
  46. int main(void)
  47. {
  48. DelayInit();
  49. init_lcd();
  50. intro_LCD();
  51. init_motor();
  52.  
  53. while (1)
  54. {
  55. lcd_update();
  56.  
  57. for(CW; CW<500; CW++)
  58. {
  59. test_motor_CW();
  60. }
  61. DelayMs(500);
  62. for(CCW; CCW>0; CCW--)
  63. {
  64. test_motor_CCW();
  65. }
  66. }
  67. }
  68.  
  69.  
  70. void init_timer()
  71. {
  72.  
  73. // Step 1: Initialize GPIO as input for rotary encoder
  74. // PB6 (encoder pin A), PB5 (encoder pin B)
  75. GPIO_InitTypeDef GPIO_InitStruct;
  76. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  77. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_5;
  78. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
  79. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
  80. GPIO_Init(GPIOB, &GPIO_InitStruct);
  81.  
  82. NVIC_InitTypeDef NVIC_InitStructure;
  83. /* Enable the TIM4 global Interrupt */
  84. NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
  85. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  86. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  87. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  88. NVIC_Init(&NVIC_InitStructure);
  89.  
  90. /* TIM4 clock enable */
  91. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
  92. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  93. TIM_TimeBaseStructure.TIM_Period = 500; // 1 MHz down to 1 KHz (1 ms)
  94. TIM_TimeBaseStructure.TIM_Prescaler = 4000; // 24 MHz Clock down to 1 MHz (adjust per your clock)
  95. TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  96. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  97. TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
  98.  
  99. TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE); // TIM IT enable
  100. TIM_Cmd(TIM4, ENABLE); // TIM4 enable counter
  101. }
  102.  
  103. void TIM4_IRQHandler(void)
  104. {
  105. if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
  106. {
  107. TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
  108. codeur++;
  109. }
  110. }
  111. void test_motor_CW()
  112. {
  113. GPIO_WriteBit(GPIOA, GPIO_Pin_3, 0);
  114. DelayUs(250);
  115. GPIO_WriteBit(GPIOA, GPIO_Pin_2, 1);
  116. DelayUs(250);
  117. GPIO_WriteBit(GPIOA, GPIO_Pin_2, 0);
  118. DelayUs(250);
  119. }
  120.  
  121. void test_motor_CCW()
  122. {
  123. GPIO_WriteBit(GPIOA, GPIO_Pin_3, 1);
  124. DelayUs(250);
  125. GPIO_WriteBit(GPIOA, GPIO_Pin_2, 1);
  126. DelayUs(250);
  127. GPIO_WriteBit(GPIOA, GPIO_Pin_2, 0);
  128. DelayUs(250);
  129. }
  130.  
  131.  
  132. void init_motor()
  133. {
  134. GPIO_InitTypeDef GPIO_InitStruct;
  135. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  136. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_2;
  137. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
  138. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
  139. GPIO_Init(GPIOA, &GPIO_InitStruct);
  140. GPIO_WriteBit(GPIOA, GPIO_Pin_2, 0);
  141. GPIO_WriteBit(GPIOA, GPIO_Pin_3, 0);
  142. }
  143.  
  144. void init_lcd()
  145. {
  146. uint8_t i;
  147.  
  148. // Initialize LCD
  149. lcd16x2_init(LCD16X2_DISPLAY_ON_CURSOR_OFF_BLINK_OFF);
  150.  
  151. // Fill custom char
  152. for (i = 0; i < 8; i++)
  153. {
  154. lcd16x2_create_custom_char(i, bar[i]);
  155. }
  156. }
  157.  
  158.  
  159. void lcd_update()
  160. {
  161. lcd16x2_clrscr();
  162. lcd16x2_gotoxy(0, 0);
  163. lcd16x2_puts("test");
  164. lcd16x2_gotoxy(0, 1);
  165. sprintf(codeur_buf, "%i", codeur);
  166. lcd16x2_puts(codeur_buf);
  167. DelayMs(250);
  168.  
  169. }
  170.  
  171. void intro_LCD()
  172. {
  173. lcd16x2_clrscr();
  174. lcd16x2_gotoxy(0, 0);
  175. lcd16x2_puts("xxx");
  176. lcd16x2_gotoxy(0, 1);
  177. lcd16x2_puts("x");
  178. lcd16x2_gotoxy(0, 2);
  179. lcd16x2_puts("Module filetage V0.1");
  180. DelayMs(1000);
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement