本人初学
单片机 准备做一个显示角度传感器转过的角度的题目 手上拿到的角度传感器是0到180度 0到5伏的电压输出
程序如下
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit DSO=P1^0;
sbit DSI=P1^1;
sbit CS=P1^2;
sbit CLK=P1^3;
uchar code
tab[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
uchar code wei[]={0xf1,0xf2,0xf4,0xf8};
uint huancun[]={0x00,0x00,0x00,0x00};
void delay(uint m)
{
while(m--);
}
void display()
{
uchar i;
for(i=0;i<4;i++)
{
P2=wei
;
P0=tab[huancun];
delay(5);
}
}
uint Read_TLC2543(uchar CHN)
{
uchar i,temp;
uint ad=0;
CHN=CHN<<4;
CLK=0;
CS=1;
CS=0;
temp=CHN;
for(i=0;i<12;i++)
{
ad=ad<<1;
if((temp&0x80)!=0){DSI=1;}
else{DSI=0;}
if(DSO){ad=ad+1;}
CLK=1;
CLK=0;
temp=temp<<1;
}
CS=1;
ad=ad&0x0fff;
return(ad);
}
void main()
{
float sum;
uint date;
while(1)
{
sum=Read_TLC2543(0x00)*18.0;
date=sum/4095;
huancun[0]=date/1000;
huancun[1]=date%1000/100;
huancun[2]=date%1000%100/10;
huancun[3]=date%10;
display();
}
}
仿真得到的结果精度不够 何解
-
一周热门 更多>