unwanted reset

Thread Starter

Far

Joined May 11, 2009
20
I have been design a simple pulse generator with an ATmega8. but when i run it in proteus with no reason it starts to reset. i have some touch button to select some modes. the reset happens when i click at this buttons. when i setup the circuit this occurs again. I have not use watchdog timer too! what should i do?
need your help!
 
Last edited:

mik3

Joined Feb 4, 2008
4,843
If you have configured pin 1 as a reset pin then you have to tight it to Vcc via a resistor. Use a 10K one.
 

Thread Starter

Far

Joined May 11, 2009
20
my circuit is very simple. i just have a LCD and an ATmega8. the reset pin has been pulled up with a resistor. there is no noise source around it. i think its the program problem. here is the cods
thanks for your help:)


Rich (BB code):
Project : 
Version : 
Date    : 5/18/2009
Author  : l                               
Company : l                               
Comments: 


Chip type           : ATmega8
Program type        : Application
Clock frequency     : 1.000000 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 256
*****************************************************/

#include <mega8.h>
#include <delay.h>
#include <stdio.h>
// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x18 ;PORTB
#endasm
#include <lcd.h>
char bufer[10];
int key2,i,key,b,mode,count,g=0,h,f,ab,n,a,j,p,v,l,k;   
unsigned int time1,time180,time2,time60,time120,time3,time4,time,d; 
void atash(int a);        
int detect_pulse(void);
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Place your code here   
if(mode==1){
if(h==0){
PORTD=0x01; 
TCNT1=time180;
TCCR1B=0x01; 
h=1;
g=1;
}   
g++;
if(g>2){
if(g==3){
PORTD=0x02;
d++;
}
/*if(d==6){
d=0; 
TCCR1B=0x00;
TCNT1=0x00;
#asm("cli");
mode=1;
p=detect_pulse();
}*/
if(g==4){
PORTD=0x01;
g=2;
}
TCNT1=time180;
TCCR1B=0x01;
}   
}
if(mode==2){
if(h==0){
PORTD=0x03; 
TCNT1=time180;
TCCR1B=0x01; 
h=1;
g=1;
}   
g++;
if(g>2){
if(g==3)
PORTD=0x0c;
if(g==4){
PORTD=0x03;
g=2;
}
TCNT1=time180;
TCCR1B=0x01;
}   
}
if(mode==3){
if(h==0){
PORTD=0x01; 
TCNT1=time120;
TCCR1B=0x01; 
h=1;
g=0;
}   
g++;
if(g>2){
if(g==3)
PORTD=0x02;
if(g==4)
PORTD=0x04;
if(g==5){
PORTD=0x01;

g=2;
}
TCNT1=time120;
TCCR1B=0x01;
}   
}
if(mode==4){
g++;
if(h==0){
PORTD=0x01; 
TCNT1=time60;
TCCR1B=0x01; 
h=1;
g=2;
}   
if(g>2){ 
TCNT1=time60;
if(g==3)
PORTD=0x02;
if(g==4)
PORTD=0x04;
if(g==5)
PORTD=0x08;
if(g==6)
PORTD=0x10;
if(g==7)
PORTD=0x20;
if(g==8){
PORTD=0x01;
g=2;
}
}   
}
}

// Declare your global variables here
int set_key(void);
int set_angle(void);

