//程序功能:读取功率并且显示在数码管上。通过串口发送数据
//波特率是9200
/******************************************
程序的问题是:编译无错,但是利用数码管显示的时候显示:05.35,正确值是02.35
在没有负载情况下,应该是显示0000,但是数码管却一直显示05.35,实在找不出错误来
有时间给我看看,这个程序那个地方有错误
**********************************************************************/
#include
#define uchar unsigned char
#define uint unsigned int
int count1s;
unsigned int count1m;
unsigned char dis_bitcount=0;
unsigned char display_o[6];
unsigned char display_q[6];
unsigned char display_pf[6];
unsigned char display_temp[6];
unsigned char code MF52_stop[3] _at_ 0x003b; //
unsigned char SEG[30]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0xbf,0x00} ;//0,1,2,3,4,5,6,7,8,9 后面是带小数点的0~9
unsigned char dis_bitdriver=0;
static uchar idata ADE7753rw[3]={0,0,0}; //ADE7753读写寄存器
static uchar idata ADE7753tm[3]={0,0,0}; //ADE7753读出状态
***it reset_7753=P1^1;
***it CSADE7753_A = P1^5;
***it SSDI = P1^2;
***it SSDO = P1^3;
***it SSCK = P1^4;
***it INT7753 = P3^2;
***it s1=P2^3;
unsigned int pf,tem;
#define FREQ 4474431
unsigned int VAENERGY;
// 延时子函数 1mS乘以count
void delay1ms(int count)
{
int j,k;
while(count--!=0)
{ for(j=0;j<9;j++)
for(k=0;k < 15; k++)
;}
}
//延时1us子函数
void nop_1us(void)
{
uchar i;
for(i = 0;i< 2; i++)
;
}
send( unsigned char a)
{
TI = 0;
SBUF = a;
while(!TI);
}
/***************************************
ADE7753读数据函数
入口参数:type:目标寄存器的地址
wdata:写进寄存器的内容
databit:目标寄存器的宽度
出口参数: NULL
返回值:LULL
*********************************/
void write7753(unsigned char type,unsigned int wdata,unsigned char databit)
{
unsigned char loop = 0;
type = type & 0x7F;
type = type | 0x80;
for(loop = 0; loop < 8; loop++)
{
SSCK = 1;
nop_1us();
SSDI = type & 0X80;
nop_1us();
SSCK = 0;
nop_1us();
type = (type << 1);
}//end of for()
delay1ms(4);
for(loop = 0; loop < databit; loop++)
{
SSCK = 1;
nop_1us();
SSDI = wdata & 0X8000;
nop_1us();
SSCK = 0;
nop_1us();
wdata = (wdata << 1);
}//end of for()
}//end of write7753()
/***********************************************
7758读寄存器函数
入口参数:type:目标寄存器的地址
databit:目标寄存器的宽度
出口参数:指定寄存器的内容
返回值:指定寄存器的内容
**************************************************/
unsigned long read7753(unsigned char type,unsigned char databit)
{
unsigned char loop = 0;
unsigned long rtdata = 0;
unsigned int i;
//ade7758 7 bits address
//ade7754 6 bits address
type = type & 0x7F;
type = type | 0x00;
for(loop = 0;loop < 8;loop ++)
{
SSCK = 1;
nop_1us();
SSDI = type & 0X80;
nop_1us();
SSCK = 0;
nop_1us();
type = (type << 1);
}
for(i=0;i<8;i++) //延长8us
{ nop_1us();
}
for(loop = 0;loop < databit;loop ++)
{
CSADE7753_A = 1;
nop_1us();
rtdata = (rtdata << 1);
if(SSDO) rtdata += 1;
nop_1us();
SSCK = 0;
nop_1us();
}
return(rtdata);
CSADE7753_A = 1;
}//end of read7753()
/*******************************************************************************************
7758读寄存器函数
入口参数:type:目标寄存器的地址
databit:目标寄存器的宽度
出口参数:指定寄存器的内容
返回值:指定寄存器的内容
该函数与 read7753(unsigned char type,unsigned char databit)的区别在于操作ADE7758之前先确定一下
SPI接口的各个状态,所以当需要从ADE7758中读出一个数据时不要直接调用
read7753(unsigned char type,unsigned char databit)而是直接调用本函数。
********************************************************************************************/
unsigned long read7753a(unsigned char type,unsigned char databit)
{
unsigned long rtdata = 0;
//EA = 0;//disable interrupt
CSADE7753_A = 1;
SSCK = 0;
SSDI = 0;
SSDO = 0;
CSADE7753_A = 0;
rtdata = read7753(type,databit);
CSADE7753_A = 1;
//EA = 1;//enable interrupt
return(rtdata);
}//end of read7753a()
/*****************************************************************
7758写数据函数
入口参数:type:目标寄存器的地址
wdata:写进寄存器的内容
databit:目标寄存器的宽度
出口参数:NULL
返回值:NULL
注意写入数据的数据类型对写入数据的影响,参数:wdata
wdata数据类型为整型,2个字节当形参的实参为字符型,1个字节时要先将
它强制转换为整型再左移8位,databit为8。当wdata字长为12位时,必须先
将它扩展为16位,并且第一字节的高半字节无任何意义.
******************************************************************/
void write7753a(unsigned char type,unsigned int wdata,unsigned char databit)
{
//EA = 0;//disable interrupt
CSADE7753_A = 1;
SSCK = 0;
SSDI = 0;
SSDO = 0;
CSADE7753_A = 0;
write7753(type,wdata,databit);
CSADE7753_A = 1;
//EA = 1;//enable interrupt
}//end of write7753a()
/*****************************************
对电能功率的读取函数
*********************************************/
void ceshi(void)
{
unsigned long temp;
unsigned int temp1,temp2;
unsigned char bw,sw,gw,qsw;
CSADE7753_A = 0;
temp = read7753(0x06,16 );
CSADE7753_A = 1;
send(temp);
// temp1=temp<<8;
// temp2=temp>>8;
VAENERGY = temp;
// VAENERGY =temp1+temp2;
bw=( VAENERGY/100 )% 10;
display_pf[1]=bw+10;
sw=( VAENERGY/10 )% 10;
display_pf[2]=sw;
gw=( VAENERGY )%10;
display_pf[3]=gw;
}
void init()
{
SCON = 0x50; /* mode 1: 8-bit UART, enable receiver */
TMOD= 0x20; /* timer 1 mode 2: 8-Bit reload */
PCON=PCON|0x80;
TH1= 0xfa; /* reload value 9600 baud /11.059M */
TR1= 1; /* timer 1 run */
TI=1;
}
main()
{
unsigned char p2code=0;
count1m=0;
count1s=0;
delay1ms(100);
// init_7753();
init();
while(1)
{
switch(dis_bitcount+1)
{
case 1:dis_bitdriver=0x01; break;/*display 1*/
case 2:dis_bitdriver=0x02; break;/*display 2*/
case 3:dis_bitdriver=0x04; break;/*display 3*/
default:dis_bitdriver=0x08; break;/*display 4 */
}
// p2code=P1;
p2code=p2code&0xF0;
p2code=p2code|dis_bitdriver;
// dcode=SEG[display_q[dis_bitcount]];
P0=SEG[display_q[dis_bitcount]];
P2=p2code<<4;
if(dis_bitcount>=3) dis_bitcount=0;
else dis_bitcount++;
count1s++;
if(count1s>=260)
{
count1m++;
count1s=0;
ceshi(); //测试
}
else
{
delay1ms(4);
}
display_q[3]=display_pf[3];
display_q[2]=display_pf[2];
display_q[1]=display_pf[1];
display_q[0]=display_pf[0];
}
}
|