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.

error in spi intrrupt

Status
Not open for further replies.

maia31

Member level 1
Joined
Jul 10, 2011
Messages
38
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Location
earth
Activity points
1,519
hi
i want recive data from pc
i use spi
and my micro is slave
i write this program but it dosent run.
can you say me why
?
spi intruppt dosent work
why?
Code:
**********************************************
This program was produced by the
CodeWizardAVR V1.25.9 Standard
Automatic Program Generator
© Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
[url=http://www.hpinfotech.com]HP InfoTech, Development Tools for Microcontrollers, C Compilers, In-System Programmers[/url]

Project : 
Version : 
Date    : 2011/10/03
Author  : F4CG                            
Company : F4CG                            
Comments: 


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

#include <mega32.h>  
#include <spi.h>
#include <delay.h>
#include <stdio.h>  







    


interrupt [SPI_STC] void spi_isr(void)
{ 
#asm("cli"); // Global disable interrupts


if(SPDR ==5)
 PORTD= PORTD|8;  
 else
 PORTD= PORTD&247;

#asm("sei"); // Global enable interrupts
}
void main(void)





{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTA=0x00;
DDRA=0x00;

// Port B 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 
PORTB=0x00;
DDRB=0x40;

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

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

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

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

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

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

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

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

// SPI initialization
// SPI Type: Slave
// SPI Clock Rate: 15.625 kHz
// SPI Clock Phase: Cycle Half
// SPI Clock Polarity: Low
// SPI Data Order: MSB First
SPCR=0xC2;
SPSR=0x00;

// Clear the SPI interrupt flag
#asm
    in   r30,spsr
    in   r30,spdr
#endasm

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




      while (1)
      {
     
      };
}
 
Last edited by a moderator:

How do you send SPI from PC?

Alex
well not generally a pc
in fact a device that contain micro - fpga - and a lot other thing
i use that device to send data with help of spi
and my micro as slave receive that data
i hope i explain good
 

The two lines to disable and then enable again the interrupts inside the SPI interrupt are not needed because this is the default behavior.

Are you using the SPI pins properly?
MISO =Master in Slave out anw will be output in your case
MOSI =Master out Slave in and will be input in your case

Are you pulling the SS pin low?
When configured as a Slave, the SPI interface will remain sleeping with MISO tri-stated as long
as the SS pin is driven high. In this state, software may update the contents of the SPI Data
Register, SPDR, but the data will not be shifted out by incoming clock pulses on the SCK pin
until the SS pin is driven low. As one byte has been completely shifted, the end of Transmission
Flag, SPIF is set. If the SPI Interrupt Enable bit, SPIE, in the SPCR Register is set, an interrupt
is requested. The Slave may continue to place new data to be sent into SPDR before reading
the incoming data. The last incoming byte will be kept in the Buffer Register for later use.

Also check the SPI modes, maybe you have a wrong setting.

Alex
 
  • Like
Reactions: maia31

    maia31

    Points: 2
    Helpful Answer Positive Rating
The two lines to disable and then enable again the interrupts inside the SPI interrupt are not needed because this is the default behavior.

Are you using the SPI pins properly?
MISO =Master in Slave out anw will be output in your case
MOSI =Master out Slave in and will be input in your case

Are you pulling the SS pin low?


Also check the SPI modes, maybe you have a wrong setting.

Alex
well i use codewizard from code vision to se the spi setting
and i check ss pin many times with watt meter and it is low
and i use spi mode 0
 

Can you explain what you mean when you say "i write this program but it doesn't run."

You never get in interrupt?

Alex
 
  • Like
Reactions: maia31

    maia31

    Points: 2
    Helpful Answer Positive Rating
Can you explain what you mean when you say "i write this program but it doesn't run."

You never get in interrupt?

Alex
it seems that i never go to interrupt
because my led dosent on
i check led and it's connection
they are correct
and i check ss pin
and check the master side
and all of them are correct
then i assume that the problem was for my spi setting or port d setting or interrupt setting
but i cant find error
did i miss any setting?
why my interrupt dosent work?
 

Try with

Code:
PORTD ^= 8;

inside the SPI interrupt (only this line) , this will toggle the led whenever there is an interrupt so that you can check if you get interrupts but if you send the data at a fast rate you will not be able to see it blinking, in that case use a counter that toggles the led every x interrupts.

For example

Code C - [expand]
1
2
3
4
5
6
static unsigned int i=0;
 
if (++i==1000) { // maybe use a different number for slower/faster toggle
    PORTD ^= 8;  // toggle the pin
    i=0;  // and reset the counter
}



Alex
 
  • Like
Reactions: maia31

    maia31

    Points: 2
    Helpful Answer Positive Rating
Try with

Code:
PORTD ^= 8;

inside the SPI interrupt (only this line) , this will toggle the led whenever there is an interrupt so that you can check if you get interrupts but if you send the data at a fast rate you will not be able to see it blinking, in that case use a counter that toggles the led every x interrupts.



For example

Code C - [expand]
1
2
3
4
5
6
static unsigned int i=0;
 
if (++i==1000) { // maybe use a different number for slower/faster toggle
    PORTD ^= 8;  // toggle the pin
    i=0;  // and reset the counter
}



Alex
i try it and work
so i think it can posh and pop from outside of interrupt
i mean if one of my variable is outside of subroutine
it dosent save
so i must write the pop and push code in subroutine interrupt
am i wrong?
 

If the interrupt is executed normally then it can be that you are getting (or sending from master) the wrong value.

Your code
Code:
if([B]SPDR ==5[/B])
 PORTD= PORTD|8;  
 else
 PORTD= PORTD&247;

expects value 5, so you are probably getting a different value.
One option to see what value you get is to set a port a output (PORTx=0xff) ans then assign the received SPI value to that port (PORTx=SPDR) but you must send only one byte from the master and then check if the slave received value was the same.

Alex
 
  • Like
Reactions: maia31

    maia31

    Points: 2
    Helpful Answer Positive Rating
If the interrupt is executed normally then it can be that you are getting (or sending from master) the wrong value.

Your code
Code:
if([B]SPDR ==5[/B])
 PORTD= PORTD|8;  
 else
 PORTD= PORTD&247;

expects value 5, so you are probably getting a different value.
One option to see what value you get is to set a port a output (PORTx=0xff) ans then assign the received SPI value to that port (PORTx=SPDR) but you must send only one byte from the master and then check if the slave received value was the same.

Alex
my master micro send 1 byte each time
i try you guide
and tell you what happened
thank s a lot for your help
thank you very very much
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top