Advertisement
JKattackk

reacttestv3

Dec 6th, 2022 (edited)
937
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.44 KB | None | 0 0
  1. /*
  2.  * File:   newavr-main.c
  3.  * Author: Jack
  4.  *
  5.  * Created on November 24, 2022, 12:16 PM
  6.  */
  7.  
  8.  
  9. #define F_CPU 11059200UL
  10. #define CONSTANT 0.02315 //corresponds to the step size of timer one, in ms
  11. #include <avr/pgmspace.h>
  12. #include <avr/io.h>
  13. #include <stdio.h>
  14. #include <stdarg.h>
  15. #include <util/delay.h>
  16. #include <avr/interrupt.h>
  17. #include <stdlib.h>
  18.  
  19. #include "hd44780.h"
  20. #include "lcd.h"
  21. #include "defines.h"
  22.  
  23. FILE lcd_str = FDEV_SETUP_STREAM(lcd_putchar, NULL, _FDEV_SETUP_WRITE);
  24.  
  25. float min = 0;
  26. float average = 0;
  27. unsigned int count = 0;
  28. volatile unsigned int overflows;
  29. int mode = 1;
  30.  
  31. volatile bool early = false;
  32.  
  33. //sets up timer0 for output to the piezo buzzer.
  34. //sets up timer1 for measuring time.
  35.  
  36. void timer_init() {
  37.     //T0 setup
  38.     TCCR0A |= (1 << WGM01); //CTC mode, top of OCRA
  39.     TCCR0B |= (1 << CS01); //clock divided by one.  May need to be changed
  40.     OCR0A = 70; //adjusting this value will change the frequency of the buzzer tone.
  41.  
  42.     //T1 setup
  43.     TCCR1B |= (1 << CS12);
  44.     TIMSK1 |= (1 << TOIE1);
  45. }
  46.  
  47. //takes two timer values, calculates and displays current reading, average, min
  48. //may need to be updated if we need to account for more overflows
  49.  
  50. void updateTime(unsigned int t0,unsigned int t1,unsigned int overflows) {
  51.     float measurement;
  52.  
  53.     measurement = ((65536 * overflows + t1) - t0) * CONSTANT;
  54.  
  55.     //update measurement on display
  56.     cli();
  57.     printf("\x1b\x01\x1b\x80%3.1f", measurement);
  58.     count++;
  59.  
  60.     //calculate new average
  61.     if ((measurement < min) | (min == 0)) {
  62.     min = measurement;
  63.     }
  64.     average = ((count - 1) * average) / count + (measurement / count);
  65.  
  66.  
  67.     printf("\x1b\xc0%3.1fms  %3.1fms", average, min);
  68.     while (PINB & (1 << PB0)) {
  69.     }
  70. }
  71.  
  72. void runTest(int mode) {
  73. START:
  74.     cli();
  75.     printf("\x1b\x80wait for signal");
  76.  
  77.     unsigned int t1, t0;
  78.     srand(TCNT1);
  79.     int delay = 2000 + rand()/4;
  80.     while (delay > 0) {
  81.         if (PINB & (1 << PB0)) {
  82.             printf("\x1b\x80   too early");
  83.             while (PINB & (1 << PB0)) {
  84.             }
  85.             _delay_ms(200);
  86.             printf("\x1b\x80      NANms   ");
  87.             goto START;
  88.         }
  89.         _delay_ms(1);
  90.         delay = delay - 1;
  91.     }
  92.     if (mode) {
  93.         PORTD ^= (1 << PD0); //toggle light
  94.     } else {//toggle connection between timer and piezo buzzer
  95.         TCCR0A ^= (1 << COM0A0);
  96.     }
  97.     t0 = TCNT1;
  98.     TIFR1 |= (1 << ICF1) | (1 << TOV1); //clear flag
  99.     overflows = 0;
  100.     sei();
  101.     while (overflows < 6) {
  102.         if (!(TIFR1 & (1 << ICF1))) {
  103.         } else {
  104.             t1 = ICR1;
  105.             updateTime(t0, t1, overflows);
  106.             break;
  107.         }
  108.     }
  109.     if (mode) {
  110.         PORTD ^= (1 << PD0); //toggle light
  111.     } else {//toggle connection between timer and piezo buzzer
  112.         TCCR0A ^= (1 << COM0A0);
  113.     }
  114.     if (overflows >= 6) {
  115.         cli();
  116.         printf("\x1b\x80  No Response");
  117.         _delay_ms(500);
  118.         printf("\x1b\x80      NANms   ");
  119.     }
  120.     sei();
  121. }
  122.  
  123. void resetValues() {
  124.     printf("\x1b\x01\x1b\x80      NANms    L");
  125.     printf("\x1b\xc0  NANms    NANms");
  126.     average = 0;
  127.     min = 0;
  128.     count = 0;
  129.     _delay_ms(200);
  130. }
  131.  
  132. int main(void) {
  133.     //response button, control button as inputs
  134.     lcd_init();
  135.     stdout = &lcd_str;
  136.     printf("\x1b\x01\x1b\x80      NANms    L");
  137.     printf("\x1b\xc0  NANms    NANms");
  138.  
  139.     //outputs for LED and buzzer
  140.     DDRD |= (1 << PD0);
  141.     DDRD |= (1 << PD6);
  142.  
  143.     DDRD &= ~(1 << PD2);
  144.     PORTD &= ~(1 << PD2);
  145.     timer_init();
  146.     //EICRA |= (1 << ISC11) | (1 << ISC10); //rising edge of INT1 generates interrupt
  147.  
  148.     EICRA |= (1 << ISC00);
  149.     //EIFR = (1 << INTF0);
  150.     sei();
  151.     while (1) {
  152.         EIMSK |= (1 << INT0);
  153.         //if response pin pressed
  154.  
  155.         if (PINB & (1 << PB0)) {
  156.             EIMSK &= ~(1 << INT0);
  157.             while (PINB & (1 << PB0)) {
  158.             }
  159.             runTest(mode);
  160.         }
  161.     }
  162. }
  163.  
  164. ISR(TIMER1_OVF_vect, ISR_BLOCK) {
  165.     overflows++;
  166. }
  167.  
  168. ISR(INT0_vect) {
  169.     int delay = 1000;
  170.     while (delay > 0) {
  171.         if (EIFR & (1 << INTF0)) {
  172.             EIFR = (1 << INTF0);
  173.             mode = !mode;
  174.             if (mode) {
  175.                 printf("\x1b\x8FL");
  176.             } else {
  177.                 printf("\x1b\x8FS");
  178.             }
  179.             goto END;
  180.         }
  181.         _delay_ms(1);
  182.         delay--;
  183.     }
  184.     resetValues();
  185. END:
  186.     EIFR = (1 << INTF0);
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement