|
多谢楼主,我在你的代码基础上修改成6通道的程序了,我看代码最多能改成8通道,但是我fs-I6没有刷固件,原厂固件测试是可以的,我看你原来程序用了PCA,我猜测你可能想要输出PWM,但是stc15的pwm不用中断的话只能3个io输出也就是3个通道,而且pwm频率较高不适合舵机,我就把pca代码删去了,还是用的定时器T2输出pwm
- //*************************************************************************
- // RX A7105 2.4Ghz protocol Flysky/turnigy 9x (v2)
- // look at thread: http://www.rcgroups.com/forums/showthread.php?t=1626362
- // many thanks to: PhracturedBlue, Dave1993, Surfbreizh, CristiV...
- // author: Thierry Pébayle
- //
- // Flysky receiver for STC11F04E
- // look at thread: http://bbs.mx3g.com/thread-473651-1-1.html
- //
- // LOLI receiver for STC15W408AS
- // look at thread: http://bbs.mx3g.com/thread-498303-1-1.html
- //
- // Flysky receiver for STC15W408AS
- // Modify by gloeagle
- // STC15W408AS μ¥Ƭ»úûóDT1¶¨ê±Æ÷£¬Dèòaìرe×¢òa£¬′®¿úÖ»Äüê1óÃT2¼Æê±Æ÷à′2úéú2¨ìØÂê
- // TO¼óìø£¬T2êä3ö êμ2a¸±òíêä3öÆμÂê136hz ½ó¶æ»úè»oóéÏμç2»¶¶¶æ£¬ò£¿ØÆ÷ÎT2ù×÷μÄê±oòóDÇá΢μĶ¶¶æ
- // ÏÂÃæ¼Æ»®PCA0êä3öμç»úPWMDÅoÅ£¬í¨1yD¾Æ¬Ö±½óÇy¶ˉμç»ú D£×¼μçμ÷êǸöÄÑμã
- // 3ìDò2aêÔÕy3££¬ê£ÏÂμľíêÇà′D£×¼μçμ÷áË
- //**********************************************************************
- //*******¾§ÕñÑ¡ÔñÄúÖà 12Mhz*******//
- #include <STC15.H>
- #include <intrins.h>
- #define GUDING //1춨꧿ØÖμ/×Ôóé×Ô¶¨
- #define u8 unsigned char
- #define u16 unsigned int
- #define u32 unsigned long int
- ////*****modify
- //¶¨òåÖ¸ê¾μÆ
- sbit LED=P5^5; //¶¨òåÖ¸ê¾μÆ
- sbit bind=P5^4; //Ïè½óμØè»oó½óêÕ»úéÏμ磬½øèë¶ÔÆμÄ£ê½
- sbit LED2=P3^7; //ÅÅ′í
- ////*****modify
- //¶¨òå4¸öí¨μàêä3ö
- sbit CH1=P3^2; //¸±òí
- sbit CH2=P3^3; //éy½μ
- sbit CH3=P3^6; //·½Ïò
- sbit CH4=P3^7; //roll
- sbit CH5=P1^0; //·½Ïò
- sbit CH6=P1^1; //roll
- //óíÃÅ-> PWMêä3ö·½ê½£¬Âö3åÔúP1.1òy½Å CCP0/P1.1òy½Å
- //*****¶¨òåÎTÏßÄ£¿éμÄ1ü½Å
- sbit GIO=P1^2;
- sbit MDI=P1^3;
- sbit SCK=P1^4;
- sbit CSN=P1^5;
- u8 code A7105_regs[]={ //Ä£¿é¼Ä′æÆ÷é趨±í
- 0xff, 0x42, 0x00, 0x14, 0x00, 0xff, 0xff ,0x00, 0x00, 0x00, 0x00, 0x01, 0x21, 0x05, 0x00, 0x50,
- 0x9e, 0x4b, 0x00, 0x02, 0x16, 0x2b, 0x12, 0x00, 0x62, 0x80, 0x80, 0x00, 0x0a, 0x32, 0xc3, 0x0f,
- 0x13, 0xc3, 0x00, 0xff, 0x00, 0x00, 0x3b, 0x00, 0x17, 0x47, 0x80, 0x03, 0x01, 0x45, 0x18, 0x00,
- 0x01, 0x0f, 0xff,
- };
- u8 code tx_channels[16][16]={ //ìøÆμ±í
- {0x0a, 0x5a, 0x14, 0x64, 0x1e, 0x6e, 0x28, 0x78, 0x32, 0x82, 0x3c, 0x8c, 0x46, 0x96, 0x50, 0xa0},
- {0xa0, 0x50, 0x96, 0x46, 0x8c, 0x3c, 0x82, 0x32, 0x78, 0x28, 0x6e, 0x1e, 0x64, 0x14, 0x5a, 0x0a},
- {0x0a, 0x5a, 0x50, 0xa0, 0x14, 0x64, 0x46, 0x96, 0x1e, 0x6e, 0x3c, 0x8c, 0x28, 0x78, 0x32, 0x82},
- {0x82, 0x32, 0x78, 0x28, 0x8c, 0x3c, 0x6e, 0x1e, 0x96, 0x46, 0x64, 0x14, 0xa0, 0x50, 0x5a, 0x0a},
- {0x28, 0x78, 0x0a, 0x5a, 0x50, 0xa0, 0x14, 0x64, 0x1e, 0x6e, 0x3c, 0x8c, 0x32, 0x82, 0x46, 0x96},
- {0x96, 0x46, 0x82, 0x32, 0x8c, 0x3c, 0x6e, 0x1e, 0x64, 0x14, 0xa0, 0x50, 0x5a, 0x0a, 0x78, 0x28},
- {0x50, 0xa0, 0x28, 0x78, 0x0a, 0x5a, 0x1e, 0x6e, 0x3c, 0x8c, 0x32, 0x82, 0x46, 0x96, 0x14, 0x64},
- {0x64, 0x14, 0x96, 0x46, 0x82, 0x32, 0x8c, 0x3c, 0x6e, 0x1e, 0x5a, 0x0a, 0x78, 0x28, 0xa0, 0x50},
- {0x50, 0xa0, 0x46, 0x96, 0x3c, 0x8c, 0x28, 0x78, 0x0a, 0x5a, 0x32, 0x82, 0x1e, 0x6e, 0x14, 0x64},
- {0x64, 0x14, 0x6e, 0x1e, 0x82, 0x32, 0x5a, 0x0a, 0x78, 0x28, 0x8c, 0x3c, 0x96, 0x46, 0xa0, 0x50},
- {0x46, 0x96, 0x3c, 0x8c, 0x50, 0xa0, 0x28, 0x78, 0x0a, 0x5a, 0x1e, 0x6e, 0x32, 0x82, 0x14, 0x64},
- {0x64, 0x14, 0x82, 0x32, 0x6e, 0x1e, 0x5a, 0x0a, 0x78, 0x28, 0xa0, 0x50, 0x8c, 0x3c, 0x96, 0x46},
- {0x46, 0x96, 0x0a, 0x5a, 0x3c, 0x8c, 0x14, 0x64, 0x50, 0xa0, 0x28, 0x78, 0x1e, 0x6e, 0x32, 0x82},
- {0x82, 0x32, 0x6e, 0x1e, 0x78, 0x28, 0xa0, 0x50, 0x64, 0x14, 0x8c, 0x3c, 0x5a, 0x0a, 0x96, 0x46},
- {0x46, 0x96, 0x0a, 0x5a, 0x50, 0xa0, 0x3c, 0x8c, 0x28, 0x78, 0x1e, 0x6e, 0x32, 0x82, 0x14, 0x64},
- {0x64, 0x14, 0x82, 0x32, 0x6e, 0x1e, 0x78, 0x28, 0x8c, 0x3c, 0xa0, 0x50, 0x5a, 0x0a, 0x96, 0x46},
- };
- u32 id; //¶ÔÂëID
- u8 chanrow,chancol,chanoffset,channel;
- u8 txid[21]={0,0,0,0,0,220,5,220,5,232,3,220,5,220,5,220,5,220,5,220,5};
- u8 packet[21];
- u8 Htemp[8]={5,5,3,5,5,5,5,5};
- u8 Ltemp[8]={220,220,232,220,220,220,220,220};
- u8 ch=0;
- u16 mss;
- bit hch,cb;
- //modify
- ////FS-I6 GUDING -- í¨1yDT¸Ä3ìDò£¬êÖ¶ˉ»ñè¡μÄ ÔúÕa¶¨òåμÄÖμoˉêyàïÃæÎaé¶óÃ2»áË
- unsigned short int ESC_MIN=990; //D¡óíÃÅDD3ì
- unsigned short int ESC_MAX=1980; //′óóíÃÅDD3ì
- unsigned short int RxESC;
- unsigned short int ESCtemp;
- //Ñó걶¨òå
- void Delay1ms(){ //@12.000MHz
- unsigned char i, j;
- _nop_();
- _nop_();
- i = 10;
- j = 83;
- do
- {
- while (--j);
- } while (--i);
- }
- void delay_ms(u16 i){
- while(i--)
- Delay1ms();
- }
- //′íÎóμÆoˉêy
- void ErrorLED(){
- LED2=0; //add 2017
- delay_ms(500);
- LED2=1;
- delay_ms(500);
- LED2=0;
- }
- void IapIdle(){
- IAP_CONTR = 0; //Close IAP function
- IAP_CMD = 0; //Clear command to standby
- IAP_TRIG = 0; //Clear trigger register
- IAP_ADDRH = 0x80; //Data ptr point to non-EEPROM area
- IAP_ADDRL = 0; //Clear IAP address to prevent misuse
- }
- u8 EEPROM_read(u8 addr){
- u8 dat; //Data buffer
- IAP_CONTR = 0x82; //Open IAP function, and set wait time
- IAP_CMD = 0x01; //Set ISP/IAP/EEPROM READ command
- IAP_ADDRL = addr; //Set ISP/IAP/EEPROM address low
- IAP_ADDRH = 0; //Set ISP/IAP/EEPROM address high
- IAP_TRIG = 0x5a; //Send trigger command1 (0x5a)
- IAP_TRIG = 0xa5; //Send trigger command2 (0xa5)
- _nop_(); //MCU will hold here until ISP/IAP/EEPROM operation complete
- dat = IAP_DATA; //Read ISP/IAP/EEPROM data
- IapIdle(); //Close ISP/IAP/EEPROM function
- return dat; //Return Flash data
- }
- void EEPROM_write(u8 addr,u8 dat){
- IAP_CONTR = 0x82; //Open IAP function, and set wait time
- IAP_CMD = 0x02; //Set ISP/IAP/EEPROM PROGRAM command
- IAP_ADDRL = addr; //Set ISP/IAP/EEPROM address low
- IAP_ADDRH = 0; //Set ISP/IAP/EEPROM address high
- IAP_DATA = dat; //Write ISP/IAP/EEPROM data
- IAP_TRIG = 0x5a; //Send trigger command1 (0x5a)
- IAP_TRIG = 0xa5; //Send trigger command2 (0xa5)
- _nop_(); //MCU will hold here until ISP/IAP/EEPROM operation complete
- IapIdle();
- }
- void EEPROM_clean(u8 addr){
- IAP_CONTR = 0x82; //Open IAP function, and set wait time
- IAP_CMD = 0x03; //Set ISP/IAP/EEPROM ERASE command
- IAP_ADDRL = 0; //Set ISP/IAP/EEPROM address low
- IAP_ADDRH = addr; //Set ISP/IAP/EEPROM address high
- IAP_TRIG = 0x5a; //Send trigger command1 (0x5a)
- IAP_TRIG = 0xa5; //Send trigger command2 (0xa5)
- _nop_(); //MCU will hold here until ISP/IAP/EEPROM operation complete
- IapIdle();
- }
- void IDWR(bit wr){ // 1:±£′æ 0:¶áè¡
- u8 i;
- if(wr)EEPROM_clean(0),delay_ms(10);
- for(i=0;i<21;i++){
- if(wr){
- EEPROM_write(i,txid[i]);
- }
- // txid[i]=packet[i];}
- else txid[i]=EEPROM_read(i);
- }
- }
- void SPIwrite(u8 command){ //ê±DòD′
- u8 n=8;
- SCK=0;MDI=0;
- while(n--) {
- if(command&0x80)MDI=1;
- else MDI=0;
- SCK=1;_nop_();SCK=0;
- command = command << 1;
- }
- MDI=1;
- }
- u8 SPIread(void){ //ê±Dò¶á
- u8 i,result=0;
- for(i=0;i<8;i++) {
- if(MDI==1)result=(result<<1)|0x01;
- else result=result<<1;
- SCK=1;_nop_();SCK=0;_nop_();
- }return result;
- }
- void AREG_write(u8 address, u8 dat){//D′¼Ä′æÆ÷
- CSN=0;
- SPIwrite(address);
- _nop_();
- SPIwrite(dat);
- CSN=1;
- }
- u8 AREG_read(u8 address){ //¶á¼Ä′æÆ÷
- u8 result;
- CSN=0;
- address |=0x40;
- SPIwrite(address);
- result = SPIread();
- CSN=1;
- return(result);
- }
- void A7105_WriteID(u32 ida) { //D′Ä£¿éID
- CSN=0;
- SPIwrite(0x06);
- SPIwrite((ida>>24)&0xff);
- SPIwrite((ida>>16)&0xff);
- SPIwrite((ida>>8)&0xff);
- SPIwrite((ida>>0)&0xff);
- CSN=1;
- }
- void Read_Packet(){ //¶áêy¾Y°ü,ò£¿Øêy¾Y
- u8 i;
- CSN=0;
- SPIwrite(0x45);
- for (i=0;i<21;i++)packet[i]=SPIread();
- CSN=1;
- }
- void Strobe(u8 address){ //Ä£¿éé趨
- CSN=0;
- SPIwrite(address);
- CSN=1;
- }
- ////end
- void bind_Flysky(){ //¶ÔÆμ
- u8 i,counter1=255;
- Strobe(0xA0);
- Strobe(0xF0);
- AREG_write(0x0F,0x00);//±è·¢éäμíò»Æμμà
- Strobe(0xC0);
- while(counter1){
- delay_ms(10);
- if(counter1&0x04)LED=1;
- else LED=0;
- if(GIO==0){
- if((AREG_read(0x00)&0x60)==0){
- Read_Packet();
- for(i=0;i<21;i++)txid[i]=packet[i];
- break;
- }
- else{
- Strobe(0xA0);
- Strobe(0xF0);
- AREG_write(0x0F,0x00);
- Strobe(0xC0);
- continue;
- }
- }
- else{
- --counter1;
- if (counter1==0)counter1=255;
- //LED2=0; //add 2017
- }
- }
- }
- void flysky_cb(){ //Ö÷Ñ-»·
- u8 i,x;
- channel=tx_channels[chanrow][chancol]-chanoffset;//ìøÆμμà
- channel-=1;
- Strobe(0xA0);
- Strobe(0xF0);
- AREG_write(0x0F,channel);//é趨Æμμà
- Strobe(0xC0);
- chancol=(chancol+1)%16;
- while(1){
- if(mss>80){ //ÎTDÅoÅ1.5soó꧿ر£»¤
- #ifdef GUDING
- Ltemp[2]=232;Htemp[2]=3;
- #endif
- for(i=0;i<8;i++){
- #ifdef GUDING
- if(i==2)continue;
- Ltemp[i]=220;
- Htemp[i]=5;
- #else
- Ltemp[i]=txid[2*i+5];
- Htemp[i]=txid[2*i+6];
- #endif
- }
- }
- if(hch){ //′óóú20ms¼óìø
- hch=0;LED=0;
- chancol=(chancol+1)%16;
- // channel=tx_channels[chanrow][chancol]-1-chanoffset;
- break;
- }
- if (GIO==1)continue;
- x=AREG_read(0x00);
- if(x&0x60)continue;//êy¾Y¼ìÑé
- Read_Packet();//¶áò£¿Øêy¾Y ¡y¡y¡y¡y¡yÆ¥ÅäID
- if (!(packet[1]==txid[1])&&!(packet[2]==txid[2])&&!(packet[3]==txid[3])&&!(packet[4]==txid[4]))continue;
- mss=0;
- if(cb){
- for (i=0;i<8;i++){//êä3ö 1000~2000
- EA=0; //×üÖD¶Ï¿a1Ø:1Ø
- x=packet[6+(2*i)];
- if((x>=3)&&(x<=8) ){ //·àÖ1òç3ö
- Ltemp[i]=packet[5+(2*i)];
- Htemp[i]=x;
- }
- EA=1; //×üÖD¶Ï¿a1Ø:¿a
- }
- }
- if(cb)LED=1;
- break;
- }
- }
- void flysky_init(){ //3õê¼»ˉ
- u8 i,k;
- u16 HT;
- P1M0 = 0x03; //(00000011B) éèÖÃ1¿ú01Îa10×′ì¬
- P3M0 = 0xcf; //(11001111B) éèÖÃ3¿ú012367Îa10×′ì¬
- P5M0 = 0x20; //(00100000B) 5.5 led£»5.4 bind
- TMOD = 0x01;AUXR = 0x00; //(00000001) T0·Ç×Ô¶ˉÖØ×°·½ê½;T2Îa12TÄ£ê½
- IE |= 0x82;IE2 |= 0x04; //(10000010) EA ET0 ¿aÖD¶Ï;//¶¨ê±Æ÷2 ¿aÖD¶Ï
- bind=1;CSN=1;MDI=1;SCK=0;
- delay_ms(10);
- AREG_write(0x00,0x00);
- A7105_WriteID(0x5475c52A);
- for (i = 0; i < 0x33; i++){
- if(A7105_regs[i] != 0xff)
- AREG_write(i, A7105_regs[i]);
- }
- Strobe(0xA0);
- AREG_write(0x02,0x01);
- //ErrorLED(); // ûÎêìa£¬¿éòÔÖ′DD
- while(AREG_read(0x02)); //Õa3öÏÖÎêìaáË£¬Ñ-»·Ã»óD3öà′
- ErrorLED(); // óDÎêìa£¬Ã»óDÖ′DD£¬ó|¸Ã¶ÏÔúÑ-»·ÖD£¬Ã»3öà′
- AREG_write(0x24,0x13);
- AREG_write(0x26,0x3b);
- AREG_write(0x0F,0x00);
- AREG_write(0x02,0x02);
- while(AREG_read(0x02));
- AREG_write(0x0F,0xA0);
- AREG_write(0x02,0x02);
- while(AREG_read(0x02));
- AREG_write(0x25,0x08);
- Strobe(0xA0);
- while(1){
- LED=1;
- delay_ms(500);
- LED=0;
- delay_ms(500);
- if(bind==1){
- IDWR(0); //¶áè¡ID
- cb=1;
- if(txid[0]==0xaa)break;
- else continue;
- }
- else{
- bind_Flysky();//¶ÔÆμ
- LED=1;
- delay_ms(500);
- LED=0;
- delay_ms(500);
- //TR0=TR1=1; //Modify
- TR0=1;AUXR|=0x10;
- cb=0;
- break;
- }
- }
- //¡y¡y¡y¡y¡y¡y¡y¡yoÏÆ′ID
- id=(txid[1]|((u32)txid[2]<<8)|((u32)txid[3]<<16)|((u32)txid[4]<<24));
- chanrow=id%16;
- chanoffset=(id&0xff)/16;
- chancol=0;
- if(chanoffset>9)chanoffset=9;
- while(TR0){
- ////if(bind){IDWR(1);LED=0;TR0=TR1=0;while(1){LED=1;delay_ms(1000);LED=0;delay_ms(1500);};}
- if(bind){IDWR(1);LED=0;TR0=0;AUXR=0x00;while(1){LED=1;delay_ms(1000);LED=0;delay_ms(1500);};} //Modify
- flysky_cb();//4:1024 5:1280 6:1536 7:1792 8:2048
- if(mss>5){mss=0;
- for(i=0;i<8;i++){
- if((i>3)||(i==2)){
- Ltemp[i]=txid[2*i+5]=packet[2*i+5];
- Htemp[i]=txid[2*i+6]=packet[2*i+6];
- continue;
- }
- if((packet[2*i+6]<5)||(packet[2*i+6]>6)){
- if(k&(1<<i)){k&=(0xfe<<i);LED=1;
- HT=(u16)txid[2*i+5]|(u16)(txid[2*i+6]<<8);
- if(packet[2*i+6]<5){if(HT>900)HT-=100;}
- else if(HT<2000)HT+=100;
- Ltemp[i]=txid[2*i+5]=HT&0xff;
- Htemp[i]=txid[2*i+6]=(HT>>8)&0xff;}
- }
- else k|=(1<<i);
- }
- }
- }
- }
- void main(){
-
- flysky_init();
- //Æô¶ˉ¼óìøoíêä3ö¶¨ê±Æ÷ -- Modify
- TR0=1;AUXR |= 0x10; //T0ÎaìøÆ춨ê±Æ÷£¬T2Îaêä3ö¶¨ê±Æ÷
- while(1)flysky_cb();
- }
- void et0()interrupt 1{ //¼óìø¶¨ê±Æ÷
- //TH0 =248;TL0 =50; //@12Mhz 2ms +2
- //3õÖμ=65536-(12000000/12)*0.020=45536=B1E0 16λ@12Mhz 20ms
- TH0=0xb1;TL0=0xe0; //@12Mhz 20ms
- //TH0=0xD8;TL0=0xF0; //@12Mhz 10ms
- //TH0=0x3C;TL0=0xB0; //@12Mhz 50ms
- hch=1;
- mss++;
- }
- void et2()interrupt 12{ //êä3ö¶¨ê±Æ÷£¬¶¨ê±Æ÷T2ÖD¶Ï3ìDò
- CH1=0;CH2=0;CH3=0;
- CH4=0;CH5=0;CH6=0;
-
- T2H=255-Htemp[ch]; //@12Mhz 252 3
- T2L=255-Ltemp[ch]; //@12Mhz 23 232
- switch (ch) {
- case 0: CH1=1;break; //¸±òí
- case 1: CH2=1;break; //éy½μ
- case 2: CH3=1;break; //·½Ïò
- case 3: CH4=1;break; //
- case 4: CH5=1;break; //
- case 5: CH6=1;break; //
- }
- ch=(ch+1)%6;
- }
复制代码
|
|