📄 智能防溜.c
字号:
//********************
//FILE: 智能防溜.c
//********************
#include <iom128v.h>
#include <macros.h>
#include<eeprom.h>
#define SIZE 100
unsigned char SPI_rx_buff[SIZE];
unsigned char SPI_tx_buff[SIZE];
unsigned char rx_wr_index,rx_rd_index,rx_counter,rx_buffer_overflow;
unsigned char tx_wr_index,tx_rd_index,tx_counter;
unsigned char aix;
unsigned char x_aix;
unsigned char y_aix;
unsigned char z_aix;
unsigned char address;
//秙ca3000加速度值//
unsigned char sca3000_aix(unsigned char address)
{
aix=0;
//address=address<<2;
PORTB=PORTB&(~0x04);
SPDR=address;
while(!(SPCR&(1<<SPIF)))
;
SPDR=0x00;
while(!(SPCR&(1<<SPIF)))
;
aix=SPDR;
// aix=x_aix<<8;
//SPDR=0x00;
//while(!(SPCR&(1<<SPIF)))
//;
//aix|=SPDR;
PORTB=PORTB|0x04;
return aix;
}
#pragma interrupt_handler spi_stc_isr:18
void spi_stc_isr(void)
{
SPI_rx_buff[rx_wr_index]=SPDR;
if(++rx_wr_index==SIZE);
rx_wr_index=0;
if(++rx_counter==SIZE)
{
rx_counter=0;
rx_buffer_overflow=1;
}
if(rx_counter)
{
--tx_counter;
SPDR=SPI_tx_buff[tx_rd_index];
if(++tx_rd_index==SIZE)
tx_rd_index=0;
}
}
unsigned char getSPIchar(void)
{
unsigned char data;
while(rx_counter==0);
data=SPI_rx_buff[rx_rd_index];
if(++rx_rd_index==SIZE)
rx_rd_index=0;
CLI();
--rx_counter;
SEI();
return data;
}
void putSPIchar(char c)
{
while(tx_counter==SIZE);
CLI();
if(tx_counter||((SPSR&0x80)==0))
{
SPI_tx_buff[tx_wr_index]=c;
if(++tx_wr_index==SIZE)
tx_wr_index=0;
++tx_counter;
}
else
SPDR=c;
SEI();
}
void spi_init(void)
{
unsigned char temp;
DDRB|=0x80;
PORTB|=0x80;
SPCR=0xD5;
SPSR=0x00;
temp=SPSR;
temp=SPDR;
}
void main(void)
{
unsigned char i;
CLI();
spi_init();
SEI();
address=0x0F; //读x轴加速度值,0x05为x轴加速度高位地址//
sca3000_aix(address);
x_aix=aix;
while(0==0)
{
putSPIchar(i);
i++;
getSPIchar();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -