- 打卡等级:常住居民II
- 打卡总天数:87
- 最近打卡:2025-04-30 10:02:32
已绑定手机
高级会员
- 积分
- 878
|
大佬们,我用STC89C51单片机读取TLC1549的数据很稳定,但是,我换成STC8A8K64D4单片机用同样的程序读取TLC1549的数据,就一直不停的变化呢,能不能帮我看看是哪儿的问题,谢谢了
主程序
#include "main.h"
#include <intrins.h>
#include "lcd1602.h"
#include "tlc1549.h"
#include "math.h"
#define SUM 50
#define VREF 4933
unsigned char aa[7]={0,0,0,0,0,0,0};
void setport()
{
P0M1=0X00;P0M0=0X00;
P2M1=0X00;P2M0=0X00;
P1M1=0X00;P1M0=0X00;
P3M1=0X00;P3M0=0X00;
}
void delay(unsigned long t)
{
while(t--);
}
void main()
{
unsigned char i=0;
unsigned long int temp1,buff;
setport();
LCD_Initial();
i=SUM;
temp1=0;
CS = 1;
for(;;)
{
temp1+=ADConvert();
i--;
if(i==0)
{
temp1=temp1/SUM;
temp1=temp1*VREF;
temp1=temp1/1024;
buff=(temp1*100)/3271;
display(temp1,buff);
i=SUM;
temp1=0;
}
delay(100);
}
}
void display(unsigned long int d,unsigned long int y)
{
aa[0]=d/1000;
aa[1]=d%1000/100;
aa[2]=d%100/10;
aa[3]=d%10;//电压值
aa[4]=y/100;
aa[5]=y%100/10;
aa[6]=y%10;//百分比
Print(0,0,aa[0]+'0');
LCD_write(1,0,".");
Print(2,0,aa[1]+'0');
Print(3,0,aa[2]+'0');
Print(4,0,aa[3]+'0');
LCD_write(5,0,"v");
Print(2,1,aa[4]+'0');
Print(3,1,aa[5]+'0');
Print(4,1,aa[6]+'0');
LCD_write(5,1,"%");
}
TLC1549读数程序
#include "stc8.h"
#include "intrins.h"
//接口定义
sbit CS = P2^4;
sbit DA = P2^3;
sbit CK = P2^2;
//读取ADC结果函数
unsigned int ADConvert(void)
{
unsigned char ii;
unsigned int temp = 0;
CS = 0; //开启控制电路,使能DA和CK IO引脚;
for(ii=0;ii<10;ii++) //采集10次 ,即10bit
{
CK = 0;
temp <<= 1;
if(DA) temp++;
CK = 1;
}
CS = 1;
return(temp);
}
|
|