Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. #include <stdint.h>
  2. #include "tm4c123gh6pm.h"
  3. #include "string.h"
  4. #define SYSTICK_RELOAD_VALUE 80000 // 5 mS
  5. #define NVIC_INT_CTRL_PEND_SYST 0x04000000 // Pend a systick int
  6. #define NVIC_INT_CTRL_UNPEND_SYST 0x02000000 // Unpend a systick int
  7. #define SYSTICK_PRIORITY 0x7E
  8.  
  9. /*
  10. 0x02 - red
  11. 0x04 - blue
  12. 0x06 - magenta (rozowy)
  13. 0x08 - green
  14. 0x0A - yellow
  15. 0x0C - cyan
  16. 0x0E - white
  17. */
  18.  
  19. volatile static int tim, zd;
  20. //for couting the interval between button clicks
  21. static int a;
  22. //a - iteration of timer, one is 5 ms
  23. static int aa,or,sw,n,b,xd,i;
  24. //aa - auto; or - reverse; sw - normal; n - clicks;
  25. //b - needed for clicking; xd - not used; i - for iteration
  26. static int vibr; //for the button vibration
  27.  
  28. void SwitchM(void)
  29. {
  30. switch (i)
  31. {
  32. case 0:
  33. GPIO_PORTF_DATA_R=0x00;
  34. i=7;
  35. break;
  36. case 1:
  37. GPIO_PORTF_DATA_R=0x08;
  38. i--;
  39. break;
  40. case 2:
  41. GPIO_PORTF_DATA_R=0x04;
  42. i--;
  43. break;
  44. case 3:
  45. GPIO_PORTF_DATA_R=0x0C;
  46. i--;
  47. break;
  48. case 4:
  49. GPIO_PORTF_DATA_R=0x02;
  50. i--;
  51. break;
  52. case 5:
  53. GPIO_PORTF_DATA_R=0x0A;
  54. i--;
  55. break;
  56. case 6:
  57. GPIO_PORTF_DATA_R=0x06;
  58. i--;
  59. break;
  60. case 7:
  61. GPIO_PORTF_DATA_R=0x0E;
  62. i--;
  63. break;
  64. default:
  65. ;
  66. }
  67.  
  68. }
  69. void SwitchP(void)
  70. {
  71. switch (i)
  72. {
  73. case 0:
  74. GPIO_PORTF_DATA_R=0x00;
  75. i++;
  76. break;
  77. case 1:
  78. GPIO_PORTF_DATA_R=0x08;
  79. i++;
  80. break;
  81. case 2:
  82. GPIO_PORTF_DATA_R=0x04;
  83. i++;
  84. break;
  85. case 3:
  86. GPIO_PORTF_DATA_R=0x0C;
  87. i++;
  88. break;
  89. case 4:
  90. GPIO_PORTF_DATA_R=0x02;
  91. i++;
  92. break;
  93. case 5:
  94. GPIO_PORTF_DATA_R=0x0A;
  95. i++;
  96. break;
  97. case 6:
  98. GPIO_PORTF_DATA_R=0x06;
  99. i++;
  100. break;
  101. case 7:
  102. GPIO_PORTF_DATA_R=0x0E;
  103. i=0;
  104. break;
  105. default:
  106. ;
  107. }
  108.  
  109. }
  110.  
  111. void SysTick_Handler(void) //interrupt for systick
  112. {
  113.  
  114. a++; //iteration
  115.  
  116. if(zd==1)
  117. {
  118. tim++;
  119. }
  120.  
  121. if(a>20 && zd==0)vibr=1; //vibration
  122.  
  123. if(a>=400 && zd==0) //auto mode
  124. {
  125. aa=1;
  126. sw=0;
  127. n=0;
  128. a=0;
  129. }
  130.  
  131. if(aa==1 && a>50 && or==0) //auto mode next, switching lights
  132. {
  133. //if (i!=0 && i!=7)i++;
  134. SwitchP();
  135. if(n>=1)
  136. {
  137. aa=0;
  138. a=0;
  139. }
  140. a=0;
  141. }
  142. if(aa==1 && a>50 && or==1) //auto mode next, switching lights
  143. {
  144. SwitchM();
  145. if(n>=1)
  146. {
  147. aa=0;
  148. a=0;
  149. }
  150. a=0;
  151. }
  152. if(n>2)n=0;
  153.  
  154. }
  155.  
  156. void enable_global_int() //enabling global intreuprts
  157. {
  158. // enable interrupts.
  159. __asm("cpsie i");
  160. }
  161.  
  162. void disable_global_int() //for disabling, but not used
  163. {
  164. // disable interrupts.
  165. __asm("cpsid i");
  166. }
  167.  
  168.  
  169. void SysTickInit() //initlaizioatn of systick
  170. {
  171.  
  172. // Disable systick timer
  173. NVIC_ST_CTRL_R &= ~(NVIC_ST_CTRL_ENABLE);
  174.  
  175. // Set current systick counter to reload value
  176. NVIC_ST_CURRENT_R = SYSTICK_RELOAD_VALUE;
  177. // Set Reload value, Systick reload register
  178. NVIC_ST_RELOAD_R = SYSTICK_RELOAD_VALUE;
  179.  
  180. // NVIC systick setup, vector number 15
  181. // Clear pending systick interrupt request
  182. NVIC_INT_CTRL_R |= NVIC_INT_CTRL_UNPEND_SYST;
  183.  
  184. // Set systick priority to 0x10, first clear then set.
  185. NVIC_SYS_PRI3_R &= ~(NVIC_SYS_PRI3_TICK_M);
  186. NVIC_SYS_PRI3_R |= (NVIC_SYS_PRI3_TICK_M & (SYSTICK_PRIORITY<<NVIC_SYS_PRI3_TICK_S));
  187.  
  188. // Select systick clock source, Use core clock
  189. NVIC_ST_CTRL_R |= NVIC_ST_CTRL_CLK_SRC;
  190.  
  191. // Enable systick interrupt
  192. NVIC_ST_CTRL_R |= NVIC_ST_CTRL_INTEN;
  193.  
  194. // Enable and start timer
  195. NVIC_ST_CTRL_R |= NVIC_ST_CTRL_ENABLE;
  196. }
  197.  
  198. void Reset() //resetting systick
  199. {
  200. // Disable systick timer
  201. NVIC_ST_CTRL_R &= ~(NVIC_ST_CTRL_ENABLE);
  202. // Set current systick counter to reload value
  203. NVIC_ST_CURRENT_R = SYSTICK_RELOAD_VALUE;
  204. NVIC_ST_RELOAD_R = SYSTICK_RELOAD_VALUE;
  205. NVIC_INT_CTRL_R |= NVIC_INT_CTRL_UNPEND_SYST;
  206. NVIC_ST_CTRL_R |= NVIC_ST_CTRL_ENABLE;
  207. }
  208. void Off() //turning off systick
  209. {
  210. // Disable systick timer
  211. NVIC_ST_CTRL_R &= ~(NVIC_ST_CTRL_ENABLE);
  212. }
  213.  
  214. int main(void)
  215. {
  216. enable_global_int(); //enabling functions, and stuff
  217. int dummy;
  218. SYSCTL_RCGC2_R = SYSCTL_RCGC2_GPIOF;
  219.  
  220. dummy = SYSCTL_RCGC2_R;
  221.  
  222. GPIO_PORTF_DIR_R=0x0E; //DDRA
  223.  
  224. GPIO_PORTF_DEN_R=0x1E; //like PORTA
  225.  
  226. GPIO_PORTF_LOCK_R = 0x4C4F434B; // Unlock the CR register
  227. GPIO_PORTF_CR_R = 0xFF; // Enable overwrite of PUR to FP0
  228.  
  229. GPIO_PORTF_PUR_R=0x10; //PULL UP HERE
  230.  
  231. //setting some variables at 0, initial values
  232. aa=or=sw=n=b=xd=i=a=tim=xd=0;
  233.  
  234. //setting iintial values for data_r, just in case
  235. GPIO_PORTF_DATA_R=0x00;
  236. // let the normal mode be at the begining
  237. sw=1;
  238.  
  239. //initalization of systick, then turning off
  240. SysTickInit();
  241. Off();
  242. //vibartion
  243. vibr=1;
  244.  
  245. while(1)
  246. {
  247.  
  248.  
  249.  
  250. if (GPIO_PORTF_DATA_R & 0x10) //not pushing!
  251. {
  252. if(aa==0 && zd==0) //if there is no automode, it doesnt count to anything
  253. {
  254.  
  255. Off();
  256. a=0;
  257. }
  258.  
  259. if(zd==0 && aa==0 && n==1 && b==1)
  260. {
  261. Off();
  262. Reset();
  263. zd=1;
  264. }
  265.  
  266. //activating switfh
  267. if(aa==0)sw=1;
  268.  
  269. //activing order reverse
  270. if(xd==1 && or==1)
  271. {
  272. or=0;
  273. xd=0;
  274. }
  275. if(xd==1 && or==0)
  276. {
  277. or=1;
  278. xd=0;
  279. }
  280.  
  281. //switch in work
  282. if (sw==1 && b==1 && or==0 && vibr ==1)
  283. {
  284.  
  285. SwitchP();
  286. }
  287. //reverse in action
  288. if (sw==1 && or==1 && b==1 && vibr==1)
  289. {
  290. SwitchM();
  291. //if (i!=0 && i!=7)i--;
  292. }
  293. b=0;
  294. }
  295. else //pushing!
  296. {
  297. if(b==0)
  298. {
  299. n++;
  300. if(n==2 && tim<100 && zd==1) //500 ms
  301. {
  302. xd=1;
  303. zd=n=a=tim=0;
  304. }
  305. else if(n==2 && zd==1)
  306. {
  307. zd=n=a=tim=0;
  308. }
  309. b=1;
  310. Reset();
  311. }
  312. vibr=0;
  313. while(vibr==0); //so the vibration would not have effect, it waits
  314. }
  315. }
  316. return(0);
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement