基于MSP430F149和ATT7022B的三相多功能电表的设计_第1页
基于MSP430F149和ATT7022B的三相多功能电表的设计_第2页
基于MSP430F149和ATT7022B的三相多功能电表的设计_第3页
基于MSP430F149和ATT7022B的三相多功能电表的设计_第4页
基于MSP430F149和ATT7022B的三相多功能电表的设计_第5页
已阅读5页,还剩66页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 1.原理图2.程序/主程序/*功能:按 KEY1 键切换显示 /P2.7-ASIG/ SPI_CS-P3.0 SPI_DO-P3.1 ATT7022 is Do. SPI_DI-P3.2 ATT7022 is Di. SPI_CLK-P3.3 电能掉电保存 */#include"msp430x14x.h"#include"SPI.h"#include"KEY1_2_3.h"#include"lcd1602.h"#include"RF2401.h"/#include"fash.h&qu

2、ot;void MIMA_Set();void MIMA_Compare_1();int MIMA_Compare_2();char key; int i=0; int array4; int bb4; int s; int WNeng4=1,1,1,1; UDWORD ActWrk24;/use to read UDWORD ReaWrk24; /char key1_count=0;void init_sysclk() unsigned char i; BCSCTL1 &=XT2OFF; do IFG1 &=OFIFG; for(i=0;i<0xee;i+); whil

3、e(IFG1&OFIFG); BCSCTL2 |=SELM_2;/MCLK 8M BCSCTL2 |=SELS;/SMCLK 8M/*/ 温度传感器程序部分/*/=DS18B20端口=#define OUT_DS18B20_0 P6OUT &=BIT7 /输出0#define OUT_DS18B20_1 P6OUT |= BIT7 /输出1#define DS18B20_IN P6DIR &=BIT7 /方向设置为输入#define DS18B20_OUT P6DIR |= BIT7 /方向设置为输出/=DS18B20寄存器指令=#define DS18B20_READ

4、_ROM 0x33#define DS18B20_MATCH_ROM 0X55#define DS18B20_SKIP_ROM 0XCC#define DS18B20_SEARCH_ROM 0XF0#define DS18B20_ALARM_SEARCH_ROM 0XEC#define DS18B20_WRITE_RAM 0X40#define DS18B20_READ_RAM 0XBE#define DS18B20_COPY_RAM 0X48 #define DS18B20_CONVERT_TEM 0X44#define DS18B20_EECALL_EEPROM 0XB8#define D

5、S18B20_READ_POWER_SUPPLY 0XB4/=DS18B20相关变量=char temp_data2=0x00,0x00;char dispaly32;/=void DS18B20_IO_set(void) P6SEL&=0x7f; /=DS18B20延时= void DS1820_delay(int i)while(i-);/=长延时=void Delay(int s)unsigned int i,j;for(i=0; i<s; i+);for(j=0; j<s; j+);/=DS18B20复位=void ds18b20_reset(void) DS18B

6、20_OUT; / OUT_DS18B20_1; OUT_DS18B20_0; DS1820_delay(3000); /500us OUT_DS18B20_1; DS18B20_IN; /设置为输入 DS1820_delay(40); while(P6IN&BIT7); DS18B20_OUT; OUT_DS18B20_1; DS1820_delay(300); /=DS18B20读操作函数=char ds18b20_read(void) char i; char temp=0; for(i=0;i<8;i+) DS18B20_OUT; OUT_DS18B20_0; temp&

7、gt;>=1; OUT_DS18B20_1; DS18B20_IN; if(P6IN&BIT7) temp|=0x80; DS1820_delay(4); DS18B20_OUT; DS1820_delay(50); return (temp);/=DS18B20写操作函数=void ds18b20_write(char Date) char i; DS18B20_OUT; for(i=0;i<8;i+) OUT_DS18B20_0; / DS1820_delay(6); if(Date & 0x01) OUT_DS18B20_1; else OUT_DS18B20

8、_0; DS1820_delay(40); OUT_DS18B20_1; Date>>=1; DS1820_delay(8); /=DS18B20温度转换函数=void Ds18b20Convert(void) ds18b20_reset(); ds18b20_write(0xCC);/DS18B20_SKIP_ROM ds18b20_write(0x44);/DS18B20_CONVERT_TEM Delay(200); ds18b20_reset(); ds18b20_write(0xCC); /DS18B20_SKIP_ROM ds18b20_write(0xBE); /DS

9、18B20_READ_RAM temp_data0=ds18b20_read();/先读转换结果的低八位 temp_data1=ds18b20_read();/读转换结果的高八位/=温度换算处理=void work_temp()char n=0; if(temp_data1>127)temp_data1=(256-temp_data1);/负值temp_data0=(256-temp_data0);n=1;dispaly6=(temp_data0&0xf0)>>4)|(temp_data1&0x0f)<<4);dispaly5=dispaly6/1

10、00;/百位dispaly4=dispaly6%100;/dispaly2=dispaly4/10;/十位dispaly1=dispaly4%10;/个位switch(temp_data0&0x0f)/小数位case 0x0f:dispaly0=9;break;case 0x0e:dispaly0=9;break;case 0x0d:dispaly0=8;break;case 0x0c:dispaly0=8;break;case 0x0b:dispaly0=7;break;case 0x0a:dispaly0=6;break;case 0x09:dispaly0=6;break;cas

11、e 0x08:dispaly0=5;break;case 0x07:dispaly0=4;break;case 0x06:dispaly0=4;break;case 0x05:dispaly0=3;break;case 0x04:dispaly0=3;break;case 0x03:dispaly0=2;break;case 0x02:dispaly0=1;break;case 0x01:dispaly0=1;break;case 0x00:dispaly0=1;break;default:break; if(n)/负值时显示aa,正直显示dddispaly3=11;/ else dispal

12、y3=10;/*ds18b20 结束/=void ATT7022_init() /送校表数据子函数 spi_write_parameter(0x2A,1); spi_write_parameter(0x20,0x000078); spi_write_parameter(0x1E,0x02E72E); spi_write_parameter(0x02,0); spi_write_parameter(0x03,0); spi_write_parameter(0x04,0); spi_write_parameter(0x05,0x02e72e); spi_write_parameter(0x06,0

13、x40ded2); spi_write_parameter(0x07,0x39374b); spi_write_parameter(0x08,0x3e353f); spi_write_parameter(0x09,0x48f27b); spi_write_parameter(0x0a,0x422d0e); spi_write_parameter(0x0b,0x48f5c2); spi_write_parameter(0x1b,0x159e83); spi_write_parameter(0x1c,0x114e3b); spi_write_parameter(0x1d,0x135a85); sp

14、i_write_parameter(0x26,0xe5c91); spi_write_parameter(0x27,0xca841); spi_write_parameter(0x28,0xccccc);/*void ProcSec(void) P2DIR &=ASIG; /P2.7 if(P2IN & ASIG)=0) ATT7022Reset();ATT7022_init();/送较表数据 /复位时对1,2,3相,总,有功,无功电能赋上次电表累积的值 spi_write_parameter(0xc9,0x000001);/关闭SPI写操作 ReadReal();*/*主函数

15、char disp_flag=1;void main(void) char key,i;WDTCTL=WDT_ADLY_1000; /系统初始化 P6DIR |=BIT1+BIT2; P6OUT &=(BIT1+BIT2); RF24L01_IO_set(); DS18B20_IO_set(); init_sysclk(); init_NRF24L01(); FCTL2=FWKEY+FSSEL_1+FN3+FN4; /flash操作选时钟 init_lcd1602(); init_key(); init_usart(); / for(i=0;i<4;i+) /读flash rom

16、 里的电能 read_awrkflash(i); read_rwrkflash(i); / ATT7022Reset(); /复位 ATT7022_init(); /校表 /spi_write_parameter(0x30,0X005678); spi_write_parameter(0xc9,0x000001);/关闭SPI写操作 Ds18b20Convert(); /读温度传感器的值 IE1 |=WDTIE; _EINT();while(1) LPM3; /以下程序每 1/16 s 执行一次 /ProcSec(); ReadReal(); Ds18b20Convert(); /读温度传感器

