您当前的位置:五五电子网电子知识单片机-工控设备AVR单片机ATMEGA32使用TA8435H作为步进电机驱动板程序 正文
ATMEGA32使用TA8435H作为步进电机驱动板程序

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

点击数:7127 次   录入时间:03-04 11:37:01   整理:http://www.55dianzi.com   AVR单片机

//ICC-AVR applICation
// Target : M8515
// Crystal: 4.0000MHz

#include <iom8515v.h>
#include <macros.h>

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

#define LED_str_off PORTC&=~(1<<PC6)
#define led_clk_off PORTC&=~(1<<PC5)
#define led_d_off PORTC&=~(1<<PC4)
#define led_str_on PORTC|=(1<<PC6)
#define led_clk_on PORTC|=(1<<PC5)
#define led_d_on PORTC|=(1<<PC4)

#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};               

#define SDA  PA1
#define SCL  PA0

#define SDA_on    PORTA|=(1<<SDA)
#define SDA_off  PORTA&=~(1<<SDA)
#define SCL_on    PORTA|=(1<<SCL)
#define SCL_off  PORTA&=~(1<<SCL)

//void start();
//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)

[1] [2]  下一页


本文关键字:步进电机  程序  AVR单片机单片机-工控设备 - AVR单片机