您的位置 首页 5G

基于飞思卡尔DZ60的AD 1302 KEY 485 CAN FLASH LCD程序

*********************************************************Hardware:EVB9S08DZ60BoardAuthor:W

/*********************************************************

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
__RESET_WATCHDOG();
for(j=0;j<500;j++);
}
}
void WEEP()
{
FCMD=0x20;
FSTAT_FCBEF=1;
asm{
NOP
NOP
NOP
NOP
}
while(FSTAT_FCCF==0);
if(FSTAT_FACCERR==1)
FSTAT_FACCERR=1;
}
/*********************主函数************************************/
void main(void)
{
static byte MonthF1,DateF1,MonthF2,DateF2,MonthF3,DateF3,MonthC1,DateC1,MonthC2,DateC2,MonthC3,DateC3; //充电放电前史记载的两次时刻变量及缓存变量
static byte FBETW0L,FBETW0H,FBETW1L,FBETW1H,CBETW0L,CBETW0H,CBETW1L,CBETW1H;
static word PupCount,PdownCount;
byte Pchange = 0 ; //充电0XFF 放电0X00转化标志
byte Failure; //毛病信息指示MW17.0 = 1过压 MW17.1 = 1过流
word BetweenH,BetweenL;
long a,b;
byte SendSum;
MCU_INIT();
IO_INIT();
Ds1302_Init();
ADC_INIT();
TPM1_Init();
SCI_INIT();
DE485 = 1; //485发送使能;
PTED_PTED6 = 1;
Ds1302_Read_Time(); //先读取一次时刻用于判别前史时刻
if(!DI1) //DI1 = 0
{
Pchange = 0x01; //充电
}
if(!DI2) //DI2 = 0
{ //放电
Pchange = 0x02;
}
if(DI1&&DI2) //DI1,DI2都 为高电平
{
Pchange = 0;
}
FCDIV = 39; //设置FLASH分频 8M总线时钟分频数39;FLASH读写200KH
asm(nop);
//**************读取记载的充电的前史记载************************
//************前1次的前1次得充电记载
p=(byte *)(0x1401); //
MonthC1= *p;
p=(byte *)(0x1402); //时刻
DateC1= *p;
p=(byte *)(0x1403); //电量的最低位
MW21L= *p;
p=(byte *)(0x1404); //
MW21H= *p;
p=(byte *)(0x1405); //
MW22L= *p;
p=(byte *)(0x1406); //
MW22H= *p;
if(MonthC1>12)
{
MonthC1 = 0;
DateC1= 0;
MW21L = 0;
MW21H = 0;
MW22L = 0;
MW22H = 0;
}
//*************前1次的充电记载*********************************
p=(byte *)(0x1411); //
MonthC2= *p;
p=(byte *)(0x1412); //时刻
DateC2= *p;
p=(byte *)(0x1413); //电量的最低位
MW25L= *p;
p=(byte *)(0x1414); //
MW25H= *p;
p=(byte *)(0x1415); //
MW26L= *p;
p=(byte *)(0x1416); //
MW26H= *p; //
if(MonthC2>12) //假如日期过错,清零
{
MonthC2 = 0;
DateC2= 0;
MW25L = 0;
MW25H = 0;
MW26L = 0;
MW26H = 0;
}
//***************缓存的充电记载,用于比较核算******************
p=(byte *)(0x1421); //
MonthC3= *p;
p=(byte *)(0x1422); //时刻
DateC3= *p;
p=(byte *)(0x1423); //电量的最低位
CBETW0L= *p;
p=(byte *)(0x1424); //
CBETW0H= *p;
p=(byte *)(0x1425); //
CBETW1L= *p;
p=(byte *)(0x1426); //
CBETW1H= *p;
if(MonthC3>12) //
{
MonthC3 = 0;
DateC3= 0;
CBETW0L = 0;
CBETW0H = 0;
CBETW1L = 0;
CBETW1H = 0;
}
//**************读取记载的放电的前史记载**************************
//************前1次的前1次的放电记载
p=(byte *)(0x1501); //
MonthF1= *p;
p=(byte *)(0x1502); //时刻
DateF1= *p;
p=(byte *)(0x1503); //电量的最低位
MW29L= *p;
p=(byte *)(0x1504); //
MW29H= *p;
p=(byte *)(0x1505); //
MW30L= *p;
p=(byte *)(0x1506); //
MW30H= *p;
if(MonthF1>12)
{
MonthF1 = 0;
DateF1= 0;
MW29L = 0;
MW29H = 0;
MW30L = 0;
MW30H = 0;
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/yingyong/5g/255161.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部