Tiny1634 Timer1 issue

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

Been trying to get the timer to blink an led - simple stuff, but I am obviously doing something wrong.  I have it running as a free running timer with TOP being 0xFF.  I have COM1B1:0 set for clear on match, in the Overflow ISR the pin is set, the port pin DDR set properly, but the pin stays low.  If I set the COM bits to toggle, and remove the ISR it works just fine, Interrupts are enabled, as is the global enable.  From the datasheet I should be able to run this without the need for a compare match interrupt

 

The idea is to create a 'pulse' of about an eighth of a second.  Right now I am trying to get half second on/off.

ISR(TIM1_OVF_vect)
{
	PORTA = PORTA | 0x40;
	
}

void avr_init(void)
{
// Port A initialization
	// Pull-up initialization
	PUEA=(0<<PUEA7) | (0<<PUEA6) | (0<<PUEA5) | (0<<PUEA4) | (0<<PUEA3) | (0<<PUEA2) | (0<<PUEA1) | (0<<PUEA0);
	// Function: Bit7=In Bit6=In Bit5=Out Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In
	DDRA=(0<<DDRA7) | (1<<DDRA6) | (0<<DDRA5) | (0<<DDRA4) | (0<<DDRA3) | (0<<DDRA2) | (0<<DDRA1) | (0<<DDRA0);
	// State: Bit7=T Bit6=T Bit5=0 Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T
	PORTA=(0<<PORTA7) | (0<<PORTA6) | (0<<PORTA5) | (0<<PORTA4) | (0<<PORTA3) | (0<<PORTA2) | (0<<PORTA1) | (0<<PORTA0);
	
	// Timer/Counter 1 initialization
	// Clock source: System Clock
	// Clock value: 31.250 kHz
	// Mode: Normal top=0xFFFF
	// OC1A output: Disconnected
	// OC1B output: Clear on compare match
	// Noise Canceler: Off
	// Input Capture on Falling Edge
	// Timer Period: 1 s
	// Output Pulse(s):
	// OC1B Period: 1 s
	// Timer1 Overflow Interrupt: On
	// Input Capture Interrupt: Off
	// Compare A Match Interrupt: Off
	// Compare B Match Interrupt: Off
	TCCR1A=(0<<COM1A1) | (0<<COM1A0) | (1<<COM1B1) | (0<<COM1B0) | (0<<WGM11) | (0<<WGM10);
	TCCR1B=(0<<ICNC1) | (0<<ICES1) | (0<<WGM13) | (0<<WGM12) | (1<<CS12) | (0<<CS11) | (0<<CS10);
	TCNT1H=0x00;
	TCNT1L=0x00;
	ICR1H=0x00;
	ICR1L=0x00;
	OCR1AH=0x00;
	OCR1AL=0x00;
	OCR1BH=0x80;
	OCR1BL=0x00;
	// Timer(s)/Counter(s) Interrupt(s) initialization
	TIMSK=(1<<TOIE1) | (0<<OCIE1A) | (0<<OCIE1B) | (0<<ICIE1) | (0<<OCIE0B) | (0<<TOIE0) | (0<<OCIE0A);
}


int main(void)
{
	avr_init();
	
	sei();
	
    while(1)
    {
        
    }
}

 

I am aware that I could use PWM or CTC mode, but I would prefer to use this for the application.  PLus this is just bugging the crud outta me now.

 

Jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

Last Edited: Tue. Mar 21, 2017 - 04:06 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When COMxYz bits are set, then the PORT output is disabled.

 

The mode you have selected is (generally) to be used with one of the "Phase Correct" counter modes.

David (aka frog_jr)

Last Edited: Tue. Mar 21, 2017 - 03:48 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Any reason why this need to be a sticky?

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

No reason at all... I hit the wrong radio button in haste

 

Jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

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

frog_jr wrote:
When COMxYz bits are set, then the PORT output is disabled.

 

Not so sure about that....From  the datasheet:

 

I have the DDR for PA6 set for output.

 

Jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

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

What I was trying to say is that the PORTA = PORTA | 0x40; inside the ISR will have no effect because:

If one or both of the COM1B[1:0] bit are written to one, the OC1B output overrides the

normal port functionality of the I/O pin it is connected to.

 

 

David (aka frog_jr)

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

If the intent is to toggle bit 6 in PORTA in the ISR, shouldn't that be;

 

      PORTA ^= 0x40;

 

I'm not that familiar with "C", but your code turns bit on and then never off again.

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

Because the COM1B[1:0] bits are configured to "Clear on Compare Match", there is nothing to set the bit high, and "normal port functionality" is disabled.

David (aka frog_jr)

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

Ahh, ok. That makes sense. To then how to clear the pin then when the timer overflows? It would seem odd there is no way other than resetting the AVR to clear the pin.

Jim

Edit: only way I see to clear the pin would be to change the pin to normal mode, clear it, and then change it back to being controlled by the timer.

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

Last Edited: Tue. Mar 21, 2017 - 08:19 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
To then how to clear the pin then when the timer overflows?
Set the COM-bits and do a "Force Output Compare".

Stefan Ernst

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

jgmdesign wrote:
Edit: only way I see to clear the pin would be to change the pin to normal mode, clear it, and then change it back to being controlled by the timer.
Won't work. The timer has its own stored output state, so "change it back to being controlled by the timer" also means to change back to the last timer controlled output state.

Stefan Ernst

Last Edited: Tue. Mar 21, 2017 - 08:29 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks. Will try it later and report back

Jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

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

What do you actually want to achieve?   2.097s period with 1s on and 1s off ?

Or do you want 0.125s on and 1.875s off ?

Or 0.125s on and 0.875s off ?

 

The Tiny1634 should work exactly like any other AVR 16-bit Timer1.  i.e. set a PWM mode and duty cycle.

It should work even with "unusual" PWM periods.

 

PWM#14 is probably the easiest one to use.

 

If you give real-life numbers for your period,  duty-cycle and polarity,   I will try it with the CodeWizard.

Normally,  I would just calculate in the program statements.

 

David.

 

Edit.  Just tried it in the CodeWizard.   It is painful to set ICR1 and OCR1B manually.   But at least it gives you instant feedback for period and pulse width.

Far easier to just calculate manually.

Last Edited: Tue. Mar 21, 2017 - 09:04 AM