Go Down

Topic: Conversion code Atmega16 vers Uno (Read 75 times) previous topic - next topic

pierrotm777

Oct 17, 2019, 04:32 pm Last Edit: Oct 17, 2019, 04:43 pm by pierrotm777
Bonjour à tous,

J'ai un code sensé fonctionner avec un atmega16 et je souhaiterais le convertir vers une carte arduino Uno.
CE code lit un signal PCM1024 d'un récepteur radio Futaba au format PCM1024 et restitue les mouvements des manches de la radio sur le port série .
Code: [Select]
#include <avr\io.h>
//#include <stdio.h>
#include <avr\interrupt.h>


unsigned long start,stop;
unsigned long counts,width_us;

unsigned int update=0,inde=0,bits=0,over_flow=0,last_index=0,z;
unsigned char Data[60];


ISR(TIMER1_CAPT_vect)
{
 stop=ICR1;
 counts=(stop-start)+(over_flow * 65536); //Calculate Ticks per width
 over_flow=0; //Rreset over flow counter
  
  //F_CPU = 7372800
 width_us=counts*(1/7.3728); //convert ticks to us

  
 bits=width_us/150;
 if ( (width_us % 150) > 120) //convert width to bit (each bit 150 us)
 {
 bits++;
 }
 if (bits==18) // start of header 18 bit = 2700 us
 {
 last_index=inde;
 inde=0;
 update=1;
 }
 else
 {
 inde++;
 }
 Data[inde]=bits; //store bits in array
 
 if ((PIND & 0x40)==0x40) // ICP pin is high
 {
 TCCR1B=TCCR1B&0xBF; //set to trigger on falling edge
 }
 else
 {
 TCCR1B=TCCR1B|0x40; //rising edge triggers next
 }
 start=ICR1;
}



ISR(TIMER1_OVF_vect)
{
 over_flow++;
}

void RS232_Init(void)
{
 UBRRH=0x00;
 UBRRL=103;//UBRRL=0x2F; // Baud rate = 9600
 UCSRA=0x00;
 UCSRB=0x98;//UCSRB=0x18;
 UCSRC=0x06;//UCSRC=0x86;
 
}

void Timer1_Init(void)
{
 TIMSK=0x24;
 TCCR1A=0x00;
 TCCR1B=0x41;
 TCNT1H=0x00;
 TCNT1L=0x00;
 ICR1H=0x00;
 ICR1L=0x00;
 OCR1AH=0x00;
 OCR1AL=0x00;
 OCR1BH=0x00;
 OCR1BL=0x00;
}

int out(char var) //Used in print() Function
{
    while(!(UCSRA&(1<<UDRE)));
    UDR = var;
    return 0;
}

