Если будет интересно то недавно писал под С8051F350, считываются данные с двух энкодеров и шлются через UART:
///////////Encoder based AS5045 chip///////////////
#include "C8051F350.h"
#include "Pin_designator.c"
#include "stdio.h"
///////////// Function ////////////////////
void del_10us(void);
void del_500ms(void);
void read_encod_1(void);
void read_encod_2(void);
void TXDataUART(void);
///////////Extern function/////////////////
extern void Init_Device (void);
///////////Variable////////////
unsigned int enc_1, enc_2; //дані з енкодера (D11, D10,....D2, D1, D0)
unsigned int enc1_avg, enc2_avg; //дані з енкодера for averaging
unsigned char stat_bits; //статус биты OCF COF LIN
unsigned char n;
unsigned char DataTxUART[8]; //8 byte for TX
static bit flag1_error; //flag1 oszibok
static bit flag2_error; //flag2 oszibok
///////////////////////////////MAIN///////////////////////////////
void main (void) //Osnownyj cykl
{
Init_Device(); //Inicializacia kontrolera
//-------------------Початкові установки -----------------------
CS_1 = 1; //dla Az-encodera
CLK_1 = 1;
CS_2 = 1; //dla El-encodera
CLK_2 = 1;
del_500ms(); // na perechidni procesy
while(1) //---------- mean loop --------------------------
{
enc1_avg=0; // обнуляем 1
enc2_avg=0; // обнуляем 2
flag1_error = 0; // обнуляем flag1 oszibok
flag2_error = 0; // обнуляем flag2 oszibok
for (n=0;n<8;n++) read_encod_1(); //useredniujem 8 raz
enc1_avg = enc1_avg >> 3; //here enc1_avg - averaging in 8 time
for (n=0;n<8;n++) read_encod_2(); //useredniujem 8 raz
enc2_avg = enc2_avg >> 3; //here enc2_avg - averaging in 8 time
TXDataUART(); //prepare data
for (n=0;n<8;n++) //Here send to UART 8 byte
{
SBUF0=DataTxUART[n];
while (!TI0); //wait while the transmission is going on
TI0=0; //Clear TI0
}
}
}
//*****************************************************
//------------Процедура зчитування енкодера N1---------
//-----------------------------------------------------
void read_encod_1(void)
{
unsigned char i;
stat_bits=0; // обнуляем
enc_1=0;
CS_1 = 0; //устанавливаем 0 CS1
del_10us(); //delay, ne menee 500ns
CLK_1 = 0; // устанавливаем 0 CLK
del_10us(); //delay, ne menee 500ns
for (i=0;i<12;i++)
{
CLK_1 = 1; // устанавливаем 1 CLK
del_10us(); //delay, ne menee 500ns
if (DO_1 == 1) //Czytajemo
{
enc_1=((0x0800>>i)|enc_1);
}
CLK_1 = 0; // устанавливаем 0 CLK
del_10us(); //delay, ne menee 500ns
}
for(i=0;i<3;i++) // Проверяем статус биты OCF COF LIN
{
CLK_1 = 1; // устанавливаем 1 CLK
del_10us(); //delay, ne menee 500ns
if (DO_1 == 1) //
{
stat_bits=((0x04>>i)|stat_bits);
}
CLK_1 = 0; // устанавливаем 0 CLK
del_10us(); //delay, ne menee 500ns
}
CS_1 = 1; // устанавливаем 1 CS
del_10us(); //delay, ne menee 500ns
if (stat_bits == 0x04) //0000 0100 проверка правильности биты OCF COF LIN
{
enc1_avg = enc1_avg + enc_1;
}
else flag1_error = 1;
}
//*****************************************************
//------------Процедура зчитування енкодера N2---------
//-----------------------------------------------------
void read_encod_2(void)
{
unsigned char i;
stat_bits=0; // обнуляем
enc_2=0;
CS_2 = 0; //устанавливаем 0 CS1
del_10us(); //delay, ne menee 500ns
CLK_2 = 0; // устанавливаем 0 CLK
del_10us(); //delay, ne menee 500ns
for (i=0;i<12;i++)
{
CLK_2 = 1; // устанавливаем 1 CLK
del_10us(); //delay, ne menee 500ns
if (DO_2 == 1) //Czytajemo
{
enc_2=((0x0800>>i)|enc_2);
}
CLK_2 = 0; // устанавливаем 0 CLK
del_10us(); //delay, ne menee 500ns
}
for(i=0;i<3;i++) // Проверяем статус биты OCF COF LIN
{
CLK_2 = 1; // устанавливаем 1 CLK
del_10us(); //delay, ne menee 500ns
if (DO_2 == 1) //
{
stat_bits=((0x04>>i)|stat_bits);
}
CLK_2 = 0; // устанавливаем 0 CLK
del_10us(); //delay, ne menee 500ns
}
CS_2 = 1; // устанавливаем 1 CS
del_10us(); //delay, ne menee 500ns
if (stat_bits == 0x04) //0000 0100 проверка правильности биты OCF COF LIN
{
enc2_avg = enc2_avg + enc_2;
}
else flag2_error = 1;
}
//---------------------------------------------
//////////////// Zaderzky ///////////////////
void del_10us(void) //~10 us
{
unsigned char j;
for (j = 0; j < 60; j++);
}
void del_500ms(void) ///~500 ms
{
unsigned char l,m,n;
for (l = 0; l < 38; l++)
{
for (m = 0; m < 255; m++)
{
for (n = 0; n < 255; n++);
}
}
}
///////////////////////////////////////////////////////////////
// --------------- Prepary data to send -----------------------
//-------------------------------------------------------------
void TXDataUART(void)
{
DataTxUART[0]= 0xE1;
DataTxUART[1]= 0xE1;
if(flag1_error) //jesli byla oszibka wo wremija sczitywanija
{
DataTxUART[2]= 0xEE; //MSB
DataTxUART[3]= 0xEE; //LSB
}
else
{
DataTxUART[2]= (enc1_avg & 0xFF00)>>8; //MSB
DataTxUART[3]= enc1_avg & 0x00FF; //LSB
}
DataTxUART[4]= 0xE2;
DataTxUART[5]= 0xE2;
if(flag2_error) //jesli byla oszibka wo wremija sczitywanija
{
DataTxUART[6]= 0xEE; //MSB
DataTxUART[7]= 0xEE; //LSB
}
else
{
DataTxUART[6]= (enc2_avg & 0xFF00)>>8; //MSB
DataTxUART[7]= enc2_avg & 0x00FF; //LSB
}
}
//-----------------------------------------------------------------------------
// UART0_Interrupt
//-----------------------------------------------------------------------------
void uart_tx(void) interrupt 4
{
if (RI0 == 1) // Check if receive flag is set
{
RI0 = 0; // Clear interrupt flag RX
}
if (TI0 == 1) // Check if transmit flag is set
{
//TI0 = 0; //Clear interrupt flag TX
}
}
//-----------------------------------------------------------------------------