/*********************************************************
Hardware : EVB9S08DZ60 Board
Author : WBR
Version : V1.0
Begin Time: 2011.5.30
**********************************************************/
#include /* for EnableInterrupts macro */
#include “derivative.h” /* include peripheral declarations */
#include “1302.h”
//#include “lcd.h”
//#include “mscan.h”
//#include “KEY.h”
/******************************************************
IO阐明:
1:四路AD输入通道 ADP19(AD1) ADP11(AD2) ADP3(AD3) ADP4(AD4)
2:485通讯 DE (PTE2) TXD1(PTE0) RXD1(PTE1)
3:四路光耦输入 DI1(PTG2) DI2 (PTE5) DI3 (PTE4) DI4(PTE3)
4:DS1302 SCL(PTF2) SDA (PTF3) RST (PTG4)
5:继电器操控 PTA6 PTB5 PTA5 PTC4 PTB6
6:按键(输入) PTC0 PTB0 PTC1 PTA0 PTC2(SW1~SW5)
7:CAN TX(PTE6) RX(PTE7)
8:LCD RW(PTG3) RS(PTF0) E(PTF1) RST(PTF7) 背光(PTG5) DATA(PTD0~PTD7)
************************************************************/
#define DI1 PTGD_PTGD2 //外触发1—–>PG2
#define DI2 PTED_PTED5 //外触发2—–>PE5
#define DI3 PTED_PTED4 //外触发3—–>PE4
#define DI4 PTED_PTED3 //外触发4—–>PE3
//********以上为输入IO********//
#define JDQ1 PTCD_PTCD4 //继电器1—–>PC4
#define JDQ2 PTAD_PTAD5 //继电器2—–>PA5
#define JDQ3 PTBD_PTBD5 //继电器3—–>PB5
#define JDQ4 PTAD_PTAD6 //继电器4—–>PA6
#define JDQ5 PTBD_PTBD6 //继电器5—–>PB6
#define DE485 PTED_PTED2 //485 T R切换—–>PE2
//#define ID_TX 0x0001 //自界说的CAN发送标识符
//#define ID_RX 0x0002 //自界说的CAN接纳标识符
//Bool can_send_enable = FALSE; //CAN发送标志位,暂定RTC0.5秒发送一次
#define Baud 19200 //串口波特率9600
#define N 51
#define T 1800 //运算电量的时刻单位1800是半个小时(1800秒)
extern byte time_buf1[8]; //1302时钟寄存器
byte ReadTimeFlag = 0; //定时器每0.5秒读1302时刻的标志位
byte ADSET = 0; //发送电流电压功率等数值到显示屏标志位
/*********************变量和函数界说****************************/
word ADCbuffer1[N] = 0; //AD转化缓冲
word ADC1,ADC2,ADC3,ADC4; //AD转化缓冲
byte ADC1L,ADC1H,ADC2L,ADC2H,ADC3L,ADC3H,ADC4L,ADC4H;
byte MW4L,MW4H,MW5L,MW5H,MW6L,MW6H,MW7L,MW7H,MW8L,MW8H,MW9L,MW9H;
byte MW21L,MW21H,MW22L,MW22H,MW25L,MW25H,MW26L,MW26H; //前史充电电量记载
byte MW29L,MW29H,MW30L,MW30H,MW33L,MW33H,MW34L,MW34H; //前史放电电量记载
byte Buffer[15]; //串口接纳缓冲
byte ADch_s; //AD通道挑选
byte ADcount;
//byte Key_value=0; //按键
//byte CANbuffer[8]; //接纳到的CAN内容缓冲区数组
word sum=0;
long P,P1,P2,P3,Premain,Pup,Pdown,Psum; //实时充电功率,实时放电功率,前史充电功率,前史放电记载
word PL,PH; //拆分P
byte P0L,P0H,P1L,P1H; //拆分PH,PL
byte RR = 1; //RR = 1;读屏的19号地址;RR = 2读屏的20号地址开端的时刻;
byte PAGE8; //进入设置时刻页面和电量页面设置计数;
byte T20 =0;
word TENT;
byte *p;
//================================
//中值滤波程序
//================================
void filter()
{
static byte count,i,j;
word value_buf[N];
word temp;
sum=0;
for(count=0;count
{
value_buf[count] = ADCbuffer1[count];;
}
for (j=0;j
{
for (i=0;i
{
if ( value_buf[i]>value_buf[i+1] )
{
temp = value_buf[i];
value_buf[i] = value_buf[i+1];
value_buf[i+1] = temp;
}
}
}
for(count=1;count
sum += value_buf[count];
}
//===================================
//函数名:MCU_INIT
//效果: 初始化MCU时钟 晶振16M总线频率8M
//====================================
void MCU_INIT(void)
{
MCGC1=0x04; //挑选内部时钟
MCGC2=0x40; //时钟源二分频
MCGTRM=0xa8;//内部时钟微调
}
//===================================
//函数名: IO_INIT
//效果: 初始化IO 输入输出
//====================================
void IO_INIT(void)
{
PTADD = 0xe6; //数据方向 1为输出,0为输入
PTBDD = 0x76;
PTCDD = 0xb0;
//PTDDD = 0xff;
PTEDD = 0x45;
PTFDD = 0xff;
PTGDD = 0x38;
PTAD = 0 ; //初始状况IO全为0
PTBD = 0;
PTCD = 0;
PTDD = 0;
PTED = 0;
PTFD = 0;
PTGD = 0;
}
//===================================
//函数名: ADC_INIT
//效果: 初始化ADC 总线时钟8M
//===================================
void ADC_INIT(void)
{
ADCCFG = 0xf9; //低功耗形式,10位精度,ADCK=总线频率/8/2=500K
ADCSC2 = 0x00; //0x00:软件触发,比较功用制止
APCTL1 = 0x14; //通道引脚使能: 运用通道为AD19 AD11 AD3 AD4
APCTL2 = 0x08;
APCTL3 = 0x08;
ADch_s = 19; //挑选19通道开端
ADCSC1 = 0x73; //0x53:中止使能,单次转化,挑选19通道发动转化.
}
//===================================
//函数名:SCI_INIT
//效果: 初始化SCI1
//====================================
void SCI_INIT(void)
{
SCI1BD = 8000000/16/Baud;
SCI1C1 = 0x00;
SCI1C2 = 0x2c; //使能承受中止
}
//===================================
//串口发送
//===================================
void SCI1send (byte x)
{
while(SCI1S1_TDRE==0);
SCI1D = x;
}
//===================================
//定时器1初始化
//===================================
void TPM1_Init(void) //定时器1初始化
{
TPM1MODH=0x3d; //
TPM1MODL=0x09; //250MS
TPM1SC=0x4F; //TPM1时钟源为总线时钟;分频系数为128;溢出中止使能
}
//===================================
//延时
//===================================
void Delay(Word time)
{
Word i,j;
for(i=0;i