Advertisement
Bobita

marfa_v2

May 24th, 2024
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 18.21 KB | None | 0 0
  1. /*
  2.  * USART2 is connected to the ST-Link virtual COM port.
  3.  * Use Tera Term to interract with STM board
  4.  *
  5.  * By default, the clock is running at 16 MHz.
  6.  * The UART2 is configured for 115200 Baud.
  7.  * PA2 - USART2 TX (AF7)
  8.  * PA3 - USART2 RX (AF7)
  9.  */
  10. #include "stm32f4xx.h"
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <ctype.h>
  14. #include <stdlib.h>
  15. #include "Types.h"
  16. #include "Drivers.h"
  17.  
  18. void delayMs(int);
  19. UART_MSG_T msg = {.data = {0}, .id = 0, .len = 0};
  20. U8 CyclicMessage_U8 = 0;
  21. U8 TimerCountDown_U16 = SECONDS_TO_COUNT_U8;
  22. U8 PrintInCycleMode_U8 = 0;
  23.  
  24. /* Lucrarea 2 de completat */
  25.  
  26. void PeriphInit(void)
  27. {
  28.  
  29.     __disable_irq();
  30.     // Configure PB[7..4] as output
  31.     RCC->AHB1ENR |= 0;           /* Enable GPIOB clock */
  32.     GPIOB->MODER &= 0; /* Reset GPIOB PB[7..4]  */
  33.     GPIOB->MODER |= 0;   /* Set GPIOB PB[7..4]  as ouput */
  34.  
  35.     // Configure PC[11..8] as input
  36.     RCC->AHB1ENR |= 0;           /* Enable GPIOC clock */
  37.     GPIOC->MODER &= 0; /* Reset GPIOC PC[11..8]  for input mode */
  38.  
  39.     // Configure PB[15..12] port as input and enables pull-ups
  40.     GPIOB->MODER &= 0; /* Reset GPIOB PB[15..12]  */
  41.     GPIOB->PUPDR |= 0;   /* Enable pull-ups on GPIOB PB[15..12]  */
  42.  
  43.     USART2_init();
  44.  
  45.     __enable_irq();
  46. }
  47.  
  48. /* TODO: change the 0's to the correct value */
  49. const U8 Hex7Segment[16] = {0};
  50.  
  51. /* end lucrarea 2*/
  52.  
  53. /* Lucrarea 3 De completat*/
  54. static const U32 STRVR = 0;
  55. static const U16 STCTRL = 0;
  56.  
  57. static const U16 TIM3_PSC = 0;
  58. static const U16 TIM3_ARR = 0;
  59.  
  60. static const U16 TIM3_CH1_PSC = 0;
  61. static const U16 TIM3_CH1_ARR = 0;
  62. static const U16 TIM3_CH1_CCMR1 = 0;
  63. static const U16 TIM3_CH1_CCR1 = 0;
  64.  
  65. static const U16 TIM8_CH3_PSC = 0;
  66. static const U16 TIM8_CH3_CCMR2 = 0;
  67.  
  68. U32 timestamp = 0;
  69. U32 capture_time = 0;
  70. U8 nr_of_pushes = 0;
  71. U8 factor = 1;
  72. /* end lucrarea  3 */
  73.  
  74. /* Lucrarea 4 */
  75.  
  76. U16 dac_factor = 600;
  77. U16 dac_hold_value = 0;
  78.  
  79.  
  80. #define ADC_8_BIT 1 //dezactivati pentru adc 12 biti
  81.  
  82. #if ADC_8_BIT
  83. static const U32 ADC_CR1 = 0; /* 8-bit resolution */
  84. static const U32 ADC_CR2 = 0; /* SW Start, Align right justified, start adc */
  85. #else
  86. static const U32 ADC_CR1 = 0; /* 12-bit resolution */
  87. static const U32 ADC_CR2 = 0; /* SW Start, Align left justified,  start ADC */
  88. #endif
  89.  
  90. static const U32 MODER = 0; /* PA0-PA4 ca intrari analogice*/
  91.  
  92. /*End of Lucrarea 4*/
  93.  
  94. /* lucrarea 5 */
  95. static const U32 MODER_IRQ_B = 0; /* PB15-PB12 ca intrari*/
  96. static const U32 MODER_IRQ_C = 0; /* PC11-PC8 ca intrari */
  97. static const U32 PUPDR_IRQ_B = 0; /* PB15-PB12 pull-up*/
  98. static const U16 EXTI_FTSR = 0; /* ISR on PB15-PB12 falling edge*/
  99. static const U16 EXTI_RTSR = 0; /* ISR on PC11-PC8 rising edge*/
  100.  
  101. void IRQInit()
  102. {
  103.     SetupIOInterrupt(MODER_IRQ_B, MODER_IRQ_C, PUPDR_IRQ_B, EXTI_FTSR, EXTI_RTSR);
  104. }
  105. /* end of lucrarea 5 */
  106.  
  107. /* Lucrarea 6 */
  108. static const U16 SPI_CR1 = 0x33C; /* viteza transfer si 8 biti date, master mode */
  109.  
  110. static const U32 GPIOB_OTYPER = ~0x00000300; /* set as open drain */
  111. static const U32 GPIOB_PUPDR = ~0x00050000; /* setup SCL, SDA ca pull-up pentru I2C*/
  112. static const U16 I2C_CR1 = 0x8000;          /* SW resetm ie;ire reset, enable I2C1*/
  113. static const U16 I2CC_CCR = 80;             /* setup as standard mode*/
  114. /* end Lucrarea 6 */
  115.  
  116. U8 ReceiveMessage(void)
  117. {
  118.     char msg_str[MAX_PACKET_STR_SIZE];
  119.     const char *pos = msg_str;
  120.  
  121.     memset(&msg, 0, sizeof(UART_MSG_T));
  122.  
  123.     fgets(msg_str, MAX_PACKET_STR_SIZE, stdin);
  124.  
  125.     if (strlen(msg_str) < 3)
  126.     {
  127.         msg.id = 0xFF;
  128.         return 0;
  129.     }
  130.  
  131.     if (sscanf(pos, "%2hhx", &msg.id) == 0)
  132.     {
  133.         msg.id = 0xFF;
  134.         return 0;
  135.     }
  136.  
  137.     if (strlen(msg_str) > 5)
  138.     {
  139.         pos += 3;
  140.  
  141.         for (int i = 1; i < strlen(msg_str) / 3; i++)
  142.         {
  143.             sscanf(pos, "%2hhx", &msg.data[i - 1]);
  144.             pos += 3;
  145.             msg.len++;
  146.         }
  147.     }
  148.     return 1;
  149. }
  150.  
  151. void PrintReceivedMessage(const UART_MSG_T msg)
  152. {
  153.     printf("the received packet is: \n");
  154.     printf("ID: %02x \n", msg.id);
  155.     printf("Size in Bytes: %d\n", msg.len);
  156.     printf("Data:");
  157.     for (int i = 0; i < msg.len; i++)
  158.         printf("%02X ", msg.data[i]);
  159.     printf("\r\n");
  160. }
  161.  
  162. void ProcessMessage(const UART_MSG_T msg)
  163. {
  164.     U32 p;
  165.     switch (msg.id)
  166.     {
  167.     case SW_VERSION:
  168.         printf("SW Version: 01.03.24-SI\n");
  169.         break;
  170.  
  171.     case PORT_INPUT:
  172.         if (msg.len < 2)
  173.         {
  174.             printf("bad-len\n");
  175.         }
  176.         else if (msg.data[0] != 0x42 && msg.data[0] != 0x43)
  177.         {
  178.             printf("bad-port\n");
  179.         }
  180.         else if (((msg.data[0] == 0x42) && (msg.data[1] > 15 || msg.data[1] < 12)) ||
  181.                  ((msg.data[0] == 0x43) && (msg.data[1] > 11 || msg.data[1] < 8)))
  182.         {
  183.             printf("bad-pin\n");
  184.         }
  185.         else
  186.         {
  187.             U32 i = 1;
  188.             p = msg.data[0] != 0x42 ? GPIOC->IDR : GPIOB->IDR;
  189.             i <<= msg.data[1];
  190.             if (CyclicMessage_U8 == 0 || PrintInCycleMode_U8 == 1)
  191.             {
  192.                 printf("%ld \n", (p & i) >> msg.data[1]);
  193.                 PrintInCycleMode_U8 = 0;
  194.             }
  195.  
  196.             if (CyclicMessage_U8 == 0 && msg.len >= 3)
  197.             {
  198.                 if (msg.data[2] > 0)
  199.                 {
  200.                     CyclicMessage_U8 = 1;
  201.                     InitPeriodicTimer(PERIODIC_TIMER_VALUE_500US_U16);
  202.                 }
  203.             }
  204.         }
  205.  
  206.         break;
  207.     case PORT_OUTPUT:
  208.         if (msg.len < 3)
  209.         {
  210.             printf("bad-len\n");
  211.         }
  212.         else if (msg.data[0] != 0x42)
  213.         {
  214.             printf("bad-port\n");
  215.         }
  216.         else if ((msg.data[1] > 7) || (msg.data[1] < 4))
  217.         {
  218.             printf("bad-pin\n");
  219.         }
  220.         else
  221.         {
  222.             U32 i = 1;
  223.             i <<= msg.data[1];
  224.             GPIOB->BSRR = msg.data[2] ? (GPIOB->BSRR | i) : (GPIOB->BSRR | (i << 16));
  225.             printf("%02lX\n", (GPIOB->ODR & 0x000000F0) >> 4);
  226.         }
  227.         break;
  228.     case PORT_HEX:
  229.         if (msg.len < 2)
  230.         {
  231.             printf("bad-len\n");
  232.         }
  233.         else if (msg.data[0] > 3)
  234.         {
  235.             printf("bad-display\n");
  236.         }
  237.         else if (msg.data[1] > 15)
  238.         {
  239.             printf("bad-number\n");
  240.         }
  241.         else
  242.         {
  243.  
  244.             SPI1_init();
  245.             SPI1_write(Hex7Segment[msg.data[1]]); /* write pattern to the seven segments */
  246.             SPI1_write(1 << msg.data[0]);         /* select digit */
  247.         }
  248.         break;
  249.     case TIMERS:
  250.         if (msg.len < 1)
  251.         {
  252.             printf("bad-len\n");
  253.         }
  254.         else
  255.         {
  256.             SystemTickTimerStop();
  257.             switch (msg.data[0])
  258.             {
  259.             /*general timer */
  260.             case GENERAL_TIM3:
  261.                 if (CyclicMessage_U8 == 0)
  262.                 {
  263.                     TIM3_Cfg(TIM3_PSC, TIM3_ARR);
  264.                     timestamp = 0;
  265.  
  266.                     if (msg.len > 1)
  267.                     {
  268.                         TIM3->ARR = ((TIM3->ARR + 1) * msg.data[1]) - 1;
  269.                         factor = msg.data[1];
  270.                     }
  271.                 }
  272.                 if (CyclicMessage_U8 == 0 || PrintInCycleMode_U8 == 1)
  273.                 {
  274.                     printf("%ld \n", timestamp);
  275.                     PrintInCycleMode_U8 = 0;
  276.                 }
  277.  
  278.                 if (CyclicMessage_U8 == 0 && msg.len >= 3)
  279.                 {
  280.                     if (msg.data[2] > 0)
  281.                     {
  282.                         CyclicMessage_U8 = 1;
  283.                         InitPeriodicTimer(PERIODIC_TIMER_VALUE_500US_U16);
  284.                     }
  285.                     else
  286.                     {
  287.                         TIM3_Stop();
  288.                     }
  289.                 }
  290.                 break;
  291.             /* out compare */
  292.             case TIM3_CH1_COMPARE:
  293.                 TIM3_CH1_Compare_Cfg(TIM3_CH1_PSC, TIM3_CH1_ARR, TIM3_CH1_CCMR1, TIM3_CH1_CCR1);
  294.                 if (msg.len > 1)
  295.                 {
  296.                     TIM3->ARR = TIM3->ARR * msg.data[1];
  297.                 }
  298.                 break;
  299.             /* in capture */
  300.             case TIM8_CH3_CAPTURE:
  301.                 if (CyclicMessage_U8 == 0)
  302.                 {
  303.                     TIM8_CH3_Capture_Cfg(TIM8_CH3_PSC, TIM8_CH3_CCMR2);
  304.                     capture_time = 0;
  305.                 }
  306.  
  307.                 if (CyclicMessage_U8 == 0 || PrintInCycleMode_U8 == 1)
  308.                 {
  309.                     printf("%d %ld \n", nr_of_pushes, capture_time);
  310.                     PrintInCycleMode_U8 = 0;
  311.                 }
  312.  
  313.                 if (CyclicMessage_U8 == 0 && msg.len >= 2)
  314.                 {
  315.                     if (msg.data[1] > 0)
  316.                     {
  317.                         CyclicMessage_U8 = 1;
  318.                         InitPeriodicTimer(PERIODIC_TIMER_VALUE_500US_U16);
  319.                     }
  320.                     else
  321.                     {
  322.                         TIM8_Stop();
  323.                     }
  324.                 }
  325.                 break;
  326.             /* PWM */
  327.             case TIM8_CH1_PWM:
  328.                 StopTIM8_CH1_PWM();
  329.                 InitTIM8_CH1_PWM();
  330.                 if (msg.len >= 2)
  331.                 {
  332.                     if (msg.data[1] <= 0x64)
  333.                     {
  334.                         SetTIM8_CH1_PWM_DutyCycle(msg.data[1]);
  335.                     }
  336.                     else
  337.                     {
  338.                         printf("bad-duty-cycle\n");
  339.                         StopTIM8_CH1_PWM();
  340.                     }
  341.                 }
  342.                 break;
  343.             case SYS_TICK_TIMER:
  344.                 SystemTickTimerInit(STRVR, STCTRL);
  345.                 break;
  346.             default:
  347.                 printf("Invalid CMD received \n");
  348.                 break;
  349.             }
  350.         }
  351.         break;
  352.     case ADC_DAC:
  353.         if (msg.len < 2)
  354.         {
  355.             printf("bad-len\n");
  356.         }
  357.         else if (msg.data[0] == 0x00) /*ADC*/
  358.         {
  359.             if (msg.data[1] > MAX_NR_OF_CHANNELS)
  360.             {
  361.                 printf("bad-channel\n");
  362.             }
  363.             else if (msg.data[1] < 2 || (msg.data[1] > 3 && msg.data[1] <5) || msg.data[1] > 16)
  364.             {
  365.                 U16 adc_value = 0xFFFF;
  366.                 ADC_Configure(MODER, ADC_CR1, ADC_CR2);
  367.  
  368.                 adc_value = ADC_SampleChannel(msg.data[1]);
  369. #if ADC_8_BIT
  370.                 adc_value &= 0x00FF;
  371. #else
  372.                 adc_value &= 0xFFF0;
  373. #endif
  374.                 printf("Channel %d value %d\n", msg.data[1], adc_value);
  375.                 ADC_Stop();
  376.             }
  377.             else
  378.             {
  379.                 printf("Channel %d value %d\n", msg.data[1], 0xFFFF);
  380.             }
  381.         }
  382.         else if (msg.data[0] == 0x01) /*DAC*/
  383.         {
  384.             if (CyclicMessage_U8 == 0)
  385.             {
  386.                 dac_factor = dac_factor * msg.data[1];
  387.                 DAC_Configure(dac_hold_value + dac_factor);
  388.             }
  389.  
  390.             if (CyclicMessage_U8 == 0 || PrintInCycleMode_U8 == 1)
  391.             {
  392.  
  393.                 dac_hold_value = dac_hold_value + dac_factor;
  394.                 DAC->DHR12R1 = dac_hold_value & 0x0FFF;
  395.  
  396.                 printf("Hold Value: %ld \n", DAC->DHR12R1);
  397.                 PrintInCycleMode_U8 = 0;
  398.             }
  399.  
  400.             if (CyclicMessage_U8 == 0)
  401.             {
  402.                 CyclicMessage_U8 = 1;
  403.                 InitPeriodicTimer(PERIODIC_TIMER_VALUE_500US_U16);
  404.             }
  405.         }
  406.         else
  407.         {
  408.             printf("Invalid module\n");
  409.         }
  410.         break;
  411.     case IRQ:
  412.         if (msg.len < 2)
  413.         {
  414.             printf("bad-len\n");
  415.         }
  416.         else if (msg.data[0] != 0x42 && msg.data[0] != 0x43)
  417.         {
  418.             printf("bad-port\n");
  419.         }
  420.         else if (((msg.data[0] == 0x42) && (msg.data[1] > 15 || msg.data[1] < 12)) ||
  421.                  ((msg.data[0] == 0x43) && (msg.data[1] > 11 || msg.data[1] < 8)))
  422.         {
  423.             printf("bad-pin\n");
  424.         }
  425.         else
  426.         {
  427.  
  428.             EnableIRQForIO(msg.data[1]);
  429.             printf("Press the button for port %c, pin %d\n", msg.data[0], msg.data[1]);
  430.             delayMs(5000);
  431.             DisableIRQForIO(msg.data[1]);
  432.         }
  433.         break;
  434.     case SPI:
  435.         if (msg.len < 1)
  436.         {
  437.             printf("bad-len\n");
  438.         }
  439.         else
  440.         {
  441.             LCD_data(msg.data[0]);
  442.         }
  443.         break;
  444.     case I2C:
  445.         if (msg.len < 1)
  446.         {
  447.             printf("bad-len\n");
  448.         }
  449.         else
  450.         {
  451.             I16 acc_g[6];
  452.             I2C1_Init(GPIOB_OTYPER, GPIOB_PUPDR, I2C_CR1, I2CC_CCR);
  453.             MPU6050_initialize();
  454.             MPU6050_getMotion6(&acc_g[0], &acc_g[1], &acc_g[2], &acc_g[3], &acc_g[4], &acc_g[5]);
  455.             if(msg.data[0] > 6)
  456.                 printf("sensor return values: %d, %d, %d, %d, %d, %d\n", acc_g[0], acc_g[1], acc_g[2], acc_g[3], acc_g[4], acc_g[5]);
  457.             else
  458.                 printf("sensor return value: %d\n", acc_g[msg.data[0]]);
  459.         }
  460.         break;
  461.     default:
  462.         printf("Invalid ID received \n");
  463.         break;
  464.     }
  465. }
  466.  
  467.     void send_str(char *str)
  468.         {
  469.         int i=0;
  470.         while(str[i]!='\0')
  471.         {
  472.             LCD_data(str[i]);
  473.             i++;
  474.         }
  475.     }
  476. /*
  477.  * Scan keypad and write the keycode to LEDs.
  478.  * The matrix keypad of the EduBase board is connected to
  479.  * col 0 - PB12
  480.  * col 1 - PB13
  481.  * col 2 - PB14
  482.  * col 3 - PB15
  483.  * row 0 - PC8
  484.  * row 1 - PC9
  485.  * row 2 - PC10
  486.  * row 3 - PC11
  487.  *
  488.  * The four LEDs are connected to
  489.  * LED3 - PB7
  490.  * LED2 - PB6
  491.  * LED1 - PB5
  492.  * LED0 - PB4
  493.  */
  494.  
  495. #include "STM32F4XX.h"
  496.  
  497. void delay(void);
  498. void keypad_init(void);
  499. char keypad_getkey(void);
  500. void writeLEDs(char n);
  501. int readRows(void);
  502. void outputEnableCols(char n);
  503. void writeCols(char n);
  504.  
  505. /* use bit 3-0 of parameter n to turn on/off LEDs
  506. void writeLEDs(char n) {
  507.     GPIOB->BSRR = 0x00F00000;   // turn off all LEDs
  508.     GPIOB->BSRR = n << 4;       // turn on LEDs
  509. }
  510. */
  511. /* system clock at 16 MHz delay about 100 us */
  512. void delay(void) {
  513.     int j;
  514.  
  515.     for (j = 0; j < 300; j++)
  516.         ;      /* do nothing */
  517. }
  518.  
  519. /* This function intializes the pins connected to the keypad. */
  520. void keypad_init(void) {
  521.     /* make rows input first */
  522.     RCC->AHB1ENR |=  4;             /* enable GPIOC clock */
  523.     GPIOC->MODER &= ~0x00FF0000;    /* clear pin mode */
  524.  
  525.     /* make columns input */
  526.     RCC->AHB1ENR |=  2;             /* enable GPIOB clock */
  527.     GPIOB->MODER &= ~0xFF000000;    /* clear pin mode */
  528. }
  529.  
  530. /*
  531.  * This is a non-blocking function to read the keypad.
  532.  * If a key is pressed, it returns a keycode. Otherwise, a zero
  533.  * is returned.
  534.  * The keypad is arranged as a 4x4 matrix. There are pull-down
  535.  * resistors for all pins of the rows on the EduBase board.
  536.  * When no keys are pressed, these pins are low.
  537.  * The columns used as output that can be driven high.
  538.  * First all columns are driven high and the row pins are read. If no
  539.  * keys are pressed, it reads a zero.  Otherwise, some key is pressed.
  540.  * If any key is pressed, the program drives one column high at a time and
  541.  * leave the rest of the columns inactive (float) then read the input (row) pins.
  542.  * Knowing which column is active and which row is active, the program
  543.  * can decide which key is pressed.
  544.  */
  545.  
  546. char keypad_getkey(void)
  547. {
  548.     int row, col;
  549.  
  550.     /* check to see any key is pressed first */
  551.     outputEnableCols(0xF);      /* enable all columns */
  552.     writeCols(0xF);             /* and drive them high */
  553.     delay();                    /* wait for signal to settle */
  554.     row = readRows();           /* read all rows */
  555.     writeCols(0x0);             /* discharge all columns */
  556.     outputEnableCols(0x0);      /* disable all columns */
  557.     if (row == 0) return 0;     /* if no key pressed, return a zero */
  558.  
  559.     /* If a key is pressed, it gets here to find out which key.
  560.      * It activates one column at a time and read the rows to see
  561.      * which is active.
  562.      */
  563.     for (col = 0; col < 4; col++) {
  564.         outputEnableCols(1 << col); /* enable one column */
  565.         writeCols(1 << col);        /* turn the active row high */
  566.         delay();                    /* wait for signal to settle */
  567.         row = readRows();           /* read all rows */
  568.         writeCols(0x0);             /* discharge all columns */
  569.         if (row != 0) break;        /* if one of the row is low, some key is pressed. */
  570.     }
  571.  
  572.     outputEnableCols(0x0);          /* disable all columns */
  573.     if (col == 4)
  574.         return 0;                   /* if we get here, no key is pressed */
  575.  
  576.     /* gets here when one of the rows has key pressed.
  577.      * generate a unique key code and return it.
  578.      */
  579.     if (row == 0x01) { return 0 + col;}
  580.     if (row == 0x02) { return 4 + col;}
  581.     if (row == 0x04) { return 8 + col;}
  582.     if (row == 0x08) { return 12 + col;} // key in row 3
  583.  
  584.     return 0;   /* just to be safe */
  585. }
  586.  
  587. /* enable columns according to bit 3-0 of the parameter n */
  588. void outputEnableCols(char n) {
  589.     GPIOB->MODER &= ~0xFF000000;    /* clear pin mode */
  590.  
  591.     /* make the pin output according to n */
  592.     if (n & 1)
  593.         GPIOB->MODER |=  0x01000000;
  594.     if (n & 2)
  595.         GPIOB->MODER |=  0x04000000;
  596.     if (n & 4)
  597.         GPIOB->MODER |=  0x10000000;
  598.     if (n & 1 << 3)
  599.         GPIOB->MODER |=  0x40000000;
  600. }
  601.  
  602. /* write columns high or low according to bit 3-0 of the parameter n */
  603. void writeCols(char n) {
  604.     GPIOB->BSRR = 0xF0000000;   // turn off all column pins
  605.     GPIOB->BSRR = n << 12;      // turn on column pins
  606. }
  607.  
  608. /* read rows and return them in bit 3-0 */
  609. int readRows(void) {
  610.     return (GPIOC->IDR & 0x0F00) >> 8;
  611. }
  612. int main(void)
  613. {
  614.  
  615.  int b2[4]={2,3,7,1};
  616.     int i=0,tries=3;
  617.  int cont=0;
  618.     U8 status = 0;
  619.  
  620.     PeriphInit();
  621.     if(SPI_CR1 != 0) {
  622.        LCD_init(SPI_CR1);
  623.     }
  624.     IRQInit();
  625.  char key;
  626.  
  627.     /* initialize pins connected to keypad */
  628.     keypad_init();
  629.  
  630.     /* initialize LEDs for display */
  631.     RCC->AHB1ENR |=  2;             /* enable GPIOB clock */
  632.     GPIOB->MODER &= ~0x0000ff00;    /* clear pin mode */
  633.     GPIOB->MODER |=  0x00005500;    /* set pins to output mode */
  634.  
  635.  
  636.     printf("Lucrarea 6 interfete seriale \n");
  637.     printf("please enter the packet in the following format ID Byte0 Byte1 ... Byte7 \n");
  638. while(1)
  639. {
  640.     char st[100];
  641.     char *s = "LOCKED";
  642. sprintf(st,"%s",s);
  643.         send_str(st);
  644. //while(tries!=0)
  645. //{
  646.     GPIOB->BSRR = 0x00F00000;
  647.     while(i<4)
  648.     {
  649.     while((key = keypad_getkey()) == 0);
  650.    
  651.         if(key != b2[i])
  652.         {
  653.              
  654.         }
  655.         else
  656.         {
  657.             cont++;
  658.         }
  659.         i++;
  660.         while(keypad_getkey() != 0);
  661.     }
  662.        
  663. if(cont == 4)
  664. {
  665. LCD_command(0x01);
  666.     s = "UNLOCKED";
  667.  
  668.     sprintf(st,"%s",s);
  669.         send_str(st);
  670. }
  671. else
  672. {
  673.     GPIOB->BSRR = ~0x00F00000;
  674. }
  675.        
  676.         if (TimerCountDown_U16 == 0)
  677.         {
  678.             CyclicMessage_U8 = 0;
  679.             TimerCountDown_U16 = (U16)2 * SECONDS_TO_COUNT_U8;
  680.             StopPeriodicTimer();
  681.             TIM3_Stop();
  682.             TIM8_Stop();
  683.             DAC_Stop();
  684.             printf("please enter the packet in the following format ID Byte0 Byte1 ... Byte7 \n");
  685.         }
  686.  
  687.         if (CyclicMessage_U8 != 1)
  688.         {
  689.             status = ReceiveMessage();
  690.         }
  691.  
  692.         if (status)
  693.         {
  694.             if (CyclicMessage_U8 != 1)
  695.                 PrintReceivedMessage(msg);
  696.             ProcessMessage(msg);
  697.             if (CyclicMessage_U8 != 1)
  698.                 printf("please enter the packet in the following format ID Byte0 Byte1 ... Byte7 \n");
  699.             delayMs(10);
  700.         }
  701.         else
  702.         {
  703.             printf("Wrong message format \n");
  704.         }
  705.     }
  706. }
  707. /* ISRs */
  708. void TIM2_IRQHandler(void)
  709. {
  710.     TIM2->SR = 0; /* clear UIF */
  711.     TimerCountDown_U16--;
  712.     PrintInCycleMode_U8 = 1; /* time to print */
  713. }
  714.  
  715. /* Lucrarea 3*/
  716. void TIM3_IRQHandler(void)
  717. {
  718.     if (TIM3->SR & 1)
  719.     {
  720.         TIM3->SR &= ~1;
  721.         timestamp += 40 * factor;
  722.     }
  723. }
  724.  
  725. void TIM8_CC_IRQHandler(void)
  726. {
  727.     if (TIM8->SR & 8)
  728.     {
  729.         TIM8->SR &= ~8;
  730.         nr_of_pushes += 1;
  731.         capture_time += TIM8->CCR3;
  732.     }
  733. }
  734.  
  735. void SysTick_Handler(void)
  736. {
  737.     GPIOB->ODR ^= 0x00000020;
  738. }
  739.  
  740. /* Lucrarea 5 */
  741. void EXTI15_10_IRQHandler(void)
  742. {
  743.     if (EXTI->PR & 0x00000400)
  744.     {                          /* isr on pin 10 */
  745.         EXTI->PR = 0x00000400; /* clear interrupt pending flag */
  746.         GPIOB->ODR ^= 0x00000040;
  747.     }
  748.     else if (EXTI->PR & 0x00000800)
  749.     {                          /* isr on pin 11 */
  750.         EXTI->PR = 0x00000800; /* clear interrupt pending flag */
  751.         GPIOB->ODR ^= 0x00000080;
  752.     }
  753.     else if (EXTI->PR & 0x00001000)
  754.     {                          /* isr on pin 12 */
  755.         EXTI->PR = 0x00001000; /* clear interrupt pending flag */
  756.         GPIOB->ODR ^= 0x00000010;
  757.     }
  758.     else if (EXTI->PR & 0x00002000)
  759.     {                          /* isr on pin 13 */
  760.         EXTI->PR = 0x00002000; /* clear interrupt pending flag */
  761.         GPIOB->ODR ^= 0x00000020;
  762.     }
  763.     else if (EXTI->PR & 0x00004000)
  764.     {                          /* isr on pin 14 */
  765.         EXTI->PR = 0x00004000; /* clear interrupt pending flag */
  766.         GPIOB->ODR ^= 0x00000040;
  767.     }
  768.     else if (EXTI->PR & 0x00008000)
  769.     {                          /* isr on pin 15 */
  770.         EXTI->PR = 0x00008000; /* clear interrupt pending flag */
  771.         GPIOB->ODR ^= 0x00000080;
  772.     }
  773. }
  774.  
  775. void EXTI9_5_IRQHandler(void)
  776. {
  777.     if (EXTI->PR & 0x000000100)
  778.     {                          /* isr on pin 8 */
  779.         EXTI->PR = 0x00000100; /* clear interrupt pending flag */
  780.         GPIOB->ODR ^= 0x00000010;
  781.     }
  782.     else if (EXTI->PR & 0x00000200)
  783.     {                          /* isr on pin 9 */
  784.         EXTI->PR = 0x00000200; /* clear interrupt pending flag */
  785.         GPIOB->ODR ^= 0x00000020;
  786.     }
  787. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement