Components and supplies
pot 10K
RGB Backlight LCD - 16x2
Arduino UNO
Project description
Code
counter
c_cpp
1/* 2 * LCD Counter 3 * Created: 12/08/2017 23:34:47 4 * Author: moty22.co.uk 5*/ 6 7#include <LiquidCrystal.h> 8 9 byte overF=0; 10 unsigned long freq; 11 double period; 12 13 // initialize the library with the numbers of the interface pins 14 LiquidCrystal lcd(12, 11, A2, A3, A4, A5); 15 16// the setup function runs once when you press reset or power the board 17void setup() { 18 19 lcd.begin(16, 2); // set up the LCD's number of columns and rows: 20 21 //1 Hz timer 22 OCR0A = 249; 23 TCCR0A = _BV(WGM00) | _BV(WGM01) | _BV(COM0A0); // 24 TCCR0B = _BV(WGM02) | _BV(CS02) | _BV(CS01); // PWM mode, input T0 pin D4 25 pinMode(6, OUTPUT); 26 27 //250 Hz - timer2 28 OCR2A =249; 29 OCR2B = 125; 30 TCCR2A = _BV(COM2B1) | _BV(COM2B0) | _BV(WGM21) | _BV(WGM20); //output B in phase, fast PWM mode 31 TCCR2B = _BV(WGM22) | _BV(CS22) | _BV(CS21); // set prescaler to 256 and start the timer 32 pinMode(3, OUTPUT); 33 34 // counter input T1 pin D5 35 OCR1A = 32767; //32768 counts 36 TCCR1A = _BV(WGM10) | _BV(WGM11) | _BV(COM1A0); // 37 TCCR1B =_BV(WGM12) | _BV(WGM13) | _BV(CS12) | _BV(CS11); //input pin D5 38 pinMode(9, OUTPUT); 39 40 lcd.setCursor(7, 0); // top middle 41 lcd.print("hi"); 42 43} 44 45 46 // the loop function runs over and over again forever 47void loop() { 48 49 //wait for high 50 while(digitalRead(6)){} 51 while(!digitalRead(6)){} 52 //start the count 53 TIFR1 = _BV(OCF1A); //reset int 54 OCR1A = 32767; 55 TCNT1=0; 56 overF=0; 57 while(digitalRead(6)){ 58 if(TIFR1 & (1<<OCF1A)) {++overF; TIFR1 = _BV(OCF1A);} //overflow 59 } 60 //count end 61 freq = (unsigned long)TCNT1 + ((unsigned long)overF * 32768); 62 period = 1000000 / (double)freq; 63 if(freq == 0){period=0;} 64 65 lcd.clear(); 66 lcd.setCursor(3, 0); // top line 67 lcd.print(freq,DEC); 68 lcd.setCursor(11, 0); 69 lcd.print("Hz"); 70 lcd.setCursor(3, 1); 71 lcd.print(period,DEC); 72 lcd.setCursor(10, 1); 73 lcd.print(" uS "); 74} 75 76 77
counter
c_cpp
1/* 2 * LCD Counter 3 * Created: 12/08/2017 23:34:47 4 * Author: 5 moty22.co.uk 6*/ 7 8#include <LiquidCrystal.h> 9 10 byte overF=0; 11 12 unsigned long freq; 13 double period; 14 15 // initialize the library 16 with the numbers of the interface pins 17 LiquidCrystal lcd(12, 11, A2, A3, A4, 18 A5); 19 20// the setup function runs once when you press reset or power the board 21void 22 setup() { 23 24 lcd.begin(16, 2); // set up the LCD's number of columns 25 and rows: 26 27 //1 Hz timer 28 OCR0A = 249; 29 TCCR0A = 30 _BV(WGM00) | _BV(WGM01) | _BV(COM0A0); // 31 TCCR0B = _BV(WGM02) | _BV(CS02) 32 | _BV(CS01); // PWM mode, input T0 pin D4 33 pinMode(6, OUTPUT); 34 35 36 //250 Hz - timer2 37 OCR2A =249; 38 OCR2B = 125; 39 TCCR2A = 40 _BV(COM2B1) | _BV(COM2B0) | _BV(WGM21) | _BV(WGM20); //output B in phase, fast 41 PWM mode 42 TCCR2B = _BV(WGM22) | _BV(CS22) | _BV(CS21); // set prescaler to 43 256 and start the timer 44 pinMode(3, OUTPUT); 45 46 // counter 47 input T1 pin D5 48 OCR1A = 32767; //32768 counts 49 TCCR1A = _BV(WGM10) 50 | _BV(WGM11) | _BV(COM1A0); // 51 TCCR1B =_BV(WGM12) | _BV(WGM13) | _BV(CS12) 52 | _BV(CS11); //input pin D5 53 pinMode(9, OUTPUT); 54 55 lcd.setCursor(7, 56 0); // top middle 57 lcd.print("hi"); 58 59} 60 61 62 // the loop 63 function runs over and over again forever 64void loop() { 65 66 //wait for 67 high 68 while(digitalRead(6)){} 69 while(!digitalRead(6)){} 70 //start 71 the count 72 TIFR1 = _BV(OCF1A); //reset int 73 OCR1A = 32767; 74 TCNT1=0; 75 76 overF=0; 77 while(digitalRead(6)){ 78 if(TIFR1 & (1<<OCF1A)) {++overF; 79 TIFR1 = _BV(OCF1A);} //overflow 80 } 81 //count end 82 freq = 83 (unsigned long)TCNT1 + ((unsigned long)overF * 32768); 84 period = 1000000 / 85 (double)freq; 86 if(freq == 0){period=0;} 87 88 lcd.clear(); 89 lcd.setCursor(3, 90 0); // top line 91 lcd.print(freq,DEC); 92 lcd.setCursor(11, 0); 93 lcd.print("Hz"); 94 95 lcd.setCursor(3, 1); 96 lcd.print(period,DEC); 97 lcd.setCursor(10, 98 1); 99 lcd.print(" uS "); 100} 101 102 103
Downloadable files
Counter
Counter
Comments
Only logged in users can leave comments