Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. #include "stm32f4xx.h"
  2. #include "stm32f4xx_gpio.h"
  3. #include "stm32f4xx_rcc.h"
  4. #include "stm32f4xx_tim.h"
  5. #include "misc.h"
  6.  
  7. int a=0;
  8. GPIO_InitTypeDef GPIO_InitStructure;
  9. void Segment(int);
  10. void Wysw(int,int,int,int,int,int,int);
  11.  
  12. void TIM2_IRQHandler()
  13. {
  14. if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
  15. {
  16. TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
  17.  
  18. a=0;
  19. if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_5)){
  20. a=a+1; }
  21. if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_6)){
  22. a=a+2; }
  23. if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_7)){
  24. a=a+4; }
  25. if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_8)){
  26. a=a+8; }
  27.  
  28. if(a>4) Segment(1);
  29. else Segnment(0);
  30. }
  31.  
  32. void Segment (int i){
  33. switch(i){
  34. case 0:
  35. Wysw(0,0,0,1,1,1,0);
  36. break;
  37. case 1:
  38. Wysw(0,1,1,0,1,1,1);
  39. break;
  40. }
  41. }
  42.  
  43. void Wysw(int a, int b, int c, int d, int e, int f, int g){
  44. if(a) GPIO_ResetBits(GPIOB, GPIO_Pin_1);
  45. else GPIO_SetBits(GPIOB, GPIO_Pin_1);
  46. if(b) GPIO_ResetBits(GPIOB, GPIO_Pin_2);
  47. else GPIO_SetBits(GPIOB, GPIO_Pin_2);
  48. if(c) GPIO_ResetBits(GPIOB, GPIO_Pin_3);
  49. else GPIO_SetBits(GPIOB, GPIO_Pin_3);
  50. if(d) GPIO_ResetBits(GPIOB, GPIO_Pin_4);
  51. else GPIO_SetBits(GPIOB, GPIO_Pin_4);
  52. if(e) GPIO_ResetBits(GPIOB, GPIO_Pin_5);
  53. else GPIO_SetBits(GPIOB, GPIO_Pin_5);
  54. if(f) GPIO_ResetBits(GPIOB, GPIO_Pin_6);
  55. else GPIO_SetBits(GPIOB, GPIO_Pin_6);
  56. if(g) GPIO_ResetBits(GPIOB, GPIO_Pin_7);
  57. else GPIO_SetBits(GPIOB, GPIO_Pin_7);
  58.  
  59. }
  60.  
  61. int main(void)
  62. {
  63. GPIO_InitTypeDef gpio;
  64. GPIO_InitTypeDef wa, wb, wc, wd, we, wf, wg;
  65. GPIO_InitTypeDef c0, c1, c2, c3;
  66. TIM_TimeBaseInitTypeDef tim;
  67. NVIC_InitTypeDef nvic;
  68.  
  69. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  70. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  71. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  72. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  73.  
  74. GPIO_StructInit(&gpio);
  75. gpio.GPIO_Pin = GPIO_Pin_5;
  76. gpio.GPIO_Mode = GPIO_Mode_OUT;
  77. GPIO_Init(GPIOA, &gpio);
  78.  
  79. GPIO_StructInit(&wa);
  80. wa.GPIO_Pin = GPIO_Pin_1;
  81. wa.GPIO_Mode = GPIO_Mode_OUT;
  82. GPIO_Init(GPIOB, &wa);
  83.  
  84. GPIO_StructInit(&wb);
  85. wb.GPIO_Pin = GPIO_Pin_2;
  86. wb.GPIO_Mode = GPIO_Mode_OUT;
  87. GPIO_Init(GPIOB, &wb);
  88.  
  89. GPIO_StructInit(&wc);
  90. wc.GPIO_Pin = GPIO_Pin_3;
  91. wc.GPIO_Mode = GPIO_Mode_OUT;
  92. GPIO_Init(GPIOB, &wc);
  93.  
  94. GPIO_StructInit(&wd);
  95. wd.GPIO_Pin = GPIO_Pin_4;
  96. wd.GPIO_Mode = GPIO_Mode_OUT;
  97. GPIO_Init(GPIOB, &wd);
  98.  
  99. GPIO_StructInit(&we);
  100. we.GPIO_Pin = GPIO_Pin_5;
  101. we.GPIO_Mode = GPIO_Mode_OUT;
  102. GPIO_Init(GPIOB, &we);
  103.  
  104. GPIO_StructInit(&wf);
  105. wf.GPIO_Pin = GPIO_Pin_6;
  106. wf.GPIO_Mode = GPIO_Mode_OUT;
  107. GPIO_Init(GPIOB, &wf);
  108.  
  109. GPIO_StructInit(&wg);
  110. wg.GPIO_Pin = GPIO_Pin_7;
  111. wg.GPIO_Mode = GPIO_Mode_OUT;
  112. GPIO_Init(GPIOB, &wg);
  113.  
  114. GPIO_StructInit(&c0);
  115. c0.GPIO_Pin = GPIO_Pin_5;
  116. c0.GPIO_Mode = GPIO_Mode_IN;
  117. c0.GPIO_PuPd = GPIO_PuPd_DOWN;
  118. GPIO_Init(GPIOC, &c0);
  119.  
  120. GPIO_StructInit(&c1);
  121. c1.GPIO_Pin = GPIO_Pin_6;
  122. c1.GPIO_Mode = GPIO_Mode_IN;
  123. c1.GPIO_PuPd = GPIO_PuPd_DOWN;
  124. GPIO_Init(GPIOC, &c1);
  125.  
  126. GPIO_StructInit(&c2);
  127. c2.GPIO_Pin = GPIO_Pin_7;
  128. c2.GPIO_Mode = GPIO_Mode_IN;
  129. c2.GPIO_PuPd = GPIO_PuPd_DOWN;
  130. GPIO_Init(GPIOC, &c2);
  131.  
  132. GPIO_StructInit(&c3);
  133. c3.GPIO_Pin = GPIO_Pin_8;
  134. c3.GPIO_Mode = GPIO_Mode_IN;
  135. c3.GPIO_PuPd = GPIO_PuPd_DOWN;
  136. GPIO_Init(GPIOC, &c3);
  137.  
  138. TIM_TimeBaseStructInit(&tim);
  139. tim.TIM_CounterMode = TIM_CounterMode_Up;
  140. tim.TIM_Prescaler = 16000 - 1;
  141. tim.TIM_Period = 2 - 1;
  142. TIM_TimeBaseInit(TIM2, &tim);
  143.  
  144. TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
  145. TIM_Cmd(TIM2, ENABLE);
  146.  
  147.  
  148. nvic.NVIC_IRQChannel = TIM2_IRQn;
  149. nvic.NVIC_IRQChannelPreemptionPriority = 0;
  150. nvic.NVIC_IRQChannelSubPriority = 0;
  151. nvic.NVIC_IRQChannelCmd = ENABLE;
  152. NVIC_Init(&nvic);
  153.  
  154.  
  155.  
  156. while (1) {
  157. }
  158.  
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement