You are on page 1of 17

#include"lcd.

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

// Watchdog Timer Enable bit (WDT disabled (control is


// Watchdog Timer Postscale Select bits (1:32768)

// 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-

#pragma config CP3 = OFF


protected)
// CONFIG5H
#pragma config CPB = OFF
0007FFh) not code-protected)
#pragma config CPD = OFF
code-protected)
// CONFIG6L
#pragma config WRT0 = OFF
protected)
#pragma config WRT1 = OFF
protected)
#pragma config WRT2 = OFF
protected)
#pragma config WRT3 = OFF
protected)

// Code Protection bit (Block 3 (00C000-00FFFFh) 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();

unsigned long read_IRMS();


char read_dataADE();
void writec_uart(char data_uart);
void write_uart(char *data_uart);
void int_w_uart();
//FUNCTION FOR ADE7753
unsigned long read_VRMS() //Khong dung cho che do WAVEFORM
{
unsigned long temp=0;
temp = read_SPI(0x17, 3);
return temp;
}
unsigned long read_IRMS() //Khong dung cho che do WAVEFORM
{
unsigned long temp=0;
temp = read_SPI(0x16, 3);
return temp;
}
char read_dataADE()
{
int temp=0;
unsigned int count_noac=0;
VRMS = 0;
IRMS = 0;
FHZ = 0;
P = 0;
count_readADE = 0;
while(count_readADE <= 9)
{
while((temp & 0x0010) == 0)
{
temp = 0;
temp = read_SPI(0x0C, 2);
count_noac++;
if(count_noac == 15)
{

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);

FHZ = (unsigned float)(3579545/4) / (FHZ*2);


P = VRMS * IRMS * 0.86;
}
if((unsigned long)(VRMS*1000)%1000<=9)
sprintf(stringU,"%lu.00%lu",(unsigned long)(VRMS),(unsigned long)(VRMS*100)%100);
else if((unsigned long)(VRMS*1000)%1000<=90)
sprintf(stringU,"%lu.0%lu",(unsigned long)(VRMS),(unsigned long)(VRMS*100)%100);
else
sprintf(stringU,"%lu.%lu",(unsigned long)(VRMS),(unsigned long)(VRMS*100)%100);
gotoxy_LCD(1, 1);
putchar_LCD('U'); putchar_LCD(':');
putstr_LCD(stringU);
if((unsigned int)(IRMS*100)%100<=9)
sprintf(&stringI[0],"%d.0%d",(unsigned int)(IRMS),(unsigned int)(IRMS*1000)%1000);
else
sprintf(&stringI[0],"%d.%d",(unsigned int)(IRMS),(unsigned int)(IRMS*1000)%1000);
gotoxy_LCD(2, 1);
putchar_LCD('I'); putchar_LCD(':');
putstr_LCD(stringI);
if((unsigned int)(FHZ*1000)%1000<=9)
sprintf(stringF,"%d.00%d",(unsigned int)(FHZ),(unsigned int)(FHZ*100)%100);
else if((unsigned int)(FHZ*1000)%1000<=90)
sprintf(stringF,"%d.0%d",(unsigned int)(FHZ),(unsigned int)(FHZ*100)%100);
else
sprintf(stringF,"%d.%d",(unsigned int)(FHZ),(unsigned int)(FHZ*100)%100);
gotoxy_LCD(1, 10);
putchar_LCD('F'); putchar_LCD(':');
putstr_LCD(stringF);
if((unsigned long)(P*1000)%1000<=9)
sprintf(stringP,"%lu.00%lu",(unsigned long)(P),(unsigned long)(P*100)%100);
else if((unsigned long)(P*1000)%1000<=90)
sprintf(stringP,"%lu.0%lu",(unsigned long)(P),(unsigned long)(P*100)%100);
else
sprintf(stringP,"%lu.%lu",(unsigned long)(P),(unsigned long)(P*100)%100);
gotoxy_LCD(2, 9);
putchar_LCD('P'); putchar_LCD(':');
putstr_LCD(stringP);
}
void write_data()
{

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

/*Driver dieu khien SPI cho ADE7753


*Ngay tao: 15/01/2016
*Nguoi viet: NQD123
*Su dung cho: PIC 18F4620
===================================*/
#include"delays.h"
#define RST_TRIS TRISEbits.TRISE1
#define RST_LAT LATEbits.LATE1
#define CS_TRIS TRISEbits.TRISE2
#define CS_LAT LATEbits.LATE2

void delay_n_ms (unsigned int n);


void enable_ade();
void disable_ade();
void int_SPI();
unsigned char writec_SPI(unsigned char data);
void write_SPI(unsigned char add, int data);
unsigned char readc_SPI();
unsigned long read_SPI(unsigned char add, int byte);
#ifndef __SPI_H
void delay_n_ms (unsigned int n)
{
unsigned int i;
for(i=0;i<n;i++)
Delay1KTCYx(2); // Delay of 1ms
// Cycles = (TimeDelay * Fosc) / 4
// Cycles = (1ms * 8MHz) / 4
// Cycles = 8,000
}
void enable_ade()
{
CS_LAT = 0; //Cho phep chip hoat dong
}
void disable_ade()
{
CS_LAT = 1; //Khong cho phep chip hoat dong
}
void int_SPI() //Cau hinh SPI chip master
{
TRISCbits.RC3 = 0; //SCK la chan out
TRISCbits.RC5 = 0; //SDO la chan out
TRISCbits.RC4 = 1; //SDI la chan in
SSPSTATbits.SMP = 1;
SSPSTATbits.CKE = 1;
//SSPSTATbits.BF = 1 --> Nhan hoan tat, SSPBUF = Full
//SSPSTATbits.BF = 0 --> Nhan chua xong, SSPBUF = Empty
SSPCON1bits.WCOL = 0; //Khong cho phep xay ra va cham khi chua truyen xong
SSPCON1bits.SSPEN = 1; //Cau hinh cac chan cho SPI
SSPCON1bits.CKP = 0;
SSPCON1bits.SSPM3 = 0; //Che do Master, Clock = FOSC/16

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));
}
}

unsigned char readc_SPI()


{
unsigned char temp;
enable_ade();
temp = SSPBUF;
PIR1bits.SSPIF = 0; //Xoa co bao ngat SPI
SSPBUF = 0x00;
while(!PIR1bits.SSPIF);
return SSPBUF;
disable_ade();
}
unsigned long read_SPI(unsigned char add, int byte)
{
unsigned long temp=0;
unsigned long temp_=0;
while(!writec_SPI(add));
delay_n_ms(1);
if(byte == 1)
return readc_SPI();
if(byte == 2)
{
temp = readc_SPI();
temp = temp << 8;
return temp | readc_SPI();
}
if(byte == 3)
{
temp = readc_SPI();
temp = temp << 16;
temp_ = readc_SPI();
temp = temp | (temp_ << 8);
return temp | readc_SPI();
}
}
#endif
/*Driver dieu khien LCD 16x2
*Ngay tao: 15/01/2016
*Nguoi viet: NQD123
*Su dung cho: PIC 18F4620

*Luu y: Khong su dung chan RW


=============================*/
#ifndef __LCD_INC
#include <p18f4620.h>
#include <delays.h>
//#define UPPER //Chon 4 bit cao, hoac thap
#define clr_lcd 0x01
#define back_home 0x02
#define cur_to_righ 0x04
#define cur_to_left 0x06
#define lcd_to_righ 0x05
#define lcd_to_left 0x07
#define offcur_offlcd 0x08
#define oncur_offlcd 0x0a
#define offcur_onlcd 0x0c
#define shift_cur_to_left 0x10
#define shift_cur_to_righ 0x14
#define shift_lcd_to_left 0x18
#define shift_lcd_to_righ 0x1c
#define cur_back_line1 0x80
#define cur_back_line2 0xc0
#define mode_8bit 0x38
#define mode_4bit 0x28
#define
#define

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

LCD_DATA &= 0xF0;


LCD_DATA |= temp; //Xuat 4 bit du lieu thap ra PORT DATA
LatchData;
#endif
}
void putcmd_LCD(char ch)
{
busy_LCD();
WriteCommand;
write_LCD(ch);
}
void putchar_LCD(char ch)
{
busy_LCD();
WriteData;
write_LCD(ch);
}
void putstr_LCD(char *ch)
{
while(*ch)
{
putchar_LCD(*ch);
ch++;
}
}
void int_LCD(void)
{
TRIS_RS = 0;
TRIS_EN = 0;
putcmd_LCD(mode_4bit);
putcmd_LCD(offcur_onlcd);
putcmd_LCD(clr_lcd);
}
void gotoxy_LCD(int x, int y)
{
int i;
if (x==1) putcmd_LCD(cur_back_line1);
if (x==2) putcmd_LCD(cur_back_line2);
if ((x>2)||(x<1)) putcmd_LCD(cur_back_line1);
for (i=0;i<(y-1);i++) putcmd_LCD(shift_cur_to_righ);

}
#endif

You might also like