本帖最後由 xu3u4rmp4 於 2011-8-27 01:00 編輯  
 
好像大家興趣缺缺.那就先改成8軸.有需要再向我索取完整32軸資料 
完整電路圖晚點會補上 
PORTA.0接74595串行數據輸入   DS 
PORTA.1接74595移位時鐘脈衝   SH_CP 
PORTA.2接74595輸出鎖存器控制脈衝   ST_CP 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
AVR部分 
//ICC-AVR application builder : 2011/8/19 下午 10:25:37 
// Target : M16 
// Crystal: 14.7456Mhz 
#include <iom16v.h> 
#include <macros.h> 
unsigned char a=0,temp=0xff; 
unsigned int i=0,model=1,c,R=0;  
unsigned char unb,u1,u2; 
 
unsigned char Out_pin[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};    
 
unsigned char PWM_in[32]={0x50,200,80,220,150,150,150,150, 
                         150,150,150,150,150,150,150,150, 
                         150,150,150,150,150,150,150,150, 
                         150,150,150,150,150,150,150,150}; 
unsigned char PWM_out[32]={100,200,80,220,150,150,150,150, 
                         150,150,150,150,150,150,150,150, 
                         150,150,150,150,150,150,150,150, 
                         150,150,150,150,150,150,150,150}; 
                   
#define  SDATA_595_1_1 PORTA|=BIT(PA0);  //串行數據輸入   DS 
#define  SDATA_595_1_0 PORTA&=~BIT(PA0);    
#define  SCLK_595_1_1 PORTA|=BIT(PA1);    //移位時鐘脈衝   SH_CP 
#define  SCLK_595_1_0 PORTA&=~BIT(PA1);    
#define  RCK_595_1_1 PORTA|=BIT(PA2);    //輸出鎖存器控制脈衝   ST_CP 
#define  RCK_595_1_0 PORTA&=~BIT(PA2);    
 
void WR_OUT_595_1(unsigned char DATA); 
 
void PortA_model(void); 
 
void Delay(void) 
{ 
 unsigned char i; 
 for(i=2;i>0;i--); 
} 
unsigned char Uart_Receive(void)  
{ 
while (!(UCSRA & (1<<RXC))); 
return UDR;     
} 
 
{ 
 
MCUCSR|=(1<<7); 
MCUCSR|=(1<<7); 
 PORTA = 0xFF; 
 DDRA  = 0xFF; 
 PORTB = 0xFF; 
 DDRB  = 0xFF; 
 PORTC = 0xFF; //m103 output only 
 DDRC  = 0xFF; 
 PORTD = 0x02; 
 DDRD  = 0x02; 
} 
//UART0 initialize 
// desired baud rate: 9600 
// actual: baud rate:9600 (0.0%) 
// char size: 8 bit 
// parity: Disabled 
void uart0_init(void) 
{ 
 UCSRB = 0x00; //disable while setting baud rate 
 UCSRA = 0x00; 
 UCSRC = BIT(URSEL) | 0x06; 
 UBRRL = 0x5F; //set baud rate lo 
 UBRRH = 0x00; //set baud rate hi 
 UCSRB = 0x18; 
} 
 
 
//TIMER0 initialize - prescale:1024 
// WGM: Normal 
// desired value: 5mSec請自行把計時器改成20mS 
// actual value:  4.931mSec (1.4%) 
void timer0_init(void) 
{ 
 TCCR0 = 0x00; //stop 
 TCNT0 = 0xB9; //set count 
 OCR0  = 0x47;  //set compare 
 TCCR0 = 0x05; //start timer 
} 
void init_devices(void) 
{ 
 //stop errant interrupts until set up 
 CLI(); //disable all interrupts 
 port_init(); 
 timer0_init(); 
 uart0_init(); 
 
 MCUCR = 0x00; 
 GICR  = 0x00; 
 TIMSK = 0x01; //timer interrupt sources 
 SEI(); //re-enable interrupts 
 //all peripherals are now initialized 
} 
#pragma interrupt_handler timer0_ovf_isr:10 
void timer0_ovf_isr(void) 
{ 
 TCNT0 = 0xB9; //reload counter value 
     temp=0xff;    
     WR_OUT_595_1(temp);    
     for(R=0;R<8;R++) 
     {                                                                      
      PWM_out[R]=PWM_in[R]; 
     } 
   for(R=0;R<300;R++) 
   { 
        Delay();  
      if(R==PWM_out[0]){temp=temp&Out_pin[0];WR_OUT_595_1(temp);}    
       if(R==PWM_out[1]){temp=temp&Out_pin[1];WR_OUT_595_1(temp);} 
      if(R==PWM_out[2]){temp=temp&Out_pin[2];WR_OUT_595_1(temp);}    
       if(R==PWM_out[3]){temp=temp&Out_pin[3];WR_OUT_595_1(temp);} 
      if(R==PWM_out[4]){temp=temp&Out_pin[4];WR_OUT_595_1(temp);}    
       if(R==PWM_out[5]){temp=temp&Out_pin[5];WR_OUT_595_1(temp);} 
      if(R==PWM_out[6]){temp=temp&Out_pin[6];WR_OUT_595_1(temp);}    
       if(R==PWM_out[7]){temp=temp&Out_pin[7];WR_OUT_595_1(temp);}    
   }      
} 
 
 
 
 
 
 
void main(void) 
{ 
init_devices();         
       while(1) 
        { 
unb=Uart_Receive(); 
u1=Uart_Receive(); 
u2=Uart_Receive(); 
PWM_in[unb]=u1+u2; 
 
        } 
 
} 
 
 
 
 
void WR_OUT_595_1(unsigned char DATA) 
{ 
   { 
        unsigned char j,x; 
         for (j=0;j<8;j++) 
        { 
          x=DATA&0x80; 
           if(x==0)   
              { 
             SDATA_595_1_0; 
            } 
            else   
            { 
             SDATA_595_1_1; 
            }      
          SCLK_595_1_1;    //上升沿發生移位 
          SCLK_595_1_0; 
        DATA=DATA<<1; 
       } 
      RCK_595_1_0; 
      RCK_595_1_1;  //上升沿將數據送到輸出鎖存器 
      RCK_595_1_0; 
   }       
} 
 
VB部分  
簡單左中右例子 
傳輸80-220即是對應的0.8mS~2.2mS PWM寬 
由於傳輸超過128會不正常(因為是ASC碼的關係) 
所以以簡單的方式解決.就是分2次傳 
 
 
Private Sub Form_Load() 
 
    MSComm1.CommPort = 3 
    MSComm1.Settings = "9600,N,8,1" 
    MSComm1.InputMode = 1 
    MSComm1.InputLen = 256 
    MSComm1.RThreshold = 1 
  MSComm1.PortOpen = True 
    Label1.Caption = HScroll1.Value 
    
End Sub 
Private Sub Command1_Click() 
Cmd_array = &H0 
MSComm1.Output = Chr(Cmd_array) 
a = 120 
b = 100 
MSComm1.Output = Chr(a) 
MSComm1.Output = Chr(b) 
Label1.Caption = 220 
HScroll1.Value = 220 
End Sub 
 
Private Sub Command2_Click() 
Cmd_array = &H0 
MSComm1.Output = Chr(Cmd_array) 
a = 100 
b = 50 
MSComm1.Output = Chr(a) 
MSComm1.Output = Chr(b) 
Label1.Caption = 150 
HScroll1.Value = 150 
End Sub 
 
Private Sub Command3_Click() 
Cmd_array = &H0 
MSComm1.Output = Chr(Cmd_array) 
a = 40 
b = 40 
MSComm1.Output = Chr(a) 
MSComm1.Output = Chr(b) 
Label1.Caption = 80 
HScroll1.Value = 80 
End Sub 
 
 
 
 
 
 
 
 |