#include "sys.h"
#include "delay.h"
#include "tim1.h"
#include "pwm.h"
#include "stdio.h"
#include "usart.h"
#include "key.h"
#include "LCD12864.h"
#include "rs485.h"
#include "IIC.h"
#define ID 0X30
#define BIT(A,B) ((A>>B)&0x01) // A ???
u8 Cur_Addr[] = {0x80,0x85,0x90,0x8d};
u8 cycle0to100[]={0,100};
u8 FrameID, SPEED=0X22;
u16 i=0,T1=0,T2=0,T3=0,TT1,TT2,TT3,TT4,TT5,TT6;
u32 flowdata=0,mearsured_flowdata=0;
typedef enum
{ FALSE=0,
TRUE=!FALSE}
bool;
struct message {
unsigned char identifier;
unsigned char data[8];
};
struct message1 {
unsigned char identifier;
unsigned char data[2];
};
unsigned int schedule_count=0,Master_count=0,Slave_count1=100,Slave_count2=200,Slave_count3=300,Master_ID=0x85,Slave_ID1=0x13,Slave_ID2=0,
Slave_ID3=0,Total_count=800;
unsigned int m=0,c=1,MM=0,kk, flag_break,Cursor_flag,Customer=2,Geely_F_position=0,
Geely_F_mode=0,Geely_F_Status=0,Geely_T_position=0,Geely_T_mode=0,Geely_T_Status=0,XP_T1_position=0,XP_T1_mode=0,XP_T1_Status=0
,XP_T2_position=0,XP_T2_mode=0,XP_T2_Status=0,XP_F_position=0,XP_F_mode=0,XP_F_Status=0,Generic_position=0;
unsigned int traget_speed=0 ,actual_speed=0,traget_speed1=0 ,actual_speed1=0,supply_voltage1=0,supply_current1=0,temperature1,voltage_status=0,speed_status=0,current_status=0,temperature_status=0,roling_counter=0,Stall_detection_status=0,block_eeeor=0,inter_error=0,Response_error;
unsigned int Error_sattus=0,running_mode=0,position_feedback=0,move_status=0,speed_level=0,Temperature_status=0,supply_voltage_status=0,Lin_communication_status=0 ;
bool a,b,B;
u16 C[10],ii=0,data1,addr=0,D[10],iii=0,E[10],iiii=0,F[10],iiiii=0,G[10],iiiiii=0,H[10],iiiiiii=0,Sum_C=0,Sum_D=0,Sum_E=0,Sum_F=0,Sum_G=0,Sum_H=0;
unsigned int receive_data,KEY_Down_flag=0,Number_add_flag=0,Number_value_0=0,Number_value_1=0
,X_Number_value_0=0,X_Number_value_1=0
,Number_value_2=0,Number_value_3=0,Number_value_4=0,Number_value_5=0,Number_value_6=0,Number_value_7=0,generic_value=0,Generic_Status=0,Generic_mode=0;
unsigned int traget_speed ,actual_speed,cycle_time,
traget_speed1 ,actual_speed1,supply_voltage1,supply_current1,temperature1;
float supply_voltage,supply_current,temperature;
struct message msg_send;
struct message msg_get;
struct message1 msg_send1;
struct message1 msg_get1;
unsigned int change=0x33,position=0xff;
enum lin_state { IDLE, _BREAK, SYNCH, PROTECTED_IDENTIFIER, DATA0, DATA1,DATA2, DATA3,DATA4, DATA5,DATA6, DATA7,
CHECKSUM };
enum lin_state1 { IDLE1, _BREAK1, SYNCH1, PROTECTED_IDENTIFIER1, DATA10, DATA11,
CHECKSUM1 };
struct frame {
unsigned char protected_id;
unsigned char data[8];
unsigned char check;
enum lin_state state;
unsigned char error;
};
struct frame frame_send,frame_receive;
struct frame1 {
unsigned char protected_id;
unsigned char data[2];
unsigned char check;
enum lin_state state;
unsigned char error;
};
struct frame1 frame_send1,frame_receive1;
void uart1_init(void)
{ char i,j;
frame_send.protected_id=0;
frame_send.state=IDLE;
frame_send.error=0;
frame_send.check=0;
for (i=0;i<8;i++)
frame_send1.data[i]=0;
frame_send1.protected_id=0;
frame_send1.state=IDLE;
frame_send1.error=0;
frame_send1.check=0;
for (j=0;j<2;j++)
frame_send1.data[j]=0;
}
bool LINSendbreak(void)
{
while(!(USART1->SR&0X0080)); //?????????(???)??
USART1->CR1|=1<<0;
USART1->CR1|=0<<0;
return(TRUE);
}
unsigned char LINCalcParity1(unsigned char id)
{unsigned char parity1, p0,p1;
parity1=id;
p0=(BIT(parity1,0)^BIT(parity1,1)^BIT(parity1,2)^BIT(parity1,4))<<6; //????
p1=(!(BIT(parity1,1)^BIT(parity1,3)^BIT(parity1,4)^BIT(parity1,5)))<<7; //????
parity1|=(p0|p1);
return parity1;
}
unsigned char LINCalcParity(unsigned char id)
{unsigned char parity, p0,p1;
parity=id;
p0=(BIT(parity,0)^BIT(parity,1)^BIT(parity,2)^BIT(parity,4))<<6; //????
p1=(!(BIT(parity,1)^BIT(parity,3)^BIT(parity,4)^BIT(parity,5)))<<7; //????
parity|=(p0|p1);
return parity;
}
unsigned char LINCalcChecksum(unsigned char *data)
{
unsigned char i;
unsigned int sum = 0;
sum+=Master_ID;
for(i = 0; i < 8; i++)
{
sum += data[i];
/* if(sum&0xc600)
sum = (sum&0x00c6) + 1;*/
}
if(sum<0x100)
{
sum = 0xFF-sum;
}
else if(sum>0xFF) {
sum=0xFF-sum%255;
}
return (unsigned char)sum;
}
unsigned char LINCalcChecksum1(unsigned char *data)
{
unsigned char i;
unsigned int sum = 0;
sum+=Master_ID;
for(i = 0; i < 2; i++)
{
sum += data[i];
/* if(sum&0xc600)
sum = (sum&0x00c6) + 1;*/
}
if(sum<0x100)
{
sum = 0xFF-sum;
}
else if(sum>0xFF) {
sum=0xFF-sum%255;
}
return (unsigned char)sum;
}
/* sum = data[0]+data[1]+msg_send.identifier;
if(sum&0xff) {
sum = (sum&0xff)+1;
}
sum^=0xff;
return (unsigned char)sum;
}*/
bool LINCheckSend(enum lin_state status, unsigned char val)
{
while(frame_send.state < status)
if(frame_send.error)
return(FALSE);
switch(status)
{
case _BREAK:
break;
case SYNCH:
break;
case PROTECTED_IDENTIFIER:
if(frame_send.protected_id != val)
return(FALSE);
break;
case DATA0:
case DATA1:
case DATA2:
case DATA3:
case DATA4:
case DATA5:
case DATA6:
case DATA7:
if(frame_send.data[status-DATA0] != val)
return(FALSE);
break;
case CHECKSUM:
if(frame_send.check != val)
return(FALSE);
break;
}
return(TRUE);
}
bool LINCheckSend1(enum lin_state1 status, unsigned char val)
{
while(frame_send1.state < status)
if(frame_send1.error)
return(FALSE);
switch(status)
{
case _BREAK1:
break;
case SYNCH1:
break;
case PROTECTED_IDENTIFIER1:
if(frame_send1.protected_id != val)
return(FALSE);
break;
case DATA10:
case DATA11:
/*case DATA2:
case DATA3:
case DATA4:
case DATA5:
case DATA6:
case DATA7: */
if(frame_send1.data[status-DATA10] != val)
return(FALSE);
break;
case CHECKSUM1:
if(frame_send1.check != val)
return(FALSE);
break;
}
return(TRUE);
}
/*************************************************************/
/* LIN?????? */
/*************************************************************/
bool LINSendChar(unsigned char ch)
{
while(!((USART1->SR&0X0080))); //?????????(???)??
USART1->DR = ch;
return(TRUE);
}
bool LINSendChar1(unsigned char ch)
{
while(!((USART1->SR&0X0080))); //?????????(???)??
USART1->DR = ch;
return(TRUE);
}
/* LIN?????? */
/*************************************************************/
bool LINSendMsg(void)
{
unsigned char check_sum, parity_id, i;
if(!LINSendbreak())
return(FALSE);
if(!LINSendChar(0x55))
return(FALSE);
parity_id=LINCalcParity1(msg_send.identifier);
if(!LINSendChar(parity_id))
return(FALSE);
for(i=0; i < 8; i++)
{
if(!LINSendChar(msg_send.data[i]))
return(FALSE); }
check_sum = LINCalcChecksum(msg_send.data);
if(!LINSendChar(check_sum))
return(FALSE);
}
bool LINSend1Msg(void)
{
unsigned char check_sum, parity_id1, i;
if(!LINSendbreak())
return(FALSE);
if(!LINSendChar(0x55))
return(FALSE);
parity_id1=LINCalcParity(msg_send.identifier);
if(!LINSendChar(parity_id1))
return(FALSE);
}
bool LINSendMsg1(void)
{
unsigned char check_sum, parity_id, i;
if(!LINSendbreak())
return(FALSE);
if(!LINSendChar1(0x55))
return(FALSE);
parity_id=LINCalcParity1(msg_send1.identifier);
if(!LINSendChar1(parity_id))
return(FALSE);
for(i=0; i < 2; i++)
{
if(!LINSendChar1(msg_send1.data[i]))
return(FALSE); }
check_sum = LINCalcChecksum1(msg_send1.data);
if(!LINSendChar1(check_sum))
return(FALSE);
}
bool LINSend1Msg1(void)
{
unsigned char check_sum, parity_id1, i;
if(!LINSendbreak())
return(FALSE);
if(!LINSendChar1(0x55))
return(FALSE);
parity_id1=LINCalcParity1(msg_send1.identifier);
if(!LINSendChar(parity_id1))
return(FALSE);
}
bool LIN1GetChar(void)
{
unsigned volatile char ch;
// LIN??????
switch(frame_send.state)
{
case IDLE:
if(!(USART1->SR&0x22))
return(FALSE);
if( USART1->DR)
return(FALSE);
break;
case _BREAK:
if(!(USART1->SR&0x20))
return(FALSE);
if(USART1->DR != 0x55)
return(FALSE);
break;
case SYNCH:
if(!(USART1->SR&0x20))
return(FALSE);
ch = USART1->DR;
frame_send.protected_id = ch;
break;
case PROTECTED_IDENTIFIER:
case DATA0:
case DATA1:
case DATA2:
case DATA3:
case DATA4:
case DATA5:
case DATA6:
if(!(USART1->SR&0x20))
return(FALSE);
ch =USART1->DR;
frame_send.data[frame_send.state-PROTECTED_IDENTIFIER] = ch;
break;
case DATA7:
if(!(USART1->SR&0x20))
return(FALSE);
ch =USART1->DR;
frame_send.check = ch;
break;
case CHECKSUM:
return(FALSE);
}
frame_send.state+=1;
return(TRUE);
}
bool LIN1GetChar1(void)
{
unsigned volatile char ch;
// LIN??????
switch(frame_send1.state)
{
case IDLE1:
if(!(USART1->SR&0x22))
return(FALSE);
if( USART1->DR)
return(FALSE);
break;
case _BREAK1:
if(!(USART1->SR&0x20))
return(FALSE);
if(USART1->DR != 0x55)
return(FALSE);
break;
case SYNCH1:
if(!(USART1->SR&0x20))
return(FALSE);
ch = USART1->DR;
frame_send1.protected_id = ch;
break;
case PROTECTED_IDENTIFIER1:
case DATA10:
case DATA11:
/*case DATA2:
case DATA3:
case DATA4:
case DATA5:
case DATA6: */
if(!(USART1->SR&0x20))
return(FALSE);
ch =USART1->DR;
frame_send1.data[frame_send1.state-PROTECTED_IDENTIFIER1] = ch;
break;
// case DATA7:
if(!(USART1->SR&0x20))
return(FALSE);
ch =USART1->DR;
frame_send1.check = ch;
break;
case CHECKSUM1:
return(FALSE);
}
frame_send1.state+=1;
return(TRUE);
}
void GPIO_Configuration(void)
{ RCC->APB2ENR|=1<<4; //使能PORTC时钟
GPIOC->CRL&=0XFF0FFFFF;
GPIOC->CRH|=0X00300000;//PC.13 推挽输出
GPIOC->ODR|=1<<5; //PC. 输出高
RCC->APB2ENR|=1<<2; //使能PORTA时钟
GPIOA->CRL&=0XFF00FFFF;
GPIOA->CRL|=0X00330000;//PA.4.5 推挽输出
GPIOA->ODR|=3<<4; //PB.8输出高
}
void lcd_sfm(unsigned char add,int data)//显示位置和数据
{
unsigned char qian,bai, shi,ge;
qian =data/1000;
bai =(data-qian*1000)/100;
shi=(data-qian*1000-bai*100)/10;
ge=(data-qian*1000-bai*100-shi*10)%10;
Write_Com(add);
//Write_Date(0x30+qian);
Write_Date(0x30+bai);
// Write_Date(0x2e);
Write_Date(0x30+shi);
Write_Date(0x30+ge);
}
void lcd2_sfm(unsigned char add,int data)//显示位置和数据
{
unsigned char qian,bai, shi,ge;
qian =data/1000;
bai =(data-qian*1000)/100;
shi=(data-qian*1000-bai*100)/10;
ge=(data-qian*1000-bai*100-shi*10)%10;
Write_Com(add);
Write_Date(0x30+0);
Write_Date(0x30+qian);
Write_Date(0x30+bai);
Write_Date(0x30+shi);
Write_Date(0x30+ge);
}
void lcd3_sfm(unsigned char add,int data)//显示位置和数据
{
unsigned char wan, qian,bai, shi,ge;
//wan=data/10000;
//qian =(data-wan*10000)/1000;
//bai =(data-wan*10000-qian*1000)/100;
//shi=(data-wan*10000-qian*1000-bai*100)/10;
ge=data%10;
//Write_Com(add);
//Write_Date(0x30+wan);
//Write_Date(0x30+qian);
//Write_Date(0x30+bai);
//Write_Date(0x30+shi);
Write_Date(0x30+ge);
}
void lcd1_sfm(unsigned char add,int data)//显示位置和数据
{
unsigned char qian,bai, shi,ge;
qian =0;
bai =(data-qian*1000)/100;
shi=(data-qian*1000-bai*100)/10;
ge=(data-qian*1000-bai*100-shi*10)%10;
Write_Com(add);
Write_Date(0x30+qian);
Write_Date(0x2e);
Write_Date(0x30+bai);
Write_Date(0x30+shi);
Write_Date(0x30+ge);
}
void TIM3_Int_Init(u16 arr,u32 psc)
{
RCC->APB1ENR|=1<<1; //TIM3时钟使能
TIM3->ARR=arr; //设定计数器自动重装值//刚好1ms
TIM3->PSC=psc; //预分频器7200,得到10Khz的计数时钟
TIM3->DIER|=1<<0; //允许更新中断
TIM3->CR1|=0x01; //使能定时器3
MY_NVIC_Init(1,1,TIM3_IRQn,1);//抢占1,子优先级3,组2
}
void TIM4_Int_Init(u16 arr,u32 psc)
{
RCC->APB1ENR|=1<<2; //TIM4时钟使能
TIM4->ARR=arr; //设定计数器自动重装值//刚好1ms
TIM4->PSC=psc; //预分频器7200,得到10Khz的计数时钟
TIM4->DIER|=1<<0; //允许更新中断
TIM4->CR1|=0x01; //使能定时器3
MY_NVIC_Init(2,2,TIM4_IRQn,1);//抢占1,子优先级3,组2
}
//............12864 Course view............//
void Dis_Cur()
{ Write_Com(Cur_Addr[addr]);
delay_ms(10);
Write_Com(0x0f);
;
delay_ms(1000);
Write_Com(0x0c);
}
int main(void)
{ u8 key=0;
u8 i=0,t=0;
u8 cnt=0;
u8 rs485buf_Send[5],rs485buf_Received[5];
u8 str[] = "ETC";
Stm32_Clock_Init(9); //系统时钟设置
uart_init(72,19200); //串口初始化为115200
delay_init(72);
GPIO_Configuration();
TIM3_Int_Init(1000,71);//10Khz的计数频率,计数5K次为500ms
uart1_init();
KEY_Init();
TIM4_Int_Init(9999,7199);
//PWM_Init();
//delay_s(1);
Init_12864();
//IIC_Init();
// RS485_Init(36,9600); //初始化RS485
/*msg_send.identifier = ID; // ???
msg_send.data[0] = 0x00; // ??
msg_send.data[1] = 0x00;
msg_send.data[2] = 0x00;
msg_send.data[3] = 0x00;
msg_send.data[4] = 0x00;
msg_send.data[5] = 0x00;
msg_send.data[6] = 0x00;
msg_send.data[7] = 0x00;
frame_send.state = IDLE; */
PAout(5)=1;
PAout(4)=1;
LCD12864xie(0,0,"XP-F ");
LCD12864xie(5,0,"Learn");
LCD12864xie(0,1,"SP:");
lcd_sfm(0x92,Number_value_3);
LCD12864xie(4,1,"FP:");
lcd_sfm(0x96,Geely_F_position);
LCD12864xie(0,2,"Stas:");
LCD12864xie(0,3,"Mode:");
LCD12864xie(5,2,"AUTO");
//LCD12864xie(4,2,"V:");
//LCD12864xie(4,3,"Lin:");
TIM_Init(719999,999,10);
while (1)
{ { if((Cursor==0)&&(Cursor_flag==1))
{delay_ms(10);
;
if(Cursor==0)
{ addr ++;
if(addr == 4)
{ addr = 0;}
Dis_Cur();
Cursor_flag=0;
}
}
if(Cursor==1)
{Cursor_flag=1;}
}
TIM_Init(719,100,30);
/* speed_status=(C[6]>>5)&0x03;//45,56
voltage_status=(C[6]>>7)&0x01+(C[7]&0x01)*2;//47,48
current_status=(C[7]>>1)&0x03;//49,50
temperature_status=(C[7]>>3)&0x03;//51,52
roling_counter=(C[7]>>5)&0x07+(C[7]&0x01)*8;//53,54,55,56
Stall_detection_status=(C[8]>>1)&0x03;//57,58
block_eeeor=(C[8]>>3)&0x03;//59,60
inter_error=(C[8]>>5)&0x03;//61,62
Response_error=(C[8]>>7)&0x01;//63
*/
//Geely_T_position=0,Geely_T_mode=0,Geely_T_Status=0
Sum_C=0XD3+C[1]+C[2]+C[3]+C[4]+C[5]+C[6]+C[7]+C[8];
if(Sum_C<0x100)
{Sum_C=0xff-Sum_C;}
else if(Sum_C>0xff)
{Sum_C=0xff-Sum_C%0xff;}
if(Sum_C==C[9]){
Geely_F_position=C[2]+(C[3]&0x03)*256;
Geely_F_Status=C[1]&0x0f;
Geely_F_mode=C[2]&0x01;}
Sum_D=0X2B+D[1]+D[2]+D[3]+D[4]+D[5]+D[6]+D[7]+D[8];
if(Sum_D<0x100)
{Sum_D=0xff-Sum_D;}
else if(Sum_D>0xff)
{Sum_D=0xff-Sum_D%0xff;}
if(Sum_D==D[9]){
Geely_T_position=D[2]+(D[3]&0x03)*256;
Geely_T_Status=D[1]&0x0F;
Geely_T_mode=D[2]&0x01;
}
Sum_G=0X97+G[1]+G[2]+G[3]+G[4];
if(Sum_G<0x100)
{Sum_G=0xff-Sum_G;}
if(Sum_G>0xff)
{Sum_G=0xff-Sum_G%0xff;}
if(Sum_G==G[5]){
XP_F_position=G[3]+(G[4]&0x03)*256;
XP_F_mode=G[1]&0x0F;
XP_F_Status=((G[1]>>5)&0x07)+(G[2]&0X01)*8;
}
Sum_E=0X55+E[2]+E[3]+E[4]+E[5];
if(Sum_E<0x100)
{Sum_E=0xff-Sum_E;}
if(Sum_E>0xff)
{Sum_E=0xff-Sum_E%0xff;}
if(Sum_E==E[6]){
XP_T1_position=E[4]+(E[5]&0x03)*256;
XP_T1_mode=E[2]&0x0F;
XP_T1_Status=((E[2]>>5)&0x07)+(E[3]&0X01)*8;
}
Sum_F=0XD6+F[1]+F[2]+F[3]+F[4];
if(Sum_F<0x100)
{Sum_F=0xff-Sum_F;}
if(Sum_F>0xff)
{Sum_F=0xff-Sum_F%0xff;}
if(Sum_F==F[5]){
XP_T2_position=F[3]+(F[4]&0x03)*256;
XP_T2_mode=F[1]&0x0F;
XP_T2_Status=((F[1]>>5)&0x07)+(F[2]&0X01)*8;
}
Sum_H=0X9C+H[1]+H[2]+H[3]+H[4]+H[5]+H[6]+H[7]+H[8];
if(Sum_H<0x100)
{Sum_H=0xff-Sum_H;}
else if(Sum_H>0xff)
{Sum_H=0xff-Sum_H%0xff;}
if(Sum_H==H[9]){
Generic_position=((H[4]>>6)&0x03)+(H[5]&0xff)*4;
}
/*Error_sattus=C[1]&0x0f;
running_mode=(C[1]>>4)&0x0f;
position_feedback=C[2]+(C[3]&0x03)*256;
move_status=(C[3]>>2&0x01);
speed_level=(C[3]>>3&0x03);
Temperature_status=(C[3]>>5&0x07);
supply_voltage_status=C[4]&0x03;
Lin_communication_status=C[4]>>2&0x01; */
/*lcd_sfm(0x91,Error_sattus);
lcd_sfm(0x89,running_mode);
lcd_sfm(0x95,Temperature_status);
lcd_sfm(0x8D,supply_voltage_status);
lcd_sfm(0x9A,position_feedback);
lcd_sfm(0x9E,Lin_communication_status);*/
if(Customer==0)
{LCD12864xie(0,0,"GEELY-F");
lcd_sfm(0x92,Number_value_3);
lcd_sfm(0x96,Geely_F_position/10);
lcd_sfm(0x8b,Geely_F_Status);
lcd_sfm(0x9b,Geely_F_mode);
schedule_count=0;
Master_count=0;
Slave_count1=100;
Slave_count2=200;
Slave_count3=300;
Master_ID=0x08;
Slave_ID1=0x13;
Slave_ID2=0;
Slave_ID3=0;
Total_count=400;
}
if(Customer==1)
{LCD12864xie(0,0,"GEELY-T");
lcd_sfm(0x92,Number_value_4);
lcd_sfm(0x96,Geely_T_position/10);
lcd_sfm(0x8b,Geely_T_Status);
lcd_sfm(0x9b,Geely_T_mode);
schedule_count=0;
Master_count=0;
Slave_count1=100;
Slave_count2=200;
Slave_count3=300;
Master_ID=0x08;
Slave_ID1=0x2B;
Slave_ID2=0;
Slave_ID3=0;
Total_count=400;
}
if(Customer==2)
{LCD12864xie(0,0,"XP-F ");
lcd_sfm(0x92,X_Number_value_0);
lcd_sfm(0x96,XP_F_position/10);
lcd_sfm(0x8b,XP_F_Status);
lcd_sfm(0x9b,XP_F_mode);
schedule_count=0;
Master_count=0;
Slave_count1=40;
Slave_count2=60;
Slave_count3=60;
Master_ID=0x47;
Slave_ID1=0x17;
Slave_ID2=0;
Slave_ID3=0;
Total_count=80;}
if(Customer==3)
{LCD12864xie(0,0,"XP-T1 ");
lcd_sfm(0x92,X_Number_value_0);
lcd_sfm(0x96,XP_T1_position/10);
lcd_sfm(0x8b,XP_T1_Status);
lcd_sfm(0x9b,XP_T1_mode);
schedule_count=0;
Master_count=0;
Slave_count1=40;
Slave_count2=60;
Slave_count3=60;
Master_ID=0x85;
Slave_ID1=0x15;
Slave_ID2=0;
Slave_ID3=0;
Total_count=80;}
if(Customer==4)
{LCD12864xie(0,0,"XP-T2 ");
lcd_sfm(0x92,X_Number_value_0);
lcd_sfm(0x96,XP_T2_position/10);
lcd_sfm(0x8b,XP_T2_Status);
lcd_sfm(0x9b,XP_T2_mode);
schedule_count=0;
Master_count=0;
Slave_count1=40;
Slave_count2=60;
Slave_count3=60;
Master_ID=0x06;
Slave_ID1=0x16;
Slave_ID2=0;
Slave_ID3=0;
Total_count=80;}
if(Customer==5)
{LCD12864xie(0,0,"Generic ");
lcd_sfm(0x92,generic_value/10);
lcd_sfm(0x96,Generic_position/10);
lcd_sfm(0x8b,Generic_Status);
lcd_sfm(0x9b,Generic_mode);
schedule_count=0;
Master_count=0;
Slave_count1=100;
Slave_count2=60;
Slave_count3=60;
Master_ID=0x5E;
Slave_ID1=0x1C;
Slave_ID2=0;
Slave_ID3=0;
Total_count=300;}
}}
void USART1_IRQHandler(void) //??1??????
{
if(USART1->SR&(1<<5))//
{
/*if(!LIN1GetChar())
{
frame_send.error = 1;
frame_send.state = IDLE;
} */
C[ii]=USART1->DR; //
D[iii]=USART1->DR;
E[iiii]=USART1->DR;
F[iiiii]=USART1->DR;
G[iiiiii]=USART1->DR;
H[iiiiiii]=USART1->DR;
if(C[0]==0xD3)
{
ii++ ;
if(ii>9)
{
ii=0;
}
}
if(D[0]==0x2b)
{
iii++ ;
if(iii>9)
{
iii=0;
}
}
if((E[0]==0x55))
{
iiii++ ;
if(iiii>6)
{
iiii=0;
}
}
if(F[0]==0xd6)
{
iiiii++ ;
if(iiiii>5)
{
iiiii=0;
}
}
if(G[0]==0x97)
{
iiiiii++ ;
if(iiiiii>5)
{
iiiiii=0;
}
}
if(H[0]==0x9c)
{
iiiiiii++ ;
if(iiiiiii>9)
{
iiiiiii=0;
}
}
}
}
void TIM3_IRQHandler(void)
{ if((Number_add==0)&&(Number_add_flag==1))
{
if(Number_add==0)
if(Number_add==0)
{ Number_add_flag=0;
switch(Cur_Addr[addr])
{
case 0x80:
Customer ++;
if(Customer ==6)
{Customer = 0;
}
break;
case 0x90:
if(Customer==0)
{Number_value_0=0X00;
Number_value_3++;
if(Number_value_3>100)
{
Number_value_3=0;
}
}
if(Customer==1)
{
Number_value_5=0X00;
Number_value_4++;
if(Number_value_4>100)
{
Number_value_4=0;
}
}
if((Customer==2)||(Customer==3)||(Customer==4))
{
X_Number_value_1=0X00;
X_Number_value_0++;
if(X_Number_value_0>100)
{
X_Number_value_0=0;
}
}
if(Customer==5)
{
Number_value_2=0X00;
generic_value=generic_value+10;
Number_value_0=generic_value&0x00ff;
Number_value_1=(generic_value>>8)&0x03;
if(generic_value>1000)
{
generic_value=0;
}
}
break;
default:
break;
}
}
}
if(Number_add==1)
{Number_add_flag=1;}
if(TIM3->SR&0X0001)//溢出中断
{if(Cur_Addr[addr]==0X85)
{
if(Customer==0)
{
if((Learning==0)&&(KEY_Down_flag==1))
{
delay_ms(1);
if(Learning==0)
{ Number_value_5=0X00;
Number_value_3=0Xff;
Number_value_0=0X04;
//Number_value_5=0Xff;
KEY_Down_flag=0;
}}
if(Learning==1)
{Number_value_0=0Xff;
KEY_Down_flag=1;}
}
if(Customer==1)
{
if((Learning==0)&&(KEY_Down_flag==1))
{
delay_ms(1);
if(Learning==0)
{Number_value_0=0X00;
Number_value_4=0Xff;
Number_value_5=0X01;
// Number_value_4=0X32;
KEY_Down_flag=0;
}}
if(Learning==1)
{Number_value_5=0Xff;
KEY_Down_flag=1;}
}
if(Customer==5)
{
if((Learning==0)&&(KEY_Down_flag==1))
{
delay_ms(1);
if(Learning==0)
{Number_value_0=0Xff;
Number_value_1=0Xff;
Number_value_2=0X01;
// Number_value_4=0X32;
KEY_Down_flag=0;
}}
if(Learning==1)
{Number_value_2=0X00;
KEY_Down_flag=1;}
}
if((Customer==2)||(Customer==3)||(Customer==4))
{
if((Learning==0)&&(KEY_Down_flag==1))
{
delay_ms(1);
if(Learning==0)
{
// X_Number_value_0=0X23;
X_Number_value_1=0X01;
X_Number_value_0=0Xff;
KEY_Down_flag=0;
}}
if(Learning==1)
{
X_Number_value_1=0X00;
KEY_Down_flag=1;}
}}
/*if((Number_add==0)&&(Number_add_flag==1))
{
delay_ms(1);
if(Number_add==0)
{
Number_value=Number_value+5;}
if(Number_value>99)
{Number_value=0;
}
Number_add_flag=0;
}
if(Number_add==1)
{Number_add_flag=1;} */
schedule_count+=1;
if (schedule_count==Total_count) {
schedule_count=0;}
if (schedule_count==Master_count) {
if((Customer==0)||(Customer==1)||(Customer==5))
{
msg_send.identifier = Master_ID; // ???
msg_send.data[0] = Number_value_0;
msg_send.data[1] = Number_value_1;
msg_send.data[2] = Number_value_2;
msg_send.data[3] = Number_value_3;
msg_send.data[4] = Number_value_4;
msg_send.data[5] = Number_value_5;
msg_send.data[6] = Number_value_6;
msg_send.data[7] = Number_value_7;
frame_send.state = IDLE;
a = LINSendMsg(); //LIN????
kk=msg_send.data[0];
}
if((Customer==2)||(Customer==3)||(Customer==4))
{msg_send1.identifier = Master_ID; // ???
msg_send1.data[0] = X_Number_value_0;
msg_send1.data[1] = X_Number_value_1;
a = LINSendMsg1(); //LIN????
}}
if (schedule_count==Slave_count1) {
msg_send.identifier = Slave_ID1; // ???
b = LINSend1Msg(); //LIN????
}
if (schedule_count==Slave_count2) {
// msg_send.identifier =Slave_ID2; // ???
// b = LINSend2Msg(); //LIN????
}
if (schedule_count==Slave_count3) {
// msg_send.identifier = Slave_ID3; // ???
// b = LINSend1Msg(); //LIN????
}
//a = LINSendMsg(); //LIN????
// PCout(13)=~ PCout(13);
}
TIM3->SR&=~(1<<0);//清除中断标志位
}
//通用定时器3中断初始化
//这里时钟选择为APB1的2倍,而APB1为36M
//arr:自动重装值。
//psc:时钟预分频数
//这里使用的是定时器3!
void TIM4_IRQHandler(void)
{
if(TIM4->SR&0X0001)//溢出中断
{ cycle_time++;
if(cycle_time/5==2)
{
cycle_time=0;
}
if(Cur_Addr[addr]==0X8d)
{ if(Customer==0)
{Number_value_0=0X00;
Number_value_3=cycle0to100[cycle_time/5];
}
if(Customer==1)
{
Number_value_5=0X00;
Number_value_4=cycle0to100[cycle_time/5];
}
if((Customer==2)||(Customer==3)||(Customer==4))
{
X_Number_value_1=0X00;
X_Number_value_0=cycle0to100[cycle_time/5];
}
if(Customer==5)
{
Number_value_2=0X00;
generic_value=cycle0to100[cycle_time/5]*10;
Number_value_0=generic_value&0x00ff;
Number_value_1=(generic_value>>8)&0x03;
}
}}
TIM4->SR&=~(1<<0);//清除中断标志位
}
|