[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  Forum
. -> [Folder]  Problems
. . -> [Subject]  Different result frequency counter on Arduino Uno stand alone
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Different result frequency counter on Arduino Uno stand alone

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Yoyokal   (2 posts)  [Biography] bio
Date Tue 17 Mar 2015 03:37 PM (UTC)

Amended on Tue 17 Mar 2015 03:43 PM (UTC) by Yoyokal

Message
Dear Sir,

Did you tried Frequency Counter sketch for Atmega328 on stand alone chip?

I have tried Frequency Counter sketch for Atmega328 at http://www.gammon.com.au/timers and succesfully runing on Arduino Uno Board R3. The result is agreed with my Oscilloscope and my TRIO frequency counter. The result from my oscillator is nicely 114 kHz. But strangely the result is different when I'm transform it into stand alone Atmega328, the result is too high. I got about 152kHz on my LCD 16x2.

I'm flashing my chip using usbasp through Arduino IDE using menu File > Upload Using Programmer.

Here it is my code. The changes is just the code to show the result on LCD.

// Timer and Counter example
// Author: Nick Gammon
// Date: 17th January 2012

#include <LiquidCrystal.h>

const int pinRS = 6;
const int pinEn = 7;
const int pinD4 = 1;
const int pinD5 = 2;
const int pinD6 = 3;
const int pinD7 = 4;
const int pinBACKLIGHT = 11;

LiquidCrystal lcd(pinRS, pinEn, pinD4, pinD5, pinD6, pinD7);

// Input: Pin D5

// these are checked for in the main program
volatile unsigned long timerCounts;
volatile boolean counterReady;

// internal to counting routine
unsigned long overflowCount;
unsigned int timerTicks;
unsigned int timerPeriod;

void startCounting (unsigned int ms)
{
counterReady = false; // time not up yet
timerPeriod = ms; // how many 1 mS counts to do
timerTicks = 0; // reset interrupt counter
overflowCount = 0; // no overflows yet

// reset Timer 1 and Timer 2
TCCR1A = 0;
TCCR1B = 0;
TCCR2A = 0;
TCCR2B = 0;

// Timer 1 - counts events on pin D5
TIMSK1 = bit (TOIE1); // interrupt on Timer 1 overflow

// Timer 2 - gives us our 1 mS counting interval
// 16 MHz clock (62.5 nS per tick) - prescaled by 128
// counter increments every 8 µS.
// So we count 125 of them, giving exactly 1000 µS (1 mS)
TCCR2A = bit (WGM21) ; // CTC mode
OCR2A = 124; // count up to 125 (zero relative!!!!)

// Timer 2 - interrupt on match (ie. every 1 mS)
TIMSK2 = bit (OCIE2A); // enable Timer2 Interrupt

TCNT1 = 0; // Both counters to zero
TCNT2 = 0;

// Reset prescalers
GTCCR = bit (PSRASY); // reset prescaler now
// start Timer 2
TCCR2B = bit (CS20) | bit (CS22) ; // prescaler of 128
// start Timer 1
// External clock source on T1 pin (D5). Clock on rising edge.
TCCR1B = bit (CS10) | bit (CS11) | bit (CS12);
} // end of startCounting

ISR (TIMER1_OVF_vect)
{
++overflowCount; // count number of Counter1 overflows
} // end of TIMER1_OVF_vect


//******************************************************************
// Timer2 Interrupt Service is invoked by hardware Timer 2 every 1ms = 1000 Hz
// 16Mhz / 128 / 125 = 1000 Hz

ISR (TIMER2_COMPA_vect)
{
// grab counter value before it changes any more
unsigned int timer1CounterValue;
timer1CounterValue = TCNT1; // see datasheet, page 117 (accessing 16-bit registers)
unsigned long overflowCopy = overflowCount;

// see if we have reached timing period
if (++timerTicks < timerPeriod)
return; // not yet

// if just missed an overflow
if ((TIFR1 & bit (TOV1)) && timer1CounterValue < 256)
overflowCopy++;

// end of gate time, measurement ready

TCCR1A = 0; // stop timer 1
TCCR1B = 0;

TCCR2A = 0; // stop timer 2
TCCR2B = 0;

TIMSK1 = 0; // disable Timer1 Interrupt
TIMSK2 = 0; // disable Timer2 Interrupt

// calculate total count
timerCounts = (overflowCopy << 16) + timer1CounterValue; // each overflow is 65536 more
counterReady = true; // set global flag for end count period
} // end of TIMER2_COMPA_vect

void setup ()
{
pinMode(pinBACKLIGHT, OUTPUT);
digitalWrite(pinBACKLIGHT, HIGH);
lcd.clear();
// Serial.begin(115200);
// Serial.println("Frequency Counter");
} // end of setup

void loop ()
{
// stop Timer 0 interrupts from throwing the count out
byte oldTCCR0A = TCCR0A;
byte oldTCCR0B = TCCR0B;
TCCR0A = 0; // stop timer 0
TCCR0B = 0;

startCounting (500); // how many mS to count for

while (!counterReady)
{ } // loop until count over

// adjust counts by counting interval to give frequency in Hz
float frq = (timerCounts * 1000.0) / timerPeriod;

// Serial.print ("Frequency: ");
// Serial.print ((unsigned long) frq);
// Serial.println (" Hz.");
lcd.setCursor(0, 0);
lcd.print((unsigned long)frq);
lcd.print(" Hz");

// restart timer 0
TCCR0A = oldTCCR0A;
TCCR0B = oldTCCR0B;

// let serial stuff finish
delay(200);
} // end of loop

Do you have a sugestions and solutions onto this problem?
[Go to top] top

Posted by Yoyokal   (2 posts)  [Biography] bio
Date Reply #1 on Wed 18 Mar 2015 12:42 AM (UTC)
Message
Dear Sir,

I just found the problem. I curious about frequency clock is lower than 16 MHz so I got high frequency. This is my mistake. My Xtal is 12 MHz. I incidently grab 12 MHz, the label of Xtal is blurred and I think it is 16 MHz. After I'm changed the real 16 MHz Xtal, now it works like it should.

Best regards.
[Go to top] top

Posted by Nick Gammon   Australia  (21,607 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Wed 18 Mar 2015 04:25 AM (UTC)
Message
Glad to hear it works. In future:

Template:Arduino Please post Arduino-related questions to the Arduino Forum or to StackExchange: Arduino. This saves splitting questions and answers between this forum and the Arduino ones.


Thank you.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


3,891 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]