/* EC0608 头文件 */ //版本: V1.0.2 //EPLC功能函数C语言头文件 //语法适合ICCAVR Version V7.19 //适应型号: EC0608RTAD, EC0808RT, EC0608TACAD, EC0808TAC, EC14N, EC16N //适应MCU芯片: ATmega48,如果使用ATmega88/168,仅仅修改第一个包含文件即可 #include #include #include #include #define BIT0 0x01 #define BIT1 0x02 #define BIT2 0x04 #define BIT3 0x08 #define BIT4 0x10 #define BIT5 0x20 #define BIT6 0x40 #define BIT7 0x80 #define AND && #define OR || #define X0 0x04 #define X1 0x08 #define X2 0x10 #define X3 0x20 #define X4 0x01 #define X5 0x02 #define X6 0x04 #define X7 0x08 #define LD_B(x, y) (x & y) #define LDI_B(x, y) (~x & y) #define SETBIT(x, y) (x |= y) #define CLRBIT(x, y) (x &= ~y) #define SET_Y0 (PORTD |= 0x40) #define SET_Y1 (PORTD |= 0x80) #define SET_Y2 (PORTB |= 0x01) #define SET_Y3 (PORTB |= 0x02) #define SET_Y4 (PORTB |= 0x04) #define SET_Y5 (PORTB |= 0x08) #define SET_Y6 (PORTB |= 0x10) #define SET_Y7 (PORTB |= 0x20) #define RST_Y0 (PORTD &= ~0x40) #define RST_Y1 (PORTD &= ~0x80) #define RST_Y2 (PORTB &= ~0x01) #define RST_Y3 (PORTB &= ~0x02) #define RST_Y4 (PORTB &= ~0x04) #define RST_Y5 (PORTB &= ~0x08) #define RST_Y6 (PORTB &= ~0x10) #define RST_Y7 (PORTB &= ~0x20) #define OUT_Y0 (PORTD |= 0x40); else (PORTD &= ~0x40) #define OUT_Y1 (PORTD |= 0x80); else (PORTD &= ~0x80) #define OUT_Y2 (PORTB |= 0x01); else (PORTB &= ~0x01) #define OUT_Y3 (PORTB |= 0x02); else (PORTB &= ~0x02) #define OUT_Y4 (PORTB |= 0x04); else (PORTB &= ~0x04) #define OUT_Y5 (PORTB |= 0x08); else (PORTB &= ~0x08) #define OUT_Y6 (PORTB |= 0x10); else (PORTB &= ~0x10) #define OUT_Y7 (PORTB |= 0x20); else (PORTB &= ~0x20) #define ALT_Y1 (PORTD ^= 0x80) #define ALT_Y0 (PORTD ^= 0x40) #define ALT_Y1 (PORTD ^= 0x80) #define ALT_Y2 (PORTB ^= 0x01) #define ALT_Y3 (PORTB ^= 0x02) #define ALT_Y4 (PORTB ^= 0x04) #define ALT_Y5 (PORTB ^= 0x08) #define ALT_Y6 (PORTB ^= 0x10) #define ALT_Y7 (PORTB ^= 0x20) #define LD_Y0 (PORTD & 0x40) #define LD_Y1 (PORTD & 0x80) #define LD_Y2 (PORTB & 0x01) #define LD_Y3 (PORTB & 0x02) #define LD_Y4 (PORTB & 0x04) #define LD_Y5 (PORTB & 0x08) #define LD_Y6 (PORTB & 0x10) #define LD_Y7 (PORTB & 0x20) #define LDI_Y0 !(PORTD & 0x40) #define LDI_Y1 !(PORTD & 0x80) #define LDI_Y2 !(PORTB & 0x01) #define LDI_Y3 !(PORTB & 0x02) #define LDI_Y4 !(PORTB & 0x04) #define LDI_Y5 !(PORTB & 0x08) #define LDI_Y6 !(PORTB & 0x10) #define LDI_Y7 !(PORTB & 0x20) #define LD_X0 !(input_XD & 0x01) #define LD_X1 !(input_XD & 0x02) #define LD_X2 !(input_XD & 0x04) #define LD_X3 !(input_XD & 0x08) #define LD_X4 !(input_XD & 0x10) #define LD_X5 !(input_XD & 0x20) #define LD_X6 !(input_XD & 0x40) #define LD_X7 !(input_XD & 0x80) #define LDI_X0 (input_XD & 0x01) #define LDI_X1 (input_XD & 0x02) #define LDI_X2 (input_XD & 0x04) #define LDI_X3 (input_XD & 0x08) #define LDI_X4 (input_XD & 0x10) #define LDI_X5 (input_XD & 0x20) #define LDI_X6 (input_XD & 0x40) #define LDI_X7 (input_XD & 0x80) #define LDP_X0 !(input_XP & 0x01) #define LDP_X1 !(input_XP & 0x02) #define LDP_X2 !(input_XP & 0x04) #define LDP_X3 !(input_XP & 0x08) #define LDP_X4 !(input_XP & 0x10) #define LDP_X5 !(input_XP & 0x20) #define LDP_X6 !(input_XP & 0x40) #define LDP_X7 !(input_XP & 0x80) #define LDF_X0 !(input_XF & 0x01) #define LDF_X1 !(input_XF & 0x02) #define LDF_X2 !(input_XF & 0x04) #define LDF_X3 !(input_XF & 0x08) #define LDF_X4 !(input_XF & 0x10) #define LDF_X5 !(input_XF & 0x20) #define LDF_X6 !(input_XF & 0x40) #define LDF_X7 !(input_XF & 0x80) #define STL switch (SET_STEP){case 0: #define S1 break; case 1: #define S2 break; case 2: #define S3 break; case 3: #define S4 break; case 4: #define S5 break; case 5: #define S6 break; case 6: #define S7 break; case 7: #define S8 break; case 8: #define S9 break; case 9: #define S10 break; case 10: #define S11 break; case 11: #define S12 break; case 12: #define S13 break; case 13: #define S14 break; case 14: #define S15 break; case 15: #define S16 break; case 16: #define S17 break; case 17: #define S18 break; case 18: #define S19 break; case 19: #define S20 break; case 20: #define SEND break; default: break;} #define LD_M(x) (relay_MD[x/8] & (1<<(x%8))) #define LDI_M(x) (~relay_MD[x/8] & (1<<(x%8))) #define SET_M(x) (relay_MD[x/8] |= (1<<(x%8))) #define RST_M(x) (relay_MD[x/8] &= ~(1<<(x%8))) #define RS485_DE (PORTC |= 0x20) #define RS485_RE (PORTC &= ~0x20) //AD转换使能 #define SET_ADC ADMUX = 0xC6; ADCSRA = 0x8E; ADCSRA |= 0x40 //允许高速计数 #define SET_C7 EICRA = 0x09; EIMSK |= 0x01 //启动时刻脉冲检测 //只有在EPLC启动的第一个扫描周期才执行相应语句 #define EPLCBOOT if(relay_MD[7] & 0x80) #pragma interrupt_handler INT0_Interrupt:iv_INT0 #pragma interrupt_handler INT1_Interrupt:iv_INT1 #pragma interrupt_handler T2OVF_Interrupt:iv_TIMER2_OVF #pragma interrupt_handler USARTRX_Interrupt:iv_USART_RX #pragma interrupt_handler USARTTX_Interrupt:iv_USART_TX #pragma interrupt_handler ADC_Interrupt:iv_ADC extern char MB[64]; //64个8位存储器(影射在32个16位存储器中) extern int MW[32]; //32个16位数据存储器影射(在64个8位存储器中) extern float MF[16]; //16个浮点数据存储器影射(在64个8位存储器中) extern char timer_c1; // 100mS辅助计数器 extern char tmr100mS[8]; extern char timer_c2; // 1S辅助计数器 extern char tmr1S[8]; extern char tmr_UCS; extern unsigned int MADC[2]; //2个通道AD转换结果 extern int C16[8]; //C4,5-PWM计数, C6,7-编码器计数 extern unsigned long C32[4]; // extern char input_XD; extern char input_XP; extern char input_XF; extern char out_YD; //Y0-Y7触点状态 extern char out_YP; //Y0-Y7上升沿 extern char out_YF; //Y0-Y7下降沿 extern char relay_TD[2]; //2个字节定义为16个时间继电器触点状态 extern char relay_TP[2]; //2个字节定义为16个时间继电器上升沿 extern char relay_TF[2]; //2个字节定义为16个时间继电器下降沿 extern char relay_CD; //计数器C0-C7触点状态 extern char relay_CP; //计数器C0-C7上升沿 extern char relay_CF; //计数器C0-C7下降沿 extern char relay_MD[8]; //8个字节共64位定义为64个中间继电器触点状态 extern char relay_MP[8]; //8个字节共64位定义为64个中间继电器上升沿 extern char relay_MF[8]; //8个字节共64位定义为64个中间继电器下降沿 extern char buf_RX[18]; // MODBUS RTU 协议接收缓冲区 // 接收数据依次为: // [0][站号]----------1字节, 标识设备站号 // [1][功能码]--------1字节, 功能代码 // [2][数据地址]------2字节, 数据开始地址 // [4][字数量]--------2字节, 读取数据数量, 表示字(W)的个数 // [5][字节数量]------1字节, 写数据字节数(字*2) // [6][写内容]--------2(4)字节, 大小等于字节数量长度 // [8(10)][CRC校验]---2字节, CRC16校验码, 多项式A001, 初值是FFFF // [11-16]------------保留 // [17][指针]---------1字节, 缓冲区指针 extern char buf_TX[16]; // 发送缓冲区支持文本自由协议, 数据依次为[站号][命令][地址][长度] // 协议规定串口通信自动写所有dat_W(32个)数据到文本MW寄存器, 寄存器对应MW0-MW31 // buf_TX[0]站号: 0-255, 默认为1 // buf_TX[1]功能码 // buf_TX[2]数据字节数 // buf_TX[3]16位数据1高字节 // buf_TX[4]16位数据1低字节 // buf_TX[5]16位数据2高字节 // buf_TX[6]16位数据2低字节 // buf_TX[7]保留 // buf_TX[8]CRC低8位 // buf_TX[9]CRC高8位 // buf_TX[14]要发送的命令长度 // buf_TX[15]指针: 发送缓冲区指针 extern char SET_STEP; //步进号 //void clr_memory(void); //void outy_load(unsigned char y); //unsigned int CRC(char *p, unsigned char n); void EPLCINIT(void); char EMB(unsigned int addr); int EMW(unsigned int addr); void SEMW(unsigned int addr, unsigned int dat); // 异步串口发送完成中断 // 中断向量21 void USARTTX_Interrupt(void) { unsigned char n; n = buf_TX[15]; if(n