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.

[SOLVED] SPI init on ATMEGA128 ?

Status
Not open for further replies.

bianchi77

Advanced Member level 4
Advanced Member level 4
Joined
Jun 11, 2009
Messages
1,313
Helped
21
Reputation
44
Reaction score
20
Trophy points
1,318
Location
California
Visit site
Activity points
9,442
Guys

Do I make a correct SPI init on ATMEGA128 ?
My clock is 16Mhz,
what's the clock for SPI (SCK) ?

Code:
Code:
void spi_init(void)
{
SPCR = 0x52; //setup SPI: Master mode, MSB first, SCK phase low, SCK idle low
SPSR = 0x00;
}
Thank you
 

Hello guys,
I am trying to initialize SPI interface on Atmega128. My code is here. But when I see SCK pin with Oscilloscope I don't have any pulse..Only there is a line with 1 volt amplitude ....Why? I think I should see a pulse with 1Mhz on SCK.....Please help me...Is there any problem on my code (I also activate usart1 on atmega128 which works correctly)?

Code:
#include <mega128a.h>
#include <delay.h>
#define ChipSelectAd PORTB.0

#ifndef RXB8
#define RXB8 1
#endif

#ifndef TXB8
#define TXB8 0
#endif

#ifndef UPE
#define UPE 2
#endif

#ifndef DOR
#define DOR 3
#endif

#ifndef FE
#define FE 4
#endif

#ifndef UDRE
#define UDRE 5
#endif

#ifndef RXC
#define RXC 7
#endif

#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<DOR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)

// Get a character from the USART1 Receiver
#pragma used+
char getchar1(void)
{
char status,data;
while (1)
      {
      while (((status=UCSR1A) & RX_COMPLETE)==0);
      data=UDR1;
      if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
         return data;
      }
}
#pragma used-

// Write a character to the USART1 Transmitter
#pragma used+
void putchar1(char c)
{
while ((UCSR1A & DATA_REGISTER_EMPTY)==0);
UDR1=c;
}
#pragma used-


// SPI functions
#include <spi.h>

// Declare your global variables here

void SPI_MasterInit(void)
{
	// Set MOSI and SCK and SS output, all others input
	              PORTB=0x00;
                   DDRB=0x07; 
                  // SPI initialization
                 // SPI Type: Master
                 // SPI Clock Rate: 1000.000 kHz
                 // SPI Clock Phase: Cycle Start
                 // SPI Clock Polarity: Low
                 // SPI Data Order: MSB First
                 SPCR=0x50;
                 SPSR=0x00;
 }



void SPI_MasterTransmit(char cData)
{
	// Start transmission
	SPDR = cData;

	//PORTC=SPDR;// JUST CHECKING!

	// Wait for transmission complete
	while(!(SPSR & (1<<SPIF)));
}



void main(void)
{

unsigned char a;


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

// USART1 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART1 Receiver: On
// USART1 Transmitter: On
// USART1 Mode: Asynchronous
// USART1 Baud Rate: 9600
UCSR1A=0x00;
UCSR1B=0x18;
UCSR1C=0x06;
UBRR1H=0x00;
UBRR1L=0x19;




while (1)
      { 
      
         ChipSelectAd=0;
       delay_ms(1000);
	     SPI_MasterInit();
		 SPI_MasterTransmit(0xff);
		  ChipSelectAd=1;
		delay_ms(1000);
     

          ChipSelectAd=0;
         delay_ms(1000);
		SPI_MasterInit();
		SPI_MasterTransmit(0x00);
		   ChipSelectAd=1;
	 	delay_ms(1000);
      
      a=getchar1(); 
            
        if (a=='u')
         {
          PORTD.4=1;   
          putchar1('A');  
         }

      
      }
}
 

My code is answered when I deleted Usart1 configuration...Does anybody know why?
 

Hi,

Why I couldn't have SPI inteface and Usart1 (by FT232r) together?

We used many times both interfaces at the same time. No problem.

--> It must be something with your hardware or your code.

Klaus
 
Thanks for your reply,
My code is attached. Would you please to take a look on it?
Thanks so much...
Code:
#include <mega128a.h>
#include <delay.h>

// SPI functions
#include <spi.h>


#define CLK1 PORTE.3
#define CLK2 PORTE.4
#define CLK PORTE.7


#ifndef RXB8
#define RXB8 1
#endif

#ifndef TXB8
#define TXB8 0
#endif

#ifndef UPE
#define UPE 2
#endif

#ifndef DOR
#define DOR 3
#endif

#ifndef FE
#define FE 4
#endif

#ifndef UDRE
#define UDRE 5
#endif

#ifndef RXC
#define RXC 7
#endif

#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<DOR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)

// Get a character from the USART1 Receiver
#pragma used+
char getchar1(void)
{
char status,data;
while (1)
      {
      while (((status=UCSR1A) & RX_COMPLETE)==0);
      data=UDR1;
      if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
         return data;
      }
}
#pragma used-

