Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. //======================================================= //
  2. // Author: Ariel Antonowicz, Arkadiusz Kwiatkowski = //
  3. // Last modified: 08.12.2016 = //
  4. // RGB + BUTTONS (INTERUPTS LABORATORY) = //
  5. // Tiva C Series TM4C123G = //
  6. // Doc: Tiva - DATA SHEET = //
  7. // Tiva - TM4C123G = //
  8. // Lab II - Example (BUTOONS INTERUPS) = //
  9. //======================================================= //
  10.  
  11. #include <stdint.h>
  12. #include <tm4c123gh6pm.h>
  13.  
  14.  
  15. #define FIRST_INTERRUPT_NUMBER 16
  16.  
  17. int isLightsChanging = 0;
  18. int isBlueBlinks = 0;
  19. int isSw1 = 0;
  20. int isSw2 = 0;
  21. int timeCounter = 0;
  22.  
  23. typedef enum EColor {
  24. GREEN = 0,
  25. BLUE,
  26. RED
  27. };
  28.  
  29. static void init(){
  30.  
  31. SYSCTL_RCGCGPIO_R = 0x20;
  32. GPIO_PORTF_LOCK_R = 0x4C4F434B;
  33. GPIO_PORTF_CR_R = 0xff;
  34. GPIO_PORTF_DIR_R = 0xe;
  35. GPIO_PORTF_PUR_R = 0x11;
  36. GPIO_PORTF_DEN_R = 0x1f;
  37.  
  38.  
  39. GPIO_PORTF_IS_R = 0x0;
  40. GPIO_PORTF_IBE_R = 0x0;
  41. GPIO_PORTF_IEV_R = 0x0;
  42. GPIO_PORTF_IM_R = 0x11;
  43.  
  44. // wlacz przerwania dla przyciskow
  45. NVIC_EN0_R |= (1<<(INT_GPIOF-FIRST_INTERRUPT_NUMBER));
  46.  
  47. SYSCTL_RCGCTIMER_R |= (1<<0);
  48.  
  49. // wylacz timer aby moc go skonfigurowac
  50. TIMER0_CTL_R &= ~(1<<0);
  51. TIMER0_CFG_R = 0x0;
  52.  
  53. //0x2 periodic mode(liczy w kolko), 0x1 one-shot mode
  54. TIMER0_TAMR_R |= (0x2<<0);
  55. // liczy w dol
  56. TIMER0_TAMR_R &= ~(1<<4);
  57.  
  58. TIMER0_TAILR_R = 0x00F42400; // 16 milionow, bo 16MHz zegar jest, czyli 16 milionow cykli da 1 sekunde
  59.  
  60. // jak doliczy do zera, zostanie wywolane przerwanie
  61. TIMER0_IMR_R |= (1<<0);
  62.  
  63. //wgranie przerwan z pliku startup, pierwszych 16 sie nie modyfikuje przerwan
  64. NVIC_EN0_R |= (1<<(INT_TIMER0A-FIRST_INTERRUPT_NUMBER));
  65.  
  66. // wlacz timer ( wartosc 1)
  67. TIMER0_CTL_R |= (1<<0);
  68.  
  69. // aktywacja UART
  70. SYSCTL_RCGCUART_R |= (1<<0);
  71.  
  72. // ustawianie portu A w trybie run mode
  73. SYSCTL_RCGCGPIO_R |= (1<<0);
  74.  
  75. // do portu a przypisujemy uart (PA0 i PA1 nie sa traktowane jako GPIO tylko jako UART)
  76. GPIO_PORTA_AFSEL_R = (1<<1) | (1<<0);
  77.  
  78. GPIO_PORTA_PCTL_R = (1<<0) | (1<<4);
  79.  
  80. GPIO_PORTA_DEN_R = (1<<0) | (1<<1);
  81.  
  82. // dezaktywacja UART w celu konfiguracji
  83. UART0_CTL_R &= ~(1<<0);
  84.  
  85. // dzielnik szybkosci
  86. UART0_IBRD_R = 104;
  87.  
  88. // dzielnik transmisji
  89. UART0_FBRD_R = 11;
  90.  
  91. // dlugosc slowa, bit parzystosci, bit stopu (8 bit, bez bitu parzystosci 1-stop bit)
  92. UART0_LCRH_R = (0x3<<5);
  93.  
  94. // wlaczamy uart
  95. UART0_CTL_R = (1<<0) | (1<<8) | (1<<9);
  96. }
  97.  
  98. static void setColor(enum EColor color){
  99. unsigned char portData;
  100. switch (color){
  101. case GREEN:
  102. portData = (1<<3);
  103. break;
  104. case BLUE:
  105. portData = (1<<2);
  106. break;
  107. case RED:
  108. portData = (1<<1);
  109. break;
  110. }
  111.  
  112. GPIO_PORTF_DATA_R &= 0xF1;
  113. GPIO_PORTF_DATA_R |= portData;
  114. }
  115.  
  116. void GPIOF_Handler(void)
  117. { // w rekacji na nacisniecie przycisku zaswiec dioda
  118. if(isLightsChanging == 0) {
  119. if (GPIO_PORTF_DATA_R & 0x10){ //1010
  120. isSw1 = 1;
  121. } else {
  122. if(isSw2) {
  123. isSw2 = 0;
  124. setColor(GREEN);
  125. }
  126. else
  127. isSw2 = 1;
  128. }
  129. }
  130.  
  131. // przerwanie zostalo obsluzone
  132. GPIO_PORTF_ICR_R = 0x11;
  133. }
  134.  
  135. //Oczekuje na wcisniecie przycisku z klawiatury, a wcisniety klawisz zapisuje do c
  136. char readChar(void)
  137. {
  138. char c;
  139. while((UART0_FR_R & (1<<4)) != 0);
  140. c = UART0_DR_R;
  141. return c;
  142. }
  143.  
  144.  
  145. //Pobiera znak i wyswietla na konsoli
  146. void printChar(char c)
  147. {
  148. while((UART0_FR_R & (1<<5)) != 0);
  149. UART0_DR_R = c;
  150. }
  151.  
  152. //Wyswietla bajt po bajcie slowo w konsoli
  153. void printString(char * string)
  154. {
  155. while(*string)
  156. {
  157. printChar(*(string++));
  158. }
  159. }
  160.  
  161. void TIMER0A_Handler(void)
  162. {
  163. if(isSw1 == 1) {
  164. isLightsChanging = 1;
  165. timeCounter++;
  166. setColor(GREEN);
  167. if(timeCounter == 5) {
  168. setColor(BLUE);
  169. TIMER0_ICR_R |= (1<<0);
  170. return;
  171. }
  172. if(timeCounter == 6)
  173. {
  174. setColor(RED);
  175. TIMER0_ICR_R |= (1<<0);
  176. return;
  177. }
  178.  
  179. if(timeCounter == 11)
  180. {
  181. setColor(BLUE);
  182. TIMER0_ICR_R |= (1<<0);
  183. return;
  184. }
  185.  
  186. if(timeCounter == 13) {
  187. setColor(GREEN);
  188. isSw1 = 0;
  189. isLightsChanging = 0;
  190. timeCounter == 0;
  191. return;
  192. }
  193. }
  194. if(isSw2 == 1) {
  195. unsigned char portData;
  196. if(isBlueBlinks == 1) {
  197. isBlueBlinks = 0;
  198. portData = (0<<2);
  199. GPIO_PORTF_DATA_R &= 0xF1;
  200. GPIO_PORTF_DATA_R |= portData;
  201.  
  202. }
  203. else {
  204. isBlueBlinks = 1;
  205. setColor(BLUE);
  206. }
  207.  
  208. }
  209.  
  210. TIMER0_ICR_R |= (1<<0); // przerwanie zostalo obsluzone, wraca do main
  211. }
  212.  
  213. int main()
  214. {
  215. char c;
  216.  
  217. init();
  218. setColor(GREEN);
  219. // brak dostepu do neta na zaliczeniu!
  220. // wszystko na pendrive!
  221.  
  222. // na pol oceny wyzej:
  223. //
  224. // R B G
  225. // |---------1s------------------------|
  226. // | |
  227. // --> G --> SW1 -5s-> B -1s-> E -5s-> B->|
  228. // | |
  229. // | |-> przycisk s -> B-blink -> Sw2---|
  230. // | |
  231. // |---------------------------------------
  232.  
  233. while(1){
  234.  
  235. printString("Enter \"s\"");
  236.  
  237. c = readChar();
  238. printChar(c);
  239. printString("\n\r");
  240. switch(c)
  241. {
  242. case 's':
  243. isSw2 = 1;
  244. break;
  245. default:
  246. break;
  247. }
  248. }
  249.  
  250. return 0;
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement