SHARE
TWEET

Untitled

a guest Sep 22nd, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "stm32l476xx.h"
  2. #include "SysClock.h"
  3. #include "LED.h"
  4. #include "UART.h"
  5. #include <string.h>
  6. #include <stdio.h>
  7.  
  8. /*Vars*/
  9. char post_failed_msg[] = "Try test again? (Y/N)\r\n";
  10. uint32_t init_time = 0; //Variable to store the time of a bucket
  11. uint32_t lower_limit = 950; //Default value for the lower limit
  12. uint8_t buckets[100]; //100 samples
  13. uint16_t measurements[1000];
  14.  
  15. void Timer_init() {
  16.     RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN; //enable gpio
  17.     GPIOA->MODER &= ~GPIO_MODER_MODER0_0;
  18.     GPIOA->AFR[0] &= ~0xF;  //not F so it only clears the first 4 bits
  19.     GPIOA->AFR[0] |= 0x1;
  20.    
  21.     TIM2->PSC = 79; //Prescaler to slow down the clock of the timer
  22.     RCC->APB1ENR1 |= RCC_APB1ENR1_TIM2EN; //enable timer 2 clock
  23.     TIM2->ARR = 0xFFFFFFFF; //how high the counter counts
  24.     TIM2->EGR |= TIM_EGR_UG; //new prescaler
  25.     TIM2->CCMR1 &= ~TIM_CCMR1_CC1S;
  26.     TIM2->CCMR1 |= 0x1;
  27.    
  28.     TIM2->CCMR1 &= ~TIM_CCMR1_IC1F;
  29.     TIM2->CCMR1 |= TIM_CCMR1_IC1F;
  30.    
  31.     TIM2->CCER &= ~TIM_CCER_CC1P;
  32.     TIM2->CCER &= ~TIM_CCER_CC1NP; 
  33.     TIM2->CCER |= TIM_CCER_CC1E;
  34.    
  35.     TIM2->CNT; //set counter
  36.    
  37.     TIM2->DIER |= TIM_DIER_CC1IE;
  38.     TIM2->DIER |= TIM_DIER_CC1DE;
  39.    
  40.     TIM2->CR1 |= TIM_CR1_CEN;
  41.     TIM2->SR &= -1;
  42. }
  43.  
  44. void post_failed() {
  45.     char select_byte;
  46.     USART_Write(USART2, (uint8_t *)post_failed_msg, strlen(post_failed_msg));   //print message if the post fails
  47.     select_byte = USART_Read(USART2);   //reads the input
  48.     if(select_byte == 'N' || select_byte == 'n'){
  49.         USART_Write(USART2, (uint8_t *)"Exiting...\r\n", 16); //prints the exit message
  50.         while(1);
  51.     }
  52.     else if(select_byte == 'Y' || select_byte == 'y') {
  53.         init_time = (uint32_t)TIM2->CCR1;
  54.     }
  55.    
  56. }
  57.  
  58. void POST() {
  59.     uint32_t current_time = 0;
  60.    
  61.     USART_Write(USART2, (uint8_t *)"Power on self test...\r\n\r\n", 27);
  62.    
  63.     TIM2->CR1 = 0x1; //input capture mode
  64.     init_time = (uint32_t)TIM2->CNT;
  65.     current_time = (uint32_t)TIM2->CNT;
  66.    
  67.     while((current_time - init_time) > 100000) {
  68.             if(((TIM2->SR & 0x2) == 0x2) & ((TIM_SR_CC1IF) != 0)){ //current_time - init_time <= 100000
  69.                 USART_Write(USART2, (uint8_t *)"POST Succeeded\r\n\r\n", 20);
  70.                 break;
  71.             }
  72.            
  73.             else {
  74.                 post_failed(); 
  75.             }
  76.             current_time = (uint32_t)TIM2->CNT;
  77.     }
  78. }
  79.  
  80. void test(){
  81.     uint32_t printuart;
  82.    
  83.     USART_Write(USART2, (uint8_t *)"TEST Again\r\n\r\n", 16);
  84.    
  85. while(1){
  86.    
  87.     if((TIM2->SR & TIM_SR_CC1IF) != 0){
  88.         USART_Write(USART2, (uint8_t *)"POST Succeeded\r\n\r\n", 20);
  89.     }
  90.     else {
  91.         USART_Write(USART2, (uint8_t *)"searching\r\n\r\n", 15);
  92.        
  93.     }
  94.     for (int i = 0; i < 10000000 ; i++){
  95.     }
  96. }
  97. }
  98.  
  99. void pulses(){
  100.  
  101.     uint8_t current_read = 0;
  102.     uint32_t past_read;
  103.     uint32_t current_time;
  104.     uint32_t difference;
  105.     uint32_t count = 0;
  106.  
  107.     TIM2->CR1 = 0x01; // begin input capture
  108.  
  109.     while(1){
  110.  
  111.         // check if input has been captured
  112.         if(TIM2->SR & 0x02){
  113.  
  114.             // break out once we've finished measurements
  115.             if(count >= 1000) {
  116.                 TIM2->CR1 = 0x00;
  117.                 break;
  118.             }
  119.  
  120.             // catch first reading for accurate first period
  121.             if(!current_read){
  122.  
  123.                 past_read = (uint32_t)TIM2->CCR1;
  124.                 current_read = 1;
  125.             } else {
  126.  
  127.                 current_time = (uint32_t)TIM2->CCR1;
  128.                 difference = current_time - past_read;
  129.                 past_read = current_time;
  130.                
  131.                 // if the reading is between our bounds
  132.                 if(difference >= lower_limit && difference <= lower_limit + 100){
  133.                     measurements[difference - lower_limit]++;
  134.                     count++;
  135.                 }
  136.             }
  137.         }
  138.     }
  139. }
  140.  
  141. void histogram(){
  142.  
  143.     int initial_value = lower_limit - 1;
  144.  
  145.     USART_Write(USART2, (uint8_t *)"\r\nHistogram:\r\n", 17);
  146.  
  147.     for(int i = 0; i < 100; i++){
  148.  
  149.         initial_value++;
  150.  
  151.         if(measurements[i] != 0){
  152.             memset(buckets, '\0', sizeof(buckets));
  153.             sprintf((char *)buckets, "%d microseconds --> %d occurance(s)\r\n", initial_value, measurements[i]);
  154.             USART_Write(USART2, buckets, sizeof(buckets));
  155.         }
  156.  
  157.     }
  158.  
  159.     USART_Write(USART2, (uint8_t *)"\r\n", 4);
  160. }
  161.  
  162. void getBound() {
  163.     char choice;
  164.     char inputBucket[5] = {'\0', '\0', '\0', '\0', '\0'};
  165.    
  166.     USART_Write(USART2, (uint8_t *)"New lower bound (50-9950): \r\n", 33);
  167.     choice = USART_Read(USART2);
  168.    
  169.     /*while(i < 5 && (choice != 13)){ //13 is a return or an enter on the keyboard
  170.         memset(buckets, '\0', sizeof(buckets));
  171.         sprintf((char *)buckets, "%c", choice);
  172.         USART_Write(USART2, buckets, sizeof(buckets));
  173.         inputBucket[i] = choice;
  174.         i++;
  175.        
  176.         choice = USART_Read(USART2);
  177.     }*/
  178.    
  179.     for(int i = 0; i < 5; i++){
  180.         memset(buckets, '\0', sizeof(buckets));
  181.         sprintf((char *)buckets, "%c", choice);
  182.         USART_Write(USART2, buckets, sizeof(buckets));
  183.         inputBucket[i] = choice;
  184.        
  185.         choice = USART_Read(USART2);
  186.     }
  187.    
  188.     sscanf(inputBucket, "%d", &lower_limit);
  189.     sprintf((char *)buckets, "New lower limit: %d\r\n", lower_limit);
  190.     USART_Write(USART2, buckets, sizeof(buckets));
  191. }
  192.  
  193. void bounds() {
  194.     char choice;
  195.    
  196.     sprintf((char *)buckets, "Change your lower limit of %d ms? (Y/N)", lower_limit);
  197.     USART_Write(USART2, buckets, sizeof(buckets));
  198.    
  199.     choice = USART_Read(USART2);
  200.    
  201.     if (choice == 'y' || choice == 'Y') {
  202.         getBound();
  203.     }
  204. }
  205.  
  206.  
  207. int main(void) {
  208.     System_Clock_Init(); //Initialize system clock
  209.     Timer_init(); //Initialize timer
  210.     LED_Init(); //Initialize LEDs
  211.     UART2_Init(); //Initialize UART2
  212.     //test();
  213.     POST(); //Initialize POST routine
  214.    
  215.     char choice;
  216.    
  217.     while(1){
  218.         bounds();
  219.         pulses();
  220.         histogram();
  221.        
  222.         USART_Write(USART2, (uint8_t *)"Repeat histogram (Y/N)\r\n", 28);
  223.         choice = USART_Read(USART2);
  224.        
  225.         if(choice == 'n' || choice == 'N'){
  226.             USART_Write(USART2, (uint8_t *)"Exiting...\r\n", 16);   //exits the program
  227.             break;
  228.         }
  229.        
  230.         for(int i = 0; i < 100; i++) {
  231.             measurements[i] = 0;    //resets measurements
  232.         }
  233.     }
  234.    
  235. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top