EXTERNAL CLOCK SOURCE FOR TIMER

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How do I give external clock source to timer of ATMEGA 328p ?I am using TCS3200 output frequency as clock source for the atmega timer.

The timer is supposed to stop at every 1s and print the output.

Is the following code correct:

 

#define S0 2
#define S1 3
#define S2 6
#define S3 5
#define sensorOut 4

int frequency = 0;

void setup() {
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(sensorOut, OUTPUT);
  
  // Setting frequency-scaling to 20%
  digitalWrite(S0,HIGH);
  digitalWrite(S1,LOW);

}
void loop() {
  // Setting red filtered photodiodes to be read
  digitalWrite(S2,LOW);
  digitalWrite(S3,LOW);
  // Reading the output frequency
  frequency = pulseIn(sensorOut, HIGH);
 }

 int timer()
{
  TCCR0B |=(1<<CS02)|(1<<CS01)|(0<<CS00);//external source on T0 pin
  TCCR0A |=(1<<WGM01)|(0<<WGM00);//ctc mode
  TCCR0B |=(0<<WGM02);//ctc mode
  TCCR0A |=(1<<COM0A1)|(0<<COM0A0)|(1<<COM0B1)|(0<<COM0B0);//clear on compare
  TIFR0 |=(0<<TOV0);
  OCR0A=0xff;
  while(1)
  {
   if(TOV0==1)
    {Serial.print("matched");TCNT0=0;}
   else
   {  
  Serial.begin(9600);Serial.print(TCNT0);} 
  }
}

Last Edited: Thu. Aug 10, 2017 - 11:30 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AFAIK only timer 2 on 328 has the ability to be clocked externally.

 

BTW as this is about 328 not Xmega I will move it to a more appropriate forum.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Topic locked