void main(void)
{
// Declare your local variables here
char bufer1[10];
float t,t120,t60,div;
unsigned int angle;
PORTB=0x00;
DDRB=0x00;
PORTC=0x00;
DDRC=0x00;
PORTD=0x00;
DDRD=0x3F;
TCCR0=0x00;
TCNT0=0x00;
TCCR1B=0x00;
TCCR1A=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
GICR|=0x00;
MCUCR=0x00;
GIFR=0x00;
TIMSK=0x04;
// LCD module initialization
#asm("cli")
lcd_init(16);
lcd_clear();     
lcd_putsf("ENTER THE MODE");
goto loop1;
loop1:
while(1){
key=set_key();
 if(key>4){
 key=0;
 i=0;
 }  
  mode=key;
 lcd_clear();
 sprintf(bufer,"NUMBER OF MODE:%i",key);
 lcd_puts(bufer);
 delay_ms(500); 
 key2 =PINC.3 & 0x01;
 if(key2 ==0x01){
   delay_ms(50); 
   key2 =PINC.3 & 0x01;  
      if(key2 ==0x01)
       b=1;
  }
 if(key>0 & b==1){
 lcd_clear();
 lcd_putsf("MODE IS ACCEPT");
 delay_ms(500);
  lcd_clear();
  lcd_gotoxy(0,0);
  sprintf(bufer1,"MODE=%i",mode);
  lcd_puts(bufer1);
  delay_ms(1000);
  goto angel;
 }
}
 br:
  switch(key)
{
    case 1:
     goto mode1;
   case 2:
     goto mode2;
   case 3:
     goto mode3;
   case 4:
     goto mode4;
   case 0:
   goto loop1; 
 }
////////////////////////////// 
mode1: 
mode=1; 
 lcd_clear();
 lcd_putsf("mode=1");
div=180/angle;
t=0.01/div;
t=t/0.000001;
time1=65535-t;
time180=65535-10000; 
p=detect_pulse();
//////////////////////////////
mode2:
mode=2; 
 lcd_clear();
 lcd_putsf("mode=2");
div=angle/180;
t=0.01/div;
t=t/0.000001;
time2=65535-t;
time180=65535-10000; 
p=detect_pulse();
//////////////////////////////
mode3:
mode=3;
div=180/angle;
t=0.01/div;
t=t/0.000001;
time3=65535-t;
t120=2*0.01/3;
t120=65535-t120;
t120=t120/0.000001;
time120=65535-t120;
TCCR1B=0x00;
p=detect_pulse();
//////////////////////////////
mode4:
mode=4;
t60=0.01/3;
t60=t60/0.000001;
t60=65535-t60;
time60=t60;
div=180/angle;
t=0.01/div;
t=t/0.000001;
time4=65535-t;
 p=detect_pulse();
  
//////////////////////////////  
angel: 
f=0;
angle=0;
goto y;
y:
ab=set_angle();
angle=ab*12;
if (angle>170 | angle<0)
{
angle=168;
f=14;
}  
sprintf(bufer,"ANGLE:%i DEG",angle);
 lcd_gotoxy(0,1);
 lcd_puts(bufer);
 delay_ms(500);
 b=0;
 key2 =PINC.3 & 0x01;
 if(key2 ==0x01)
 {
   delay_ms(50); 
   key2 =PINC.3 & 0x01;  
      if(key2 ==0x01)
      {
       b=1;
     } 
 }
 if (b==1)
 goto br;
 else
goto y;

}

//////////////////////////////////
int set_key(void)
{    
 key2 =PINC.1 & 0x01;
 if(key2 ==0x01)
 {
   delay_ms(50); 
   key2 =PINC.1 & 0x01;  
      if(key2 ==0x01)
      {
       i++;
      } 
 }
 key2 =PINC.2 & 0x01;
 if(key2 ==0x01)
 {
   delay_ms(50); 
   key2 =PINC.2 & 0x01;  
   if(key2 ==0x01)
     {
     i--;
     }
  }
  if(i<0)
  {
  i=0;
  }
  return i;
 }
////////////////////////////////////////////
 int set_angle(void)
 {
 key2 =PINC.1 & 0x01;
 if(key2 ==0x01)
 {
   delay_ms(50); 
   key2 =PINC.1 & 0x01;  
      if(key2 ==0x01)
      {
       f++;
      } 
 }
 key2 =PINC.2 & 0x01;
 if(key2 ==0x01)
 {
   delay_ms(50); 
   key2 =PINC.2 & 0x01;  
   if(key2 ==0x01)
     {
     f--;
     }
  }
  if(i<0)
  {
  i=0;
  }
  return f;
 }

////////////////////////////////////////////      
int detect_pulse(void){   
v=0;
TCCR1B=0x00;
#asm("cli");
goto s;
s:
key2 =PINC.4 & 0x00;
 if(key2 ==0x00){
   goto f;
   f:
   key2 =PINC.4 & 0x01;  
      if(key2==0x01){
       v=1;
       }
       else
       {
       goto f;
       }
  }
  if(v==1){
  atash(mode);
  }
  else
  {
  goto s;
  }
 return v;
}
////////////////////////////////////////////
void atash(int a){  
#asm("sei")
if(a==1){ 
TCNT1=time1;
TCCR1B=0x01;
h=0;  
g=1;  
while(1){ 
}
}
if(a==2){
TCNT1=time2;
TCCR1B=0x01;
h=0;
#asm("sei")
g=1;
while(1){

}
}
if(a==3){
TCNT1=time3;
TCCR1B=0x01;
h=0;
#asm("sei")
g=1;
while(1){
}
}  
if(a==4){
TCNT1=time4;
TCCR1B=0x01;
h=0;
#asm("sei")
g=1;
while(1){
}
}
}
 

Thread Starter

Far

Joined May 11, 2009
20
This is a simple firing circuit for thyristors industrial electronic circuits. it makes some pulses with adjustable firing angle. the selected mods are about different types of circuits and thyristors number.
 

nanovate

Joined May 7, 2007
666
It is a software problem.
Do you understand goto statements? Why do you need to use them?
I'd restructure your program so that it is easier to debug and doesn't jump all around.
Also double check that you are using the '&' operator correctly.

For example
PINC.2 & 0x01 = ??
 

Vaughanabe13

Joined May 4, 2009
102
That code is very poorly structured when it comes to proper coding practices like indenting, comment blocks, etc. Maybe the formatting got all messed up when you copied the code but I can barely read it the way it is.
 

nanovate

Joined May 7, 2007
666
The OP states he/she is using Proteus so for now the problem is software related. It is not doing what the OP intended... a quick glance found some potential logical errors. But as the code lacks readable formatting I do not have the time to plod through it in detail.
 

SgtWookie

Joined Jul 17, 2007
22,230
The OP states he/she is using Proteus so for now the problem is software related. It is not doing what the OP intended... a quick glance found some potential logical errors. But as the code lacks readable formatting I do not have the time to plod through it in detail.
Here's the code re-indented.
Our OP used the dreaded GOTO statement freely. :eek:
Rich (BB code):
/*****************************************************
Project : 
Version : 
Date    : 5/18/2009
Author  : l                               
Company : l                               
Comments: 


Chip type           : ATmega8
Program type        : Application
Clock frequency     : 1.000000 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 256
*****************************************************/

#include <mega8.h>
#include <delay.h>
#include <stdio.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x18 ;PORTB
#endasm
#include <lcd.h>
char bufer[10];
int key2,i,key,b,mode,count,g=0,h,f,ab,n,a,j,p,v,l,k;   
unsigned int time1,time180,time2,time60,time120,time3,time4,time,d; 
void atash(int a);        
int detect_pulse(void);
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
	// Place your code here   
	if(mode==1){
		if(h==0){
			PORTD=0x01; 
			TCNT1=time180;
			TCCR1B=0x01; 
			h=1;
			g=1;
		}   
		g++;
		if(g>2){
			if(g==3){
				PORTD=0x02;
				d++;
			}
			/*if(d==6){
d=0; 
TCCR1B=0x00;
TCNT1=0x00;
#asm("cli");
mode=1;
p=detect_pulse();
}*/
			if(g==4){
				PORTD=0x01;
				g=2;
			}
			TCNT1=time180;
			TCCR1B=0x01;
		}   
	}
	if(mode==2){
		if(h==0){
			PORTD=0x03; 
			TCNT1=time180;
			TCCR1B=0x01; 
			h=1;
			g=1;
		}   
		g++;
		if(g>2){
			if(g==3)
			PORTD=0x0c;
			if(g==4){
				PORTD=0x03;
				g=2;
			}
			TCNT1=time180;
			TCCR1B=0x01;
		}   
	}
	if(mode==3){
		if(h==0){
			PORTD=0x01; 
			TCNT1=time120;
			TCCR1B=0x01; 
			h=1;
			g=0;
		}   
		g++;
		if(g>2){
			if(g==3)
			PORTD=0x02;
			if(g==4)
			PORTD=0x04;
			if(g==5){
				PORTD=0x01;

				g=2;
			}
			TCNT1=time120;
			TCCR1B=0x01;
		}   
	}
	if(mode==4){
		g++;
		if(h==0){
			PORTD=0x01; 
			TCNT1=time60;
			TCCR1B=0x01; 
			h=1;
			g=2;
		}   
		if(g>2){ 
			TCNT1=time60;
			if(g==3)
			PORTD=0x02;
			if(g==4)
			PORTD=0x04;
			if(g==5)
			PORTD=0x08;
			if(g==6)
			PORTD=0x10;
			if(g==7)
			PORTD=0x20;
			if(g==8){
				PORTD=0x01;
				g=2;
			}
		}   
	}
}

// Declare your global variables here
int set_key(void);
int set_angle(void);

void main(void)
{
	// Declare your local variables here
	char bufer1[10];
	float t,t120,t60,div;
	unsigned int angle;
	PORTB=0x00;
	DDRB=0x00;
	PORTC=0x00;
	DDRC=0x00;
	PORTD=0x00;
	DDRD=0x3F;
	TCCR0=0x00;
	TCNT0=0x00;
	TCCR1B=0x00;
	TCCR1A=0x00;
	TCNT1H=0x00;
	TCNT1L=0x00;
	ICR1H=0x00;
	ICR1L=0x00;
	OCR1AH=0x00;
	OCR1AL=0x00;
	OCR1BH=0x00;
	OCR1BL=0x00;
	TCCR2=0x00;
	TCNT2=0x00;
	OCR2=0x00;
	GICR|=0x00;
	MCUCR=0x00;
	GIFR=0x00;
	TIMSK=0x04;
	// LCD module initialization
#asm("cli")
	lcd_init(16);
	lcd_clear();     
	lcd_putsf("ENTER THE MODE");
	goto loop1;
loop1:
	while(1){
		key=set_key();
		if(key>4){
			key=0;
			i=0;
		}  
		mode=key;
		lcd_clear();
		sprintf(bufer,"NUMBER OF MODE:%i",key);
		lcd_puts(bufer);
		delay_ms(500); 
		key2 =PINC.3 & 0x01;
		if(key2 ==0x01){
			delay_ms(50); 
			key2 =PINC.3 & 0x01;  
			if(key2 ==0x01)
			b=1;
		}
		if(key>0 & b==1){
			lcd_clear();
			lcd_putsf("MODE IS ACCEPT");
			delay_ms(500);
			lcd_clear();
			lcd_gotoxy(0,0);
			sprintf(bufer1,"MODE=%i",mode);
			lcd_puts(bufer1);
			delay_ms(1000);
			goto angel;
		}
	}
br:
	switch(key)
	{
	case 1:
		goto mode1;
	case 2:
		goto mode2;
	case 3:
		goto mode3;
	case 4:
		goto mode4;
	case 0:
		goto loop1; 
	}
	////////////////////////////// 
mode1: 
	mode=1; 
	lcd_clear();
	lcd_putsf("mode=1");
	div=180/angle;
	t=0.01/div;
	t=t/0.000001;
	time1=65535-t;
	time180=65535-10000; 
	p=detect_pulse();
	//////////////////////////////
mode2:
	mode=2; 
	lcd_clear();
	lcd_putsf("mode=2");
	div=angle/180;
	t=0.01/div;
	t=t/0.000001;
	time2=65535-t;
	time180=65535-10000; 
	p=detect_pulse();
	//////////////////////////////
mode3:
	mode=3;
	div=180/angle;
	t=0.01/div;
	t=t/0.000001;
	time3=65535-t;
	t120=2*0.01/3;
	t120=65535-t120;
	t120=t120/0.000001;
	time120=65535-t120;
	TCCR1B=0x00;
	p=detect_pulse();
	//////////////////////////////
mode4:
	mode=4;
	t60=0.01/3;
	t60=t60/0.000001;
	t60=65535-t60;
	time60=t60;
	div=180/angle;
	t=0.01/div;
	t=t/0.000001;
	time4=65535-t;
	p=detect_pulse();

	//////////////////////////////  
angel: 
	f=0;
	angle=0;
	goto y;
y:
	ab=set_angle();
	angle=ab*12;
	if (angle>170 | angle<0)
	{
		angle=168;
		f=14;
	}  
	sprintf(bufer,"ANGLE:%i DEG",angle);
	lcd_gotoxy(0,1);
	lcd_puts(bufer);
	delay_ms(500);
	b=0;
	key2 =PINC.3 & 0x01;
	if(key2 ==0x01)
	{
		delay_ms(50); 
		key2 =PINC.3 & 0x01;  
		if(key2 ==0x01)
		{
			b=1;
		} 
	}
	if (b==1)
	goto br;
	else
	goto y;

}

//////////////////////////////////
int set_key(void)
{    
	key2 =PINC.1 & 0x01;
	if(key2 ==0x01)
	{
		delay_ms(50); 
		key2 =PINC.1 & 0x01;  
		if(key2 ==0x01)
		{
			i++;
		} 
	}
	key2 =PINC.2 & 0x01;
	if(key2 ==0x01)
	{
		delay_ms(50); 
		key2 =PINC.2 & 0x01;  
		if(key2 ==0x01)
		{
			i--;
		}
	}
	if(i<0)
	{
		i=0;
	}
	return i;
}
////////////////////////////////////////////
int set_angle(void)
{
	key2 =PINC.1 & 0x01;
	if(key2 ==0x01)
	{
		delay_ms(50); 
		key2 =PINC.1 & 0x01;  
		if(key2 ==0x01)
		{
			f++;
		} 
	}
	key2 =PINC.2 & 0x01;
	if(key2 ==0x01)
	{
		delay_ms(50); 
		key2 =PINC.2 & 0x01;  
		if(key2 ==0x01)
		{
			f--;
		}
	}
	if(i<0)
	{
		i=0;
	}
	return f;
}

////////////////////////////////////////////      
int detect_pulse(void){   
	v=0;
	TCCR1B=0x00;
#asm("cli");
	goto s;
s:
	key2 =PINC.4 & 0x00;
	if(key2 ==0x00){
		goto f;
f:
		key2 =PINC.4 & 0x01;  
		if(key2==0x01){
			v=1;
		}
		else
		{
			goto f;
		}
	}
	if(v==1){
		atash(mode);
	}
	else
	{
		goto s;
	}
	return v;
}
////////////////////////////////////////////
void atash(int a){  
#asm("sei")
	if(a==1){ 
		TCNT1=time1;
		TCCR1B=0x01;
		h=0;  
		g=1;  
		while(1){ 
		}
	}
	if(a==2){
		TCNT1=time2;
		TCCR1B=0x01;
		h=0;
#asm("sei")
		g=1;
		while(1){

		}
	}
	if(a==3){
		TCNT1=time3;
		TCCR1B=0x01;
		h=0;
#asm("sei")
		g=1;
		while(1){
		}
	}  
	if(a==4){
		TCNT1=time4;
		TCCR1B=0x01;
		h=0;
#asm("sei")
		g=1;
		while(1){
		}
	}
}
 
Top