//pfm.c
#include <stmduino.h>
uchar per=0;
void datacame(){
pinHigh(C3);
pinmode(A1,INPUTFL);
uchar dbuf[24];
for(uchar i=0;i<24;i++)
{
//send clock
pinhigh(A2);
pinlow(A2);
//read out
dbuf=pinread(A1);
}
for(uchar k=0;k<3;k++)
{
//send code for next transfer
pinhigh(A2);
pinlow(A2);
}
pinmode(A1,INPUTWAITING);
if(per==0){
uchar buf[3]={0,0,0};
for(uchar b=0;b<3;b++)//24bit -> 3byte
{
for(uchar i=0;i<8;i++)
{
if(dbuf[b*8+i])//if this bit is 1
{
//set this bit of this buffer byte to 1
buf|=1<<(7-i);
}
}
}
//most significant 16bit to int
int value;
*(((uchar*)&value)+0)=buf[0];
*(((uchar*)&value)+1)=buf[1];
printf("%d",value);
endl();
}
pinlow(C3);
}
void main()
{
clock(16M);
cpudivider(1);
//connect D0 to A1
//connect CK to A2
pinmode(A1,INPUTWAITING);//data
pinmode(A2,OUTPUT);//clock
pinmode(C3,OUTPUT);//status pin
attach(datacame,A);
waitfor(A,FALL);
serial(115200);
interrupts();
while(1){
sleep();
//save power
}
}