I am using atmega328P , as it shown in the attached picture when the interruption is executed , the program doesn't turn back to the main to execute the rest of the program ? i made 2 functions ; one to blink led in portC and the other in PORT D the Led in PORT D (interruption) is working fine but the Led for PORT C in the main is not executed is there a problem ??
#ifndef F_CPU
#define F_CPU 16000000UL
#endif
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#define ACK 0x01
void portc_led(void)
{
PORTC|=(1<<5);
_delay_ms(100);
PORTC=(0<<5) ;
_delay_ms(100);
PORTC|=(1<<5);
_delay_ms(100);
PORTC=(0<<5) ;
_delay_ms(100);
}
void portd_led(void)
{
PORTD|=(1<<7);
_delay_ms(1000);
PORTD=(0<<7) ;
_delay_ms(100);
PORTD|=(1<<7);
_delay_ms(1000);
PORTD=(0<<7) ;
_delay_ms(100);
PORTD|=(1<<7);
_delay_ms(1000);
PORTD=(0<<7) ;
_delay_ms(100);
PORTD|=(1<<7);
_delay_ms(1000);
PORTD=(0<<7) ;
_delay_ms(100);
}
int main(void)
{
DDRB |= (1<<2)|(1<<3)|(1<<5); // SCK, MOSI and SS as outputs
DDRB &= ~(1<<4); // MISO as input
SPCR |= (1<<MSTR); // Set as Master
SPCR |= (1<<SPR0)|(1<<SPR1); // divided clock by 128
SPCR |= (1<<SPIE); // Enable SPI Interrupt
SPCR |= (1<<SPE); // Enable SPI
DDRC= 0xFF ; // set PORT C as output
DDRD = 0xFF ;
sei();
spi_send_data(ACK);
portc_led();
}
ISR(SPI_STC_vect)
{
portd_led();
}
first of all your code will have a compile Error! because you don't provide a reference to
spi_send_datafunctionbut let us imagine that you include it above this piece of code
analyse your code
you say that
the program execution path definitely will ruturn to main routine... where will it go ?! :) the code will execut the
portc_ledfunction one time (and only one time) maybe before interrupt or maybe after interrupt or maybe interrupt happen in between the function ... so maybeportc_ledalredy excuted happen befor interupt but you did not see it becuse it executed in only400 ms!! and after finsh interupting nothing to do just wait for other interupt ! ..simple solution : try to change the
100msdelay inportc_ledto bigger delay and you will see ...little advices for improve practicing techniques
PORTB=(0<<5)equivalent toportB=0b00000000when you try to clear single bit in register you clear all register's bits ! which is not good ...use this codePORTB&=~(1<<5)for clear single bit which make bitwaise & between portc and 0b11101111 only change single bit and keep other bits as it ismodify the code
not that the following code will not excute blancking in parallel (simultaneously) it will excute them in series (not simultaneously).. if you like to have a parallel blanking use timer interupt in
portc_ledinsted of delay /or use an RTOS (a little bit advanced topic)