Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32l476xx.h"
- #include "SysClock.h"
- #include "LED.h"
- #include "UART.h"
- #include <string.h>
- #include <stdio.h>
- /*Vars*/
- char post_failed_msg[] = "Try test again? (Y/N)\r\n";
- uint32_t init_time = 0; //Variable to store the time of a bucket
- uint32_t lower_limit = 950; //Default value for the lower limit
- uint8_t buckets[100]; //100 samples
- uint16_t measurements[1000];
- void Timer_init() {
- RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN; //enable gpio
- GPIOA->MODER &= ~GPIO_MODER_MODER0_0;
- GPIOA->AFR[0] &= ~0xF; //not F so it only clears the first 4 bits
- GPIOA->AFR[0] |= 0x1;
- TIM2->PSC = 79; //Prescaler to slow down the clock of the timer
- RCC->APB1ENR1 |= RCC_APB1ENR1_TIM2EN; //enable timer 2 clock
- TIM2->ARR = 0xFFFFFFFF; //how high the counter counts
- TIM2->EGR |= TIM_EGR_UG; //new prescaler
- TIM2->CCMR1 &= ~TIM_CCMR1_CC1S;
- TIM2->CCMR1 |= 0x1;
- TIM2->CCMR1 &= ~TIM_CCMR1_IC1F;
- TIM2->CCMR1 |= TIM_CCMR1_IC1F;
- TIM2->CCER &= ~TIM_CCER_CC1P;
- TIM2->CCER &= ~TIM_CCER_CC1NP;
- TIM2->CCER |= TIM_CCER_CC1E;
- TIM2->CNT; //set counter
- TIM2->DIER |= TIM_DIER_CC1IE;
- TIM2->DIER |= TIM_DIER_CC1DE;
- TIM2->CR1 |= TIM_CR1_CEN;
- TIM2->SR &= -1;
- }
- void post_failed() {
- char select_byte;
- USART_Write(USART2, (uint8_t *)post_failed_msg, strlen(post_failed_msg)); //print message if the post fails
- select_byte = USART_Read(USART2); //reads the input
- if(select_byte == 'N' || select_byte == 'n'){
- USART_Write(USART2, (uint8_t *)"Exiting...\r\n", 16); //prints the exit message
- while(1);
- }
- else if(select_byte == 'Y' || select_byte == 'y') {
- init_time = (uint32_t)TIM2->CCR1;
- }
- }
- void POST() {
- uint32_t current_time = 0;
- USART_Write(USART2, (uint8_t *)"Power on self test...\r\n\r\n", 27);
- TIM2->CR1 = 0x1; //input capture mode
- init_time = (uint32_t)TIM2->CNT;
- current_time = (uint32_t)TIM2->CNT;
- while((current_time - init_time) > 100000) {
- if(((TIM2->SR & 0x2) == 0x2) & ((TIM_SR_CC1IF) != 0)){ //current_time - init_time <= 100000
- USART_Write(USART2, (uint8_t *)"POST Succeeded\r\n\r\n", 20);
- break;
- }
- else {
- post_failed();
- }
- current_time = (uint32_t)TIM2->CNT;
- }
- }
- void test(){
- uint32_t printuart;
- USART_Write(USART2, (uint8_t *)"TEST Again\r\n\r\n", 16);
- while(1){
- if((TIM2->SR & TIM_SR_CC1IF) != 0){
- USART_Write(USART2, (uint8_t *)"POST Succeeded\r\n\r\n", 20);
- }
- else {
- USART_Write(USART2, (uint8_t *)"searching\r\n\r\n", 15);
- }
- for (int i = 0; i < 10000000 ; i++){
- }
- }
- }
- void pulses(){
- uint8_t current_read = 0;
- uint32_t past_read;
- uint32_t current_time;
- uint32_t difference;
- uint32_t count = 0;
- TIM2->CR1 = 0x01; // begin input capture
- while(1){
- // check if input has been captured
- if(TIM2->SR & 0x02){
- // break out once we've finished measurements
- if(count >= 1000) {
- TIM2->CR1 = 0x00;
- break;
- }
- // catch first reading for accurate first period
- if(!current_read){
- past_read = (uint32_t)TIM2->CCR1;
- current_read = 1;
- } else {
- current_time = (uint32_t)TIM2->CCR1;
- difference = current_time - past_read;
- past_read = current_time;
- // if the reading is between our bounds
- if(difference >= lower_limit && difference <= lower_limit + 100){
- measurements[difference - lower_limit]++;
- count++;
- }
- }
- }
- }
- }
- void histogram(){
- int initial_value = lower_limit - 1;
- USART_Write(USART2, (uint8_t *)"\r\nHistogram:\r\n", 17);
- for(int i = 0; i < 100; i++){
- initial_value++;
- if(measurements[i] != 0){
- memset(buckets, '\0', sizeof(buckets));
- sprintf((char *)buckets, "%d microseconds --> %d occurance(s)\r\n", initial_value, measurements[i]);
- USART_Write(USART2, buckets, sizeof(buckets));
- }
- }
- USART_Write(USART2, (uint8_t *)"\r\n", 4);
- }
- void getBound() {
- char choice;
- char inputBucket[5] = {'\0', '\0', '\0', '\0', '\0'};
- USART_Write(USART2, (uint8_t *)"New lower bound (50-9950): \r\n", 33);
- choice = USART_Read(USART2);
- /*while(i < 5 && (choice != 13)){ //13 is a return or an enter on the keyboard
- memset(buckets, '\0', sizeof(buckets));
- sprintf((char *)buckets, "%c", choice);
- USART_Write(USART2, buckets, sizeof(buckets));
- inputBucket[i] = choice;
- i++;
- choice = USART_Read(USART2);
- }*/
- for(int i = 0; i < 5; i++){
- memset(buckets, '\0', sizeof(buckets));
- sprintf((char *)buckets, "%c", choice);
- USART_Write(USART2, buckets, sizeof(buckets));
- inputBucket[i] = choice;
- choice = USART_Read(USART2);
- }
- sscanf(inputBucket, "%d", &lower_limit);
- sprintf((char *)buckets, "New lower limit: %d\r\n", lower_limit);
- USART_Write(USART2, buckets, sizeof(buckets));
- }
- void bounds() {
- char choice;
- sprintf((char *)buckets, "Change your lower limit of %d ms? (Y/N)", lower_limit);
- USART_Write(USART2, buckets, sizeof(buckets));
- choice = USART_Read(USART2);
- if (choice == 'y' || choice == 'Y') {
- getBound();
- }
- }
- int main(void) {
- System_Clock_Init(); //Initialize system clock
- Timer_init(); //Initialize timer
- LED_Init(); //Initialize LEDs
- UART2_Init(); //Initialize UART2
- //test();
- POST(); //Initialize POST routine
- char choice;
- while(1){
- bounds();
- pulses();
- histogram();
- USART_Write(USART2, (uint8_t *)"Repeat histogram (Y/N)\r\n", 28);
- choice = USART_Read(USART2);
- if(choice == 'n' || choice == 'N'){
- USART_Write(USART2, (uint8_t *)"Exiting...\r\n", 16); //exits the program
- break;
- }
- for(int i = 0; i < 100; i++) {
- measurements[i] = 0; //resets measurements
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement