Comencé a trabajar en Rs485 (Sp3485c) que está interconectado con el microcontrolador Pic24fj128ga202 IC. Este controlador tiene 4 uart's uno está conectado a la PC a través de max3232 ic, gps, gsm y otro para rs485.uart1,2,3 se ha configurado muy correctamente y puedo enviar y recibir datos a través de estos uarts, pero cuando envío datos a través del cuarto uart se transmiten los datos, verifiqué la transmisión a través de tx interrupt. Para verificar el propósito hice un microcontrolador como transmisor y otro mocricontroller como receptor, ambos controladores están conectados a través de RS485. El principal problema es que no recibo ningún dato en otro microcontrolador a través de rs485.
Aquí está el código del transmisor
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include<stdbool.h>
//#include "p24FJ128GA202.h" // Pic24FJ128GA202 header file
#include "C:\Program Files\Microchip\xc16\v1.24\support\PIC24F\h\p24FJ128GA202.h"
#include "uart.h"
#define PWRKEY PORTBbits.RB12
// Function Prototypes
void InitProcessor(void);
void InitDelayms(void);
void Timer2Init(void);
void Delayms(uint16_t delay);
void __attribute__ ((interrupt,no_auto_psv)) _U4TXInterrupt(void)
{
uart1tx('O');
IFS5bits.U4TXIF = 0;
}
int main()
{
unsigned int i=0;
InitProcessor();
Delayms(500); // oscillator to get lock
uart1str((unsigned char *)"\r\nRs485 Transmitter\r\n");
while(1)
{
uart4tx('T');
Delayms(3000);
}
return 0;
}
void InitProcessor(void)
{
// Oscillator
OSCCON = 0x0011;
CLKDIV = 0x0000;
//PPS for UART1
RPINR18bits.U1RXR = 7; //UART1 receive set to RB7
RPOR4bits.RP8R = 3; //UART1 transmit set to RB8
//PPS for UART2
RPINR19bits.U2RXR = 14; // uart2 receive set to RB14
RPOR6bits.RP13R = 5; //UART2 transmit set to RB13
//PPS for UART4
RPINR27bits.U4RXR = 4; // uart4 RX
RPOR3bits.RP6R = 21; //UART4
// Configure Digital pins
ANSA = 0x0003;
ANSB = 0x0000;
// Assign IO values for Ports
PORTA = 0x0000;
TRISA = 0x001B;
TRISB = 0x469f; // 7 as receive and 8 as transmitter
//PORTB = 0x0000;
// Init UART1
U1MODE = 0x0000;
U1STA = 0x0000; //Enable Transmission, Clear all flags
U1BRG = 103;//25;9600
U1MODEbits.UARTEN = 1; // And turn the peripheral on
U1STAbits.UTXEN = 1;
// Init UART2
U2MODE = 0x0000;
U2STA = 0x0000; //Enable Transmission, Clear all flags
U2BRG = 103;
U2MODEbits.UARTEN = 1; // And turn the peripheral on
U2STAbits.UTXEN = 1;
U4MODE = 0x0000;
U4STA = 0x0000; //Enable Transmission, Clear all flags
U4BRG = 103;
IFS5bits.U4TXIF = 0;
IEC5bits.U4TXIE = 1;
U4MODEbits.UARTEN = 1; // And turn the peripheral on
U4STAbits.UTXEN = 1;
// Interrupt Bits
INTCON1 = 0x0000; // Disable Interrupts
INTCON2 = 0x0000;
InitDelayms();
PWRKEY = 1;
Delayms(1000);
}
void InitDelayms(void)
{
// T1 - Clk Source: Fosc/2 (Fcyc)
T1CONbits.TCS = 0;
// T1 - Pre scale: 1:1 (4MHz / 250nS per tick)
T1CONbits.TCKPS = 0;
// PR1 adjusted to account for sw delays intro'd in loop below at 16MHz
PR1 = 16000;
// Clear TMR1
TMR1 = 0;
// Reset T1IF
IFS0bits.T1IF = 0;
// T1 - Turn on
T1CONbits.TON = 1;
}
void Delayms(uint16_t delay)
{
// basic kernel is 1mS (32 ticks @ 31.25uS/tick)
while(delay>0)
{
// Clear TMR1
TMR1 = 0;
// Reset T1IF
IFS0bits.T1IF = 0;
// Wait 1mS
while(!IFS0bits.T1IF);
delay--;
}
}
que transmite datos por cada 3 segundos. Código del receptor
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include<stdbool.h>
//#include "p24FJ128GA202.h" // Pic24FJ128GA202 header file
#include "C:\Program Files\Microchip\xc16\v1.24\support\PIC24F\h\p24FJ128GA202.h"
#include "uart.h"
#define PWRKEY PORTBbits.RB12
// Function Prototypes
void InitProcessor(void);
void InitDelayms(void);
void Timer2Init(void);
void Delayms(uint16_t delay);
void __attribute__ ((interrupt,no_auto_psv)) _U4RXInterrupt(void)
{
uart1tx(uart4rx());
IFS5bits.U4RXIF = 0;
}
int main()
{
unsigned int i=0;
InitProcessor();
Delayms(500); // oscillator to get lock
uart1str((unsigned char *)"\r\nRs485 with Modbus Protocol\r\n");
Rs485Re_De=0; // receiver
while(1)
{
Delayms(1000);
}
return 0;
}
void InitProcessor(void)
{
// Oscillator
OSCCON = 0x0011;
CLKDIV = 0x0000;
//PPS for UART1
RPINR18bits.U1RXR = 7; //UART1 receive set to RB7
RPOR4bits.RP8R = 3; //UART1 transmit set to RB8
//PPS for UART2
RPINR19bits.U2RXR = 14; // uart2 receive set to RB14
RPOR6bits.RP13R = 5; //UART2 transmit set to RB13
//PPS for UART4
RPINR27bits.U4RXR = 4; // uart4 RX
RPOR3bits.RP6R = 21; //UART4
// Configure Digital pins
ANSA = 0x0003;
ANSB = 0x0000;
// Assign IO values for Ports
PORTA = 0x0000;
TRISA = 0x001B;
TRISB = 0x469f; // 7 as receive and 8 as transmitter
//PORTB = 0x0000;
// Init UART1
U1MODE = 0x0000;
U1STA = 0x0000; //Enable Transmission, Clear all flags
U1BRG = 103;//25;9600
U1MODEbits.UARTEN = 1; // And turn the peripheral on
U1STAbits.UTXEN = 1;
// Init UART2
U2MODE = 0x0000;
U2STA = 0x0000; //Enable Transmission, Clear all flags
U2BRG = 103;
U2MODEbits.UARTEN = 1; // And turn the peripheral on
U2STAbits.UTXEN = 1;
U4MODE = 0x0000;
U4STA = 0x0000; //Enable Transmission, Clear all flags
U4BRG = 103;
IFS5bits.U4RXIF = 0; // Clear the Receive Interrupt Flag
IEC5bits.U4RXIE = 1; // Enable Receive Interrupts
U4MODEbits.UARTEN = 1; // And turn the peripheral on
U4STAbits.UTXEN = 1;
// Interrupt Bits
INTCON1 = 0x0000; // Disable Interrupts
INTCON2 = 0x0000;
InitDelayms();
PWRKEY = 1;
Delayms(1000);
}
void InitDelayms(void)
{
// T1 - Clk Source: Fosc/2 (Fcyc)
T1CONbits.TCS = 0;
// T1 - Pre scale: 1:1 (4MHz / 250nS per tick)
T1CONbits.TCKPS = 0;
// PR1 adjusted to account for sw delays intro'd in loop below at 16MHz
PR1 = 16000;
// Clear TMR1
TMR1 = 0;
// Reset T1IF
IFS0bits.T1IF = 0;
// T1 - Turn on
T1CONbits.TON = 1;
}
void Delayms(uint16_t delay)
{
// basic kernel is 1mS (32 ticks @ 31.25uS/tick)
while(delay>0)
{
// Clear TMR1
TMR1 = 0;
// Reset T1IF
IFS0bits.T1IF = 0;
// Wait 1mS
while(!IFS0bits.T1IF);
delay--;
}
}
He escrito una función de interrupción para el receptor que imprime los datos en la recepción, el problema está aquí donde no obtengo ningún dato
Diagrama de conexión Pic to PIC Pic1 A - Pic2 A Pic1 B - Pic2 B Pic1 Gnd - Pic2 Gnd respectivamente.
Pic2 uart1 a max 3232 a pc para ver la salida.
estos son el PIC, RS485, diagrama de salida respectivamente.