资源简介

通过ATTINY24A 芯片,IIC作为slave 接收数据并控制直流电机程序,亲自测试好用

资源截图

代码片段和文件信息

#include 
#include “ioavr.h“
#include “inavr.h“
#include “USI_TWI_Slave.h“




extern uint8_t TWI_RxBuf[TWI_RX_BUFFER_SIZE];
extern  unsigned char TWI_slaveAddress;







extern unsigned char data ;
unsigned char RX_data[4];
unsigned char i =0;



void DC_Motor_Set(unsigned char Indexunsigned char Dirunsigned short Speed)
{  
   unsigned short pwm =Speed*255/100;
   switch(Index)
     {
     case 0:  if(Dir==1){
                  
                  PORTA|=BIT(2); PORTA&=~BIT(3);
                   PORTA|=BIT(1); PORTA&=~BIT(0);
                 }
              else{
                  PORTA|=BIT(0); PORTA&=~BIT(1);
                  PORTA|=BIT(3); PORTA&=~BIT(2);
              }
              OCR0A = pwm;
              OCR0B = pwm;
              break;
     case 1:  
              if(Dir==1)
                 {
                    PORTA|=BIT(1); PORTA&=~BIT(0);
                 }
              else 
                 {
                    
                    PORTA|=BIT(0); PORTA&=~BIT(1);
                 }
               
              OCR0A = pwm;
              break;
     case 2:  if(Dir==1)
                 {
                   PORTA|=BIT(2); PORTA&=~BIT(3);
                 }
              else 
                 {
                   PORTA|=BIT(3); PORTA&=~BIT(2);
                 }
              OCR0B = pwm; 
              break;
     default: OCR0B = 0x00;
              OCR0A = 0x00; 
              break;
     
     }
}
void main(void)

  unsigned char TWI_slaveAddress=0;

  CLKPR = 0x80;//配置内部时钟      8M晶振

  DDRA |=BIT(0);DDRA |=BIT(1);DDRA |=BIT(2);DDRA |=BIT(3);
  DDRB |=BIT(2);DDRA |=BIT(7);
  PORTA|=BIT(3); PORTA&=~BIT(2);
  PORTA|=BIT(1); PORTA&=~BIT(0);
  
  
  TCCR0A = 0XA3;TCCR0B = 0X01;OCR0B  = 0; OCR0A  = 0;
  TWI_slaveAddress = 0x50;
  USI_TWI_Slave_Initialise( TWI_slaveAddress );
  __enable_interrupt();

  while(1)
  {   
      if( USI_TWI_Data_In_Receive_Buffer() )
          {
             RX_data[i++]  = USI_TWI_Receive_Byte();
             if(RX_data[0]==0xA3&&i==4)
              {
                i=0;
                DC_Motor_Set(RX_data[1]RX_data[2]RX_data[3]);
              }
         }
  }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       2493  2018-03-19 17:08  Ex_DCMotor\Debug\Exe\Ultransic.hex

     文件       4008  2018-03-19 17:08  Ex_DCMotor\Debug\Exe\Ultransic.sym

     文件     105374  2018-03-19 17:08  Ex_DCMotor\Debug\Obj\main.pbi

     文件      10826  2018-03-19 17:08  Ex_DCMotor\Debug\Obj\main.pbi.cout

     文件      19836  2018-03-19 17:08  Ex_DCMotor\Debug\Obj\main.r90

     文件     110454  2018-03-19 17:08  Ex_DCMotor\Debug\Obj\Ultransic.pbd

     文件     110454  2018-03-19 17:08  Ex_DCMotor\Debug\Obj\Ultransic.pbd.browse

     文件     103821  2018-03-19 17:08  Ex_DCMotor\Debug\Obj\USI_TWI_Slave.pbi

     文件      10835  2018-03-19 17:08  Ex_DCMotor\Debug\Obj\USI_TWI_Slave.pbi.cout

     文件      25837  2018-03-19 17:08  Ex_DCMotor\Debug\Obj\USI_TWI_Slave.r90

     文件      88958  2011-09-04 18:01  Ex_DCMotor\Debug\progisp172\config.xml

     文件        417  2009-12-30 10:06  Ex_DCMotor\Debug\progisp172\eng\93c46.xml

     文件       1283  2010-02-09 11:29  Ex_DCMotor\Debug\progisp172\eng\A25L40P.xml

     文件       1029  2009-03-29 09:16  Ex_DCMotor\Debug\progisp172\eng\AT1358.xml

     文件        391  2008-11-11 21:21  Ex_DCMotor\Debug\progisp172\eng\AT24C01.xml

     文件        559  2010-02-09 11:34  Ex_DCMotor\Debug\progisp172\eng\AT25DF021.xml

     文件       1286  2010-02-09 11:32  Ex_DCMotor\Debug\progisp172\eng\AT25DF041A.xml

     文件       1286  2010-02-09 11:32  Ex_DCMotor\Debug\progisp172\eng\AT25DF081.xml

     文件       1286  2010-02-09 11:32  Ex_DCMotor\Debug\progisp172\eng\AT25DF161.xml

     文件       1286  2010-02-09 11:31  Ex_DCMotor\Debug\progisp172\eng\AT25DF321.xml

     文件       1287  2010-02-09 11:31  Ex_DCMotor\Debug\progisp172\eng\AT25DF321A.xml

     文件       1286  2010-02-09 11:31  Ex_DCMotor\Debug\progisp172\eng\AT25DF641.xml

     文件        553  2010-01-05 09:44  Ex_DCMotor\Debug\progisp172\eng\AT25DFxx.xml

     文件       1284  2010-02-09 11:31  Ex_DCMotor\Debug\progisp172\eng\AT25F512B.xml

     文件       1285  2010-02-09 11:31  Ex_DCMotor\Debug\progisp172\eng\AT25FS010.xml

     文件       1285  2010-02-09 11:30  Ex_DCMotor\Debug\progisp172\eng\AT25FS040.xml

     文件       1285  2010-02-09 11:30  Ex_DCMotor\Debug\progisp172\eng\AT26DF041.xml

     文件       1287  2010-02-09 11:30  Ex_DCMotor\Debug\progisp172\eng\AT26DF081A.xml

     文件       1286  2010-02-09 11:30  Ex_DCMotor\Debug\progisp172\eng\AT26DF161.xml

     文件       1287  2010-02-09 11:29  Ex_DCMotor\Debug\progisp172\eng\AT26DF161A.xml

............此处省略229个文件信息

评论

共有 条评论