Components and supplies
Arduino UNO
Apps and platforms
VB6 Software
Project description
Code
vf meter
c_cpp
1/* 2 * Counter 3 * Created: 12/08/2017 23:34:47 4 * Author: moty22.co.uk 5*/ 6 7 8 byte inByte=0, overF=0; 9 unsigned int freq=0; 10 11// the setup function runs once when you press reset or power the board 12void setup() { 13 // initialize serial communication at 9600 bits per second: 14 Serial.begin(9600); 15 16 //1 Hz timer 17 OCR0A = 249; 18 TCCR0A = _BV(WGM00) | _BV(WGM01) | _BV(COM0A0); // 19 TCCR0B = _BV(WGM02) | _BV(CS02) | _BV(CS01); // PWM mode, input T0 pin D4 20 pinMode(6, OUTPUT); 21 22 //250 Hz - timer2 23 OCR2A =249; 24 OCR2B = 125; 25 TCCR2A = _BV(COM2B1) | _BV(COM2B0) | _BV(WGM21) | _BV(WGM20); //output B in phase, fast PWM mode 26 TCCR2B = _BV(WGM22) | _BV(CS22) | _BV(CS21); // set prescaler to 256 and start the timer 27 pinMode(3, OUTPUT); 28 29 // counter input T1 pin D5 30 OCR1A = 32767; //32768 counts 31 TCCR1A = _BV(WGM10) | _BV(WGM11) | _BV(COM1A0); // 32 TCCR1B =_BV(WGM12) | _BV(WGM13) | _BV(CS12) | _BV(CS11); //input pin D5 33 pinMode(9, OUTPUT); 34 35 //wait for connection from PC 36 while(Serial.available() == 0) {} 37 if(Serial.read() == 255){Serial.write(255);} // if incoming byte=255 send 255. 38 39} 40 41 42 // the loop function runs over and over again forever 43void loop() { 44 45 if (Serial.available() > 0) { 46 // read the incoming byte: 47 inByte = Serial.read(); 48 if(inByte == 255){Serial.write(255);} 49 50 } 51 //counter 52 53 while(digitalRead(6)){} 54 while(!digitalRead(6)){} 55 56 TIFR1 = _BV(OCF1A); //reset int 57 OCR1A = 32767; 58 TCNT1=0; 59 overF=0; 60 while(digitalRead(6)){ 61 if(TIFR1 & (1<<OCF1A)) {++overF; TIFR1 = _BV(OCF1A);} //overflow 62 } 63 freq = TCNT1 ; 64 65 Serial.write(highByte(analogRead(A1))); //send low byte of 10 bits analogue read 66 Serial.write(lowByte(analogRead(A1))); //send high byte of 10 bits analogue read 67 68 Serial.write(0); 69 Serial.write(overF); 70 Serial.write(highByte(freq)); 71 Serial.write(lowByte(freq)); 72 73} 74 75 76
vf meter
c_cpp
1/* 2 * Counter 3 * Created: 12/08/2017 23:34:47 4 * Author: moty22.co.uk 5*/ 6 7 8 9 byte inByte=0, overF=0; 10 unsigned int freq=0; 11 12// the setup function 13 runs once when you press reset or power the board 14void setup() { 15 // 16 initialize serial communication at 9600 bits per second: 17 Serial.begin(9600); 18 19 20 //1 Hz timer 21 OCR0A = 249; 22 TCCR0A = _BV(WGM00) | 23 _BV(WGM01) | _BV(COM0A0); // 24 TCCR0B = _BV(WGM02) | _BV(CS02) | _BV(CS01); 25 // PWM mode, input T0 pin D4 26 pinMode(6, OUTPUT); 27 28 //250 29 Hz - timer2 30 OCR2A =249; 31 OCR2B = 125; 32 TCCR2A = _BV(COM2B1) 33 | _BV(COM2B0) | _BV(WGM21) | _BV(WGM20); //output B in phase, fast PWM mode 34 35 TCCR2B = _BV(WGM22) | _BV(CS22) | _BV(CS21); // set prescaler to 256 and start 36 the timer 37 pinMode(3, OUTPUT); 38 39 // counter input T1 pin 40 D5 41 OCR1A = 32767; //32768 counts 42 TCCR1A = _BV(WGM10) | _BV(WGM11) 43 | _BV(COM1A0); // 44 TCCR1B =_BV(WGM12) | _BV(WGM13) | _BV(CS12) | _BV(CS11); 45 //input pin D5 46 pinMode(9, OUTPUT); 47 48 //wait for connection 49 from PC 50 while(Serial.available() == 0) {} 51 if(Serial.read() == 255){Serial.write(255);} 52 // if incoming byte=255 send 255. 53 54} 55 56 57 // the loop 58 function runs over and over again forever 59void loop() { 60 61 if (Serial.available() 62 > 0) { 63 // read the incoming byte: 64 inByte = Serial.read(); 65 66 if(inByte == 255){Serial.write(255);} 67 68 } 69 //counter 70 71 72 while(digitalRead(6)){} 73 while(!digitalRead(6)){} 74 75 TIFR1 = 76 _BV(OCF1A); //reset int 77 OCR1A = 32767; 78 TCNT1=0; 79 overF=0; 80 81 while(digitalRead(6)){ 82 if(TIFR1 & (1<<OCF1A)) {++overF; TIFR1 = _BV(OCF1A);} 83 //overflow 84 } 85 freq = TCNT1 ; 86 87 Serial.write(highByte(analogRead(A1))); 88 //send low byte of 10 bits analogue read 89 Serial.write(lowByte(analogRead(A1))); 90 //send high byte of 10 bits analogue read 91 92 Serial.write(0); 93 Serial.write(overF); 94 95 Serial.write(highByte(freq)); 96 Serial.write(lowByte(freq)); 97 98} 99 100 101
Downloadable files
vf meter
vf meter
Comments
Only logged in users can leave comments