go_away

Author Topic: Port Manipulation - Pulse Counter  (Read 1398 times)

0 Members and 1 Guest are viewing this topic.

Offline derloneTopic starter

  • Beginner
  • *
  • Posts: 2
  • Helpful? 0
Port Manipulation - Pulse Counter
« on: July 14, 2015, 10:24:11 AM »
I'm trying to do a counter of pulses and I can not use digitalRead nor Interrupts. The speed of pulses I am getting is too fast, and that is why I am planning to use port manipulation. I am using arduino ATmega 2560. The code I have so far and that is not working is this:

Code: [Select]
volatile unsigned int pulses = 0;
void count() {
  // This function is called
  pulses++;
}
void setup() {
 
   DDRD &= ~(1<<PD6); //set pin 49 as input

   Serial.begin(9600);
}
void loop() {
  if (PIND & (1 <<PD6)) {
            count (); }     
  Serial.println( pulses );
}

Offline mklrobo

  • Supreme Robot
  • *****
  • Posts: 516
  • Helpful? 14
  • From Dream to Design at the speed of Imagination!
Re: Port Manipulation - Pulse Counter
« Reply #1 on: July 14, 2015, 12:55:16 PM »
 ;D Hello!
Your code, as I interpret, logically;

assigned the variable pulses;
volatile unsigned int pulses = 0;
void count() {
  // This function is called
  pulses++;
}
count function to get variable pulses to count 1 every time count() is called.

void setup() {
 
   DDRD &= ~(1<<PD6); //set pin 49 as input

   Serial.begin(9600);
}
Setup the pin 49 as an input; Serial.begin opens a function for 9600 transmission(?)

void loop() {
  if (PIND & (1 <<PD6)) {
            count (); }     
  Serial.println( pulses );
}
loop() function detects a "1" on pin 49, calls the count function, then prints the amount of whatever is in
the variable pulses.


logically, it looks good; the only problem I see is the println call; where in C should be printn; unless this
command is "print line" command.
What is the output, or errors given? The only other problem I would contemplate is the pin assignment
commands, in which I do not have knowledge of.(at this time,  ;) )

Offline derloneTopic starter

  • Beginner
  • *
  • Posts: 2
  • Helpful? 0
Re: Port Manipulation - Pulse Counter
« Reply #2 on: July 14, 2015, 05:52:43 PM »
So, I have used `println` as shown in the code down below because I want to see on the serial monitor the counter working. The problem with this code is that I dont want to use interrupt method.

Code: [Select]
int pin = 3;
unsigned long duration;
// Initialize the counter
volatile unsigned int pulses = 0;
void count() {
  // This function is called by the interrupt
  pulses++;
}
void setup() {
  Serial.begin(9600);
  pinMode(pin, INPUT);
  attachInterrupt(1, count, RISING);
}
void loop() {
  duration = pulseIn(pin, HIGH);
  Serial.println("duration in us");
  Serial.println(duration);
  Serial.println("");
  Serial.println("pulses");
  Serial.println(pulses);
  Serial.println("");
 }

Offline mklrobo

  • Supreme Robot
  • *****
  • Posts: 516
  • Helpful? 14
  • From Dream to Design at the speed of Imagination!
Re: Port Manipulation - Pulse Counter
« Reply #3 on: July 15, 2015, 08:37:00 AM »
 :) Your statement;
The problem with this code is that I dont want to use interrupt method.
This is the part where I was concerned, when I started working with Atmel MCU processors.
I would like to have a list, book, or documentation of what commands are available. Obviously,
each processor may use the basics of Atmel programming, but because of the size and internal architecture,
the commands are not the same at all levels. This is  a concern, where the learning curve is steep.  :'(
I do know that there are bugs in the programming, as stated in the help documentation.
There might be a better command that you can use, but digging up the documentation is going to be
a laborious task, as well as dodging the potential "bug" that may exist. I will try to review the commands for
that processor, hopefully it will not be a terrible experience.   :'(  Good Luck!! ;D

 


Get Your Ad Here

data_list