Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Problem with CTC mode of timer

Status
Not open for further replies.

abdi110

Member level 3
Joined
Mar 5, 2015
Messages
60
Helped
0
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,728
Hi every one,
I am going to turn on a relay each 200 hours for 5 minutes.
I use ATtiny13 and for the first test I am going to do this for each 1 houre.
the problem is that the system works randomly, I mean it works for the first or second hour properly but after one or two hours it turns the relay on after 3 or 4 minutes(instead of one hour).
i use ctc timer mode with an 1 second interrupt.
the Fcpu is 128kHz internal RC and the the prescale is 1024 so the OCR0A IS:
((128000/1024=125)-1)=124 decimal or 7CH.
I made the circuit on a bread board.
The code is as below:
Chip type : ATtiny13
AVR Core Clock frequency: 0.128000 MHz
Memory model : Tiny
External RAM size : 0
Data Stack size : 16
*****************************************************/


#include <tiny13.h>
#include <delay.h>


eeprom char sec=0x00;
eeprom char minutes=0x00;
eeprom char hours=0x00;
// Timer 0 output compare A interrupt service routine
interrupt [TIM0_COMPA] void timer0_compa_isr(void)
{

if(++sec == 60) {

if(++minutes == 60) {

++hours;

minutes = 0;
}
sec = 0;
}


}
void main(void)
{

int i;
// Crystal Oscillator division factor: 1
#pragma optsize-
CLKPR=0x80;
CLKPR=0x00;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

// Input/Output Ports initialization
// Port B initialization
// Func5=In Func4=In Func3=In Func2=In Func1=In Func0=Out
// State5=P State4=P State3=P State2=P State1=P State0=1
PORTB=0x3F;
DDRB=0x01;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 0.125 kHz
// Mode: CTC top=OCR0A
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0x02;
TCCR0B=0x05;
TCNT0=0x00;
OCR0A=0x7C;
OCR0B=0x00;
TIMSK0=0x04;
// Analog Comparator initialization
// Analog Comparator: Off
ACSR=0x80;
ADCSRB=0x00;
DIDR0=0x00;

#asm("sei")
while (1){
if(hours>=1){
PORTB.0=0; //turn the relay on

for (i=1;i<=60;i++) { // 5 minutes deay

delay_ms(5000);
};

PORTB.0=1;//turn the relay off

sec=0;
minutes=0;
hours=0;

}

else
{
PORTB.0=1;//turn the relay off
};


}
}
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top