Advertisement
Bobita

marfa_v3_ultimate

May 24th, 2024
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 18.46 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.  
  14. #include "Types.h"
  15. #include "Drivers.h"
  16.  
  17. //----------------------------------------
  18. #define RS 1    /* BIT0 mask for reg select */
  19. #define EN 2    /* BIT1 mask for E */
  20. #define CORRECT_PASSWORD 1234
  21.  
  22.  
  23.  
  24. //-------------------------------------------
  25.  
  26. int corect[4]={2,2,2,2};
  27.     int tries=3;
  28.     int key=0;
  29.     int count=0;
  30.     int k=0;
  31.  
  32.  
  33.  
  34. void delay(void);
  35. void delayMs(int);
  36. void LCD_nibble_write(char data, unsigned char control);
  37. void LCD_command(unsigned char command);
  38. void LCD_data(char data);
  39. void LCD_init(void);
  40. void SPI1_write(unsigned char data);
  41. int readRows(void);
  42. int getPasswordFromKeypad(void);
  43. void outputEnableCols(char n);
  44. void writeCols(char n);
  45. void writeLEDs(char n);
  46. void keypad_init(void);
  47. char keypad_getkey(void);
  48. void writeStringLCD(char *line);
  49. void newLine(unsigned int size);
  50. void stopProgram();
  51. int len =4;
  52. UART_MSG_T msg = {.data = {0}, .id = 0, .len = 0};
  53. U8 CyclicMessage_U8 = 0;
  54. U8 TimerCountDown_U16 = SECONDS_TO_COUNT_U8;
  55. U8 PrintInCycleMode_U8 = 0;
  56. static const U16 SPI_CR1 = 0x33F;
  57.  
  58. int parola=6;
  59. /* Lucrarea 2 de completat */
  60.  
  61. void PeriphInit(void)
  62. {
  63.  
  64.     __disable_irq();
  65.     // Configure PB[7..4] as output
  66.     RCC->AHB1ENR |= 0;           /* Enable GPIOB clock */
  67.     GPIOB->MODER &= 0; /* Reset GPIOB PB[7..4]  */
  68.     GPIOB->MODER |= 0;   /* Set GPIOB PB[7..4]  as ouput */
  69.  
  70.     // Configure PC[11..8] as input
  71.     RCC->AHB1ENR |= 0;           /* Enable GPIOC clock */
  72.     GPIOC->MODER &= 0; /* Reset GPIOC PC[11..8]  for input mode */
  73.  
  74.     // Configure PB[15..12] port as input and enables pull-ups
  75.     GPIOB->MODER &= 0; /* Reset GPIOB PB[15..12]  */
  76.     GPIOB->PUPDR |= 0;   /* Enable pull-ups on GPIOB PB[15..12]  */
  77.  
  78.     USART2_init();
  79.  
  80.     __enable_irq();
  81. }
  82.  
  83. /* TODO: change the 0's to the correct value */
  84. const U8 Hex7Segment[16] = {
  85.     0,
  86.     0xF9,
  87.     0xA4,
  88.     0xB0,
  89.     0x99,
  90.     0,
  91.     0,
  92.     0,
  93.     0,
  94.     0,
  95.     0,
  96.     0,
  97.     0,
  98.     0,
  99.     0,
  100.     0};
  101.  
  102. /* end lucrarea 2*/
  103.  
  104. /* Lucrarea 3 De completat*/
  105. static const U32 STRVR = 0;
  106. static const U16 STCTRL = 0;
  107.  
  108. static const U16 TIM3_PSC = 0;
  109. static const U16 TIM3_ARR = 0;
  110.  
  111. static const U16 TIM3_CH1_PSC = 0;
  112. static const U16 TIM3_CH1_ARR = 0;
  113. static const U16 TIM3_CH1_CCMR1 = 0;
  114. static const U16 TIM3_CH1_CCR1 = 0;
  115.  
  116. static const U16 TIM8_CH3_PSC = 0;
  117. static const U16 TIM8_CH3_CCMR2 = 0;
  118.  
  119. U32 timestamp = 0;
  120. U32 capture_time = 0;
  121. U8 nr_of_pushes = 0;
  122. U8 factor = 1;
  123. /* end lucrarea  3 */
  124.  
  125. /* Lucrarea 4 */
  126.  
  127. U16 dac_factor = 600;
  128. U16 dac_hold_value = 0;
  129.  
  130.  
  131. #define ADC_8_BIT 1 //dezactivati pentru adc 12 biti
  132.  
  133. #if ADC_8_BIT
  134. static const U32 ADC_CR1 = 0; /* 8-bit resolution */
  135. static const U32 ADC_CR2 = 0; /* SW Start, Align right justified, start adc */
  136. #else
  137. static const U32 ADC_CR1 = 0; /* 12-bit resolution */
  138. static const U32 ADC_CR2 = 0; /* SW Start, Align left justified,  start ADC */
  139. #endif
  140.  
  141. static const U32 MODER = 0x000003FF; /* PA0-PA4 ca intrari analogice*/
  142.  
  143. /*End of Lucrarea 4*/
  144.  
  145. void stopProgram(){
  146.     while(1){}
  147. }
  148.  
  149. U8 ReceiveMessage(void)
  150. {
  151.     //U8 error = 0;
  152.     char msg_str[MAX_PACKET_STR_SIZE];
  153.     const char *pos = msg_str;
  154.  
  155.     memset(&msg, 0, sizeof(UART_MSG_T));
  156.  
  157.     fgets(msg_str, MAX_PACKET_STR_SIZE, stdin);
  158.  
  159.     // printf("Unformated string: %s, size %d\n", msg_str, strlen(msg_str));
  160.  
  161.     if (strlen(msg_str) < 3)
  162.     {
  163.         msg.id = 0xFF;
  164.         return 0;
  165.     }
  166.  
  167.     if (sscanf(pos, "%2hhx", &msg.id) == 0)
  168.     {
  169.         msg.id = 0xFF;
  170.         return 0;
  171.     }
  172.  
  173.     if (strlen(msg_str) > 5)
  174.     {
  175.         pos += 3;
  176.  
  177.         for (int i = 1; i < strlen(msg_str) / 3; i++)
  178.         {
  179.             sscanf(pos, "%2hhx", &msg.data[i - 1]);
  180.             pos += 3;
  181.             msg.len++;
  182.         }
  183.     }
  184.     return 1;
  185. }
  186.  
  187. void PrintReceivedMessage(const UART_MSG_T msg)
  188. {
  189.     printf("the received packet is: \n");
  190.     printf("ID: %02x \n", msg.id);
  191.     printf("Size in Bytes: %d\n", msg.len);
  192.     printf("Data:");
  193.     for (int i = 0; i < msg.len; i++)
  194.         printf("%02X ", msg.data[i]);
  195.     printf("\r\n");
  196. }
  197.  
  198. void ProcessMessage(const UART_MSG_T msg)
  199. {
  200.     U32 p;
  201.     switch (msg.id)
  202.     {
  203.     case SW_VERSION:
  204.         printf("Current program: Lucrarea 5 SI\n");
  205.         break;
  206.  
  207.     case PORT_INPUT:
  208.         if (msg.len < 2)
  209.         {
  210.             printf("bad-len\n");
  211.         }
  212.         else if (msg.data[0] != 0x42 && msg.data[0] != 0x43)
  213.         {
  214.             printf("bad-port\n");
  215.         }
  216.         else if (((msg.data[0] == 0x42) && (msg.data[1] > 15 || msg.data[1] < 12)) ||
  217.                 ((msg.data[0] == 0x43) && (msg.data[1] > 11 || msg.data[1] < 8)))
  218.         {
  219.             printf("bad-pin\n");
  220.         }
  221.         else
  222.         {
  223.             U32 i = 1;
  224.             p = msg.data[0] != 0x42 ? GPIOC->IDR : GPIOB->IDR;
  225.             i <<= msg.data[1];
  226.             if (CyclicMessage_U8 == 0 || PrintInCycleMode_U8 == 1)
  227.             {
  228.                 printf("%d \n", (p & i) >> msg.data[1]);
  229.                 PrintInCycleMode_U8 = 0;
  230.             }
  231.  
  232.             if (CyclicMessage_U8 == 0 && msg.len >= 3)
  233.             {
  234.                 if (msg.data[2] > 0)
  235.                 {
  236.                     CyclicMessage_U8 = 1;
  237.                     InitPeriodicTimer(PERIODIC_TIMER_VALUE_500US_U16);
  238.                 }
  239.             }
  240.         }
  241.  
  242.         break;
  243.     case PORT_OUTPUT:
  244.         if (msg.len < 3)
  245.         {
  246.             printf("bad-len\n");
  247.         }
  248.         else if (msg.data[0] != 0x42)
  249.         {
  250.             printf("bad-port\n");
  251.         }
  252.         else if ((msg.data[1] > 7) || (msg.data[1] < 4))
  253.         {
  254.             printf("bad-pin\n");
  255.         }
  256.         else
  257.         {
  258.             U32 i = 1;
  259.             i <<= msg.data[1];
  260.             GPIOB->BSRR = msg.data[2] ? (GPIOB->BSRR | i) : (GPIOB->BSRR | (i << 16));
  261.             printf("%02X\n", (GPIOB->ODR & 0x000000F0) >> 4);
  262.         }
  263.         break;
  264.     case PORT_HEX:
  265.         if (msg.len < 2)
  266.         {
  267.             printf("bad-len\n");
  268.         }
  269.         else if (msg.data[0] > 3)
  270.         {
  271.             printf("bad-display\n");
  272.         }
  273.         else if (msg.data[1] > 15)
  274.         {
  275.             printf("bad-number\n");
  276.         }
  277.         else
  278.         {
  279.  
  280.             SPI1_init();
  281.             SPI1_write(Hex7Segment[msg.data[1]]); /* write pattern to the seven segments */
  282.             SPI1_write(1 << msg.data[0]);         /* select digit */
  283.         }
  284.         break;
  285.     case TIMERS:
  286.         if (msg.len < 1)
  287.         {
  288.             printf("bad-len\n");
  289.         }
  290.         else
  291.         {
  292.             SystemTickTimerStop();
  293.             switch (msg.data[0])
  294.             {
  295.             /*general timer */
  296.             case GENERAL_TIM3:
  297.                 if (CyclicMessage_U8 == 0)
  298.                 {
  299.                     TIM3_Cfg(TIM3_PSC, TIM3_ARR);
  300.                     timestamp = 0;
  301.  
  302.                     if (msg.len > 1)
  303.                     {
  304.                         TIM3->ARR = ((TIM3->ARR + 1) * msg.data[1]) - 1;
  305.                         factor = msg.data[1];
  306.                     }
  307.                 }
  308.                 if (CyclicMessage_U8 == 0 || PrintInCycleMode_U8 == 1)
  309.                 {
  310.                     printf("%d \n", timestamp);
  311.                     PrintInCycleMode_U8 = 0;
  312.                 }
  313.  
  314.                 if (CyclicMessage_U8 == 0 && msg.len >= 3)
  315.                 {
  316.                     if (msg.data[2] > 0)
  317.                     {
  318.                         CyclicMessage_U8 = 1;
  319.                         InitPeriodicTimer(PERIODIC_TIMER_VALUE_500US_U16);
  320.                     }
  321.                     else
  322.                     {
  323.                         TIM3_Stop();
  324.                     }
  325.                 }
  326.                 break;
  327.             /* out compare */
  328.             case TIM3_CH1_COMPARE:
  329.                 TIM3_CH1_Compare_Cfg(TIM3_CH1_PSC, TIM3_CH1_ARR, TIM3_CH1_CCMR1, TIM3_CH1_CCR1);
  330.                 if (msg.len > 1)
  331.                 {
  332.                     TIM3->ARR = TIM3->ARR * msg.data[1];
  333.                 }
  334.                 break;
  335.             /* in capture */
  336.             case TIM8_CH3_CAPTURE:
  337.                 if (CyclicMessage_U8 == 0)
  338.                 {
  339.                     TIM8_CH3_Capture_Cfg(TIM8_CH3_PSC, TIM8_CH3_CCMR2);
  340.                     capture_time = 0;
  341.                 }
  342.  
  343.                 if (CyclicMessage_U8 == 0 || PrintInCycleMode_U8 == 1)
  344.                 {
  345.                     printf("%d %d \n", nr_of_pushes, capture_time);
  346.                     PrintInCycleMode_U8 = 0;
  347.                 }
  348.  
  349.                 if (CyclicMessage_U8 == 0 && msg.len >= 2)
  350.                 {
  351.                     if (msg.data[1] > 0)
  352.                     {
  353.                         CyclicMessage_U8 = 1;
  354.                         InitPeriodicTimer(PERIODIC_TIMER_VALUE_500US_U16);
  355.                     }
  356.                     else
  357.                     {
  358.                         TIM8_Stop();
  359.                     }
  360.                 }
  361.                 break;
  362.             /* PWM */
  363.             case TIM8_CH1_PWM:
  364.                 StopTIM8_CH1_PWM();
  365.                 InitTIM8_CH1_PWM();
  366.                 if (msg.len >= 2)
  367.                 {
  368.                     if (msg.data[1] <= 0x64)
  369.                     {
  370.                         SetTIM8_CH1_PWM_DutyCycle(msg.data[1]);
  371.                     }
  372.                     else
  373.                     {
  374.                         printf("bad-duty-cycle\n");
  375.                         StopTIM8_CH1_PWM();
  376.                     }
  377.                 }
  378.                 break;
  379.             case SYS_TICK_TIMER:
  380.                 SystemTickTimerInit(STRVR, STCTRL);
  381.                 break;
  382.             default:
  383.                 printf("Invalid CMD received \n");
  384.                 break;
  385.             }
  386.         }
  387.         break;
  388.     case ADC_DAC:
  389.         if (msg.len < 2)
  390.         {
  391.             printf("bad-len\n");
  392.         }
  393.         else if(msg.data[0] == 0x00) /*ADC*/
  394.         {
  395.             if(msg.data[1] > MAX_NR_OF_CHANNELS)
  396.             {
  397.                 printf("bad-channel\n");
  398.             }
  399.             else if (msg.data[1] < 2 || (msg.data[1] > 3 && msg.data[1] <5) || msg.data[1] > 16)
  400.             {
  401.                 U16 adc_value = 0xFFFF;
  402.                 ADC_Configure(MODER, ADC_CR1, ADC_CR2);
  403.                
  404.                 adc_value = ADC_SampleChannel(msg.data[1]);
  405.                 #if ADC_8_BIT
  406.                     adc_value &= 0x00FF;
  407.                 #else
  408.                     adc_value &= 0xFFF0;
  409.                 #endif
  410.                 printf("Channel %d value %d\n", msg.data[1], adc_value);
  411.                 ADC_Stop();
  412.             }
  413.             else {
  414.                 printf("Channel %d value %d\n", msg.data[1], 0xFFFF);
  415.             }
  416.         }
  417.         else if(msg.data[0] == 0x01) /*DAC*/
  418.         {
  419.             if (CyclicMessage_U8 == 0)
  420.             {
  421.                 dac_factor = dac_factor * msg.data[1];
  422.                 DAC_Configure(dac_hold_value + dac_factor);
  423.             }
  424.  
  425.             if (CyclicMessage_U8 == 0 || PrintInCycleMode_U8 == 1)
  426.             {
  427.                
  428.                 dac_hold_value = dac_hold_value + dac_factor;
  429.                 DAC->DHR12R1 = dac_hold_value & 0x0FFF;
  430.                
  431.                 printf("Hold Value: %d \n", DAC->DHR12R1);
  432.                 PrintInCycleMode_U8 = 0;
  433.             }
  434.  
  435.             if (CyclicMessage_U8 == 0)
  436.             {
  437.                 CyclicMessage_U8 = 1;
  438.                 InitPeriodicTimer(PERIODIC_TIMER_VALUE_500US_U16);
  439.             }
  440.         }
  441.         else
  442.         {
  443.             printf("Invalid module\n");
  444.         }
  445.         break;
  446.     default:
  447.         printf("Invalid ID received \n");
  448.         break;
  449.     }
  450. }
  451.  
  452. void citire()
  453. {
  454.  while(k<4)
  455.     {
  456.     while((key = keypad_getkey()) == 0);
  457.    
  458.         if(key != corect[k])
  459.         {
  460.              
  461.         }
  462.         else
  463.         {
  464.             ++count;
  465.         }
  466.         ++k;
  467.         while(keypad_getkey() != 0);
  468.     }
  469. }
  470.  
  471. int main(void)
  472. {
  473.     U8 status = 0;
  474.  
  475.     PeriphInit();
  476.    
  477.   keypad_init();
  478.     LCD_init();
  479.     RCC->AHB1ENR |=  2;             /* enable GPIOB clock */
  480.   GPIOB->MODER &= ~0x0000ff00;    /* clear pin mode */
  481.   GPIOB->MODER |=  0x00005500;    /* set pins to output mode */
  482.    
  483.        
  484.        
  485.         while(1)
  486.             {
  487.         LCD_data('P');
  488.         LCD_data('A');
  489.         LCD_data('S');
  490.         LCD_data('S');
  491.         LCD_data('W');
  492.         LCD_data('O');
  493.         LCD_data('R');
  494.         LCD_data('D');
  495.         LCD_data(':');
  496.         delayMs(1000);
  497.                 if(tries>0)
  498.                 {
  499.                     citire();
  500.                    
  501.                     if(count==4)
  502.            
  503.                     {
  504.                          /* clear LCD display */
  505.                         LCD_command(1);
  506.                         delayMs(1000);
  507.                    
  508.                         //writeLEDs(0x0);
  509.                         LCD_data('U');
  510.                         LCD_data('N');
  511.                         LCD_data('L');
  512.                         LCD_data('O');
  513.                         LCD_data('C');
  514.                         LCD_data('K');
  515.                         LCD_data('E');
  516.                         LCD_data('D');
  517.                         LCD_data('!');
  518.                         k=0;
  519.                       count=0;
  520.                         //while(1);
  521.                     }
  522.                     else
  523.                     {
  524.                          /* clear LCD display */
  525.                         LCD_command(1);
  526.                         delayMs(1000);
  527.                    
  528.                         tries--;
  529.                         LCD_data('W');
  530.                         LCD_data('R');
  531.                         LCD_data('O');
  532.                         LCD_data('N');
  533.                         LCD_data('G');
  534.                         writeLEDs(0xF);
  535.                         k=0;
  536.                       count=0;
  537.                         delayMs(1000);
  538.                         LCD_command(1);
  539.                         citire();
  540.                     }
  541.             }
  542.         else
  543.                 {
  544.            
  545.                 LCD_data('L');
  546.         LCD_data('O');
  547.                 LCD_data('C');
  548.                 LCD_data('K');
  549.                 LCD_data('E');
  550.                 LCD_data('D');
  551.                 LCD_data('!');
  552.                 delayMs(1000);
  553.                 LCD_command(1);
  554.                 }
  555.     }
  556.        
  557. }
  558.     // incercare 2.0
  559.  
  560.  
  561. //-----------------------------------------------------------------------------
  562.  
  563. /* configure SPI1 and the associated GPIO pins */
  564. void LCD_init(void) {
  565.     RCC->AHB1ENR |= 1;              /* enable GPIOA clock */
  566.     RCC->AHB1ENR |= 4;              /* enable GPIOC clock */
  567.     RCC->APB2ENR |= 0x1000;         /* enable SPI1 clock */
  568.  
  569.     /* PORTA 5, 7 for SPI1 MOSI and SCLK */
  570.     GPIOA->MODER &= ~0x0000CC00;    /* clear pin mode */
  571.     GPIOA->MODER |=  0x00008800;    /* set pin alternate mode */
  572.     GPIOA->AFR[0] &= ~0xF0F00000;   /* clear alt mode */
  573.     GPIOA->AFR[0] |=  0x50500000;   /* set alt mode SPI1 */
  574.  
  575.     /* PA12 as GPIO output for SPI slave select */
  576.     GPIOA->MODER &= ~0x03000000;    /* clear pin mode */
  577.     GPIOA->MODER |=  0x01000000;    /* set pin output mode */
  578.  
  579.     /* initialize SPI1 module */
  580.     SPI1->CR1 = 0x31F;
  581.     SPI1->CR2 = 0;
  582.     SPI1->CR1 |= 0x40;              /* enable SPI1 module */
  583.  
  584.     /* LCD controller reset sequence */
  585.     delayMs(20);
  586.     LCD_nibble_write(0x30, 0);
  587.     delayMs(5);
  588.     LCD_nibble_write(0x30, 0);
  589.     delayMs(1);
  590.     LCD_nibble_write(0x30, 0);
  591.     delayMs(1);
  592.     LCD_nibble_write(0x20, 0);  /* use 4-bit data mode */
  593.     delayMs(1);
  594.     LCD_command(0x28);          /* set 4-bit data, 2-line, 5x7 font */
  595.     LCD_command(0x06);          /* move cursor right */
  596.     LCD_command(0x01);          /* clear screen, move cursor to home */
  597.     LCD_command(0x0F);          /* turn on display, cursor blinking */
  598. }
  599.  
  600. void LCD_nibble_write(char data, unsigned char control) {
  601.     data &= 0xF0;       /* clear lower nibble for control */
  602.     control &= 0x0F;    /* clear upper nibble for data */
  603.     SPI1_write (data | control);           /* RS = 0, R/W = 0 */
  604.     SPI1_write (data | control | EN);      /* pulse E */
  605.     delayMs(0);
  606.     SPI1_write (data);
  607. }
  608.  
  609. void LCD_command(unsigned char command) {
  610.     LCD_nibble_write(command & 0xF0, 0);    /* upper nibble first */
  611.     LCD_nibble_write(command << 4, 0);      /* then lower nibble */
  612.  
  613.     if (command < 4)
  614.         delayMs(2);         /* command 1 and 2 needs up to 1.64ms */
  615.     else
  616.         delayMs(1);         /* all others 40 us */
  617. }
  618.  
  619. void LCD_data(char data) {
  620.     LCD_nibble_write(data & 0xF0, RS);      /* upper nibble first */
  621.     LCD_nibble_write(data << 4, RS);        /* then lower nibble */
  622.  
  623.     delayMs(1);
  624. }
  625.  
  626. /* This function enables slave select, writes one byte to SPI1, */
  627. /* wait for transmit complete and deassert slave select. */
  628. void SPI1_write(unsigned char data) {
  629.     while (!(SPI1->SR & 2)) {}      /* wait until Transfer buffer Empty */
  630.     GPIOA->BSRR = 0x10000000;       /* assert slave select */
  631.     SPI1->DR = data;                /* write data */
  632.     while (SPI1->SR & 0x80) {}      /* wait for transmission done */
  633.     GPIOA->BSRR = 0x00001000;       /* deassert slave select */
  634. }
  635.  
  636. /* 16 MHz SYSCLK */
  637. void delayMs(int n) {
  638.     int i;
  639.     for (; n > 0; n--)
  640.         for (i = 0; i < 3195; i++) ;
  641. }
  642.  
  643.  
  644. void writeStringLCD(char *line) {
  645.     for(unsigned int i=0; i < strlen(line); i++)
  646.     {
  647.         LCD_data(line[i]);
  648.     }
  649. }
  650.  
  651. void newLine(unsigned int size){
  652.     for(unsigned int i=0; i < 40-size; i++)
  653.     {
  654.         LCD_data(' ');
  655.     }
  656. }
  657.  
  658. int getPasswordFromKeypad(void) {
  659.     int password = 0;
  660.     char key;
  661.     for (int i = 0; i < 4; i++) {
  662.         do {
  663.             key = keypad_getkey();
  664.         } while (key == 0);  /* wait for a key press */
  665.         password = password * 10  + (key - '0');  /* assuming single digit keys */
  666.                 // btn 1 -> 0 * 10 + ( 0 - 0) = 0
  667.                 // btn 2 -> 0 * 10 + ( 1 - 0 ) = 0 + 1 = 1
  668.                 //btn 3 -> 1 * 10 + (2 - 0 ) = 10 + 2 = 12
  669.                 // btn 4 ->
  670.         delay();  /* debounce delay */
  671.         }
  672.     return password;
  673. }
  674.  
  675.  
  676.  
  677.  
  678. char keypad_getkey(void)
  679. {
  680.     int row, col;
  681.  
  682.     /* check to see any key is pressed first */
  683.     outputEnableCols(0xF);      /* enable all columns */
  684.     writeCols(0xF);             /* and drive them high */
  685.     delay();                    /* wait for signal to settle */
  686.     row = readRows();           /* read all rows */
  687.     writeCols(0x0);             /* discharge all columns */
  688.     outputEnableCols(0x0);      /* disable all columns */
  689.     if (row == 0) return 0;     /* if no key pressed, return a zero */
  690.  
  691.     /* If a key is pressed, it gets here to find out which key.
  692.      * It activates one column at a time and read the rows to see
  693.      * which is active.
  694.      */
  695.     for (col = 0; col < 4; col++) {
  696.         outputEnableCols(1 << col); /* enable one column */
  697.         writeCols(1 << col);        /* turn the active row high */
  698.         delay();                    /* wait for signal to settle */
  699.         row = readRows();           /* read all rows */
  700.         writeCols(0x0);             /* discharge all columns */
  701.         if (row != 0) break;        /* if one of the row is low, some key is pressed. */
  702.     }
  703.  
  704.     outputEnableCols(0x0);          /* disable all columns */
  705.     if (col == 4)
  706.         return 0;                   /* if we get here, no key is pressed */
  707.  
  708.     /* gets here when one of the rows has key pressed.
  709.      * generate a unique key code and return it.
  710.      */
  711.     if (row == 0x01) {return 0 + col;}    // key in row 0
  712.     if (row == 0x02) {return 4 + col;  }  // key in row 1
  713.     if (row == 0x04) {return 8 + col;   } // key in row 2
  714.     if (row == 0x08) {return 12 + col;   }// key in row 3
  715.  
  716.     return 0;   /* just to be safe */
  717. }
  718.  
  719. /* enable columns according to bit 3-0 of the parameter n */
  720. void outputEnableCols(char n) {
  721.     GPIOB->MODER &= ~0xFF000000;    /* clear pin mode */
  722.  
  723.     /* make the pin output according to n */
  724.     if (n & 1)
  725.         GPIOB->MODER |=  0x01000000;
  726.     if (n & 2)
  727.         GPIOB->MODER |=  0x04000000;
  728.     if (n & 4)
  729.         GPIOB->MODER |=  0x10000000;
  730.     if (n & 1 << 3)
  731.         GPIOB->MODER |=  0x40000000;
  732. }
  733.  
  734. /* write columns high or low according to bit 3-0 of the parameter n */
  735. void writeCols(char n) {
  736.     GPIOB->BSRR = 0xF0000000;   // turn off all column pins
  737.     GPIOB->BSRR = n << 12;      // turn on column pins
  738. }
  739.  
  740. /* read rows and return them in bit 3-0 */
  741. int readRows(void) {
  742.     return (GPIOC->IDR & 0x0F00) >> 8;
  743. }
  744.  
  745.  
  746.  
  747.  
  748.  
  749. void writeLEDs(char n) {
  750.     GPIOB->BSRR = 0x00F00000;   // turn off all LEDs
  751.     GPIOB->BSRR = n << 4;       // turn on LEDs
  752. }
  753.  
  754. /* system clock at 16 MHz delay about 100 us */
  755. void delay(void) {
  756.     int j;
  757.  
  758.     for (j = 0; j < 300; j++)
  759.         ;      /* do nothing */
  760. }
  761.  
  762. /* This function intializes the pins connected to the keypad. */
  763. void keypad_init(void) {
  764.     /* make rows input first */
  765.     RCC->AHB1ENR |=  4;             /* enable GPIOC clock */
  766.     GPIOC->MODER &= ~0x00FF0000;    /* clear pin mode */
  767.  
  768.     /* make columns input */
  769.     RCC->AHB1ENR |=  2;             /* enable GPIOB clock */
  770.     GPIOB->MODER &= ~0xFF000000;    /* clear pin mode */
  771. }
  772.  
  773. //-----------------------------------------------------------------------------
  774.  
  775. //void delayMs(int n)
  776. //{
  777. //  int i;
  778. //  for (; n > 0; n--)
  779. //      for (i = 0; i < 3195; i++)
  780. //      {
  781. //          __NOP();
  782. //      }
  783. //}
  784.  
  785. /* ISRs */
  786. void TIM2_IRQHandler(void)
  787. {
  788.     TIM2->SR = 0; /* clear UIF */
  789.     TimerCountDown_U16--;
  790.     PrintInCycleMode_U8 = 1; /* time to print */
  791. }
  792.  
  793. /* Lucrarea 3*/
  794. void TIM3_IRQHandler(void)
  795. {
  796.     if (TIM3->SR & 1)
  797.     {
  798.         TIM3->SR &=~ 1;
  799.         timestamp += 40 * factor;
  800.     }
  801. }
  802.  
  803. void TIM8_CC_IRQHandler(void)
  804. {
  805.     if (TIM8->SR & 8)
  806.     {
  807.         TIM8->SR &=~ 8;
  808.         nr_of_pushes += 1;
  809.         capture_time += TIM8->CCR3;
  810.     }
  811. }
  812.  
  813. void SysTick_Handler(void)
  814. {
  815.     GPIOB->ODR ^= 0x00000020;
  816. }
  817.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement