📄 mcp2510.c
字号:
//ICC-AVR application builder : 2007-10-17 16:47:59
// Target : M16
// Crystal: 16.000Mhz
#include <iom16v.h>
#include <macros.h>
#define READ 0x03 // 读MCP2510指令代码
#define WRITE 0x02 // 写MCP2510指令代码
#define RESET 0xC0 // 复位MCP2510指令代码
#define RTS 0x80 // MCP2510请求发送指令代码
#define STA2510 0xA0 // 读MCP2510状态指令代码
#define BITMOD 0x05 // MCP2510位修改指令代码
int timecount;
int a[]; // SPI发送或接收数据寄存器
int b[]; // 发送或接收的数据
int c[]; // 发送或接收的数据
int i; // 临时变量
int count; // 发送接收计数器
int count1=0; // for test
int RecID_H=0;
int RecID_L=0;
int DLC;
void port_init(void)
{
PORTA = 0xFF;
DDRA = 0x00;
PORTB = 0xFF;
DDRB = 0x00;
PORTC = 0xFF; //m103 output only
DDRC = 0x00;
PORTD = 0xFF;
DDRD = 0x00;
}
//TIMER0 initialize - prescale:1
// WGM: Normal
// desired value: 100KHz
// actual value: 100.000KHz (0.0%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0x60; //set count
OCR0 = 0xA0; //set compare
TCCR0 = 0x01; //start timer
}
#pragma interrupt_handler timer0_comp_isr:20
void timer0_comp_isr(void)
{/*int i=0;
timecount=0;
timecount++;
if (timecount>=1000) timecount=0;
SPDR=READ;
SPDR=0x66;
b[i]=SPDR;
b[i++]=SPDR;
while(!(SPSR & (1<<SPIF)))*/
;
//compare occured TCNT0=OCR0
}
//TIMER1 initialize - prescale:1
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 100KHz
// actual value: 100.000KHz (0.0%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xFF; //setup
TCNT1L = 0x60;
OCR1AH = 0x00;
OCR1AL = 0xA0;
OCR1BH = 0x00;
OCR1BL = 0xA0;
//OCR1CH = $OCR1CH$;
//OCR1CL = $OCR1CL$;
ICR1H = 0x00;
ICR1L = 0xA0;
TCCR1A = 0x50;
TCCR1B = 0x01; //start Timer
}
//TIMER2 initialize - prescale:1
// WGM: Normal
// desired value: 100KHz
// actual value: 100.000KHz (0.0%)
void timer2_init(void)
{
TCCR2 = 0x00; //stop
ASSR = 0x00; //set async mode
TCNT2 = 0x60; //setup
OCR2 = 0xA0;
TCCR2 = 0x11; //start
}
//SPI initialize
// clock rate: 4000000hz
void spi_init(void)
{
SPCR = 0x50; //setup SPI
SPSR = 0x00; //setup SPI
}
//ADC initialize
// Conversion time: 3uS
void adc_init(void)
{
ADCSR = 0x00; //disable adc
ADMUX = 0x00; //select adc input 0
ACSR = 0x80;
ADCSR = 0xE2;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer0_init();
timer1_init();
timer2_init();
spi_init();
adc_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x02; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void SPIINT()// SPI中断服务子程序
{
SPSR=0x00;
a[i++]=SPDR; // 数据暂存a[]中
count-=1;
if(count>0) SPDR=a[i];// 未发送完,继续
else PORTB|=BIT(PB4);//否则,片选信号置高电平
return;
}
void SPIEXCHANGE(count)// 启动SPI传送
int count;
{
if(count>0) { // 有数据可送?
i=0,i=i++;
PORTB&=~BIT(PB4); // 片选位置低电平
SPDR=a[i]; // 送数
}
else
; // 否则,空操作,并返回
return;
}
void WAIT_SPI()// 等待SPI传送完成
{
do{
;
}while(count>0); // 当count!=0时,等待 to add "CLRWDT"
return;
}
void RESET2510()//对MCP2510芯片进行复位
{
a[0]=RESET;
count=1;
SPIEXCHANGE(count); // 送复位指令
WAIT_SPI();
return;
}
void RD2510(adress,n)//从地址
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -