Professional Documents
Culture Documents
inc"
#include"spi.h"
#include"function.h"
// CONFIG1H
#pragma config OSC = HS
// Oscillator Selection bits (HS oscillator)
#pragma config FCMEN = OFF
// Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock
Monitor disabled)
#pragma config IESO = ON
// Internal/External Oscillator Switchover bit (Oscillator
Switchover mode disabled)
// CONFIG2L
#pragma config PWRT = OFF
// Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = SBORDIS // Brown-out Reset Enable bits (Brown-out Reset enabled
in hardware only (SBOREN is disabled))
#pragma config BORV = 3
// Brown Out Reset Voltage bits (Minimum setting)
// CONFIG2H
#pragma config WDT = OFF
placed on the SWDTEN bit))
#pragma config WDTPS = 2048
// CONFIG3H
#pragma config CCP2MX = PORTC // CCP2 MUX bit (CCP2 input/output is multiplexed with
RC1)
#pragma config PBADEN = OFF
// PORTB A/D Enable bit (PORTB<4:0> pins are
configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF
// Low-Power Timer1 Oscillator Enable bit (Timer1
configured for higher power operation)
#pragma config MCLRE = ON
// MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin
disabled)
// CONFIG4L
#pragma config STVREN = ON
// Stack Full/Underflow Reset Enable bit (Stack
full/underflow will cause Reset)
#pragma config LVP = OFF
// Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
#pragma config XINST = OFF
// Extended Instruction Set Enable bit (Instruction set
extension and Indexed Addressing mode disabled (Legacy mode))
// CONFIG5L
#pragma config CP0 = OFF
protected)
#pragma config CP1 = OFF
protected)
#pragma config CP2 = OFF
protected)
// Code Protection bit (Block 0 (000800-003FFFh) not code// Code Protection bit (Block 1 (004000-007FFFh) not code// Code Protection bit (Block 2 (008000-00BFFFh) not code-
// Boot Block Code Protection bit (Boot block (000000// Data EEPROM Code Protection bit (Data EEPROM not
// Write Protection bit (Block 0 (000800-003FFFh) not write// Write Protection bit (Block 1 (004000-007FFFh) not write// Write Protection bit (Block 2 (008000-00BFFFh) not write// Write Protection bit (Block 3 (00C000-00FFFFh) not write-
// CONFIG6H
#pragma config WRTC = OFF
// Configuration Register Write Protection bit (Configuration
registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF
// Boot Block Write Protection bit (Boot Block (0000000007FFh) not write-protected)
#pragma config WRTD = OFF
// Data EEPROM Write Protection bit (Data EEPROM not
write-protected)
// CONFIG7L
#pragma config EBTR0 = OFF
// Table Read Protection bit (Block 0 (000800-003FFFh) not
protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF
// Table Read Protection bit (Block 1 (004000-007FFFh) not
protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF
// Table Read Protection bit (Block 2 (008000-00BFFFh) not
protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF
// Table Read Protection bit (Block 3 (00C000-00FFFFh) not
protected from table reads executed in other blocks)
// CONFIG7H
#pragma config EBTRB = OFF
// Boot Block Table Read Protection bit (Boot Block (0000000007FFh) not protected from table reads executed in other blocks)
//Khai bao ham
void int_ade();
unsigned int reset_int_ade();
unsigned int read_status_ade();
void setup_int();
void int_ade()
{
int_SPI(); //Cau hinh SPI cho ADE7753
delay_n_ms(1);
}
unsigned int reset_int_ade() //Doc thanh ghi trang thai & xoa cac co ngat
{
unsigned int temp;
temp = read_SPI(0x0C, 2);
return temp;
}
unsigned int read_status_ade() //Doc thanh ghi trang thai
{
unsigned int temp;
temp = read_SPI(0x0B, 2);
return temp;
}
void setup_int()
{
//Thiet lap ngat ADE7753
write_SPI(0x0A, 0x10); //Cho phep ngat ZERO CROSSING
}
void main()
{
unsigned int i;
unsigned long n=100;
char temp=0;
//Khoi dong chip
RST_TRIS = 0;
CS_TRIS = 0;
disable_ade();
enable_ade();
RST_LAT = 0;
RST_LAT = 1;
disable_ade();
//Cau hinh
TRISBbits.TRISB5 = 0;
LATBbits.LATB5 = 0;
int_LCD();
delay_n_ms(5);
int_LCD();
delay_n_ms(5);
int_ade(); //Cau hinh ADE
int_uart(8, 0, 9600); //truyen 8 bits, toc do truyen 9600 bits/s
int_w_uart();
setup_int(); //Thiet lap cau hinh cho ngat
while(1)
{
//Doc du lieu
read_dataADE();
}
}
/*Ham su dung cho ADE7753 + UART
*Ngay tao: 15/01/2016
*Nguoi viet: NQD123
*Su dung cho: PIC 18F4620
=============================*/
#ifndef __FUNCTION_H
#include <p18f4620.h>
#include<math.h>
#include<stdio.h>
#define Fosc 8000000
#define can2 1.414213562
unsigned double VRMS=0, IRMS=0, P=0;
unsigned float FHZ=0;
unsigned short count_readADE=0;
char stringU[8]="000.000";
char stringI[8]="000.000";
char stringF[8]="000.000";
char stringP[8]="000.000";
char start[4]="#1*";
char end[4]="*1#";
unsigned long read_VRMS();
temp = 0;
VRMS = 0;
IRMS = 0;
FHZ = 0;
P = 0;
count_readADE = 0;
count_noac = 0;
sprintf(stringU, "000.000");
sprintf(stringI, "000.000");
sprintf(stringF, "000.000");
sprintf(stringP, "000.000");
break;
}
}
if(temp != 0)
{
VRMS += read_VRMS();
IRMS += read_IRMS();
count_readADE++;
}
else break;
/*if(count_readADE == 1)
{
VRMS = read_VRMS();
IRMS = read_IRMS();
P = read_SPI(0x06, 3);
VRMS = 0;
IRMS = 0;
P = 0;
FHZ = 0;
}*/
}
VRMS = (VRMS/(unsigned double)10); //Lay gia tri trung binh
VRMS = VRMS / 1561400 * 200.936177 / 1.10909;
//VRMS = VRMS / 1561400;
IRMS = (IRMS/(unsigned double)10); //Lay gia tri trung binh
IRMS = fabs(IRMS / (1868467 * 3 * can2) * 125);// - 0.021);
if(IRMS>=1)IRMS = IRMS * 0.97 * 1.033973906;
else IRMS = IRMS * 0.97 * 0.98 * 1.033973906;
if(VRMS > 0)
{
FHZ = read_SPI(0x27, 2);
int i;
char temp;
writec_uart(start[0]);
writec_uart(start[1]);
writec_uart(start[2]);
for(i=0;i<=6;i++)
{
if(stringU[i] != 0x00)
temp = stringU[i];
else
temp = '0';
writec_uart(temp);
}
for(i=0;i<=6;i++)
{
if(stringI[i] != 0x00)
temp = stringI[i];
else
temp = '0';
writec_uart(temp);
}
for(i=0;i<=6;i++)
{
if(stringF[i] != 0x00)
temp = stringF[i];
else
temp = '0';
writec_uart(temp);
}
for(i=0;i<=6;i++)
{
if(stringP[i] != 0x00)
temp = stringP[i];
else
temp = '0';
writec_uart(temp);
}
writec_uart(end[0]);
writec_uart(end[1]);
writec_uart(end[2]);
writec_uart('!');
writec_uart('!');
writec_uart('!');
}
//FUNCTION FOR UART
void int_uart(unsigned char bits, unsigned char bits9, unsigned int speed)
{
unsigned int temp;
TRISCbits.RC7 = 1; //RX chan doc data
TRISCbits.RC6 = 1; //TX chan viet data
TXSTAbits.SYNC = 0; //chon che do Asynchronous
switch(bits)
{
case 9:
TXSTAbits.TX9 = 1; //lua chon viet 9 bits
TXSTAbits.TX9D = bits9; //bit thu 9
RCSTAbits.RX9 = 1; //lua chon doc 9 bits
RCSTAbits.RX9D = bits9; //bit thu 9
break;
default:
TXSTAbits.TX9 = 0; //lua chon viet 8 bits
RCSTAbits.RX9 = 0; //lua chon doc 8 bits
}
BAUDCONbits.BRG16 = 0; //8 bits baud rate generator - SPBRG
TXSTAbits.BRGH = 1; //khong su dung trong che do Synchronous
temp = ((Fosc/speed)/16)-1; //tinh toan toc do = speed bits/s
SPBRG = temp;
/*SYNC=0, BRG16=0, BRGH=0 --> 8 bits/Asynchronous
FOSC/[64(n+1)] --> n
SYNC=0, BRG16=0, BRGH=1 --> 8 bits/Asynchronous
FOSC/[16(n+1)] --> n
SYNC=0, BRG16=1, BRGH=0 --> 16 bits/Asynchronous
FOSC/[16(n+1)] --> n
SYNC=0, BRG16=1, BRGH=1 --> 16 bits/Asynchronous
FOSC/[4(n+1)] --> n
SYNC=1, BRG16=0, BRGH=X --> 8 bits/Asynchronous
FOSC/[4(n+1)] --> n
SYNC=1, BRG16=1, BRGH=X --> 16 bits/Asynchronous
FOSC/[4(n+1)] --> n*/
RCSTAbits.SPEN = 1; //cho phep UART
}
void int_w_uart()
{
unsigned int temp;
//Cau hinh cho viet du lieu
//TXSTAbits.CSRC = 0; //chon nguon xung cap, Asynchronous khong can thiet
TXSTAbits.SENDB = 0; //send break character bit, khong can thiet trong che do Synchronous
//TXSTAbits.TRMT bit trang thai "=1" khi thanh ghi "GUI" empty, "=0" khi full
TXSTAbits.TXEN = 1; //cho phep truyen du lieu
}
void writec_uart(char data_uart)
{
TXREG = data_uart;
while(!TXSTAbits.TRMT);
}
void write_uart(char *data_uart)
{
while(*data_uart)
{
TXREG = *data_uart;
while(!TXSTAbits.TRMT);
data_uart++;
}
}
#endif
SSPCON1bits.SSPM2 = 0;
SSPCON1bits.SSPM1 = 0;
SSPCON1bits.SSPM0 = 1;
}
unsigned char writec_SPI(unsigned char data)
{
unsigned char temp;
enable_ade();
temp = SSPBUF;
PIR1bits.SSPIF = 0; //Xoa co bao ngat SPI
SSPCON1bits.WCOL = 0;
SSPBUF = data; //Ghi du lieu
if ( SSPCON1 & 0x80 ) //Kiem tra xem co xung dot khong
return 0; //Neu co xung dot --> return 0
else
while(!PIR1bits.SSPIF); //Cho den khi co gui xong
return 1;
disable_ade();
}
void write_SPI(unsigned char add, int data)
{
unsigned char temp=0;
add |= 0x80;
while(!writec_SPI(add));
delay_n_ms(1);
if(data <= 255)
{
temp = data;
while(!writec_SPI(0x00));
while(!writec_SPI(temp));
}
if(data > 255)
{
temp = (data & 0xFF00) >> 8;
while(!writec_SPI(temp));
temp = data & 0x00FF;
while(!writec_SPI(temp));
}
}
LCD_DATA LATA
TRIS_LCD_DATA TRISA
#define
LCD_RS
LATBbits.LATB4
#define TRIS_RS
TRISBbits.TRISB4
#define
LCD_EN
LATBbits.LATB3
#define TRIS_EN
TRISBbits.TRISB3
#define
#define
#define
WriteCommand
WriteData
LatchData
void int_LCD(void);
void write_LCD(char ch);
void putchar_LCD(char ch);
void putstr_LCD(char *ch);
void putcmd_LCD(char ch);
{LCD_RS = 0;}
{LCD_RS = 1;}
{LCD_EN = 1; LCD_EN = 0;}
void busy_LCD(void);
void gotoxy_LCD(int x, int y);
void _delay_1ms (void)
{
Delay1KTCYx(4); // Delay of 1ms
// Cycles = (TimeDelay * Fosc) / 4
// Cycles = (1ms * 16MHz) / 4
// Cycles = 4,000
}
void busy_LCD(void)
{
int i;
#if defined (UPPER)
TRIS_LCD_DATA &= 0b00001111; //4 bit cao la dau ra, xuat du lieu
#else
TRIS_LCD_DATA &= 0b11110000; //4 bit thap la dau ra, xuat du lieu
#endif
for(i=0;i<10;i++)
_delay_1ms();
}
void write_LCD(char ch) //Ghi du lieu 8 bit len LCD che do 4 bit
{
char temp;
busy_LCD();
#if defined (UPPER)
temp = ch & 0xF0; //Xoa 4 bit thap
LCD_DATA &= 0x0F; //Xoa 4 bit cao PORT DATA
LCD_DATA |= temp; //Xuat 4 bit du lieu cao ra PORT DATA
LatchData;
temp = (ch << 4) & 0xF0; //Dua 4 bit thap len cao
LCD_DATA &= 0x0F;
LCD_DATA |= temp; //Xuat 4 bit du lieu thap ra PORT DATA
LatchData;
#else
temp = (ch >> 4) & 0x0F; //Dua 4 bit cao xuong thap
LCD_DATA &= 0xF0; //Xoa 4 bit thap PORT DATA
LCD_DATA |= temp; //Xuat 4 bit du lieu cao ra PORT DATA
LatchData;
temp = ch & 0x0F; //Xoa 4 bit cao
}
#endif