基于飛思卡爾DZ60的AD 1302 KEY 485 CAN FLASH LCD程序
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