em có đồ án làm đồng hồ số đo các đại lượng điện 1 pha U,I,P,Q,cos bằng con ADE7753 và ATMEGA32. Code của em sao bị lỗi chỗ I2C là sao ạ mong các anh chỉ giáo>>>>>>>>>>ADE.pdfADE.pdf
còn đây là code
còn đây là code
Code:
#include <mega32.h> #include <stdio.h> #include <math.h> #include <delay.h> #include <spi.h> #define CLK1 PORTB.0 #define STR PORTB.2 #define DATA PORTB.3 #define RS PORTC.7 #define EN PORTC.6 #define menu PIND.3 #define up PIND.4 #define down PIND.5 #define run PIND.6 #define ok PIND.7 #define rst PORTB.1 #define DOUT PINB.6 #define CLK PORTB.7 #define IN PORTB.5 #define CS PORTB.4 //Dia chi cac thanh ghi SPI_ADE7753 #define WAVEFORM 0x01,3 #define AENERGY 0x02,3 #define RAENERGY 0x03,3 #define LAENERGY 0x04,3 #define VAENERGY 0x05,3 #define RVAENERGY 0x06,3 #define LVAENERGY 0x07,3 #define LVARENERGY 0x08,3 #define MODE 0x09,2 #define IRQEN 0x0A,2 #define STATUS 0x0B,2 #define RSTSTATUS 0x0C,2 #define CH1OS 0x0D,1 #define CH2OS 0x0E,1 #define GAIN 0x0F,1 #define PHCAL 0x10,1 #define APOS 0x11,2 #define WGAIN 0x12,2 #define WDIV 0x13,1 #define CFNUM 0x14,2 #define CFDEN 0x15,2 #define IRMS 0x16,3 #define VRMS 0x17,3 #define IRMSOS 0x18,2 #define VRMSOS 0x19,2 #define VAGAIN 0x1A,2 #define VADIV 0x1B,1 #define LINECYC 0x1C,2 #define ZXTOUT 0x1D,2 #define SAGCYC 0x1E,1 #define SAGLVL 0x1F,1 #define IPKLVL 0x20,1 #define VPKLVL 0x21,1 #define IPEAK 0x22,3 #define RSTIPEAK 0x23,3 #define VPEAK 0x24,3 #define RSTVPEAK 0x25,3 #define TEMP 0x26,1 #define PERIOD 0x27,2 #define TMODE 0x3D,1 #define CHKSUM 0x3E,1 #define DIEREV 0x3F,1 void spi_t(unsigned char data7753); unsigned char spi_r(void); void ade7753_write(char addr,char sobyte,char data1,char data2,char data3); unsigned long int ade7753_read(char addr, char sobyte); void ade7753_write6(char addr,char data7753); unsigned char ade7753_read6(char addr); void ade7753_write12(char addr,char data7753); unsigned long int ade7753_read12(char addr); void ade7753_write15(char addr,char data7753); unsigned long int ade7753_read15(char addr); void ade7753_init(void); //////////////////////HIEN THI RA LCD//////////////////////////////////// void write_c(unsigned char LCD_c)//ghi lenh { delay_ms(10); PORTA=LCD_c; RS=0; EN=1; delay_us(50); EN=0; delay_us(50); } void write_data(unsigned char LCD_data)//ghi du lieu { delay_ms(10); if(LCD_data=='\n') {write_c(0xc0);return;} PORTA=LCD_data; RS=1; EN=1; delay_us(50); EN=0; delay_us(50); } void write_string(flash char *s)//ghi chuoi du lieu { while(*s) { write_data(*s); s++; } } void write_num(unsigned long int number)//ghi so nguyen { int j,m; unsigned char d[10]; for(j=0 ; j<10 ; j++){ d[j] = number%10; number /= 10; if(number==0){ m = j; break; } } for(j=m ; j>=0 ; j--) write_data(d[j]+0x30); } void init(void)//khoi tao LCD,che do 8bit,font 5x7,con tro { write_c(0x03); write_c(0x38); write_c(0x0e); write_c(0x06); write_c(0x01); } /////////////////////HIEN THI RA LED////////////////////////////////////// flash unsigned char bcdled7[10]={0x14,0x9f,0x52,0x13,0x99,0x31,0x30,0x1f,0x10,0x11};//ma led 7doan void out_1led(unsigned char a)//xuat du lieu ra 1led { int ii; for(ii=0;ii<8;ii++) { if(a&0x80) //0x01 neu la dich phai DATA=1; else DATA=0; CLK1=1; delay_us(5); CLK1=0; a=a<<1; } } void tatled(void) { int j=0; for(j=0 ; j<8 ; j++) out_1led(0xff); STR=1; delay_us(2); STR=0; } void out_num(unsigned long int number)//xuat du lieu la 1 so nguyen ra 6 led { int j,m; unsigned char d[10]; for(j=0 ; j<10 ; j++){ d[j] = number%10; number /= 10; if(number==0){ m = j; break; } } tatled(); for(j=m ; j>=0 ; j--) out_1led(bcdled7[d[j]]); STR=1; delay_us(5); STR=0; } ///////////////////////INTERRUP////////////////////////////////// unsigned long int irq=0; int mod=1; interrupt [EXT_INT0] void ext_int0_isr(void) { // Place your code here } void key(void)//chon che do do { char temp=1; write_c(0x01);//xoa LCD write_c(0x80);//hien thi dau dong thu nhat write_string(" Chon che do do "); while(1) { if(!run){write_c(0x01);break;} if(!up){mod++;if(mod>4)mod=1;temp=1;} if(!down){mod--;if(mod<1)mod=4;temp=1;} if(temp==1) { write_c(0xc0);//hien thi dau dong thu 2 switch(mod) { case 1:{write_string("1.DO Urms(V) ");temp=0;break;} case 2:{write_string("2.DO Irms(mA) ");temp=0;break;} case 3:{write_string("3.DO P(W) ");temp=0;break;} case 4:{write_string("4.DO Q(VAR) ");temp=0;break;} } } } } interrupt [EXT_INT1] void ext_int1_isr(void)//phuc vu ngat ngoai 1 { // Place your code here tatled(); key(); } /////////////////////////ADE 7753//////////////////////////////////////// unsigned char read[10]; void spi_t(unsigned char data7753) //truyen 8bit vao ade { int i=0; for(i=0;i<8;i++) { CLK = 1;//delay_us(10); if((data7753&0x80)==0x80) IN = 1; else IN=0; CLK = 0;//delay_us(10); data7753=data7753<<1; } } unsigned char spi_r(void) //doc 8bit tu ade { unsigned char rd_bit,dat; int i=0,j=7; dat = 0x00; for(i=0;i<8;i++) { rd_bit = DOUT; dat = dat | (rd_bit<<j); j--; CLK = 1;delay_us(10); CLK = 0;delay_us(10); } return dat; } //ghi 8bit,16bit,24bit: addr:dia chi can ghi,sobyte:sobyte cua thanh ghi can ghi, //24bit thi data1:byte cao nhat,data3:byte thap nhat //16bit thi data1 la byte cao,data2 la byte thap //8bit thi data1 la du lieu, void ade7753_write(char addr,char sobyte,char data1,char data2,char data3) { int i=0; char dat[5]; dat[1]=data1;dat[2]=data2;dat[3]=data3; CS=0;delay_us(50); addr = addr|0x80; spi_t(addr); delay_us(20); for(i=1;i<=sobyte;i++) { spi_t(dat[i]); delay_us(20); } delay_us(50); CS=1; } //doc du lieu tu ade,addr la dia chi thanh ghi,sobyte la so byte trong thanh ghi do unsigned long int ade7753_read(char addr, char sobyte)//8bit,16bit,24bit { int i=0; unsigned long int ketqua=0; addr=addr&0x3f; CS=0;delay_us(50); spi_t(addr); delay_ms(30); for(i=0;i<=sobyte;i++) {read[i]=spi_r();delay_us(20);} delay_us(50); CS=1; if(sobyte==1)ketqua=read[0]; if(sobyte==2)ketqua=read[0]*256+read[1]; if(sobyte==3)ketqua=read[0]*65536+read[1]*256+read[2]; return ketqua; } void ade7753_write6(char addr,char data7753)//ghi 6bit { int i=0; CS=0;delay_us(100); addr = addr|0x80; spi_t(addr);delay_us(20); for(i=0;i<6;i++) { if((data7753&0x80)==0x80) IN = 1; else IN=0; CLK = 1;delay_us(10); CLK = 0;delay_us(10); data7753=data7753<<1; } delay_us(100); CS=1; } unsigned char ade7753_read6(char addr)//doc 6bit { unsigned char rd_bit,dat; int i=0,j=5; addr=addr&0x3f; CS=0;delay_us(100); spi_t(addr); delay_ms(50); dat = 0x00; for(i=0;i<6;i++) { rd_bit = DOUT; dat = dat | (rd_bit<<j); j--; CLK = 1;delay_us(10); CLK = 0;delay_us(10); } delay_us(100); CS=1; return dat; } void ade7753_write12(char addr,unsigned char data7753)//ghi 12bit { int i=0; CS=0;delay_us(100); addr = addr|0x80; spi_t(addr);delay_us(20); for(i=0;i<12;i++) { if((data7753&0x80)==0x80) IN = 1; else IN=0; CLK = 1;delay_us(10); CLK = 0;delay_us(10); data7753=data7753<<1; } delay_us(100); CS=1; } unsigned long int ade7753_read12(char addr)//doc 12bit { char rd_bit;unsigned long int dat; int i=0,j=11; addr=addr&0x3f; CS=0;delay_us(100); spi_t(addr); delay_ms(50); dat = 0x00; for(i=0;i<12;i++) { rd_bit = DOUT; dat = dat | (rd_bit<<j); j--; CLK = 1;delay_us(10); CLK = 0;delay_us(10); } delay_us(100); CS=1; return dat; } void ade7753_write15(char addr,char data7753)//ghi 15bit { int i=0; CS=0;delay_us(100); addr = addr|0x80; spi_t(addr);delay_us(20); for(i=0;i<15;i++) { if((data7753&0x80)==0x80) IN = 1; else IN=0; CLK = 1;delay_us(10); CLK = 0;delay_us(10); data7753=data7753<<1; } delay_us(100); CS=1; } unsigned long int ade7753_read15(char addr)//doc 15bit { char rd_bit;unsigned long int dat; int i=0,j=14; addr=addr&0x3f; CS=0;delay_us(100); spi_t(addr); delay_ms(50); dat = 0x00; for(i=0;i<15;i++) { rd_bit = DOUT; dat = dat | (rd_bit<<j); j--; CLK = 1;delay_us(10); CLK = 0;delay_us(10); } delay_us(100); CS=1; return dat; } void ade7753_init(void) //khoi tao ade { ade7753_write(MODE,0x00,0x00,0X00);delay_us(100); // cho phep dau ra CF ade7753_write(SAGLVL,0X2a,0X00,0X00);delay_us(20); ade7753_write(SAGCYC,0X04,0X00,0X00);delay_us(20); } ////////////////////KHOI TAO CHIP ATMEGA 32///////////////////////////////////// void khoitao(void) { // Declare your local variables here // Input/Output Ports initialization // Port A initialization // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out // State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 PORTA=0x00; DDRA=0xFF; // Port B initialization // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=In Func0=Out // State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=T State0=0 PORTB=0xbd; //FF DDRB=0xBd; //bd // Port C initialization // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out // State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 PORTC=0x00; DDRC=0xFF; // Port D initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTD=0x00; DDRD=0x00; // External Interrupt(s) initialization // INT0: On // INT0 Mode: Rising Edge // INT1: On // INT1 Mode: Rising Edge // INT2: Off GICR|=0xC0; MCUCR=0x0F; MCUCSR=0x00; GIFR=0xC0; /* // SPI initialization // SPI Type: Master // SPI Clock Rate: 62.500 kHz // SPI Clock Phase: Cycle Start // SPI Clock Polarity: Low // SPI Data Order: MSB First SPCR=0xdb; SPSR=0x00; // Clear the SPI interrupt flag #asm in r30,spsr in r30,spdr #endasm */ // TWI initialization // TWI disabled TWCR=0x00; // Global enable interrupts #asm("sei") } ///////////////////MAIN////////////////////////////////////////////// void main(void) { int i=0,u[20]; unsigned long dem=0,U=0,I=0,P=0,S=0,Q=0,Wh=0,x=0,y=0; khoitao(); i2c_init(); init(); tatled(); CLK=0;CS=1; write_c(0x80);write_string(" DO AN \nDONG HO DO DIEN "); delay_ms(500); for(i=0;i<10;i++)//test LCD va Led7 { out_num(dem);dem=dem+111111; if((i<3)||(i>=8))write_c(0x18); //dich trai noi dung lcd if((i<8)&&(i>=3))write_c(0x1c);//dich phai noi dung lcd delay_ms(500); } delay_ms(500); ade7753_init(); write_c(0x01); write_string("1.DO Urms(V) "); i=0; while (1) { write_c(0x80); switch(mod) { case 1:{write_string("1.DO Urms(V) ");break;} case 2:{write_string("2.DO Irms(mA) ");break;} case 3:{write_string("3.DO P(W) ");break;} case 4:{write_string("4.DO Q(VAR) ");break;} } if(mod==1) { U=ade7753_read(VRMS); U=U/1420; out_num(U); write_c(0xc0);write_num(U); } if(mod==2) { I=ade7753_read(IRMS); out_num(I*1000/48000); write_c(0xc0);write_num(I); } if(mod==3) { P=ade7753_read(RAENERGY); //03H,p out_num(P); write_c(0xc0);write_num(P); } if(mod==4) { //Q=ade7753_read(LVARENERGY); //0x8h ,s=06h P=ade7753_read(RAENERGY); //03H,p S=ade7753_read(RVAENERGY); if((S!=0)&&(S>P))Q=sqrt(S*S-P*P); out_num(Q); write_c(0xc0);write_num(Q); } write_string(" "); } } ////////tinh cong suat hieu dung doc thanh 48bit,23bit cao add=02h, //23bit thap add=03h,doc 23 bit thap truoc ,datasheet trang 20 //neu muon tinh p theo xung ra CF //set tan so dau ra CF bang 2 thanh ghi CFNUM[11:0]and CFDEN[11:0],datasheet trang 21 //thiet lap chu ky tich luy nang luong :set bit 7 CYCMODE trong thanh MODE,datasheet trang 22 //thanh ghi lien quan LINECYC(1Ch) ,LAENERGY(04h) ////tinh cong suat phan khang(datasheet 23);Q= can bac2 cua(S^2-p^2)=08h ///cos phi=PF = sign(LVARENERGY) * LAENERGY/ LVAENERGY ,sign nghia la dau hieu ////tinh cong suat bieu kien(datasheet 24) ;thanh ghi VAENERGY(05h) la 24bit cao //thanh RVAENERGY doc cong suat bieu kien S,sau khi doc thanh nay thi se reset ve 0, ///kVarHr= can bac 2(VA –kWHr^2) = can bac 2(LVAENERGY -LAENERGY^2) //tai lieu tham khao:datasheet,Multi-functional Energy Metering IC(Energy Meter-ADE7753 intropres.pdf)
Comment