void print(unsigned int value)
{
 out(value/1000+0x30);
 value=value%1000;
 out(value/100+0x30);
 value=value%100;
 out(value/10+0x30);
 out(value%10+0x30);
 out(' ');
}
unsigned char ten2six(unsigned int word) // Convert ten bits to six bits
{
 unsigned char high = (word & 0x300) >> 8;
 unsigned char low = (word & 0x0FF) >> 0;

 if( high == 0x03 )
 {
 if( low == 0xF8 ) return 0x00; /* 1111111000 */
 if( low == 0xF3 ) return 0x01; /* 1111110011 */
 if( low == 0xE3 ) return 0x02; /* 1111100011 */
 if( low == 0xE7 ) return 0x03; /* 1111100111 */
 if( low == 0xC7 ) return 0x04; /* 1111000111 */
 if( low == 0xCF ) return 0x05; /* 1111001111 */
 if( low == 0x8F ) return 0x06; /* 1110001111 */
 if( low == 0x9F ) return 0x07; /* 1110011111 */

 if( low == 0x3F ) return 0x0B; /* 1100111111 */
 if( low == 0x1F ) return 0x0C; /* 1100011111 */
 if( low == 0x0F ) return 0x0D; /* 1100001111 */
 if( low == 0x87 ) return 0x0E; /* 1110000111 */
 if( low == 0xC3 ) return 0x0F; /* 1111000011 */

 if( low == 0xCC ) return 0x14; /* 1111001100 */
 if( low == 0x9C ) return 0x15; /* 1110011100 */
 if( low == 0x3C ) return 0x16; /* 1100111100 */
 if( low == 0x33 ) return 0x17; /* 1100110011 */
 if( low == 0xF0 ) return 0x18; /* 1111110000 */
 if( low == 0xE0 ) return 0x19; /* 1111100000 */
 if( low == 0x83 ) return 0x1A; /* 1110000011 */
 if( low == 0x07 ) return 0x1B; /* 1100000111 */
 if( low == 0x1C ) return 0x1C; /* 1100011100 */
 if( low == 0x98 ) return 0x1D; /* 1110011000 */
 if( low == 0x8C ) return 0x1E; /* 1110001100 */
 if( low == 0x38 ) return 0x1F; /* 1100111000 */

 if( low == 0x30 ) return 0x2C; /* 1100110000 */
 if( low == 0x18 ) return 0x2D; /* 1100011000 */
 if( low == 0x0C ) return 0x2E; /* 1100001100 */
 if( low == 0x03 ) return 0x2F; /* 1100000011 */

 if( low == 0xC0 ) return 0x35; /* 1111000000 */
 if( low == 0x80 ) return 0x36; /* 1110000000 */
 if( low == 0x00 ) return 0x37; /* 1100000000 */
 } else
 if( high == 0x00 )
 {
 if( low == 0xFF ) return 0x08; /* 0011111111 */
 if( low == 0x7F ) return 0x09; /* 0001111111 */
 if( low == 0x3F ) return 0x0A; /* 0000111111 */

 if( low == 0xFC ) return 0x10; /* 0011111100 */
 if( low == 0xF3 ) return 0x11; /* 0011110011 */
 if( low == 0xE7 ) return 0x12; /* 0011100111 */
 if( low == 0xCF ) return 0x13; /* 0011001111 */
 
 if( low == 0xC7 ) return 0x20; /* 0011000111 */
 if( low == 0x73 ) return 0x21; /* 0001110011 */
 if( low == 0x67 ) return 0x22; /* 0001100111 */
 if( low == 0xE3 ) return 0x23; /* 0011100011 */
 if( low == 0xF8 ) return 0x24; /* 0011111000 */
 if( low == 0x7C ) return 0x25; /* 0001111100 */
 if( low == 0x1F ) return 0x26; /* 0000011111 */
 if( low == 0x0F ) return 0x27; /* 0000001111 */
 if( low == 0xCC ) return 0x28; /* 0011001100 */
 if( low == 0xC3 ) return 0x29; /* 0011000011 */
 if( low == 0x63 ) return 0x2A; /* 0001100011 */
 if( low == 0x33 ) return 0x2B; /* 0000110011 */

 if( low == 0x3C ) return 0x30; /* 0000111100 */
 if( low == 0x78 ) return 0x31; /* 0001111000 */
 if( low == 0xF0 ) return 0x32; /* 0011110000 */
 if( low == 0xE0 ) return 0x33; /* 0011100000 */
 if( low == 0xC0 ) return 0x34; /* 0011000000 */

 if( low == 0x60 ) return 0x38; /* 0001100000 */
 if( low == 0x70 ) return 0x39; /* 0001110000 */
 if( low == 0x30 ) return 0x3A; /* 0000110000 */
 if( low == 0x38 ) return 0x3B; /* 0000111000 */
 if( low == 0x18 ) return 0x3C; /* 0000011000 */
 if( low == 0x1C ) return 0x3D; /* 0000011100 */
 if( low == 0x0C ) return 0x3E; /* 0000001100 */
 if( low == 0x07 ) return 0x3F; /* 0000000111 */
 }

 return 0xFF;
}

int Extract_Data(unsigned long int end_data)
{
 unsigned int start_data=0,ind=0,m,check_sum=0,pos[4],byte[16],channel[9];
 unsigned char value=0,bit_counter=0,n,packet_byte[16],frame=0,aux[4],dif[4],crc[4];
 
 if (Data[0] != 18)
 {
 return 0;
 }
 if (Data[1]==4)
 {
 frame=1;
 }
 else
 {
 frame=2;
 }
 if (Data[2]>2)
 {
 Data[2] -=2;
 start_data=2;
 value=1;
 }
 else
 {
 start_data=3;
 value=0;
 }
 
 for (ind=0;ind<=15;ind++)
 {
 for (m=start_data;m<= (end_data-1);m++)
 {
 for (n=1;n<=Data[m];n++)
 {
 byte[ind]=byte[ind]<<1;
 byte[ind] |= value;
 bit_counter++;
 if (bit_counter==10)
 {
 if (n<Data[m])
 {
 Data[m] -= n;
 start_data = m;
 value --;
 }
 else
 {
 start_data =m+1;
 }
 bit_counter=0;
 break;
 }
 }
 value++;
 if(value>=2)
 {
 value=0;
 }
 if (bit_counter==0)
 {
 break;
 }
 }
 }
 
 for (ind=0;ind<=15;ind++)
 {
 packet_byte[ind]=ten2six( byte[ind]);
 if (packet_byte[ind]==0xff)
 {
 return 0;
 }
 }
 
 for (ind=0;ind<=3;ind++)
 {
 aux[ind]=(packet_byte[4*ind+0] & 0x30) >> 4;
 dif[ind]=(packet_byte[4*ind+0] & 0x0F) >> 0;
 pos[ind]=(packet_byte[4*ind+1] & 0xFF) << 4 | (packet_byte[4*ind+2] & 0x3C) >> 2;
 crc[ind]=(packet_byte[4*ind+2] & 0x03) << 6 | (packet_byte[4*ind+3] & 0x3F) >> 0;
 }


 if (frame==1)
 {
 if(aux[0]==2)
 {
 channel[1] = pos[0];
 channel[2] += (dif[0]-8)*4;
 }
 if(aux[1]==0)
 {
 channel[3] = pos[1];
 channel[4] += (dif[1]-8)*4;
 }
 if(aux[2]==2)
 {
 channel[5] = pos[2];
 channel[6] += (dif[2]-8)*4;
 }
 if(aux[3]==0)
 {
 channel[7] = pos[3];
 channel[8] += (dif[3]-8)*4;
 channel[9] = 40;
 }
 if(aux[3]==1)
 {
 channel[7] = pos[3];
 channel[8] += (dif[3]-8)*4;
 channel[9] = 984;
 }
 
 }
 if (frame==2)
 {
 if(aux[0]==2)
 {
 channel[2] = pos[0];
 channel[1] += (dif[0]-8)*4;
 }
 if(aux[1]==0)
 {
 channel[4] = pos[1];
 channel[3] += (dif[1]-8)*4;
 }
 if(aux[2]==2)
 {
 channel[6] = pos[2];
 channel[5] += (dif[2]-8)*4;
 }
 if(aux[3]==0)
 {
 channel[8] = pos[3];
 channel[7] += (dif[3]-8)*4;
 channel[9] = 40;
 }
 if(aux[3]==1)
 {
 channel[8] = pos[3];
 channel[7] += (dif[3]-8)*4;
 channel[9] = 984;
 }
 }
 for (ind=1;ind<=9;ind++)
 {
 check_sum ^=channel[ind];
 }
 print(channel[1]);
 print(channel[2]);
 print(channel[3]);
 print(channel[4]);
 print(channel[5]);
 print(channel[6]);
 print(channel[7]);
 print(channel[8]);
 print(channel[9]);
 print(check_sum);
 out(0x0A);
 out(0x0d);
 
 return 1;
}


int main (void)
{
 RS232_Init();
 Timer1_Init();
 //fdevopen(out,0);
 DDRD=0x00;
 PORTD=0xff;
 DDRA=0xFF;
 PORTA=0x01;

 sei();
 out('R');
 while(1)
 {
 if(update==1)
 {
 PORTA=0x00;
 z=Extract_Data(last_index);
 PORTA=0x01;
 update=0;
 }
 
 }
 return 0;
}


Au final, je voudrais que la sortie série soit remplacée par 9 sorties servo.
Par défaut, il fonctionne à la fréquence 7372800 Mhz.
Il faudrait donc que le code fonctionne à 16Mhz.

Merci de votre aide par avance.

Pierre

Go Up