// Write a character to the USART1 Transmitter
#pragma used+
void putchar1(char c)
{
while ((UCSR1A & DATA_REGISTER_EMPTY)==0);
UDR1=c;
}
#pragma used-




// Declare your global variables here

void main(void)
{

unsigned char a;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=Out Func1=Out Func0=Out 
// State7=T State6=T State5=T State4=T State3=T State2=0 State1=0 State0=0 
PORTB=0x00;
DDRB=0x07;          



// Port D 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 
PORTD=0x00;
DDRD=0x30;



PORTE=0x00;
DDRE=0xF8;           



// USART1 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART1 Receiver: On
// USART1 Transmitter: On
// USART1 Mode: Asynchronous
// USART1 Baud Rate: 9600
UCSR1A=0x00;
UCSR1B=0x18;
UCSR1C=0x06;
UBRR1H=0x00;
UBRR1L=0x19;


// SPI initialization
// SPI Type: Master
// SPI Clock Rate: 31.250 kHz
// SPI Clock Phase: Cycle Start
// SPI Clock Polarity: Low
// SPI Data Order: MSB First
SPCR=0x51;
SPSR=0x00;

while (1)
      {
      PORTB.0=0 ;             
      spi(0x00); 
      delay_ms(1);
      spi(0x1E);
      PORTB.0=1 ;
      
       PORTB.0=0 ;               //Mux bit
     // spi(0xBB);     
      spi(0x10); 
      delay_ms(1);
      spi(0x08);
      PORTB.0=1 ;
      
       PORTB.0=0 ;             
      spi(0x21); 
      delay_ms(1);
      spi(0x07);
      PORTB.0=1 ; 
     
      PORTB.0=0 ;            
      spi(0x00); 
      delay_ms(1);
      spi(0x1F);
      PORTB.0=1 ;       
      
        CLK2=0;
        CLK=1;
        delay_us(5 );
        CLK=0;
        delay_us(2 );
        CLK2=1;
        delay_us(3 );
        CLK=1;       
        delay_us(1 );       
        CLK2=0;
        delay_us(2 );
        CLK=0;  
        
        
         a=getchar1(); 
            
         if (a=='u')
         {
          PORTD.4=1;   
          putchar1('A');  
         }
        
         
      }
}

- - - Updated - - -
 

I use uart and SPI at once...
code for uart :

Code:
void usart_init( unsigned int ubrr )
{
	/* Set baud rate */

	UBRR0H = (unsigned char)(ubrr>>8);
	UBRR0L = (unsigned char)ubrr;
	/* Enable receiver and transmitter */
	UCSR0B = (1<<RXEN)|(1<<TXEN);
	/* Set frame format: 8data, 2stop bit */
	UCSR0C = (1<<USBS)|(3<<UCSZ0);
}
For SPI :

//8MHz SPI start
        SPCR = (1<<SPE)|(1<<MSTR);
        SPSR |= (1<<SPI2X); //8MHz
 

Thanks,
My code is similar to yours, But I don't know why it doesn't answer....If I should do any special job in this case?
 

I reduced SPI speed and tested it...then I increased the SPI speed....But the SCK pin is 1 always....It doesn't show any pulse.....

- - - Updated - - -

I found a point. It has a problem with getchar1 command. When I remove "a=getchar1();" from my code I have pulses on SCK pin.
Anybody knows why?
 

Hi,

The only thing I can imagine is, that this function acts like a busy wait. Maybe it waits for a character to receive at the UART.
Then no other function can be processed.

Klaus
 

I found getchar1() acts as polling it means it waits for data and it doesn't generate sck pulses...What Can I do to solve this problem?
I really need ti use getchar1() command for receive data in micro by USB?
 
Last edited:

do polling for few seconds, if there's no character accepted...exit polling ( loop ) and do SPI after SPI finish, back to polling again ?
 

Hi,

with async data receive, I´d go for a interrupt solution.

Easy to implement, high reliability, low processing power, easy to access data with software fifo.

I know, the first time it seems to be difficult. But there should be plenty of code examples..

Klaus
 

Would you please to take an example.... By applying rx interrupt, I use below code.....Please help me about details....
Code:
// USART1 Receiver interrupt service routine
interrupt [USART1_RXC] void usart1_rx_isr(void)
{
char status,data;
status=UCSR1A;
data=UDR1;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
   {
   rx_buffer1[rx_wr_index1++]=data;
#if RX_BUFFER_SIZE1 == 256
   // special case for receiver buffer size=256
   if (++rx_counter1 == 0)
      {
#else
   if (rx_wr_index1 == RX_BUFFER_SIZE1) rx_wr_index1=0;
   if (++rx_counter1 == RX_BUFFER_SIZE1)
      {
      rx_counter1=0;
#endif
      rx_buffer_overflow1=1;
      }
   }
}
 

Hi,

in main loop i´d just check if "rx_wr_index1 > 0)
Then you know if you received data. Now you should do an atomic [copy of buffer to a variable in main loop; and clearing the index] to further process the data.

The squared brackets mean that all within the brackes should be atomic.

Klaus
 
Nobody can help me?
I want to receive data by Interrupt SPI from computer...
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top