Hi everybody, Debug or my code problem?? :(

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

Before i post the code let me tell you all that i don't know if this the right place in forum to post this. If not, can a moderator move it to the right place? Thanks in advance.

Well, my problem is about the code above, because i can't (because i'm a begginer in coding and avr) reach the problem here. When debugging, the program stucks in the first condition and even when the button variable is forced = '0', till the variable "button_pressed" reaches 40, time to debounce (my debounce way, please correct me if there is a better way), and the timer does not start.
WHY???? :(

/*
 * Scanner_de_larson_KIT.cpp
 *
 * Created: 20-06-2014 13:08:39
 *  Author: Nelson
 */ 


#include 
#include 
#include 
#define F_CPU 8000000UL
#include "bit_tools.h"
uint8_t i = 0, j = 0;
uint8_t dir=0;
uint8_t count = 0;


int main(void)
{
	
	CLKPR	=	(1<<CLKPCE);		//TO PREPARE CLK PRESCALER FOR EDITION
	CLKPR	=	(1<<CLKPS1)	|	(1<<CLKPS0);	//PRESCALER EDITION CLK/8
	OCR1A	=	1953;				//(INTEGER) VALUE CALCULATED TO FIT ON TIMER, IN CASE TIMER1(16BITS) CLK WILL BE DIVIDED BY 64
	//TCCR1B	=	(1<<WGM12)	|	(1<<CS10)	|	(1<<CS11);	//WGM12 = CTC MODE (CLEAR TIMER ON COMPARE) (CS10, CS11) =1 FOR CLK/64
	DDRD	=	0xFF;				//ENABLE PORTD ALL OUTPUTS
	DDRC	=	0x00;				//ENABLE PORTC AS INPUTS
	PORTC	=	0x01;				//ENABLE INTERNAL PULL-UP RESISTOR ON PINC0
	PORTD	=	0x00;				//TURN LOW ALL PORTD BITS
	TIMSK1	=	(1<<OCIE1A); //COLOCA-SE UM "1" NO BIT OCIE1A (OUTPUT COMPARE INTERRUPT ENABLE TIMER1 REGISTO A)DO REGISTO TIMSK1
	uint8_t button_pressed = 0;
	
	
    while(1)
    {
		if (bit_read(PORTC,0) == 0) //TEST CONDITION TO TEST BUTTON (Can it be "if (bit_read(PINC,0)==0)"???
			{
				button_pressed ++; //Increase button_pressed variable;
				if (button_pressed>40) //To be sure about if it is really pressed
				{
					sei();		//enable global interrupts
					j++;		//increment j variable once the button is pressed
					TCCR1B	=	(1<<WGM12)	|	(1<<CS10)	|	(1<<CS11);	//WGM12 = CTC MODE (CLEAR TIMER ON COMPARE) (CS10, CS11) =1 FOR CLK/64
					//sei();
					//button_pressed = 0; //Reset condition variable to allow next evaluation
				}
				else 
				{
					cli();		//disable global interrupts
				}				
			}
		
      
    }
}
ISR(TIMER1_COMPA_vect)	//to do if the button was pressed and how many times it was pressed
{
	if (j==1)		//if pressed one time do this (Larson_Scanner (my simple version))
	{
	 if (dir==0)	// check direction to light the leds
		{
		PORTD	=	(1<<i) |	(1<<(i-1));	//	write on portd to light up two led's each time will light up led on pin(i) and (i-1)
		i++;		//	increment "i" variable to run all pin's on portd 
		if (i>7) {	//test if was reached the end of portd (PIND7)
			dir=1;	// if condition was satisfied (end of portd, change direction)
			}
		}		
	else	//else to the same thing in the other direction
	{
		PORTD	= (1<<i)	|	(1<<(i-1));
		i--;
		if (i==0) dir=0;	// when reaches the PINDO change direction again
	}	
	}
	if (j==2)		//in case of j=2 just toggle pind0
	{
		PORTD |= 0x01;		//PIND0 HIGH
		PORTD ^=0xFF;		//toggle ao pinD0
	}
	if (j==3)		// here use the pins on portd and light up the even and then toggle all
	{
		PORTD	|=	0xAA;	//HIGH LEVEL EVEN PINS ON PORTD	
		PORTD ^=0xFF;		//TOGGLE PORTD
	}
	if (j==4)		// if j=4 portd will start from counting from 0 to 255 binary count
	{
		PORTD	|=	0x00;	//LOW LEVEL ON ALL PORTD BITS
		count++;
		PORTD = count;		//WRITE BINARY DATA IN COUNT VARIABLE ON PORTD
	}
	if (j>=5)		
		{
		j==0;
		return;		//here is just a return to main and then wait to press the button again
		}		
		
}					//Please help me, why the timer doesn't start???
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Which processor ?. (there are differences between timers)

Which 'first condition' ?

Quote:
if (bit_read(PORTC,0) == 0)
or
Quote:
if(j==1)

The timer is operating after you initialise it and you never stop it, therefore there is no need to reinitialise it in the while(1) loop after the j++;

The variable 'j' is used inside the interrupt handler and in main() therefore you should do

volatile uint8_t j;

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

Please use CODE tags.

Quote:
Can it be "if (bit_read(PINC,0)==0)"???
Not only can it be, it must be. PORTC is for output, PINC is for input.

Regards,
Steve A.

The Board helps those that help themselves.

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

Quote:

Please use CODE tags.

In fact he did but he also have "disable BBcode" ticked which then prevented it working. @Nelsonx, I don't know if you just ticked "disable BB" for that one post (if so why?) or whether maybe your profile is set to create all posts with "disable BB"? If the latter then I'd recommend you change it as I cannot think of a reason why you'd ever really want BB to be disabled and on the rare occasion you think of a reason just manually tick it for that one post.

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

Thanks all for your replys.

Quote:

Which processor ?. (there are differences between timers)

Which 'first condition' ?

if (bit_read(PORTC,0) == 0)
or
Quote:
if(j==1)

The timer is operating after you initialise it and you never stop it, therefore there is no need to reinitialise it in the while(1) loop after the j++;

The variable 'j' is used inside the interrupt handler and in main() therefore you should do
Code:
volatile uint8_t j;

Quote:

First, the answer to Mikech:
Processor - Atmega328P
First Condition - if(bit_read)...

Second to Koshchi
Sorry, but what are the code Tags??
PS: Thanks for the quote and the answer

At last, Clawson
I had disabled it as i disable the emoticons (only at this post), because i don't know what is the BBcode and i don't want it to mess my "code"

By the way... What do you suggest to debounce? Is that ok? I'm trying to grow "inside" AVR's and need to learn a lot.

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

Why my timer doesn't start?
I had set it up in TCCR1B i had activated the TIMSK1 regiter bit OCIE1A (to enable the output compare interrupt enable bit)and after incremented the "j" variable i call "sei();" Why it does not start? What am i doing wrong?
By the way, i have used this on main now

Quote:
Code:
volatile uint8_t j;

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

Quote:
Quote:
Can it be "if (bit_read(PINC,0)==0)"???
Not only can it be, it must be. PORTC is for output, PINC is for input.

I don't understand. I had changed my code and that way i am unable to test the PINC,0.
I had enabled the internal pull up on PORTC

CLKPR	=	(1<<CLKPCE);		//TO PREPARE CLK PRESCALER FOR EDITION
	CLKPR	=	(1<<CLKPS1)	|	(1<<CLKPS0);	//PRESCALER EDITION CLK/8
	OCR1A	=	1953;				//(INTEGER) VALUE CALCULATED TO FIT ON TIMER, IN CASE TIMER1(16BITS) CLK WILL BE DIVIDED BY 64
	TCCR1B	=	(1<<WGM12)	|	(1<<CS10)	|	(1<<CS11);	//WGM12 = CTC MODE (CLEAR TIMER ON COMPARE) (CS10, CS11) =1 FOR CLK/64
	DDRD	=	0xFF;				//ENABLE PORTD ALL OUTPUTS
	DDRC	=	0x00;				//ENABLE PORTC AS INPUTS
	PINC	=	0x01;				//ENABLE INTERNAL PULL-UP RESISTOR ON PINC0
	PORTD	=	0x00;				//TURN LOW ALL PORTD BITS
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

PINC = 0x01; //ENABLE INTERNAL PULL-UP RESISTOR ON PINC0

But that is not what that line of code does?!? To enable a pull-up you write to PORTC not PINC

It's when you come to read an input: That is when it is PINC not PORTC you must access.

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

Quote:

Quote:

PINC = 0x01; //ENABLE INTERNAL PULL-UP RESISTOR ON PINC0

But that is not what that line of code does?!? To enable a pull-up you write to PORTC not PINC


LOL, Cliff--trick question and trick answer.

The OP's line of code will indeed "ENABLE INTERNAL PULL-UP RESISTOR ON PINC0" -- IF it was not already enabled.

(On modern AVR models, including the mentioned MEga328P, write to PINx toggles the corresponding bit(s) in PORTx.)

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Quote:
Quote:

Quote:

PINC = 0x01; //ENABLE INTERNAL PULL-UP RESISTOR ON PINC0

But that is not what that line of code does?!? To enable a pull-up you write to PORTC not PINC

You are correct...

I can only test if defined PORTC = 0x01; and then If(PINC,0) == 0 otherwise i'm unable to debug.
Tested already.
Sorry about my questions but i'm starting with AVR's and restart coding, since i had learned when graduated, but... nothing lasts. I'm just a begginner
Hope you all understand and help when need.`
After all this forum is to help eachother right?
Thanks to all the community to make learning easy

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

Quote:

You are correct...

Well, no, as Lee pointed out I was wrong about that.