下面的源代码实现了AMIS-30542简单功能的实现,其中
测试平台如下:
下面进行简单的介绍
#include
#include
#include
#include
#include"SPI.h"
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#include
//SPI interfage define
#define SPI_CS PB4
#define SPI_DO PB5
#define SPI_DI PB6
#define SPI_SCK PB7
//Define SPI COMMAND
/*以下定义读写命令
#define Read_WR 0X00
#define Read_CR0 0X01
#define Read_CR1 0X02
#define Read_CR2 0X03
#define Read_CR0 0X04
#define Read_SR1 0X05
#define Read_SR2 0X06
#define Read_SR3 0X07
#define Read_SR3 0X08
*/
#define Write_WR 0X80
#define Write_CR0 0X81
#define Write_CR1 0X82
#define Write_CR2 0X83
#define Write_CR3 0X88
//Int0 for WD and Int1 for ERR
#define WD PD2
#define ERR PD3
//Phase and direct for motor
#define Phase PB3
#define DIR PB2
//Define for CS
//#define CS PB1
#define CLR PB0
//Define for ADC
#define SLA PA0
/*利用定时器0输出脉冲和方向信号
//#pragma interrupt_handler Timer0Interrupt:12
//#pragma interrupt_handler Int0Interrupt:2
#pragma interrupt_handler Timer0Interrupt:12
/******************************
Time0 init
******************************/
void Timer0_Init(void)
{
TCCR0|=0x05; //fosc/1024
TCNT0=248; //f=64
SREG|=BIT(7); //
TIMSK|=BIT(TOIE0);//
DDRB|=BIT(Phase);
}
void Timer0Interrupt(void)
{
static uint i,j;
TCNT0=248;
i++;
if(i>=6400)
{
i=0;
// j++;
// if(j>=20)j=0;
PORTB^=BIT(DIR);
//uart_display(j);
}
//OCR1A=2000;
PORTB^=BIT(Phase);
}
/*SPI初始化和读写程序,这里只利用到了写寄存器程序
void Spi_init(void)
{
DDRB|=BIT(SPI_CS)|BIT(SPI_DO)|BIT(SPI_SCK);
DDRB&=~BIT(SPI_DI);
PORTB|=BIT(SPI_CS);
SPSR|= 0x00; //加速
SPCR = 0x00;
SPCR|= 0x5c;
}
void SPI_Write(uchar a)
{
SPDR=a;
while(!(SPSR&(1< }
uchar SPI_Read(void)
{
while(!(SPSR&(1< return SPDR;
}
uchar ReadWriteSPI(uchar Data)
{
SPDR=Data;
while(!(SPSR & 0x80));
return SPDR;
})));
)));
/******************************
Diver initiation
这里的驱动器初始化程序,配置了4个功能配置寄存器,没有使用到看门狗程序
****************************/
void Driver_init(void)
{
PORTB&=~BIT(SPI_CS);
SPI_Write(Write_CR0);
SPI_Write(0x68);//Full step,current=500mA
PORTB|=BIT(SPI_CS);
/*
PORTB&=~BIT(SPI_CS);
SPI_Write(Write_CR0);
SPI_Write(0x6F);//Full step,current=500mA
PORTB|=BIT(SPI_CS);
PORTB&=~BIT(SPI_CS);
SPI_Write(Write_CR0);
SPI_Write(0x76);//Full step,current=500mA
PORTB|=BIT(SPI_CS);
PORTB&=~BIT(SPI_CS);
SPI_Write(Write_CR0);
SPI_Write(0x7F);//Full step,current=500mA
PORTB|=BIT(SPI_CS);
*/
PORTB&=~BIT(SPI_CS);
SPI_Write(Write_CR1);
SPI_Write(0x01);//
PORTB|=BIT(SPI_CS);
PORTB&=~BIT(SPI_CS);
SPI_Write(Write_CR2);
SPI_Write(0x90);//Full step,current=500mA
PORTB|=BIT(SPI_CS);
PORTB&=~BIT(SPI_CS);
SPI_Write(Write_CR3);
SPI_Write(0x11);//Full step,current=500mA
PORTB|=BIT(SPI_CS);
}
void WD_Reset(void)
{
PORTB&=~BIT(SPI_CS);
SPI_Write(Write_WR);
SPI_Write(0XF8);
PORTB|=BIT(SPI_CS);
}
/*************************************************************************/
/*初始化端口中,配置输入输出
void IO_Init(void)
{
DDRB|=BIT(DIR);
PORTB|=BIT(DIR);//PB2 DIR=1;
DDRD&=~BIT(WD);
DDRD&=~BIT(ERR);
DDRA&=~BIT(SLA);
DDRB|=BIT(CLR);
DDRB|=BIT(DIR);
DDRB|=BIT(Phase);
PORTB|=BIT(Phase);
}
void Delay(void)
{
uint i,j;
for(i=20;i>0;i--)
{
for(j=80;j>0;j--);
}
}
/*主函数中,初始化完成以后,进行延时循环。
void main(void)
{
IO_Init();
Spi_init();
Driver_init();
WD_Reset();
Timer0_Init();
// DDRA|=BIT(PA4);
PORTB&=~BIT(SPI_DO);
while(1)
{
//Driver_init();
// WD_Reset();
// Driver_init();
//PORTA^=BIT(PA4);
Delay();
}
}
用户1714690 2014-2-10 07:27
用户1625273 2014-1-26 09:36