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.

need help on avr programing

Status
Not open for further replies.

vvarlord

Member level 4
Joined
Mar 15, 2012
Messages
74
Helped
4
Reputation
8
Reaction score
3
Trophy points
1,288
Activity points
1,756
i wrote a code for an atmega8, for using as a clock which shows the time on a lcd ,and it can be set by an external interupt, and makes an output 1 at certain times,
the problem is that in intrupt mode, when i want to enter the clock, the column for digits of 1 , 4 , 7 &the zero digit , don't work
everything else is ok
i tried to change the ports , for inputs and outputs, but doesn't change a thing

Code:
/////
/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.3 Standard
Automatic Program Generator
© Copyright 1998-2011 Pavel Haiduc, HP InfoTech s.r.l.
[url]http://www.hpinfotech.com[/url]

Project : 
Version : 
Date    : 10/31/2012
Author  : PerTic@n
Company : If You Like This Software,Buy It
Comments: 


Chip type               : ATmega8L
Program type            : Application
AVR Core Clock frequency: 8.000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 256
*****************************************************/

#include <mega8.h>
#include <stdio.h>
#include <delay.h>

// Alphanumeric LCD functions
#include <alcd.h>
 int M=0,H=0,x=0;
int a,b=0,c=0,d=0,e=0,y=0;
char S[40],T[40];
// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
lcd_clear();
while(y<4)
{


while(1)
{
PORTD.0=0;
PORTC.3=1;


    if(PIND.1==1)
    {a=1;
    PORTC=0;PORTD=0;break;}
    if(PINC.1==1)
    {a=2;
    PORTC=0;PORTD=0;break;}
    if(PINC.2==1)
    {a=3;          
    PORTC=0;PORTD=0;break;}

PORTC.3=0;
PORTC.4=1 ;


    if(PIND.1==1)
    {a=4;
    PORTC=0;PORTD=0;break;}
    if(PINC.1==1)
    {a=5;
    PORTC=0;PORTD=0;break;}
    if(PINC.2==1)
    {a=6;
    PORTC=0;PORTD=0;break;}


PORTC.4=0;
PORTC.5=1;


    if(PIND.1==1)
    {a=7;
    PORTC=0;PORTD=0;break;}
    if(PINC.1==1)
    {a=8;
    PORTC=0;PORTD=0;break;}
    if(PINC.2==1)
    {a=9;
    PORTC=0;PORTD=0;break;}


PORTC.5=0;
PORTD.0=1;


    if(PINC.1==1)
    {a=0;
    PORTC=0;PORTD=0;break;}
}
delay_ms(50);
if(y==0&&a<3)
{
b=a;
sprintf(T,"%d",b);
lcd_puts(T);
y=1;                                                                                                                                              
}
else if(y==1&&b<2)
{
c=a;
sprintf(T,"%d",c);
lcd_puts(T);
lcd_puts(":");
y=2;
}
else if(y==1&&b==2&&a<4)
{
c=a;
sprintf(T,"%d",c);
lcd_puts(T);
lcd_puts(":");
y=2;
}
else if(y==2&&a<6)
{
d=a;
sprintf(T,"%d",d);
lcd_puts(T);
y=3;
}
else if(y==3)
{
e=a;
sprintf(T,"%d",e);
lcd_puts(T);
y=4;
}


}
y=0;
M=d*10+e;
H=b*10+c;// Place your code here

}

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port B initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out 
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 
PORTB=0x00;
DDRB=0xFF;

// Port C initialization
// Func6=In Func5=Out Func4=Out Func3=Out Func2=In Func1=In Func0=In 
// State6=T State5=0 State4=0 State3=0 State2=T State1=T State0=T 
PORTC=0x00;
DDRC=0x38;

// Port D initialization
// Func7=In Func6=Out Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=0 State5=T State4=T State3=T State2=T State1=T State0=T 
PORTD=0x00;
DDRD=0x01;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Rising Edge
// INT1: Off
GICR|=0x40;
MCUCR=0x03;
GIFR=0x40;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// USART initialization
// USART disabled
UCSRB=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC disabled
ADCSRA=0x00;

// SPI initialization
// SPI disabled
SPCR=0x00;

// TWI initialization
// TWI disabled
TWCR=0x00;

// Alphanumeric LCD initialization
// Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTB Bit 0
// RD - PORTB Bit 1
// EN - PORTB Bit 2
// D4 - PORTB Bit 4
// D5 - PORTB Bit 5
// D6 - PORTB Bit 6
// D7 - PORTB Bit 7
// Characters/line: 8
lcd_init(8);

// Global enable interrupts
#asm("sei")


while(1)

{
while(H<24)
      {
      while(M<60)
      {
       lcd_clear();
       sprintf (S,"%d : %d",H,M);
       lcd_puts(S);
       if((H==7&&M==30)||(H==9&&M==0)||(H==9&&M==10)||(H==10&&M==40)||(H==10&&M==50)||(H==12&&M==20)||(H==12&&M==30)||(H==13&&M==15))
       {
       x=1;
      PORTB.3=1;
      delay_ms(2482);
      PORTB.3=0;
       }
     
       if(x==0)
       {delay_ms(7370);
       }
       if(x==1)
       {delay_ms(4870);
       }
       x=0;
       M=M+1;
         
      } 
       H=H+1;
       M=0;// Place your code here
      }
      H=0;
      }
      
      
}
 
Last edited by a moderator:

Without code tags it is hard to follow the code, but in any case there is an infinite loop ( while(1) ) inside the external interrupt routine, this doesn't seem to be right, the code will never escape from the ISR. You should update flags from the interrupt and execute the process from main().

The code will be trapped inside interrupt forever since global interrupt flag is not set inside interrupt. If you set this flag you will have a (sort of) recursive function, where stack overflow will be unavoidable. If this code is just for fun then it's ok, if this is a real application you need to reconsider code structure.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top