#include <msp430g2553.h>
#include <stdio.h>
#include <string.h>
#define A0 1<<2
#define A1 1<<3
#define A2 1<<4
#define CTRL 1<<3
unsigned long int Count0=0;
float Count1=0;
float Count3=100000.0;
float Count4=2000.0;
float Count5=3000.0;
float hedongsheng=0.0;
unsigned long int Firstime=0;
unsigned long int ZeroNum=0;
unsigned long int C0; //秒计数
unsigned char FirstFlag=1; //调零标志
unsigned char CountFlag=0;
unsigned char Relay=0; //继电器状态
unsigned char Nouse=0; //30分钟重新调零,此后不再调零
unsigned char Tend[7];
char Command[25];
unsigned char RSV_COUNTER0=0;
unsigned char k1,k2,k3,fuwei,fuwei1;
/********************************************
函数名称:void Init_Systemclock(void)
功 能:初始化系统时钟,MCLK=1MHz,SMCLK=1MHz,ACLK No Use
参 数:无
返回值 :无
*********************************************/
void Init_Systemclock(void)
{
BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1 MHz
DCOCTL = CALDCO_1MHZ;
}
/********************************************
函数名称:void Init_WATCHDOG(void)
功 能:Watch Dog定时1S
参 数:无
返回值 :无
*********************************************/
void Init_WATCHDOG(void)
{
WDTCTL = WDT_ADLY_1000;// Set Watchdog Timer interval to ~1000ms
IE1 |= WDTIE;
}
/***************************从串口0接收数据*********************************/
unsigned char UART0_GetChar ( void )
{
unsigned char Rcv_Data;
while ((UCA0STAT & UCBUSY)); //等待有效数据
Rcv_Data=UCA0RXBUF; //读取数据
return(Rcv_Data);
}
/********************************************
函数名称:Init_UART(void)
功 能:初始化UART,Baud rate=9600
参 数:无
返回值 :无
*********************************************/
void Init_UART(void)
{
P1SEL |= BIT1 + BIT2 ; // Select TX and RX functionality for P1.1 & P1.2
P1SEL2 |= BIT1 + BIT2 ;
UCA0CTL1 |= UCSSEL_2; // Have USCI use System MCLK=1MHz
UCA0BR0 = 104; // 1MHz 9600, see user manual
UCA0BR1 = 0;
UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1
// UCA0CTL1 |= UCSSEL_1; // Have USCI use System MCLK=1MHz
// UCA0BR0 = 27; // 1MHz 9600, see user manual
// UCA0BR1 = 0;
// UCA0MCTL = UCBRS1; // Modulation UCBRSx = 1
UCA0CTL1 &= ~UCSWRST; // Start USCI state machine
IE2 |=UCA0RXIE;
P1DIR |=BIT3;
}
/********************************************
函数名称:Init_TA0()
功 能:初始化TA0,计算IN1的频率
参 数:无
返回值 :无
*********************************************/
void SENT485()
{
// while ((UCA0STAT & UCBUSY));
// UCA0TXBUF=0x11;
char *str_1=(char*)&Count1;
for(unsigned char i=0;i<4;i++)
{
Tend[i+2]=str_1[i];
}
Tend[0]=0x01; //设备码
Tend[1]=0x02; //功能码
Tend[6]=0x00;
for(unsigned char i=0;i<6;i++)
Tend[6]+=Tend[i];
P1OUT|=CTRL;
for(unsigned char i=0;i<7;i++)
{
while ((UCA0STAT & UCBUSY));
UCA0TXBUF=Tend[i];
}
char *str_4=(char*)&hedongsheng;
for(unsigned char i=0;i<4;i++)
{
str_4[i]=str_1[i];
}
// UART_TX(str_1);//发送计数值
while ((UCA0STAT & UCBUSY));
CountFlag=0;
P1OUT&=~BIT3;
}
void Init_TA0()
{
TA0CTL |= TASSEL_0+MC_2+TACLR; //选择TACLK时钟作为计数时钟源,2分频,连续计数模式
P1SEL |= BIT0; //P1.0为T0的时钟TACLK输入,接外部待测信号,这样T0就当作计数器用
P1SEL2&=~BIT0;
P1DIR &=~BIT0;
}
/********************************************
函数名称:UART_TX(char * tx_data)
功 能:UART transmit
参 数:无
返回值 :无
*********************************************/
void UART_TX(char * tx_data)
{
unsigned int i=0;
while(tx_data[i])
{
while ((UCA0STAT & UCBUSY));// Wait if line TX/RX module is busy with data
UCA0TXBUF = tx_data[i]; // Send out element i of tx_data array on UART bus
i++; // Increment variable for array address
}
}
//******************************************************************************
//*****************************读写flash****************************************
float data1[3]={0.0001,0.01,1.0};
float data2[3];
char *data1_adr=(char*)&data1;
float data[3]={0.0,0.0,0.0};
char *data_adr=(char*)&data;
char *Flash_ptr=(char*)0x1000;// Flash A information segement A
char *Flash_ptrB=(char*)0x1080;// Flash B information segement B
//华丽的分割线,读写Flash函数
/*void soft_reset(void)
{
}*/
/******************************************
函数名称:Write_SegB (char *ptr,int num)
功 能:写flash函数
参 数:char *ptr目标数据首地址,int num目标占用内存大小
返回值 :无
*******************************************/
void Write_SegB (char *ptr,int num)
{
FCTL2 = FWKEY + FSSEL0 + FN1;
FCTL1 = FWKEY + ERASE;
FCTL3 = FWKEY;
*Flash_ptrB=0; // 空写
FCTL1 = FWKEY + WRT;
for(char i=0;i<num;i++)
{
*(Flash_ptrB+i)=* (ptr+i);
}
FCTL1 = FWKEY;
FCTL3 = FWKEY + LOCK;
}
/******************************************
函数名称:Read_SegB(char *ptr,int num)
功 能:读flash函数
参 数:char *ptr目标数据首地址,int num目标占用内存大小
返回值 :无
*******************************************/
void Read_SegB (char *ptr,int num)
{
FCTL2 = FWKEY + FSSEL0 + FN1;
FCTL1 = FWKEY + ERASE;
FCTL3 = FWKEY;
FCTL1 = FWKEY + WRT;
for(int i=0;i<num;i++)
{
*(ptr+i)=*(Flash_ptrB+i);
}
FCTL1 = FWKEY;
FCTL3 = FWKEY + LOCK;
}
//******************************************************************************
//*****************************以上是读写flash**********************************
//******************************************************************************
/********************************************
函数名称:main()
功 能:无
参 数:无
返回值 :无
*********************************************/
void main(void)
{
Init_Systemclock();
Init_WATCHDOG();
Init_UART();
Init_TA0();
// Write_SegB(data1_adr,12);
P3DIR|=BIT2+BIT3+BIT4; //2,3,4脚变为输出
P3OUT|=BIT2;
P3OUT&=~BIT3;
P3OUT&=~BIT4;
_EINT(); // 使能中断
while(FirstFlag)
{
if(Firstime==9)
{
ZeroNum=Count0;
}
if(Firstime==10)
{
ZeroNum+=Count0;
ZeroNum/=2;
FirstFlag=0;
}
}
P3OUT|=BIT4;
P3OUT&=~BIT2;
Relay=3;
// Count0=0;
while(1) // While 1 is equal to 1 (forever)
{
if(fuwei)
{
P3OUT|=BIT2;
P3OUT&=~BIT3;
P3OUT&=~BIT4;
fuwei=0;
SENT485();
}
if(fuwei1)
{
ZeroNum=Count0;
P3OUT|=BIT4;
P3OUT&=~BIT2;
Relay=3;
fuwei1=0;
SENT485();
}
if(CountFlag)
{
Read_SegB(data_adr,12);
switch(Relay)
{
case 3:
{
Count1=(Count0-ZeroNum)/data[0];
if(Count0<65)
{
P3OUT|=BIT3;
P3OUT&=~BIT4;
Relay=2;
}
}
break;
case 2:
{
Count1=(Count0-ZeroNum)/data[1];
if(Count0<65)
{
P3OUT&=~BIT2;
P3OUT&=~BIT3;
Relay=1;
}
if(Count0>9900)
{
P3OUT|=BIT4;
P3OUT&=~BIT3;
Relay=3;
}
}
break;
case 1:
{
Count1=(Count0-ZeroNum)/data[2];
if(Count0>9900)
{
P3OUT|=BIT3;
P3OUT&=~BIT2;
Relay=2;
}
}
break;