Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.83 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <tm4c123gh6pm.h>
  3.  
  4.  
  5. #define FIRST_INTERRUPT_NUMBER 16  
  6.  
  7.  
  8. char mama_macieja = 0;
  9.  
  10. char readChar(void)
  11. {
  12.   char c;
  13.   while((UART0_FR_R & (1<<4)) != 0);    
  14.   c = UART0_DR_R;
  15.   return c;
  16. }
  17.  
  18. char getChar() {
  19.     if (mama_macieja) {
  20.         char x = mama_macieja;
  21.         mama_macieja = 0;
  22.         return x;
  23.     }
  24.     return 0;
  25. }
  26.  
  27.          
  28. void printChar(char c)
  29. {
  30.   while((UART0_FR_R & (1<<5)) != 0);    
  31.   UART0_DR_R = c;        
  32. }
  33.    
  34. void printString(char * string)              
  35. {
  36.   while(*string)
  37.   {
  38.     printChar(*(string++));            
  39.   }
  40. }
  41.  
  42.  
  43.  
  44. typedef enum EColor {
  45.   GREEN = 0,
  46.   BLUE,
  47.   RED,
  48.   NONE
  49. };
  50.  
  51.  
  52. static void init(){
  53.   SYSCTL_RCGCUART_R |= (1<<0);
  54.  
  55.  
  56.   SYSCTL_RCGCGPIO_R |= (1<<0);
  57.  
  58.   GPIO_PORTA_AFSEL_R = (1<<1) | (1<<0);              
  59.  
  60.   GPIO_PORTA_PCTL_R = (1<<0) | (1<<4);
  61.  
  62.   GPIO_PORTA_DEN_R = (1<<0) | (1<<1);
  63.    
  64.   UART0_CTL_R &= ~(1<<0);        
  65.  
  66.   UART0_IBRD_R = 104;
  67.  
  68.  
  69.   UART0_FBRD_R = 11;
  70.  
  71.  
  72.   UART0_LCRH_R = (0x3<<5);      
  73.  
  74.    
  75.   UART0_CTL_R = (1<<0) | (1<<8) | (1<<9);
  76.  
  77.  
  78.  SYSCTL_RCGCGPIO_R |= (1<<5);
  79.  
  80.   SYSCTL_RCGCGPIO_R = 0x20;
  81.   GPIO_PORTF_LOCK_R = 0x4C4F434B;
  82.   GPIO_PORTF_CR_R = 0xff;
  83.   GPIO_PORTF_DIR_R = 0xe;
  84.   GPIO_PORTF_PUR_R = 0x11;
  85.   GPIO_PORTF_DEN_R = 0x1f;
  86.  
  87.  
  88.   GPIO_PORTF_IS_R = 0x0;                     
  89.   GPIO_PORTF_IBE_R = 0x0;                  
  90.   GPIO_PORTF_IEV_R = 0x0;                  
  91.   GPIO_PORTF_IM_R = 0x11;                  
  92.  
  93.   NVIC_EN0_R |= (1<<(INT_GPIOF-FIRST_INTERRUPT_NUMBER));
  94.  
  95.  
  96.     // konfiguracja timer
  97.   SYSCTL_RCGCTIMER_R |= (1<<0); // wylacz timer
  98.  
  99.  
  100.   TIMER0_CTL_R &= ~(1<<0);              
  101.   TIMER0_CFG_R = 0x0;                  
  102.  
  103.   TIMER0_TAMR_R |= (0x2<<0);     // okresowy      
  104.  
  105.   TIMER0_TAMR_R &= ~(1<<4);      // down      
  106.  
  107.   TIMER0_TAILR_R = 0x00F42400;   // 16 mln, bo 16 mhz -- sekunda  
  108.  
  109.   TIMER0_IMR_R |= (1<<0); //w³¹czamy timer
  110.   NVIC_EN0_R |= (1<<(INT_TIMER0A-FIRST_INTERRUPT_NUMBER));
  111.   TIMER0_CTL_R |= (1<<0); // start
  112. }
  113.  
  114. static int timer_w = 1;
  115. static int F = -1;
  116.  static char blink = 0;
  117. static enum EColor top[12] = {GREEN, GREEN, GREEN, GREEN, GREEN, BLUE, RED, RED, RED, RED, RED, BLUE};
  118. static int topIdx = 0;
  119. static enum EColor bot[2] = {BLUE, RED};
  120. static int botIdx = 0;
  121.  
  122.  
  123. static void setColor(enum EColor color){
  124.   unsigned char portData;
  125.   switch (color){
  126.     case GREEN:
  127.       portData = (1<<3);
  128.       break;
  129.     case BLUE:
  130.       portData = (1<<2);
  131.       break;
  132.     case RED:
  133.       portData = (1<<1);
  134.       break;
  135.   case NONE:
  136.         portData = ~((1<<1) | (1<<2) | (1<<3));
  137.         break;
  138.   }
  139.  
  140.   GPIO_PORTF_DATA_R &= 0xF1;               
  141.   GPIO_PORTF_DATA_R |= portData;
  142. }
  143.  
  144. void T_Handler(void)
  145. {
  146.     char c = getChar();
  147.    
  148.     if (F==0)
  149.       setColor(GREEN);
  150.     if (F==0 && c == 's') {
  151.         F = 2;
  152.     }
  153.     else if (F==1) {
  154.      setColor(top[topIdx++]);
  155.      if (topIdx == 12) {
  156.        F = 0;
  157.        topIdx = 0;
  158.      }
  159.     } else if (F==2) {
  160.       setColor(bot[botIdx++]);
  161.       botIdx %= 2;
  162.     }
  163.    
  164.   TIMER0_ICR_R |= (1<<0); //przerrwanie obs³u¿one
  165. }
  166.  
  167. void GPIOF_Handler(void)
  168. {
  169.   char prev_second = 0;
  170.    
  171.     char first = 0, second = 0;
  172.     switch(GPIO_PORTF_DATA_R & 0x11)    
  173.     {
  174.     case 0x00:                  // dwa
  175.       first = second = 1;      
  176.       break;
  177.      
  178.     case 0x01:        // jedna             
  179.       first = 1;       
  180.       break;
  181.      
  182.     case 0x10:                // druga     
  183.       second = 1;      
  184.       break;
  185.     }
  186.  
  187.     if (F==0 && first) {
  188.       F = 1;
  189.     } else if (F==0 && second) {
  190.     } else if (F==2&& second) {
  191.       F = 0;
  192.       botIdx = 0;
  193.       setColor(GREEN);
  194.     }
  195.    
  196.   GPIO_PORTF_ICR_R = 0x11; // przerwanie obsluzone
  197. }
  198.  
  199. int main()
  200. {
  201.   init();
  202.   setColor(GREEN);
  203.   F= 0;
  204.   while (1){
  205.     mama_macieja = getChar();
  206.   }
  207.   return 0;
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement