PIC and 485 Communication Source Code
#include "HardwareProfile.h"
// Configuration bits
__CONFIG(HS & WDTDIS & PWRTDIS & BORDIS & LVPDIS);
void Board_Init(void);
#if defined(PIC_USE_HC595)
bit b_Reflash;
#endif
#if defined(PIC_USE_KB)
#define Free 1
#define Press 0
bit b_KeyActive;
bit b_KeyPress;
bit b_KeyState;
BYTE u_KeyValue;
BYTE u_SCANACC;
BYTE i_KeyValueBuffer[2];
#endif
BYTE u_10ms_Acc = 0;
BYTE u_50ms_Acc = 0;
BYTE u_100ms_Acc = 0;
BYTE u_200ms_Acc = 0;
WORD i_500ms_Acc = 0;
WORD i_1s_Acc = 0;
BYTE u_frame = 0;
WORD i_ADC;
void interrupt ISR(void)
{
if (T0IF)
{
T0IF = 0;
TMR0 = 0x0a; // 1ms timer
u_10ms_Acc++;
u_50ms_Acc++;
u_100ms_Acc++;
u_200ms_Acc++;
i_500ms_Acc++;
i_1s_Acc++;
#if defined(PIC_USE_HC595)
b_Reflash = 1;
#endif
if (u_10ms_Acc >= 10)
{
u_10ms_Acc = 0;
// Add tasks to execute every 10ms
}
if (u_50ms_Acc >= 50)
{
u_50ms_Acc = 0;
// Add tasks to execute every 50ms
#if defined(PIC_USE_KB)
ADIF = 0;
ADGO = 1;
#endif
}
if (u_100ms_Acc >= 100)
{
u_100ms_Acc = 0;
// Add tasks to execute every 100ms
}
if (u_200ms_Acc >= 200)
{
u_200ms_Acc = 0;
// Add tasks to execute every 200ms
}
if (i_500ms_Acc >= 500)
{
i_500ms_Acc = 0;
// Add tasks to execute every 500ms
}
}
if (ADIF)
{
ADIF = 0;
#if defined(PIC_USE_KB)
if (ADRESH != 0)
{
b_KeyPress = 1; // Button pressed
i_ADC = ADRESH;
i_ADC = i_ADC << 8;
i_ADC = i_ADC | ADRESL;
i_ADC = i_ADC & 0xfff8l;
i_ADC = i_ADC >> 2;
}
else
{
b_KeyState = Free; // No button pressed
b_KeyPress = 0;
LED_IO = 0x00;
}
#endif
if (TXIF && TXEN)
{
TXIF = 0;
}
}
}
void main()
{
Board_Init();
while (1)
{
#if defined(PIC_USE_HC595)
if (b_Reflash)
{
LED_Reflash();
b_Reflash = 0;
}
#endif
if (BUTTON1_IO == 0)
{
DisplayNumber_Process(8888);
}
if (BUTTON2_IO == 0)
{
DisplayNumber_Process(0);
}
#if defined(PIC_USE_RS485)
if (RCIF)
{
LED0_IO = LED0_IO ^ 1;
DisplayNumber_Process(RS485_RW());
}
#endif
}
}
////////////////////////////////////////////////////////////
// Function: void Board_Init(void)
// Input: None
// Output: None
// Overview: Initialize all devices on the board based on application needs.
void Board_Init(void)
{
BUTTON1_TRIS = 1;
BUTTON2_TRIS = 1;
LED_TRIS = 0;
LED_IO = 0x01;
TMR0_Init();
#if defined(PIC_USE_HC595)
HC595_Init();
#endif
#if defined(PIC_USE_LCD12864)
LCD_Init();
#endif
#if defined(PIC_USE_ISD1700)
ISD1700_Init();
#endif
#if defined(PIC_USE_KB)
KeyBoard_Init();
#endif
#if defined(PIC_USE_RS485)
RS485_Init();
#endif
}
#include
#include "HardwareProfile.h"
#if defined(PIC_USE_RS485)
void RS485_Init(void)
{
RS485_DIR_IO = Receive;
RS485_DIR_TRIS = 0;
RS485_RX_TRIS = 1;
RS485_TX_TRIS = 0;
//SPBRG = DIVIDER;
//RCSTA = (NINE_BITS|0x90);
//TXSTA = (SPEED|NINE_BITS|0x20);
SPBRG = 64; // Baud rate is 9600
BRGH = 1; // High speed baud rate enable
TXEN = 1; // Enable transmission
CREN = 1; // Enable continuous reception
SPEN = 1; // Enable serial port
TXIE = 0;
RCIE = 0; // Disable receive interrupt
TXIF = 0;
RCIF = 0;
PEIE = 1;
GIE = 1;
}
void RS485_Putch(BYTE byte)
{
// Send one byte
while (!TXIF); // Wait until transmit buffer is empty
RS485_DIR_IO = Send;
TXREG = byte;
while (!TRMT); // Wait until transmission is complete
RS485_DIR_IO = Receive;
}
BYTE RS485_Getch(void)
{
// Retrieve one byte
while (!RCIF); // Wait until receive buffer is not empty
RCIF = 0;
return RCREG;
}
BYTE RS485_RW(void)
{
BYTE temp;
RS485_Putch(temp = RS485_Getch());
return temp;
}
#endif
ETOP WIREHARNESS LIMITED , https://www.etopwireharness.com