17、的值 work_temp(); /温度读数数据处理 key=getkey(); /查键值 if(key=push_key1)/key=push_key1 key1_count+; if(key1_count=22)key1_count=0; P6OUT =BIT2; if(key=push_key2) disp_flag=0; P4OUT=0X01; order(); P4OUT=0X80; order(); i=0; while(1) key=getkey(); if(key=push_key1) arrayi=0; write_char('*'); i+; if(key=p

18、ush_key2) arrayi=1; write_char('*'); i+; if(key=push_key3) arrayi=2; write_char('*'); i+; if(i=4) MIMA_Compare_1(); disp_flag=1; break; /*密码设置 密码设置 密码设置 */ /*改密模式*/ if(key=push_key3) disp_flag=0; i=0; P4OUT=0X01; order(); P4OUT=0X80; order(); while(1) key=getkey(); if(key=push_key1)

19、arrayi=0; write_char('*'); i+; if(key=push_key2) arrayi=1; write_char('*'); i+; if(key=push_key3) arrayi=2; write_char('*'); i+; if(i=4) s = MIMA_Compare_2(); if(s=0) disp_flag=1; break; if(s=1) MIMA_Set(); disp_flag=1; break; / void MIMA_Set() int Mm4; i=0; P4OUT=0X01; order

20、(); P4OUT=0X80; order(); while(1) key=getkey(); if(key=push_key1) Mmi=0; write_char('0');/ flash_ww(0x1000,unsigned intMmi ); i+; if(key=push_key2) Mmi=1; write_char('1'); /Flash_RecordWord(ai); i+; if(key=push_key3) Mmi=2; write_char('2'); / Flash_RecordWord(ai); i+; if(i=4)

21、 flash_clr(0x1000); for(i=0;i<4;i+) flash_ww(0x1000+i*2),(unsigned int) Mmi ); break; /while void MIMA_Compare_1() int j; for(j=0;j<4;j+) bbj= flash_rw(0x1000+j*2); if(arrayj!=bbj) break; if(j=4&&array3=bb3) flash_clr(0x1080);/flash 清零 ATT7022Reset(); /复位 ATT7022_init(); /校表 spi_write_

22、parameter(0xc9,0x000001);/关闭SPI写操作 ActWrk20=0;/清零 ActWrk21=0; ActWrk22=0; ActWrk23=0; ReaWrk20=0; ReaWrk21=0; ReaWrk22=0; ReaWrk23=0; P4OUT=0X01; order(); P4OUT=0X80; order(); write_char('S');write_char('e');write_char('c');write_char('c'); write_char('e');wri

23、te_char('s');write_char('s'); else P4OUT=0X01; order(); P4OUT=0X80; order(); write_char('F');write_char('a');write_char('i');write_char('l');/fail /*核对密码 (改密用)*/int MIMA_Compare_2() int j; for(j=0;j<4;j+) /bbj= Flash_Read(j); if(arrayj!=WNengj) brea

24、k; if(j=4&&array3=WNeng3) / Display_WELCOM(); return 1; else return 0; /SPI.C/#include"msp430x14x.h"#include"lcd1602.h"#include"fash.h"#include"RF2401.h"/ Data type define /*typedef unsigned char BCD11;typedef unsigned char BCD22;typedef unsigned char

25、BCD33;typedef unsigned char BCD44;typedef unsigned char BCD55;typedef unsigned char BCD66;typedef unsigned char BCD77;typedef char BYTE;typedef int WORD;typedef long DWORD;typedef unsigned charUBYTE;typedef unsigned intUWORD;typedef unsigned longUDWORD;/*/ define SPI port-> P2.0P2.3#defineSPI_CS0

26、x01/ P2.0O/#defineSPI_DO0x02/ P2.1O| ATT7022 is Do.#defineSPI_DI0x04/ P2.2I| ATT7022 is Di.#defineSPI_CLK0x08/ P2.3O/#definePhsA0#definePhsB1#definePhsC2#definePhsAll3#define _10_bit0x400#define _15_bit0x8000#define _16_bit0x10000#define _17_bit0x20000#define _23_bit0x800000#define _24_bit0x1000000/

27、*/较表数据寄存器地址const UBYTE CheckMeterAddr36=/- 相位补偿区域设置: 0 0x02, 0x03, 0x04, 0x05,/- 功率增益: 4 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,/- 相位校正: 10 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, /15 0x12, 0x13, 0x14, 0x15, 0x16, /20 0x17, 0x18, 0x19, 0x1A,/- 电压校正: 25 0x1B, 0x1C, 0x1D,/- 比差补偿区域设置: 28 0x1E,/- 启动电流: 29 0x1F,

