基于MSP430F149和ATT7022B的三相多功能電表的設(shè)計_第1頁
基于MSP430F149和ATT7022B的三相多功能電表的設(shè)計_第2頁
基于MSP430F149和ATT7022B的三相多功能電表的設(shè)計_第3頁
基于MSP430F149和ATT7022B的三相多功能電表的設(shè)計_第4頁
基于MSP430F149和ATT7022B的三相多功能電表的設(shè)計_第5頁
已閱讀5頁,還剩66頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

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 /方向設(shè)置為輸入#define DS18B20_OUT P6DIR |= BIT7 /方向設(shè)置為輸出/=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相關(guān)變量=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復(fù)位=void ds18b20_reset(void) DS18B

6、20_OUT; / OUT_DS18B20_1; OUT_DS18B20_0; DS1820_delay(3000); /500us OUT_DS18B20_1; DS18B20_IN; /設(shè)置為輸入 DS1820_delay(40); while(P6IN&BIT7); DS18B20_OUT; OUT_DS18B20_1; DS1820_delay(300); /=DS18B20讀操作函數(shù)=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寫操作函數(shù)=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溫度轉(zhuǎn)換函數(shù)=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();/先讀轉(zhuǎn)換結(jié)果的低八位 temp_data1=ds18b20_read();/讀轉(zhuǎn)換結(jié)果的高八位/=溫度換算處理=void work_temp()char n=0; if(temp_data1>127)temp_data1=(256-temp_data1);/負(fù)值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)/小數(shù)位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)/負(fù)值時顯示aa,正直顯示dddispaly3=11;/ else dispal

12、y3=10;/*ds18b20 結(jié)束/=void ATT7022_init() /送校表數(shù)據(jù)子函數(shù) 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();/送較表數(shù)據(jù) /復(fù)位時對1,2,3相,總,有功,無功電能賦上次電表累積的值 spi_write_parameter(0xc9,0x000001);/關(guān)閉SPI寫操作 ReadReal();*/*主函數(shù)

15、char disp_flag=1;void main(void) char key,i;WDTCTL=WDT_ADLY_1000; /系統(tǒng)初始化 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(); /復(fù)位 ATT7022_init(); /校表 /spi_write_parameter(0x30,0X005678); spi_write_parameter(0xc9,0x000001);/關(guān)閉SPI寫操作 Ds18b20Convert(); /讀溫度傳感器的值 IE1 |=WDTIE; _EINT();while(1) LPM3; /以下程序每 1/16 s 執(zhí)行一次 /ProcSec(); ReadReal(); Ds18b20Convert(); /讀溫度傳感器

17、的值 work_temp(); /溫度讀數(shù)數(shù)據(jù)處理 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; /*密碼設(shè)置 密碼設(shè)置 密碼設(shè)置 */ /*改密模式*/ 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(); /復(fù)位 ATT7022_init(); /校表 spi_write_

22、parameter(0xc9,0x000001);/關(guān)閉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、*/較表數(shù)據(jù)寄存器地址const UBYTE CheckMeterAddr36=/- 相位補償區(qū)域設(shè)置: 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,/- 比差補償區(qū)域設(shè)置: 28 0x1E,/- 啟動電流: 29 0x1F,

28、/- 脈沖輸出設(shè)置:30 0x20, /0x21,/- 電流校正: 31 0x26, 0x27, 0x28,/斷相閾值電壓設(shè)置 0x29, /35/合相能量累加模式(代數(shù)加/絕對值) 0x2A ;*/讀取的數(shù)據(jù)存儲變量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讀函數(shù) 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寫函數(shù) 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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論