新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 基于飛思卡爾DZ60的AD 1302 KEY 485 CAN FLASH LCD程序

基于飛思卡爾DZ60的AD 1302 KEY 485 CAN FLASH LCD程序

作者: 時(shí)間:2016-12-01 來(lái)源:網(wǎng)絡(luò) 收藏
/*********************************************************
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說(shuō)明:
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 //外觸發(fā)1----->PG2
#define DI2 PTED_PTED5 //外觸發(fā)2----->PE5
#define DI3 PTED_PTED4 //外觸發(fā)3----->PE4
#define DI4 PTED_PTED3 //外觸發(fā)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發(fā)送標(biāo)識(shí)符
//#define ID_RX 0x0002 //自定義的CAN接收標(biāo)識(shí)符
//Bool can_send_enable = FALSE; //CAN發(fā)送標(biāo)志位,暫定RTC0.5秒發(fā)送一次
#define Baud 19200 //串口波特率9600
#define N 51
#define T 1800 //運(yùn)算電量的時(shí)間單位1800是半個(gè)小時(shí)(1800秒)
extern byte time_buf1[8]; //1302時(shí)鐘寄存器
byte ReadTimeFlag = 0; //定時(shí)器每0.5秒讀1302時(shí)間的標(biāo)志位
byte ADSET = 0; //發(fā)送電流電壓功率等數(shù)值到顯示屏標(biāo)志位
/*********************變量和函數(shù)定義****************************/
word ADCbuffer1[N] = 0; //AD轉(zhuǎn)換緩沖
word ADC1,ADC2,ADC3,ADC4; //AD轉(zhuǎn)換緩沖
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內(nèi)容緩沖區(qū)數(shù)組
word sum=0;
long P,P1,P2,P3,Premain,Pup,Pdown,Psum; //實(shí)時(shí)充電功率,實(shí)時(shí)放電功率,歷史充電功率,歷史放電記錄
word PL,PH; //拆分P
byte P0L,P0H,P1L,P1H; //拆分PH,PL
byte RR = 1; //RR = 1;讀屏的19號(hào)地址;RR = 2讀屏的20號(hào)地址開(kāi)始的時(shí)間;
byte PAGE8; //進(jìn)入設(shè)置時(shí)間頁(yè)面和電量頁(yè)面設(shè)置計(jì)數(shù);
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];
}
//===================================
//函數(shù)名:MCU_INIT
//作用: 初始化MCU時(shí)鐘 晶振16M總線頻率8M
//====================================
void MCU_INIT(void)
{
MCGC1=0x04; //選擇內(nèi)部時(shí)鐘
MCGC2=0x40; //時(shí)鐘源二分頻
MCGTRM=0xa8;//內(nèi)部時(shí)鐘微調(diào)
}
//===================================
//函數(shù)名: IO_INIT
//作用: 初始化IO 輸入輸出
//====================================
void IO_INIT(void)
{
PTADD = 0xe6; //數(shù)據(jù)方向 1為輸出,0為輸入
PTBDD = 0x76;
PTCDD = 0xb0;
//PTDDD = 0xff;
PTEDD = 0x45;
PTFDD = 0xff;
PTGDD = 0x38;
PTAD = 0 ; //初始狀態(tài)IO全為0
PTBD = 0;
PTCD = 0;
PTDD = 0;
PTED = 0;
PTFD = 0;
PTGD = 0;
}
//===================================
//函數(shù)名: ADC_INIT
//作用: 初始化ADC 總線時(shí)鐘8M
//===================================
void ADC_INIT(void)
{
ADCCFG = 0xf9; //低功耗模式,10位精度,ADCK=總線頻率/8/2=500K
ADCSC2 = 0x00; //0x00:軟件觸發(fā),比較功能禁止
APCTL1 = 0x14; //通道引腳使能: 使用通道為AD19 AD11 AD3 AD4
APCTL2 = 0x08;
APCTL3 = 0x08;
ADch_s = 19; //選擇19通道開(kāi)始
ADCSC1 = 0x73; //0x53:中斷使能,單次轉(zhuǎn)換,選擇19通道啟動(dòng)轉(zhuǎn)換.
}
//===================================
//函數(shù)名:SCI_INIT
//作用: 初始化SCI1
//====================================
void SCI_INIT(void)
{
SCI1BD = 8000000/16/Baud;
SCI1C1 = 0x00;
SCI1C2 = 0x2c; //使能接受中斷
}
//===================================
//串口發(fā)送
//===================================
void SCI1send (byte x)
{
while(SCI1S1_TDRE==0);
SCI1D = x;
}
//===================================
//定時(shí)器1初始化
//===================================
void TPM1_Init(void) //定時(shí)器1初始化
{
TPM1MODH=0x3d; //
TPM1MODL=0x09; //250MS
TPM1SC=0x4F; //TPM1時(shí)鐘源為總線時(shí)鐘;分頻系數(shù)為128;溢出中斷使能
}
//===================================
//延時(shí)
//===================================
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;
}
/*********************主函數(shù)************************************/
void main(void)
{
static byte MonthF1,DateF1,MonthF2,DateF2,MonthF3,DateF3,MonthC1,DateC1,MonthC2,DateC2,MonthC3,DateC3; //充電放電歷史記錄的兩次時(shí)間變量及緩存變量
static byte FBETW0L,FBETW0H,FBETW1L,FBETW1H,CBETW0L,CBETW0H,CBETW1L,CBETW1H;
static word PupCount,PdownCount;
byte Pchange = 0 ; //充電0XFF 放電0X00轉(zhuǎn)換標(biāo)志
byte Failure; //故障信息指示MW17.0 = 1過(guò)壓 MW17.1 = 1過(guò)流
word BetweenH,BetweenL;
long a,b;
byte SendSum;
MCU_INIT();
IO_INIT();
Ds1302_Init();
ADC_INIT();
TPM1_Init();
SCI_INIT();
DE485 = 1; //485發(fā)送使能;
PTED_PTED6 = 1;
Ds1302_Read_Time(); //先讀取一次時(shí)間用于判斷歷史時(shí)間
if(!DI1) //DI1 = 0
{
Pchange = 0x01; //充電
}
if(!DI2) //DI2 = 0
{ //放電
Pchange = 0x02;
}
if(DI1&&DI2) //DI1,DI2都 為高電平
{
Pchange = 0;
}
FCDIV = 39; //設(shè)置FLASH分頻 8M總線時(shí)鐘分頻數(shù)39;FLASH讀寫(xiě)200KH
asm(nop);
//**************讀取記錄的充電的歷史記錄************************
//************前1次的前1次得充電記錄
p=(byte *)(0x1401); //
MonthC1= *p;
p=(byte *)(0x1402); //時(shí)間
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); //時(shí)間
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) //如果日期錯(cuò)誤,清零
{
MonthC2 = 0;
DateC2= 0;
MW25L = 0;
MW25H = 0;
MW26L = 0;
MW26H = 0;
}
//***************緩存的充電記錄,用于比較計(jì)算******************
p=(byte *)(0x1421); //
MonthC3= *p;
p=(byte *)(0x1422); //時(shí)間
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); //時(shí)間
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;
}
上一頁(yè) 1 2 3 下一頁(yè)

關(guān)鍵詞: 飛思卡爾DZ60AD

評(píng)論


技術(shù)專區(qū)

關(guān)閉