ATMEGA32使用TA8435H作为步进电机驱动板程序

//ICC-AVR applICation
// Target : M8515
// Crystal: 4.0000MHz
#include
#include
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x03;
PORTB = 0x00;
DDRB = 0x70;
PORTC = 0x00;
DDRC = 0x70;
PORTD = 0xff;
DDRD = 0x00;
PORTE = 0x00;
DDRE = 0x00;
}



//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //dISAble all interrupts
port_init();
MCUCR = 0x00;
EMCUCR = 0x00;
GICR = 0x40;
TIMSK = 0x00;
SEI(); //re-enable interrupts
//all peripherals are now initialized
}

#define uchar unsigned char
#define uint unsigned int
#defineLED_str_off PORTC&=~(1<#define led_clk_off PORTC&=~(1<#define led_d_off PORTC&=~(1<#define led_str_on PORTC|=(1<#define led_clk_on PORTC|=(1<#define led_d_on PORTC|=(1<#define OP_READ 0xa1 // 器件地址以及读取操作
#define OP_WRITE 0xa0 // 器件地址以及写入操作
#define MAX_ADDR 0x7f // AT24C02最大地址
unsigned char dis_code[] = {0x81,0xB7,0xC2,0x92,0xB4,0x98,0x88,0xB3,0x80,0x90};
unsigned char a[]={90,11,12,13,14,15,22,7,8,9};
uchar led_buffer[4]={0,3};
【ATMEGA32使用TA8435H作为步进电机驱动板程序】#define SDA PA1
#define SCL PA0
#define SDA_on PORTA|=(1<#define SDA_off PORTA&=~(1<#define SCL_on PORTA|=(1<#define SCL_off PORTA&=~(1//void stop();
//unsigned char shin();
//unsigned char shout(unsigned char write_data);
unsigned char read_random(unsigned char random_addr);
void write_byte( unsigned char addr, unsigned char write_data);
void fill_byte(unsigned char fill_data);
void delayms(unsigned char ms);
void update_display_dp(void);
void jisuan(uchar b_data );
unsigned char ndatatodisplay,gewei,shiwei;

main(void)
{
uchar i;
port_init();
SDA_on;
SCL_on;
fill_byte(0xff); // 全部填充0xff

for(i = 0 ; i < 10; i++) //写入显示代码到AT24Cxx
{
write_byte(i, a);
}
init_devices();
while(1);
}
#pragma interrupt_handler int0_isr:2
void int0_isr(void)
{
uchar i;
jisuan((read_random(i)));
ndatatodisplay =( shiwei*10+gewei); // 循环读取24Cxx内容,并输出到P0口
i++;
if(i>9)i=0; // 循环读取范围为0x00~0x07
delayms(250);
update_display_dp();
}
void _nop_()
{
}
void update_display_dp(void)
{
uchar bit_count=0;
uchar byte_counter=0;
uchar i=0;
led_buffer[3]=ndatatodisplay/1000;
led_buffer[2]=(ndatatodisplay%1000)/100;
led_buffer[1]=(ndatatodisplay%100)/10;
led_buffer[0]=ndatatodisplay%10;
led_str_off;
led_clk_off;
for(byte_counter=0;byte_counter<4;byte_counter++)
{
bit_count=8;
i=dis_code[led_buffer][byte_counter]];
while(bit_count>0)
{
if((i&0x01)==0)
{
led_d_off;
}
else
{
led_d_on;
}
i=(i>>1);


led_clk_on;

led_clk_off;

bit_count--;

}

}
led_str_on;


}
void jisuan(uchar b_data )
{
uchar e,d;
e=b_data;
d=e;
e=e&0x0f;
d=d>>4;
gewei=(e&0x01)+((e>>1)&0x01)*2+((e>>2)&0x01)*4+((e>>3)&0x01)*8;
shiwei=(d&0x01)+((d>>1)&0x01)*2+((d>>2)&0x01)*4+((d>>3)&0x01)*8;
}

void start()
// 开始位
{
SDA_on;
SCL_on;
_nop_();
_nop_();
SDA_off;
_nop_();
_nop_();
_nop_();
_nop_();
SCL_off;
}
void stop()
// 停止位
{
SDA_off;
_nop_();
_nop_();
SCL_on;
_nop_();
_nop_();
_nop_();
_nop_();
SDA_on;
}
unsigned char shin()
// 从AT24Cxx移入数据到MCU
{
unsigned char i,read_data;

for(i = 0; i < 8; i++)
{
SCL_on;
DDRA&=0xfd;
read_data <<= 1;
read_data |= (PINA&0X02);
SCL_off;
DDRA&=0xfd;
}
return(read_data);
}
uchar shout(unsigned char write_data)
// 从MCU移出数据到AT24Cxx
{
unsigned char i;
uchar ack_bit;

for(i = 0; i < 8; i++) // 循环移入8个位
{
if(write_data & 0x80)
{
SDA_on;
DDRA=0XFF;
_nop_();
}
else
{
SDA_off;
DDRA=0XFF;
_nop_();
}

SCL_on;
DDRA=0XFF;
_nop_();
_nop_();
SCL_off;
DDRA=0XFF;
write_data <<= 1;
}
SDA_on;
DDRA=0XFF; // 读取应答
_nop_();
_nop_();
SCL_on;
DDRA=0XFF;
_nop_();
_nop_();
_nop_();
_nop_();
ack_bit = PINA&0X02;
SCL_off;
DDRA=0XFF;
return ack_bit; // 返回AT24Cxx应答位
}
void write_byte(unsigned char addr, unsigned char write_data)
// 在指定地址addr处写入数据write_data
{
start();
shout(OP_WRITE);
shout(addr);
shout(write_data);
stop();
delayms(10); // 写入周期
}
void fill_byte(unsigned char fill_data)
// 填充数据fill_data到EEPROM内
{
unsigned char i;
for(i = 0; i < MAX_ADDR; i++)
{
write_byte(i, fill_data);
}
}
unsigned char read_current()
// 在当前地址读取
{
unsigned char read_data;

start();
shout(OP_READ);

read_data = http://www.dg8.com.cn/tool/shin();
stop();
return read_data;
}

    推荐阅读