#include <STC8G.H>
#include "stdio.h"
#include "intrins.h"
#include "uart.h"
#include "led.h"
#include "main.h"
#include "ADC.h"
#include "EEPROM.h"
#include "delay.h"
#include "Tim.h"
#include "crc.h"
#define CKSEL (*(unsigned char volatile xdata *)0xfe00)
#define CLKDIV (*(unsigned char volatile xdata *)0xfe01)
#define HIRCCR (*(unsigned char volatile xdata *)0xfe02)
#define XOSCCR (*(unsigned char volatile xdata *)0xfe03)
#define IRC32KCR (*(unsigned char volatile xdata *)0xfe04)
#define ADC_CH_NUM 3
bit busy;
int working_vcc=0;
unsigned char RX_State=0;
int ADC_nowmax[3]={0};
int ADC_Max[3]={0};
int circle_check=0;
int ADC_flag=0;
int ADC_count = 0;
int ADC_Max_Value=0;
char ADC_Max_buffer[3]=0;
unsigned char channel_flag=0;
unsigned char limit_current[3]={0};
unsigned short tim_flag = 0;
unsigned char tim_task = 0;
float current_t[3]=0;
extern unsigned char send_frame[11];
extern unsigned char answer_frame[11];
extern unsigned char USART2_RXD_frame_index;
extern unsigned char USART2_RXD_frame_buffer[USART1_TXD_BUFFER_LENGTH];
extern unsigned char USART2_RXD_frame_complete;
extern unsigned char RTC[4];
unsigned char current_state[3]={0};
unsigned char light_flag[3]={0};
unsigned char light_interupt_flag=0;
unsigned char light_count=0;
//重写putchar函数
char putchar(char c)
{
send_char(c);
return c;
}
void ADC_num_to_current(int *ADC)
{
int j=0;
float volt = 0;
for(j=0;j<3;j++)
{
volt = (((ADC[j]-19)/1024.0)*working_vcc);
current_t[j]=1.057*(((volt-(working_vcc/2))/(80*1.4142))*1000+800);
if(current_t[j]<=200)
current_t[j]=0;
}
}
void Delay1000ms() //@11.0592MHz
{
unsigned char i, j, k;
i = 57;
j = 27;
k = 112;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
int i=0;
GPIO_Init();
working_vcc = Get_working_vcc(); //ADC 初始化
LED_Init();
UART2_Init();
ADC_Init();
for(i=0;i<3;i++)
{
limit_current[i]=Read_IAP(0x0200+i);
}
while (1)
{
//调试打印
// Delay1000ms();
// printf("%d \n\r ",working_vcc);
// if(tim_task == 1)//20s
// {
// tim_task=0;
// stc_send_processing();
// }
if(ADC_flag == 1)
{
ADC_flag = 0;
ADC_count = 0;
ADC_num_to_current(ADC_Max);
for(i=0;i<3;i++)
{
if(current_t[i]>limit_current[i])
{
stc_fast_send();
switch(i)
{
case 0:
LED1_G = 0;
light_flag[0]=1;
break;
case 1:
LED2_G = 0;
light_flag[1]=1;
break;
case 2:
LED3_G = 0;
light_flag[2]=1;
break;
}
}
}
for(i=0;i<3;i++)
{
printf("%f ",current_t[i]);
}
for(i=0;i<3;i++)
{
ADC_Max[i]=0;
}
ADC_CONTR |= 0x40; //继续 AD 转换
}
if(USART2_RXD_frame_complete)
{
if(USART2_RXD_frame_buffer[0]==0x01)
{
switch(USART2_RXD_frame_buffer[1])
{
case 0x71:
RTC[0]=USART2_RXD_frame_buffer[2];
RTC[1]=USART2_RXD_frame_buffer[3];
RTC[2]=USART2_RXD_frame_buffer[4];
RTC[3]=USART2_RXD_frame_buffer[5];
current_state[0]=USART2_RXD_frame_buffer[6];
current_state[1]=USART2_RXD_frame_buffer[7];
current_state[2]=USART2_RXD_frame_buffer[8];
for(i=0;i<3;i++)
{
if(current_state[i]==0)
{
light_flag[i]=2;
}
}
break;
case 0x21:
Get_Answer_Frame();
send_string(answer_frame);
Sector_Erase_IAP(0x0200);
for(i=0;i<3;i++)
{
Write_IAP(*(USART2_RXD_frame_buffer+3+i),0x0200+i);
}
break;
default:
break;
}
}
for(i=0;i<USART1_TXD_BUFFER_LENGTH;i++)
{
USART2_RXD_frame_buffer[i]=0;
}
USART2_RXD_frame_index=0;
USART2_RXD_frame_complete=0;
}
}
}
void TM0_Isr() interrupt 1
{
tim_flag++;
for(light_interupt_flag=0;light_interupt_flag<3;light_interupt_flag++)
{
switch(light_flag[light_interupt_flag])//模式标志位
{
case 1://过流闪烁
switch(light_interupt_flag)
{
case 0:
LED1_R=!LED1_R;
break;
case 1:
LED2_R=!LED2_R;
break;
case 2:
LED3_R=!LED3_R;
break;
}
break;
case 2://关闭后闪烁2~3s
if(light_count<3)
{
switch(light_interupt_flag)
{
case 0:
LED1_R=!LED1_R;
break;
case 1:
LED2_R=!LED2_R;
break;
case 2:
LED3_R=!LED3_R;
break;
}
light_count++;
}
else
{
light_count=0;
light_flag[light_interupt_flag]=0;
switch(light_interupt_flag)
{
case 0:
LED1_R=0;
LED1_G=1;
break;
case 1:
LED2_R=0;
LED2_G=1;
break;
case 3:
LED3_R=0;
LED3_G=1;
break;
}
}
break;
default :
break;
}
}
if(tim_flag == 200)
{
tim_flag=0;
tim_task =1;
}
}
/*
1.注意每进一次中断,只能读取一个通道的ADC(无法在一次中断中读取三个通道的ADC)!!!
2.如果增加通道仅需要修改ADC_CH_NUM,channel_flag这个数组;
例如,4个通道 ADC_CH_NUM=4 channel_flag[4]={0,1,2,3};channel_flag数组值必须从0开始
*/
void ADC_Isr() interrupt 5
{
ADC_CONTR &= ~0x20; //清中断标志
for(circle_check = 0;circle_check<ADC_CH_NUM;circle_check++)
{
if(channel_flag == circle_check)
{
ADC_nowmax[channel_flag]=ADC_RES << 8 | ADC_RESL; //读取 ADC 结果
if(ADC_Max[channel_flag]<ADC_nowmax[channel_flag])
ADC_Max[channel_flag]=ADC_nowmax[channel_flag];
//printf("%d ",ADC_Max[channel_flag]);
switch(channel_flag)
{
case 0:
P1M0 = 0x00;
P1M1 = 0x02;//设置 P1.1为 ADC 口
ADC_CONTR|=0x01;
ADC_CONTR&=~0x0E;//1110
break;
case 1:
P1M0 = 0x00;
P1M1 = 0x00;//设置 P1.1为 ADC 口
P3M0 = 0x00;
P3M1 = 0x40;//设置 P1.1为 ADC 口
ADC_CONTR|=0x0E;
ADC_CONTR&=~0x01;
break;
case 2:
P3M0 = 0x00;
P3M1 = 0x00;//设置 P1.1为 ADC 口
P1M0 = 0x00;
P1M1 = 0x01;//设置 P1.1为 ADC 口
ADC_CONTR&=~0x0F;
break;
}
break;
}
}
channel_flag++;
if(channel_flag ==3)
{
channel_flag=0;
ADC_count++;
}
if(ADC_count == 285)
ADC_flag=1;
else
ADC_CONTR |= 0x40; //继续 AD 转换
}