?? sspi_usart.c
字號:
#include <iom16v.h>
#include <macros.h>
#define FOSC 8000000
#define BAUD0 9600
#define uchar unsigned char
#define uint unsigned int
#define TxRxBuf_Len 32
uchar TxRxBuffer[TxRxBuf_Len];
void delay(uint z)
{
uint i,j;
for(i=0;i<z;i++)
for(j=0;j<1000;j++);
}
/**************************/
void port_init(void)
{PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00;
DDRC = 0xFF;
DDRD=0XFF;
PORTD=0X00;
}
/***********************/
void init_usart(void)
{ UCSRC|=0X86;
UBRRH = (FOSC/BAUD0/16-1)/256;
UBRRL = (FOSC/BAUD0/16-1)%256;
UCSRB|=(1<<RXEN)|(1<<TXEN);
}
void send_data(uchar data)
{
while(!(UCSRA&(1<<UDRE)));
UDR=data;
}
/********************/
void spi_init(void)
{DDRB|=BIT(PB6);
SPCR = 0x40;
SPSR = 0x00;
}
/***********************/
//功能:使用SPI發送一個字節
void spi_write(uchar sData)
{
SPDR = sData;
while(!(SPSR & BIT(SPIF)));
}
/******************/
//功能:使用SPI接收一個字節
uchar spi_read(void)
{
//SPDR = 0x00;
while(!(SPSR & BIT(SPIF)));
return SPDR;
}
/************************/
void init_devices(void)
{
CLI(); //禁止所有中斷
MCUCR = 0x00;
MCUCSR = 0x80;//禁止JTAG
GICR = 0x00;
port_init();
init_usart();
spi_init();
SEI();//開全局中斷
}
//主函數
void main(void)
{uchar i=0,j=0;
init_devices();
for(i=0;i<32;i++)
{PORTC|=BIT(PC0)|BIT(PC1);
TxRxBuffer[i]=spi_read();
delay(10);
}
delay(10);
for(j=0;j<32;j++)
{
send_data((TxRxBuffer[j]+0x30));
send_data(0x0d);
send_data(0x0a);
delay(1);
}
PORTC|=BIT(PC0);
PORTC&=~BIT(PC1);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -