STM32_ADE7758驱动
时间:11-11
来源:互联网
点击:
- d(ADD_AVAHR,16);
- temp_data[ADD_BVAHR-1]=ADE_Read(ADD_BVAHR,16);
- temp_data[ADD_CVAHR-1]=ADE_Read(ADD_CVAHR,16);
- for(i=0;i<9;i++)
- {
- if(temp_data[i]>0x7fff)
- temp_data[i]=0xffff-temp_data[i]+1;
- }
- if(divider>1)
- {
- for(i=0;i<9;i++)
- temp_data[i]=temp_data[i]*divider;//乘上分频器的值
- }
- //能量的计算
- for(i=0;i<9;i++)
- energy[i]+=temp_data[i];//累加电能值,单位为 WS(瓦秒)
- //转换成千瓦时
- for(i=0;i<3;i++)
- {
- working.watt_hour[i]+=(energy[i]/3600000);//转换成千瓦时
- energy[i]=energy[i]%3600000;
- }
- working.watt_hour[3]=working.watt_hour[0]+working.watt_hour[1]+working.watt_hour[2];//总和
- //转换成千伏安时
- for(i=0;i<3;i++)
- {
- working.va_hour[i]+=(energy[i+6]/3600000);//转换成千瓦时
- energy[i+6]=energy[i+6]%3600000;
- }
- working.va_hour[3]=working.va_hour[0]+working.va_hour[1]+working.va_hour[2];//总和
- for(working.watt[3]=0,i=0;i<3;i++)
- {
- working.watt[i]=temp_data[i]/1000;//千瓦
- working.watt[3]+=working.watt[i];
- }
- for(working.var[3]=0,i=0;i<3;i++)
- {
- working.var[i]=temp_data[i+3]/1000;
- working.var[3]+=working.var[i];
- }
- for(working.va[3]=0,i=0;i<3;i++)
- {
- working.va[i]=temp_data[i+6]/1000;//千伏安
- if(working.va[i]
- working.va[i]=working.watt[i];
- working.va[3]+=working.va[i];
- }
- }
- /**
- *功能:实时读取电流电压值
- */
- void ADE_ReadVC(void)
- {
- unsigned char i,j;
- for(i=0;i<3;i++)
- {
- working.voltage[i]=0;
- working.current[i]=0;
- }
- for(i=0;i<3;i++)
- {
- for(j=0;j<5;j++)
- {
- working.voltage[i]+=vo_buffer[j][i];
- working.current[i]+=io_buffer[j][i];
- }
- }
- for(i=0;i<3;i++)
- {
- working.voltage[i]=working.voltage[i]/5;
- working.current[i]=working.current[i]/5;
- }
- //电压电流的三相平均值
- working.voltage[3]=(working.voltage[0]+working.voltage[1]+working.voltage[2])/3;
- working.current[3]=(working.current[0]+working.current[1]+working.current[2])/3;
- printf("voltage=%x current=%x\n",working.voltage[0],working.current[0]);
- }
- /**
- *功能:从ADE7758中取出三相电压电流功率等电参量
- */
- void ADE_Update(void)
- {
- static unsigned char sample_cycle=0;//电压采样周期,5次取平均
- static unsigned char bit_3s=0;
- unsigned char j;
- if(!bWorkModel)//正常工作模式
- {
- if(bit_1s)
- {
- bit_1s=0;
- ADE_ReadHR();
- if((bit_3s++)>=3)/*三秒检测一次异常*/
- {
- //ADE_AuCheck();
- bit_3s=0;
- }
- }
- for(j=0;j<3;j++)
- {
- vo_buffer[sample_cycle][j]=ADE_Read(ADD_AVRMS+j,24)/*>>12*/;//voltage
- io_buffer[sample_cycle][j]=ADE_Read(ADD_AIRMS+j,24)/*>>13*/;//current
- }
- if(sample_cycle==4)/*读取5次取平均值*/
- ADE_ReadVC();
- }
- if(sample_cycle<4)
- sample_cycle+=1;
- else
- sample_cycle=0;
- }
- /**
- *测试硬件连接是否正确
- */
- u8 ADE_TestHard(void)
- {
- unsignedintrdata,wdata=0xaa5577;//AEHF=1,VAEHF=1,低8位无用
- u8 ret=0;
- ADE_Write(ADD_MASK,wdata,24);
- rdata=ADE_Read(ADD_MASK,24);//验证通讯是否有问题
- if(rdata!=wdata)
- printf("ADE error\r\n");
- else
- {
- ret=1;
- printf("ADE OK\r\n");
- }
- return ret;
- }
- /**
- *功能:7758初始化函数
- */
- void ADE_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(ADE_RCC,ENABLE);
- GPIO_InitStructure.GPIO_Pin=ADE_PIN;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//推挽输出
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//IO口速度为50MHz
- GPIO_Init(ADE_GPIO,&GPIO_InitStructure);
- ADE_CS(1);
- if(ADE_TestHard())
- {
- ADE_Write(ADD_OPMODE,0x44,8);//软件复位
- ADE_udelay();//添加延时 确保复位成功
- }
- }
- void ADE_thread_entry(void)
- {
- SPI2_Init();
- ADE_Init();
- while(1)
- {
- ADE_Update();
- delay_ms(50);/*等待,让出cpu权限,切换到其他线程*/
- }
- }
STM32ADE7758驱 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)