پاسخ : آموزش زبان سی
این عدد 10 داخل کروشه که به جای TIM0_OVF قرار دادین نقشش چیه.
interrupt [10] void timer0_ovf_isr(void)
//*****************************************************************************
//
// File Name : 'modbus.c'
// Title : modbus over serial line
// Author : Hamid Rostami
//
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
#include "modbus.h"
const unsigned int POLYNUMERIC = 0xA001;
void modbusInit(unsigned int baudrate){
uartInit();
uartSetBaudRate(baudrate);
MODBUS_DIRECTION_DDR |= 1 << MODBUS_DIRECTION_BIT;
}
int modbus_WriteSingleRegister(unsigned int slaveAddr, unsigned int registerAddr, unsigned int value){
unsigned char PDU[7];
unsigned int CRC = 0xFFFF;
int i, j;
PDU[0] = slaveAddr; // Slave Address
PDU[1] = 0x06; // Function Code
PDU[2] = (registerAddr & 0xFF00) >> 8; // Register Address High Byte
PDU[3] = registerAddr & 0x00FF; // Register Address Low Byte
PDU[4] = (value & 0xFF00) >> 8; // Value High Byte
PDU[5] = value & 0x00FF; // Value Low Byte
// Generate CRC16
for(i=0; i<=5; i++){
CRC ^= PDU[i];
for( j=0; j<=7; j++){
if( CRC & 1 ){
CRC >>= 1;
CRC ^= POLYNUMERIC;
}else{
CRC >>= 1;
}
}
}
PDU[6] = CRC & 0x00FF; // CRC Low Byte
PDU[7] = (CRC & 0xFF00) >> 8; // CRC High Byte
// Send PDU
MODBUS_SEND_ENABLE;
for(i=0; i<=7; i++){
uartSendByte(PDU[i]);
}
return 0;
/*
// Recieve PDU
PDU[0] = uartGetByte();
if( PDU[0] !=
for(i=0; i<=7; i++){
}
*/
return 0;
}
int modbus_ReadSingleRegister(unsigned int slaveAddr, unsigned int registerAddr, unsigned int *buff, unsigned int count){
unsigned int PDU[7];
unsigned int CRC=0xFFFF;
int i, j;
PDU[0] = slaveAddr; // Slave Address
PDU[1] = 0x04; // Function Code
PDU[2] = registerAddr & 0x00FF; // Register Address High Byte
PDU[3] = (registerAddr & 0xFF00) >> 8; // Register Address Low Byte
PDU[4] = count & 0x00FF; // Value High Byte
PDU[5] = count & 0x00FF; // Value Low Byte
// Generate CRC16
for(i=0; i<=5; i++){
CRC ^= PDU[i];
for( j=0; j<=7; j++){
if( CRC & 1 ){
CRC >>= 1;
CRC ^= POLYNUMERIC;
}else{
CRC >>= 1;
}
}
}
//CRC16 = crc16(PDU, 6);
PDU[6] = CRC & 0x00FF; // CRC High Byte
PDU[7] = CRC & 0x00FF; // CRC Low Byte
// Send PDU
MODBUS_SEND_ENABLE;
for(i=0; i<=7; i++){
uartSendByte(PDU[i]);
}
return TRUE;
}
///***************************************************************************** // // File Name : 'modbus.h' // Title : modbus over serial line // Author : Hamid Rostami // // This code is distributed under the GNU Public License // which can be found at http://www.gnu.org/licenses/gpl.txt #ifndef MODBUS #define MODBUS #include "uart.h" #include <avr/io.h> // Not used yet ---------------------------- #define MODBUS_DIRECTION_PORT PORTA #define MODBUS_DIRECTION_DDR DDRA #define MODBUS_DIRECTION_BIT 3 #define MODBUS_SEND_ENABLE MODBUS_DIRECTION_PORT |= 1<<MODBUS_DIRECTION_BIT #define MODBUS_RECIEVE_ENABLE MODBUS_DIRECTION_PORT &= ~(1<<MODBUS_DIRECTION_BIT) // ----------------------------------------- // Initial modbus void modbusInit(unsigned int baudrate); // Write single register int modbus_WriteSingleRegister(unsigned int slaveAddr, unsigned int registerAddr, unsigned int value); // Read single Register (not work yet !) int modbus_ReadSingleRegister(unsigned int slaveAddr, unsigned int registerAddr, unsigned int *buff, unsigned int count); #endif
#include <avr/io.h>
#include <mega2561.h>



#include "modbus.h"
دیدگاه