bianchi77
Advanced Member level 4
- Joined
- Jun 11, 2009
- Messages
- 1,313
- Helped
- 21
- Reputation
- 44
- Reaction score
- 20
- Trophy points
- 1,318
- Location
- California
- Activity points
- 9,442
void spi_init(void)
{
SPCR = 0x52; //setup SPI: Master mode, MSB first, SCK phase low, SCK idle low
SPSR = 0x00;
}
#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');
}
}
}
Why I couldn't have SPI inteface and Usart1 (by FT232r) together?
#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');
}
}
}
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
// 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;
}
}
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?