#include "tq_sdt_led.h" #include "ALL_Includes.h" #include #include "stm32f0xx_exti.h" #include "stm32f0xx_syscfg.h" #include "tq.h" uint8_t data_[20]; uint16_t data1_[20]; uint8_t buf [40]; uint8_t i,pos,posi,sendpos,temp,startTQ=0; uint16_t timer; extern uint8_t sendbuf[10]; extern uint8_t MyType; extern void sendstr(void); ///////////////////////////////////////////////////////////////////////////////////////////////////////// void NVIC_init(uint8_t NVIC_IRQChannel,uint8_t Priority) { NVIC_InitTypeDef NVIC_InitStruct; NVIC_InitStruct.NVIC_IRQChannel = NVIC_IRQChannel; NVIC_InitStruct.NVIC_IRQChannelPriority = Priority; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStruct); } void TQ_SDT_Init(void) { EXTI_InitTypeDef EXTI_InitStruct; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); config_io_PuPd_DOWN(GPIOA, GPIO_Pin_5); config_io_PuPd_DOWN(GPIOA, GPIO_Pin_7); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); NVIC_init(EXTI4_15_IRQn,0x02); SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource5); EXTI_ClearITPendingBit(EXTI_Line5); EXTI_InitStruct.EXTI_Line = EXTI_Line5; EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt; if (MyType == 6) EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling; else EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStruct.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStruct); if (MyType==2 || MyType==5 || MyType==6|| MyType==7) //EZ4300 or 1040XL or 2500 { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); config_io_PuPd_DOWN(GPIOB, GPIO_Pin_1); NVIC_init(EXTI0_1_IRQn,0x01); SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource1); EXTI_ClearITPendingBit(EXTI_Line1); EXTI_InitStruct.EXTI_Line = EXTI_Line1; EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt; if (MyType == 6 || MyType == 7) EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling; else EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStruct.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStruct); } } void I2C_InitConfig(void) { GPIO_InitTypeDef GPIO_InitStructure; I2C_InitTypeDef I2C_InitStructure; NVIC_InitTypeDef NVIC_InitStruct; //GPIO配置,I2C1:PA9->SCL, PA10->SDA RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_4); GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_4); //I2C初始化 RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK); //I2C1时钟源设置为SYSCLK I2C_DeInit(I2C1); I2C_Cmd(I2C1, DISABLE); I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; //I2C模式 I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;//7位地址模式 I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable; //模拟滤波启用 I2C_InitStructure.I2C_DigitalFilter = 0x00; //数字滤波禁用 I2C_InitStructure.I2C_OwnAddress1 = 0x38<<1; //本机I2C地址 I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; //ACK启用 I2C_InitStructure.I2C_Timing = 0x20E30000; I2C_Init(I2C1, &I2C_InitStructure); I2C1->CR1|=0x7F<<1; //开启中断 NVIC_init(I2C1_IRQn,0x01); NVIC_Init(&NVIC_InitStruct); I2C_Cmd(I2C1, ENABLE); //开启I2C } void printnum_1040(int iii) { switch (iii) { case 0x1100: sendbuf[sendpos] = 0x2D; break; case 0x00FC: sendbuf[sendpos] = 0x30; break; case 0x0018: sendbuf[sendpos] = 0x31; break; case 0x116C: sendbuf[sendpos] = 0x32; break; case 0x113C: sendbuf[sendpos] = 0x33; break; case 0x1198: sendbuf[sendpos] = 0x34; break; case 0x11B4: sendbuf[sendpos] = 0x35; break; case 0x11F4: sendbuf[sendpos] = 0x36; break; case 0x001C: sendbuf[sendpos] = 0x37; break; case 0x11FC: sendbuf[sendpos] = 0x38; break; case 0x119C: sendbuf[sendpos] = 0x39; break; case 0x02: sendbuf[sendpos] = 0x02; break; //case 0x20: sendbuf[sendpos] = 0x20; break; case 0x0D: sendbuf[sendpos] = 0x0D; break; case 0x00: sendbuf[sendpos] = 0x20; break; default: sendbuf[sendpos] = 0xFF; break; } sendpos++; } void printnum_2500(int iii) { switch (iii) { case 0x2529: sendbuf[sendpos] = 0x30; break; case 0x0500: sendbuf[sendpos] = 0x31; break; case 0x2325: sendbuf[sendpos] = 0x32; break; case 0x2724: sendbuf[sendpos] = 0x33; break; case 0x070C: sendbuf[sendpos] = 0x34; break; case 0x262C: sendbuf[sendpos] = 0x35; break; case 0x262D: sendbuf[sendpos] = 0x36; break; case 0x2625: sendbuf[sendpos] = 0x36; break; case 0x0520: sendbuf[sendpos] = 0x37; break; case 0x272D: sendbuf[sendpos] = 0x38; break; case 0x272C: sendbuf[sendpos] = 0x39; break; case 0x0204: sendbuf[sendpos] = 0x2D; break; case 0x02: sendbuf[sendpos] = 0x02; break; case 0x20: sendbuf[sendpos] = 0x20; break; case 0x0D: sendbuf[sendpos] = 0x0D; break; default: sendbuf[sendpos] = 0x20; break; } sendpos++; } void EXTI0_1_IRQHandler(void) { u8 i; if ( EXTI_GetITStatus(EXTI_Line1)!= RESET ) { EXTI_ClearITPendingBit(EXTI_Line1); if (MyType==5 && pos>=113) { sendpos=0; for (i=0;i<6;i++) sendbuf[i]=0; printnum_1040(data1_[4]); printnum_1040(data1_[3]); printnum_1040(data1_[2]); printnum_1040(data1_[1]); printnum_1040(data1_[0]); printnum_1040(0x0D); for(i=0;i<20;i++) data1_[i]=0; sendstr(); }; pos=0; posi=0; startTQ=1; } } void printnum_sitrex(int iii) { switch (iii) { case 0x7E: sendbuf[sendpos] = 0x30; break; case 0x0C: sendbuf[sendpos] = 0x31; break; case 0xB6: sendbuf[sendpos] = 0x32; break; case 0x9E: sendbuf[sendpos] = 0x33; break; case 0xCC: sendbuf[sendpos] = 0x34; break; case 0xDA: sendbuf[sendpos] = 0x35; break; case 0xFA: sendbuf[sendpos] = 0x36; break; case 0x0E: sendbuf[sendpos] = 0x37; break; case 0xFE: sendbuf[sendpos] = 0x38; break; case 0xDE: sendbuf[sendpos] = 0x39; break; case 0x80: sendbuf[sendpos] = 0x2D; break; case 0x02: sendbuf[sendpos] = 0x02; break; case 0x20: sendbuf[sendpos] = 0x20; break; case 0x0D: sendbuf[sendpos] = 0x0D; break; default: sendbuf[sendpos] = 0x20; break; } sendpos++; } void EXTI4_15_IRQHandler(void) { u8 i; if ( EXTI_GetITStatus(EXTI_Line5)!= RESET ) { EXTI_ClearITPendingBit(EXTI_Line5); temp = ((GPIOA->IDR&GPIO_Pin_7)>0)&1; if (MyType==1) { if (pos>0 && pos<9 ) data_[0]=(data_[0]<<1)|temp; else if (pos<17) data_[1]=(data_[1]<<1)|temp; else if (pos<25) data_[2]=(data_[2]<<1)|temp; else if (pos<33) data_[3]=(data_[3]<<1)|temp; else if (pos<=35) data_[4]=(data_[4]<<1)|temp; pos++; } else if (MyType==2) { if (startTQ) { posi=pos>>3; data_[posi]=(data_[posi]<<1)|temp; pos++; if (pos>=143) { EZ_TQ();//结束 pos=0; startTQ=0; } } } else if (MyType==4) { posi=pos>>3; data_[posi]=(data_[posi]<<1)|temp; pos++; if (posi==12) { sendpos = 0; for (i=7;i<12;i++) printnum_sitrex(data_[i]); printnum_sitrex(0x0D); sendstr(); } else if (pos%8==0) { if ((data_[posi] ==0x04) || (data_[posi] ==0x08)) { pos = 8; data_[0] = data_[posi]; // startTQ=1; } } } else if (MyType==5) { temp = ~temp; if (startTQ) { if (pos>96 && pos<113) { data1_[0]=data1_[0]<<1; data1_[0]=data1_[0]|(temp&0x01); } else if (pos>80) { data1_[1]=data1_[1]<<1; data1_[1]=data1_[1]|(temp&0x01); } else if (pos>64) { data1_[2]=data1_[2]<<1; data1_[2]=data1_[2]|(temp&0x01); } else if (pos>48) { data1_[3]=data1_[3]<<1; data1_[3]=data1_[3]|(temp&0x01); } else if (pos>32) { data1_[4]=data1_[4]<<1; data1_[4]=data1_[4]|(temp&0x01); } else if (pos>16) { data1_[5]=data1_[5]<<1; data1_[5]=data1_[5]|(temp&0x011); } pos++; } } else if (MyType == 6||MyType == 7) { if (startTQ) { posi=pos>>3; data_[posi]=(data_[posi]<<1)|temp; pos++; if (pos>=160) { EZ_2500();//结束 for(i=0;i<20;i++) data_[i]=0; pos=0; startTQ=0; } } } } } void printnum_(uint8_t iii) { iii = (iii&0xFE)>>1; switch (iii) { case 0x01: sendbuf[sendpos] = 0x2D; break; case 0x7E: sendbuf[sendpos] = 0x30; break; case 0x30: sendbuf[sendpos] = 0x31; break; case 0x6D: sendbuf[sendpos] = 0x32; break; case 0x79: sendbuf[sendpos] = 0x33; break; /////////////////// case 0x33: sendbuf[sendpos] = 0x34; break; case 0x5B: sendbuf[sendpos] = 0x35; break; //////////// case 0x5F: sendbuf[sendpos] = 0x36; break; ////////////// case 0x70: sendbuf[sendpos] = 0x37; break; case 0x7F: sendbuf[sendpos] = 0x38; break; //////////////////// case 0x7B: sendbuf[sendpos] = 0x39; break; case 0x02: sendbuf[sendpos] = 0x02; break; case 0x20: sendbuf[sendpos] = 0x20; break; case 0x0D: sendbuf[sendpos] = 0x0D; break; case 0x00: sendbuf[sendpos] = 0x20; break; case 0x40: sendbuf[sendpos] = 0x20; break; default: sendbuf[sendpos] = 0xFF; break; } sendpos++; } void printnum_ez(int iii) { switch (iii) { case 0x2625: sendbuf[sendpos] = 0x30; break; case 0x2200: sendbuf[sendpos] = 0x31; break; case 0x07A4: sendbuf[sendpos] = 0x32; break; case 0x2784: sendbuf[sendpos] = 0x33; break; case 0x2381: sendbuf[sendpos] = 0x34; break; case 0x2585: sendbuf[sendpos] = 0x35; break; case 0x25A5: sendbuf[sendpos] = 0x36; break; case 0x2204: sendbuf[sendpos] = 0x37; break; case 0x27A5: sendbuf[sendpos] = 0x38; break; case 0x2785: sendbuf[sendpos] = 0x39; break; case 0x0300: sendbuf[sendpos] = 0x2D; break; case 0x02: sendbuf[sendpos] = 0x02; break; case 0x20: sendbuf[sendpos] = 0x20; break; case 0x0D: sendbuf[sendpos] = 0x0D; break; default: sendbuf[sendpos] = 0x20; break; } sendpos++; } void EZ_TQ(void) { int x,y,z, xx; x = data_[6] & 0x40; if (x==0x40) { sendpos = 0; printnum_ez(0x02); x = (data_[9] & 0x0F)<<10; y = (data_[13] & 0x1F)<<5; z = (data_[17] & 0x1F); xx = x|y|z; printnum_ez(xx); if ((data_[8] & 0x07)==0x02 && (data_[12] & 0x07)==0x04) printnum_ez(0x2200); else printnum_ez(0x20); x = (data_[8] & 0x78)<<7; y = (data_[12] & 0xF8)<<2; z = (data_[16] & 0xF8)>>3; xx = x|y|z; printnum_ez(xx); x = (data_[7] & 0x0F)<<10; y = (data_[11] & 0x1F)<<5; z = data_[15] & 0x1F; xx = x|y|z; printnum_ez(xx); x = (((data_[6] & 0x01)<<3)|((data_[7] & 0xE0)>>5))<<10; y = (((data_[10] & 0x03)<<3)|((data_[11] & 0xE0)>>5))<<5; z = (((data_[14] & 0x03)<<3)|((data_[15] & 0xE0)>>5)); xx = x|y|z; printnum_ez(xx); x = (data_[6] & 0x3C)<<8; y = (data_[10] & 0x7C)<<3; z = (data_[14] & 0x7C)>>2; xx = x|y|z; printnum_ez(xx); printnum_ez(0x0D); sendstr(); } } void EZ_2500(void) { u16 i; sendpos=0; for (i=0;i<6;i++) sendbuf[i]=0; i = data_[16]<< 8 | data_[17]; if (i==0x0204){ printnum_2500(i); } else{ i = data_[14]<< 8 | data_[15]; printnum_2500(i); } i = data_[12]<< 8 | data_[13]; printnum_2500(i); i = data_[10]<< 8 | data_[11]; printnum_2500(i); i = data_[8]<< 8 | data_[9]; printnum_2500(i); i = data_[6]<< 8 | data_[7]; printnum_2500(i); printnum_2500(0x0D); for(i=0;i<20;i++) data1_[i]=0; sendstr(); } void SDT_TQ(void) { uint8_t i; if (timer>10) { if (pos>0x23) { sendpos=0; for (i=0;i<8;i++) sendbuf[i]=0; printnum_(0x0D<<1); printnum_(data_[0]); printnum_(data_[1]); printnum_(data_[2]); printnum_(data_[3]); if(data_[4]==0x01) printnum_(0x30<<1); else printnum_(0x20<<1); sendstr(); pos=0; } else LED_Off(); timer=0; } } u8 LED_Coding(u8 temp,u8 tempp) { u8 out; u8 in1,in2; in1 = temp; in2 = tempp; if((in1 == 0x00) && (in2 == 0x00)) out = 0x20; else if((in1 == 0xe0) && (in2 == 0x07)) out = 0x30; else if((in1 == 0x60) && (in2 == 0x00)) out = 0x31; else if((in1 == 0xc4) && (in2 == 0x23)) out = 0x32; else if((in1 == 0xe0) && (in2 == 0x21)) out = 0x33; else if((in1 == 0x64) && (in2 == 0x24)) out = 0x34; else if((in1 == 0xA4) && (in2 == 0x25)) out = 0x35; else if((in1 == 0xa4) && (in2 == 0x27)) out = 0x36; else if((in1 == 0xe0) && (in2 == 0x00)) out = 0x37; else if((in1 == 0xe4) && (in2 == 0x27)) out = 0x38; else if((in1 == 0xe4) && (in2 == 0x25)) out = 0x39; else if((in1 == 0x00) && (in2 == 0x20)) out = 0x2D; else if((in1 == 0xff) && (in2 == 0xff)) out = 0x0D; else out = 0xff; sendbuf[sendpos++] = out; return out; } u8 LED_Coding_640(u8 temp,u8 tempp) { u8 out; u8 in1,in2; in1 = temp; in2 = tempp; if((in1 == 0x00) && (in2 == 0x00)) out = 0x20; else if((in1 == 0x00) && (in2 == 0xFC)) out = 0x30; else if((in1 == 0x00) && (in2 == 0x18)) out = 0x31; else if((in1 == 0x11) && (in2 == 0x6C)) out = 0x32; else if((in1 == 0x11) && (in2 == 0x3C)) out = 0x33; else if((in1 == 0x11) && (in2 == 0x98)) out = 0x34; else if((in1 == 0x11) && (in2 == 0xB4)) out = 0x35; else if((in1 == 0x11) && (in2 == 0xF4)) out = 0x36; else if((in1 == 0x00) && (in2 == 0x1C)) out = 0x37; else if((in1 == 0x11) && (in2 == 0xFC)) out = 0x38; else if((in1 == 0x11) && (in2 == 0x9C)) out = 0x39; else if((in1 == 0x11) && (in2 == 0x00)) out = 0x2D; else if((in1 == 0xff) && (in2 == 0xff)) out = 0x0D; else out = 0xff; sendbuf[sendpos++] = out; return out; } u16 RecIndex=0; //接收索引,用于处理接收数据流程 void I2C1_IRQHandler(void) { u8 i; __IO uint32_t I2CFlagStatus = 0x00000000; I2CFlagStatus = (uint32_t)(I2C1->ISR & (uint16_t)0x0000100FE); if ((I2CFlagStatus & I2C_ISR_ADDR) != 0) { if((I2C1->ISR&I2C_ISR_DIR)==0) //rx mode { RecIndex= 0; for(i=0;i<20;i++) data_[i]=0; I2C1->ICR |= I2C_ICR_ADDRCF; } } else if ((I2CFlagStatus & I2C_ISR_RXNE) != 0) { data_[RecIndex++] = I2C_ReceiveData(I2C1); if(RecIndex>=30) RecIndex=0; else if (data_[0]==0xCD && RecIndex==19) { RecIndex = 0; sendpos=0; for (i=0;i<8;i++) sendbuf[i]=0; LED_Coding_640(0xFF,0xFF); LED_Coding_640(data_[17],data_[18]); LED_Coding_640(data_[15],data_[16]); LED_Coding_640(data_[13],data_[14]); LED_Coding_640(data_[11],data_[12]); LED_Coding_640(data_[9],data_[10]); sendstr(); } else if (data_[0]==0xE0 && RecIndex==12) { RecIndex = 0; sendpos=0; for (i=0;i<8;i++) sendbuf[i]=0; LED_Coding(0xFF,0xFF); LED_Coding(data_[2],data_[3]); LED_Coding(data_[4],data_[5]); LED_Coding(data_[6],data_[7]); LED_Coding(data_[8],data_[9]); LED_Coding(data_[10],data_[11]); sendstr(); } } else if ((I2CFlagStatus & I2C_ISR_STOPF) != 0) { I2C1->ICR |= I2C_ICR_STOPCF; if (data_[0]==0xE0) { RecIndex = 0; sendpos=0; for (i=0;i<8;i++) sendbuf[i]=0; LED_Coding(0xFF,0xFF); LED_Coding(data_[2],data_[3]); LED_Coding(data_[4],data_[5]); LED_Coding(data_[6],data_[7]); LED_Coding(data_[8],data_[9]); LED_Coding(data_[10],data_[11]); sendstr(); } else if (data_[0]==0xCD) { RecIndex = 0; sendpos=0; for (i=0;i<8;i++) sendbuf[i]=0; LED_Coding_640(0xFF,0xFF); LED_Coding_640(data_[17],data_[18]); LED_Coding_640(data_[15],data_[16]); LED_Coding_640(data_[13],data_[14]); LED_Coding_640(data_[11],data_[12]); LED_Coding_640(data_[9],data_[10]); sendstr(); } } } ///////////////////////////////////////////////////////////////////////////////////////////////////////