|
|
View previous topic :: View next topic |
Author |
Message |
yvzd
Joined: 16 Feb 2021 Posts: 9
|
18f4520 working modbus don't work on 18f46k22 |
Posted: Tue Feb 16, 2021 2:59 am |
|
|
I am trying a little Modbus communication with pc via modbus poll. If I use 18f4520 communication is ok. But when I tried same things on 18f46k22 ıt doesn't work. Is there any special things for 18f46k22 ?
Compiler version: PCH Compiler V5.076
My slave code is:
Code: |
#include <18F46k22.h>
#device ADC=10
#include <internal_eeprom.c>
#fuses HSM,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,NOCPD
#use delay(clock=8000000) //cristal de 20MHz
#define MODBUS_TYPE MODBUS_TYPE_SLAVE
#define MODBUS_SERIAL_TYPE MODBUS_RTU
#define MODBUS_SERIAL_RX_BUFFER_SIZE 64
#define MODBUS_SERIAL_BAUD 9600
#define MODBUS_SERIAL_RX_PIN PIN_C7
#define MODBUS_SERIAL_TX_PIN PIN_C6
#define MODBUS_SERIAL_ENABLE_PIN PIN_D4
#define MODBUS_SERIAL_RX_ENABLE PIN_D4
#define MODBUS_SERIAL_INT_SOURCE MODBUS_INT_RDA
#include <modbus.c>
#define MODBUS_ADDRESS 22
#use fast_io(a)
#use fast_io(c)
#use fast_io(d)
#include <math.h>
int16 hold_regs[]={0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x1111};
int16 a,b;
void main()
{
#byte ANSELA = 0xF38
#byte ANSELB = 0xF39
#byte ANSELC = 0xF3A
#byte ANSELD = 0xF3B
#byte ANSELE = 0xF3C
ANSELA = 0;
ANSELB = 0;
ANSELC = 0;
ANSELD = 0;
ANSELE = 0;
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
modbus_init();
delay_ms(100);
while(TRUE)
{
a=0;
b=14;
hold_regs[0]=make16(05,29);
hold_regs[1]=960;
hold_regs[2]=670;
while(!modbus_kbhit());
delay_ms(50);
if((modbus_rx.address == MODBUS_ADDRESS|| modbus_rx.address == 0) )
{
if(modbus_rx.func==FUNC_READ_HOLDING_REGISTERS)
{
modbus_read_holding_registers_rsp(MODBUS_ADDRESS,(modbus_rx.data[3]*2),hold_regs + modbus_rx.data[1]);
}
}
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Feb 16, 2021 3:52 am |
|
|
Quote: | #use delay(clock=8000000) //cristal de 20MHz |
What crystal is on the board ? 8 MHz or 20 MHz ? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Tue Feb 16, 2021 3:55 am |
|
|
Also, 'critical difference'. The K22, _requires_ 'PIN_SELECT', to
setup the hardware UART. You won't get INT_RDA, unless the hardware
UART is setup. |
|
|
yvzd
Joined: 16 Feb 2021 Posts: 9
|
|
Posted: Tue Feb 16, 2021 4:27 am |
|
|
Dear PCM programmer thank you very much for your reply. On the board 8Mhz connected. I am using EasyPICv7 development board.
PCM programmer wrote: | Quote: | #use delay(clock=8000000) //cristal de 20MHz |
What crystal is on the board ? 8 MHz or 20 MHz ? |
|
|
|
yvzd
Joined: 16 Feb 2021 Posts: 9
|
|
Posted: Tue Feb 16, 2021 4:59 am |
|
|
Dear Ttelmah thank you very much for your reply.
Do you mean
Code: |
#pin_select U1TX = PIN_C6
#pin_select U1RX = PIN_C7
|
instead of
Code: |
#define MODBUS_SERIAL_RX_PIN PIN_C7
#define MODBUS_SERIAL_TX_PIN PIN_C6
| ?
Ttelmah wrote: | Also, 'critical difference'. The K22, _requires_ 'PIN_SELECT', to
setup the hardware UART. You won't get INT_RDA, unless the hardware
UART is setup. |
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9224 Location: Greensville,Ontario
|
|
Posted: Tue Feb 16, 2021 6:05 am |
|
|
#pin_select configures the HARDWARE of the PIC.... it connects an I/O pin to an internal peripheral, in this case the HW UART. Modern PICs can have a 'matrix' of possible I/O pins vs peripheral connections unlike other PICs.
#define ...... configures the SOFTWARE (program), in this case 'naming' an I/O pin. This 'macro' allows for quick and complete reassigning of pins. Say you need to use PIN_B4 instead of PIN_C7. ONE 'define' will change 100's of PIN_C7 into PIN_B4. Sure saves a LOT of typing ! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Tue Feb 16, 2021 8:48 am |
|
|
The K42, doesn't actually need PPS on UART1.
However looking at what you post, there are major things 'wrong'.
You claim it is working on a 4520, yet your header lines do not specify the
MODBUS_PROTOCOL. Without this defined, the modbus driver will select
the tcpip physical layer, not RTU.
If you look in modbus.c, it has:
#if (MODBUS_PROTOCOL == MODBUS_PROTOCOL_SERIAL)
before the selection of the RTU driver.....
Then you specify fast_io, but do not setup the TRIS. The modbus driver
does not do this, so things like the output to enable the RS485 driver will
not actually be happening. If you specify fast_io, you must handle TRIS.
This would not have worked on the 4520 as posted....
Change your header lines to:
Code: |
#define MODBUS_TYPE MODBUS_TYPE_SLAVE
#define MODBUS_PROTOCOL MODBUS_PROTOCOL_SERIAL
#define MODBUS_SERIAL_TYPE MODBUS_RTU
#define MODBUS_SERIAL_RX_BUFFER_SIZE 64
#define MODBUS_SERIAL_BAUD 9600
#define MODBUS_SERIAL_RX_PIN PIN_C7
#define MODBUS_SERIAL_TX_PIN PIN_C6
#define MODBUS_SERIAL_ENABLE_PIN PIN_D4
#define MODBUS_SERIAL_RX_ENABLE PIN_D4
#define MODBUS_SERIAL_INT_SOURCE MODBUS_INT_RDA
#define MODBUS_ADDRESS 22
#include <modbus.c>
//#use fast_io(a)
//#use fast_io(c)
//#use fast_io(d)
|
You then have a chance of the code working.... |
|
|
yvzd
Joined: 16 Feb 2021 Posts: 9
|
|
Posted: Tue Feb 16, 2021 1:10 pm |
|
|
Dear Ttelmah thank you so much for your reply. I tried several things for a week and thats why I missed "#define MODBUS_PROTOCOL MODBUS_PROTOCOL_SERIAL" sorry for confusion. I wrote my code all which works on 18F4520 and not working on 18F46k22. (of course fuses are different for each MCU).
Actually at the beginning the "PPS" was interesting so I tried #pin_select for both C7 and C6 but compiler gives error about it. I checked the 18F46k22.h file but there isn't any info about #pin_select.
Code: |
#include <18F46k22.h>
#fuses HSM,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,NOCPD,NOPLLEN
#use delay(crystal=8Mhz)
#define MODBUS_TYPE MODBUS_TYPE_SLAVE
#define MODBUS_PROTOCOL MODBUS_PROTOCOL_SERIAL
#define MODBUS_SERIAL_TYPE MODBUS_RTU
#define MODBUS_SERIAL_RX_BUFFER_SIZE 64
#define MODBUS_SERIAL_BAUD 9600
#define MODBUS_SERIAL_RX_PIN PIN_C7
#define MODBUS_SERIAL_TX_PIN PIN_C6
#define MODBUS_SERIAL_ENABLE_PIN PIN_D4
#define MODBUS_SERIAL_RX_ENABLE PIN_D4
#define MODBUS_SERIAL_INT_SOURCE MODBUS_INT_RDA
#include <modbus.c>
#define MODBUS_ADDRESS 22
#include <math.h>
//holdings..........................................................
int16 hold_regs[]={0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x1111};
//.............................................................................
int16 a,b;
void main()
{
setup_adc_ports(NO_ANALOGS);
#byte ANSELA = 0xF38
#byte ANSELB = 0xF39
#byte ANSELC = 0xF3A
#byte ANSELD = 0xF3B
#byte ANSELE = 0xF3C
ANSELA = 0;
ANSELB = 0;
ANSELC = 0;
ANSELD = 0;
ANSELE = 0;
clear_interrupt(INT_RDA);
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
modbus_init();
while(TRUE)
{
a=0;
b=14;
hold_regs[0]=make16(05,29);
hold_regs[1]=960;
hold_regs[2]=670;
while(!modbus_kbhit());
delay_us(50);
if((modbus_rx.address == MODBUS_ADDRESS|| modbus_rx.address == 0) )
{
if(modbus_rx.func==FUNC_READ_HOLDING_REGISTERS)
{
modbus_read_holding_registers_rsp(MODBUS_ADDRESS,(modbus_rx.data[3]*2),hold_regs + modbus_rx.data[1]);
}
}
}
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Wed Feb 17, 2021 3:06 am |
|
|
Yes, when I originally read your post, I immediately thought 'lots of the K
chips have PPS', which is why I raised this. Then went and looked at the data
sheet for your specific chip and found PPS did not apply to UART1, which is
why I then looked for other problems and 'spotted' the protocol one....
Glad you have it sorted now. |
|
|
yvzd
Joined: 16 Feb 2021 Posts: 9
|
|
Posted: Thu Feb 18, 2021 2:04 pm |
|
|
Ttelmah wrote: | Yes, when I originally read your post, I immediately thought 'lots of the K
chips have PPS', which is why I raised this. Then went and looked at the data
sheet for your specific chip and found PPS did not apply to UART1, which is
why I then looked for other problems and 'spotted' the protocol one....
Glad you have it sorted now. |
Dear Ttelmah yes thank you again
According to my problem I still have no solution for 18f46k22 :( |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Feb 19, 2021 2:03 am |
|
|
yvzd wrote: |
According to my problem I still have no solution for 18f46k22 :(
|
If it still doesn't work with an 18F46K22, then post your current test program. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9224 Location: Greensville,Ontario
|
|
Posted: Fri Feb 19, 2021 8:28 am |
|
|
I have to agree with PCMP...need to see your code...
The 46K22 is a solid, reliable PIC,
your code worked on another PIC
so ...
you must have done 'something' wrong... |
|
|
yvzd
Joined: 16 Feb 2021 Posts: 9
|
|
Posted: Sat Feb 20, 2021 9:20 am |
|
|
Dear PCM programmer and temtronic I am really sure that I am making something wrong with 18f46k22 and need advice.
Code: |
#fuses HSM,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,NOCPD,NOPLLEN
#use delay(clock=8000000)
#define MODBUS_TYPE MODBUS_TYPE_SLAVE
#define MODBUS_PROTOCOL MODBUS_PROTOCOL_SERIAL
#define MODBUS_SERIAL_TYPE MODBUS_RTU
#define MODBUS_SERIAL_RX_BUFFER_SIZE 64
#define MODBUS_SERIAL_BAUD 9600
#define MODBUS_SERIAL_RX_PIN PIN_C7
#define MODBUS_SERIAL_TX_PIN PIN_C6
#define MODBUS_SERIAL_ENABLE_PIN PIN_D4
#define MODBUS_SERIAL_RX_ENABLE PIN_D4
#define MODBUS_SERIAL_INT_SOURCE MODBUS_INT_RDA
#include <modbus.c>
#define MODBUS_ADDRESS 253
//.............................................................................
//modbus ..........................................................
int16 hold_regs[]={0x0001,0x0002,0x0003,0x1111};
int16 mbus=1,hr_1,hr_2,hr_3,hr_4,hr_5,hr_6,hr_7,hr_8,hr_9,hr_10,hr_11,hr_12,hr_13,hr_14,hr_15,hr_16,hr_17,hr_18,hr_19,hr_20,hr_21,hr_22;
//.............................................................................
void main()
{
#byte ANSELA = 0xF38
#byte ANSELB = 0xF39
#byte ANSELC = 0xF3A
#byte ANSELD = 0xF3B
#byte ANSELE = 0xF3C
ANSELA = 0;
ANSELB = 0;
ANSELC = 0;
ANSELD = 0;
ANSELE = 0;
setup_adc_ports(NO_ANALOGS);
clear_interrupt(INT_RDA);
enable_interrupts(GLOBAL);
modbus_init();
delay_ms(100);
while(TRUE)
{
output_high(pin_b7);
delay_ms(1000);
output_low(pin_b7);
hold_regs[0]=make16(05,29);
hold_regs[1]=960;
hold_regs[2]=670;
//modbus send holdings.........................................................
while(modbus_kbhit());
if((modbus_rx.address == MODBUS_ADDRESS|| modbus_rx.address == 0) )
{
if(modbus_rx.func==FUNC_READ_HOLDING_REGISTERS)
{
modbus_read_holding_registers_rsp(MODBUS_ADDRESS,(modbus_rx.data[3]*2),hold_regs + modbus_rx.data[1]);
}
}
}
}
|
Also I can see that 18F46k22 is sending RX to Modbuspoll soft. I can see RX sent right register values but soft gives a checksum error.
When I check the compiler I saw a warning about timer issue also added here. I am not sure about that because it gives same warning also on my working 18f4520 program.
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9224 Location: Greensville,Ontario
|
|
Posted: Sat Feb 20, 2021 11:56 am |
|
|
Since the 4520 version works, post that code AND the 46k22 code. As the modbus.c is the same ,that must NOT be the cause of the problem.
Have to ask is the hardware the same ?
Proper bias on the RS485 devices ?
Are they the same MFR of devices ?
What cabling is used between PIC and PC ? |
|
|
yvzd
Joined: 16 Feb 2021 Posts: 9
|
|
Posted: Sat Feb 20, 2021 12:51 pm |
|
|
temtronic wrote: | Since the 4520 version works, post that code AND the 46k22 code. As the modbus.c is the same ,that must NOT be the cause of the problem.
Have to ask is the hardware the same ?
Proper bias on the RS485 devices ?
Are they the same MFR of devices ?
What cabling is used between PIC and PC ? |
I am using easypicv7 board and rs485 chip is on a breadboard. Also there is a rs485 to usb converter to connect to modbus poll. I dont think there is a hardware issue. I also tried another laptop another ccs c complier version but same result. I think I have the problem with the arrangements for 18f46k22 but coulndt find why. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|