28、/- 脉冲输出设置:30 0x20, /0x21,/- 电流校正: 31 0x26, 0x27, 0x28,/断相阈值电压设置 0x29, /35/合相能量累加模式(代数加/绝对值) 0x2A ;*/读取的数据存储变量typedef struct UDWORD ActPwr4;UDWORD ReaPwr4;UDWORD AppPwr4;UDWORD UU3;BCD2 U3;UDWORD II4;BCD2 I4;UDWORD Fct4;BCD2 Factor4;UDWORD Phs4;BCD2 Phss4;BCD4 ActE4;BCD4 ReaE4;UDWORD SpiStatus1;UDWOR

29、D SpiStatus2;UDWORD SpiReg1;UDWORD SpiReg2;UDWORD Freq;BCD2 MainF; UDWORD ActWrk4;/use to display UDWORD ReaWrk4; /UDWORD ActWrk24;/use to read /UDWORD ReaWrk24; UDWORD ActWrk34;/use to store UDWORD ReaWrk34; UDWORD xiangxu;SPIRegister;/*/ global data /*typedef struct SPIRegister SPI;UBYTE WakeFlg;U

30、BYTE ClkFlg;UBYTE bt_int;DRAM;DRAM DM;/void delayus(unsigned int dly) char i; unsigned int j; for(j=0;j<dly;j+) for(i=0;i<8;i+);unsigned long spi_read_parameter(unsigned char Addr)/基本spi读函数 unsigned long tmpData=0; unsigned char i; P2DIR |=(BIT0+BIT2+BIT3); P2DIR &=BIT1; P2OUT |= SPI_CS; P

31、2OUT &=SPI_CLK; _NOP(); _NOP(); _NOP(); P2OUT &=SPI_CS; _NOP(); / write address to SPI device.for(i=1;i<=8;i+)P2OUT |= SPI_CLK;_NOP();_NOP();if (Addr&0x80)=0x80) P2OUT |= SPI_DI;else P2OUT &=SPI_DI;Addr <<= 1;_NOP();_NOP();P2OUT &=SPI_CLK;_NOP();_NOP(); / read data from

32、SPI device. delayus(3); tmpData = 0;for(i=1;i<=24;i+)P2OUT |= SPI_CLK;_NOP(); _NOP();tmpData <<= 1;if (P2IN&SPI_DO)=SPI_DO) tmpData |= 0x0001;else tmpData &=0x0001;_NOP();_NOP();P2OUT &=SPI_CLK; _NOP(); P2OUT |= SPI_CS; return tmpData;void spi_write_parameter(UBYTE Addr, UDWORD

33、Data)/基本spi写函数 UBYTE i; P2DIR |=(BIT0+BIT2+BIT3); P2DIR &=BIT1; P2OUT |= SPI_CS; P2OUT &=SPI_CLK; _NOP(); _NOP(); _NOP(); P2OUT &=SPI_CS; _NOP(); Addr |= 0x80;/ write address to SPI device.最高位置1,表示写命令for(i=1;i<=8;i+)P2OUT |= SPI_CLK;_NOP();_NOP();if (Addr&0x80)=0x80) P2OUT |= SPI_DI;else P2OUT &=SPI_DI;Addr <<= 1;_NOP();_NOP();P2OUT &=SPI_CLK;_NOP();_NOP(); / write data to SPI device.for(i=1;i<=24;i+)P2OUT |= SPI_CLK;_NOP();_NOP();if (Data&0x800000)=0x

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论