本文包含原理图、PCB、源代码、封装库、中英文PDF等资源
您需要 登录 才可以下载或查看,没有账号?注册会员
×
[code]
#include <compiler_defs.h>
#include <C8051F500_defs.h>
#include <stdio.h>
#define BAUDRATE 115200
#define SYSCLK 24000000
#define F_SCK_MAX 2000000
#define T_NSS_DISABLE_MIN 500
#define Write 0x40
#define Read 0x00
#define MODE 0x30
#define PF 25 //cos@ PF
#define PP 9 //P瞬时值
#define PPa 10 //P real 时值
#define ET3_1 EIE1|=0x40
#define ET3_0 EIE1&=0xbf
#define TR3_1 TMR3CN|=0x04
#define TR3_0 TMR3CN&=0xfb
SBIT (LED, SFR_P1, 3);
//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------
void PCA0_Init (void);
void OSCILLATOR_Init (void);
void PORT_Init (void);
void TIMER2_Init (void);
void TIMER3_Init (void);
void UART0_Init (void);
void SPI0_Init (void);
void Init_Device (void);
void Delay_us(U8 time_us);
void Delay_ms(U8 time_ms);
void CS5463_Write(U8 addr,U8 *value);
U8 CS5463_Read(U8 addres,U8 *value);
void Write_Byte(U8 byte);
void CS5463_Init(void);
U8 buf[3] = {0};
bit flag = 0;
INTERRUPT_PROTO (TIMER3_ISR, INTERRUPT_TIMER3);
//-----------------------------------------------------------------------------
// main() Routine
//-----------------------------------------------------------------------------
void main (void)
{
U16 address;
U8 test_byte;
U8 Irms[3],Vrms[3],Pactive[3],cos[3],PPP[3];
Init_Device ();
SFRPAGE = ACTIVE_PAGE;
LED = 1;
EA = 1;
ET3_1;
TR3_1;
while(1)
{
CS5463_Read(11,Irms);
CS5463_Read(12,Vrms);
CS5463_Read(PF,cos);
CS5463_Read(PPa,Pactive);
if(flag)
{
flag = 0;
printf ("Irms[0]: %3d ", Irms[0]);
printf ("Irms[1]: %3d ", Irms[1]);
printf ("Irms[2]: %3d ", Irms[2]);
printf ("Vrms[0]: %3d ", Vrms[0]);
printf ("Vrms[1]: %3d ", Vrms[1]);
printf ("Vrms[2]: %3d ", Vrms[2]);
printf ("cos[0]: %3d ", cos[0]);
printf ("cos[1]: %3d ", cos[1]);
printf ("cos[2]: %3d ", cos[2]);
printf ("Pactive[0]: %3d ", Pactive[0]);
printf ("Pactive[1]: %3d ", Pactive[1]);
printf ("Pactive[2]: %3d ", Pactive[2]);
Delay_ms(100);
ET3_1;
TR3_1;
}
}
}
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
void PCA0_Init (void)
{
U8 SFRPAGE_save = SFRPAGE;
SFRPAGE = ACTIVE_PAGE;
PCA0MD &= ~0x40;
SFRPAGE = SFRPAGE_save;
}
//-----------------------------------------------------------------------------
// OSCILLATOR_Init
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void)
{
U8 SFRPAGE_save = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
OSCICN = 0x87;
SFRPAGE = SFRPAGE_save;
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function configures the crossbar and GPIO ports.
//
// P0.0 - SCK (SPI0), Push-Pull, Digital
// P0.1 - MISO (SPI0), Open-Drain, Digital
// P0.2 - MOSI (SPI0), Push-Pull, Digital
// P0.3 - NSS (SPI0), Push-Pull, Digital
// P0.4 - TX0 (UART0), Push-Pull, Digital
// P0.5 - RX0 (UART0), Open-Drain, Digital
//
// P1.3 - Skipped, Push-Pull, Digital (LED D2 on Target Board)
// P1.4 - Skipped, Open-Drain, Digital (Switch S2 on Target Board)
//
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
U8 SFRPAGE_save = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
P0MDOUT = 0x1D;
P1MDOUT = 0x08;
P1SKIP = 0x18;
XBR0 = 0x05;
XBR2 = 0x40;
SFRPAGE = SFRPAGE_save;
}
//-----------------------------------------------------------------------------
// TIMER2_Init
//-----------------------------------------------------------------------------
void TIMER2_Init (void)
{
CKCON |= 0x10;
}
void TIMER3_Init (void)
{
U8 SFRPAGE_save = SFRPAGE;
SFRPAGE = ACTIVE_PAGE;
TMR3CN &= 0x00; // Stop Timer2; Clear TF2;
// use SYSCLK as timebase, 16-bit
// auto-reload
CKCON &= 0xbf; // Select SYSCLK for Timer 2 source
TMR3RL = 65535-(SYSCLK /20000);//-30;//-60; // Init reload value for 10uS//600)-60;//
TMR3 =0xffff; // Set to reload immediately
SFRPAGE = SFRPAGE_save;
}
//-----------------------------------------------------------------------------
// UART0_Init
//-----------------------------------------------------------------------------
void UART0_Init (void)
{
U8 SFRPAGE_save = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
SCON0 = 0x10;
#if ((SYSCLK / BAUDRATE / 2 / 0xFFFF) < 1)
SBRL0 = -(SYSCLK / BAUDRATE / 2);
SBCON0 |= 0x03;
#elif ((SYSCLK / BAUDRATE / 2 / 0xFFFF) < 4)
SBRL0 = -(SYSCLK / BAUDRATE / 2 / 4);
SBCON0 &= ~0x03;
SBCON0 |= 0x01;
#elif ((SYSCLK / BAUDRATE / 2 / 0xFFFF) < 12)
SBRL0 = -(SYSCLK / BAUDRATE / 2 / 12);
SBCON0 &= ~0x03;
#else
SBRL0 = -(SYSCLK / BAUDRATE / 2 / 48);
SBCON0 &= ~0x03;
SBCON0 |= 0x02;
#endif
SBCON0 |= 0x40;
TI0 = 1;
SFRPAGE = SFRPAGE_save;
}
//-----------------------------------------------------------------------------
// SPI0_Init
//-----------------------------------------------------------------------------
void SPI0_Init()
{
U8 SFRPAGE_save = SFRPAGE;
SFRPAGE = ACTIVE_PAGE;
SPI0CFG = 0x40;
SPI0CN = 0x0D;
SPI0CKR = (SYSCLK / (2 * F_SCK_MAX));
SFRPAGE = SFRPAGE_save;
}
//-----------------------------------------------------------------------------
// Init_Device
//-----------------------------------------------------------------------------
void Init_Device (void)
{
PCA0_Init ();
OSCILLATOR_Init ();
PORT_Init ();
TIMER2_Init ();
TIMER3_Init ();
UART0_Init ();
SPI0_Init ();
CS5463_Init();
}
void CS5463_Init(void)
{
Write_Byte(0xd8);
Write_Byte(0xc0);
Delay_ms(5);
buf[0]=0xff; //SYNC1
buf[1]=0xff; //SYNC1
buf[2]=0xfe; //SYNC0
CS5463_Write(0xff,buf);
Delay_us(20);
buf[0] = 0xff;
buf[1] = 0xff;
buf[2] = 0xff;
CS5463_Write(0x5e,buf);
Delay_us(20);
buf[0] = 0x00;
buf[1] = 0x00;
buf[2] = 0x60;
CS5463_Write(MODE,buf);
Delay_us(100);
buf[0] = 0x00;
buf[1] = 0x00;
buf[2] = 0x61;
CS5463_Write(0x00,buf); //write Config register
Delay_us(20);
buf[0] = 0x00;
buf[1] = 0x0f;
buf[2] = 0xa0;
CS5463_Write(0x0a,buf); //write cycle cnt reg 0000fa = 4000
Delay_us(20);
buf[0] = 0x83;
buf[1] = 0x12;
buf[2] = 0x6c;
CS5463_Write(0x15,buf); //write time base reg 0x83126c = 1.024
Delay_us(20);
buf[0] = 0x00;
buf[1] = 0x03;
buf[2] = 0x0c;
CS5463_Write(0x0c,buf); //write pulse rate reg 0x0003c0 = 30
Delay_us(20);
Write_Byte(0xe8); ///启动转换
}
//-----------------------------------------------------------------------------
// Delay_us
//-----------------------------------------------------------------------------
void Delay_us (U8 time_us)
{
U8 SFRPAGE_save = SFRPAGE;
SFRPAGE = ACTIVE_PAGE;
TR2 = 0;
TF2H = 0;
TMR2 = -((U16)(SYSCLK / 1000000) * (U16)(time_us));
TR2 = 1;
while (!TF2H);
TR2 = 0;
SFRPAGE = SFRPAGE_save;
}
//-----------------------------------------------------------------------------
// Delay_ms
//-----------------------------------------------------------------------------
void Delay_ms (U8 time_ms)
{
U8 i;
while(time_ms--)
{
for(i = 0; i< 10; i++)
{
Delay_us (100);
}
}
}
//-----------------------------------------------------------------------------
// CS5463_Write
//-----------------------------------------------------------------------------
void |