Do you have a question? Post it now! No Registration Necessary
- Alex Kekin
August 1, 2003, 4:48 pm

Привет All!
Сабж, собственно...
код ниже, прием пакета и передача его же обратно, _HО_! по RS485 со всеми
вытекающими...
код чего-то глючит, возвращает битый пакет...
-------------------------------- код ------------------------------------
#include <pic.h>
#include "sl.h"
#include "delay.h"
#include "i2c.h"
__CONFIG(WDTEN & PWRTEN & DUNPROT & BOREN & LVPDIS & UNPROTECT & HS & DEBUGDIS
& WRTEN);
#define PWR_ON_PIN RA5 // RA.5 - power to DS1990A
#define PWR_ON_DIR TRISA5
#define ID_DQ_PIN RA4 // RA.4 - DQ for DS1990A
#define ID_DQ_DIR TRISA4
#define Z1 RA0 // Loop zone 1
#define Z2 RA1 // Loop zone 2
#define X10RX RA2 // X.10 RX input
#define X10TX RA3 // X.10 TX output
#define X10ZC RB0 // X.10 Zero crossing input
#define KBD PORTD // Keypad interface
#define RELAY RC0 // Relay output
#define RED RC1 // Bicolor LED red
#define GREEN RC2 // Bicolor LED green
#define DE RC5 // Driver enable output
#define CONFIG RB7 // CONFIG jumper
#define Timer1L 0x88 // TMR1L and TMR1H value
#define Timer1H 0x13 // generates interrupt period 1ms (1kHz)
static unsigned char CRC;
static unsigned char CRC8;
static unsigned char buff[8];
static unsigned char SerialBuffer[32];
static unsigned char SerialIndex;
static unsigned char SerialLen;
static unsigned char ByteCnt;
static unsigned char index;
static unsigned char ADDRESS = 0x1F; // ModBus-X.10 Address (RB2-RB6)
<00xxxxxx>
static unsigned char Year = 0;
static unsigned char Month = 1;
static unsigned char Day = 1;
static unsigned char Hour = 0;
static unsigned char Min = 0;
static unsigned char Sec = 0;
static unsigned char m100sec = 100;
static unsigned char m10sec = 10;
static unsigned char delaycnt = 0;
static unsigned char RX_TMR = 0;
static unsigned char TX_TMR = 0;
static unsigned char DAT[8];
static unsigned char DataLen;
static unsigned char ACK;
static unsigned char SAB;
static unsigned char COMMAND;
static unsigned char CmdLen;
static bit CMD;
static bit PING;
static bit CMDPresent;
static bit DataPresent;
static bit ConfigMode;
static bit KeyPresent = 0;
static bit EndRead;
static bit PassOK;
static bit PassPresent = 0;
static bit TX_Flag = 0;
static bit RX_Flag = 0;
static bit RX_Complete = 0;
static bit flash = 0;
const char mnt[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const char CRCTable[] = { 0, 94, 188, 226, 97, 63, 221, 131, 194, 156,
126, 32, 163, 253, 31, 65, 157, 195, 33, 127,
252, 162, 64, 30, 95, 1, 227, 189, 62, 96,
130, 220, 35, 125, 159, 193, 66, 28, 254, 160,
225, 191, 93, 3, 128, 222, 60, 98, 190, 224,
2, 92, 223, 129, 99, 61, 124, 34, 192, 158,
29, 67, 161, 255, 70, 24, 250, 164, 39, 121,
155, 197, 132, 218, 56, 102, 229, 187, 89, 7,
219, 133, 103, 57, 186, 228, 6, 88, 25, 71,
165, 251, 120, 38, 196, 154, 101, 59, 217, 135,
4, 90, 184, 230, 167, 249, 27, 69, 198, 152,
122, 36, 248, 166, 68, 26, 153, 199, 37, 123,
58, 100, 134, 216, 91, 5, 231, 185, 140, 210,
48, 110, 237, 179, 81, 15, 78, 16, 242, 172,
47, 113, 147, 205, 17, 79, 173, 243, 112, 46,
204, 146, 211, 141, 111, 49, 178, 236, 14, 80,
175, 241, 19, 77, 206, 144, 114, 44, 109, 51,
209, 143, 12, 82, 176, 238, 50, 108, 142, 208,
83, 13, 239, 177, 240, 174, 76, 18, 145, 207,
45, 115, 202, 148, 118, 40, 171, 245, 23, 73,
8, 86, 180, 234, 105, 55, 213, 139, 87, 9,
235, 181, 54, 104, 138, 212, 149, 203, 41, 119,
244, 170, 72, 22, 233, 183, 85, 11, 136, 214,
52, 106, 43, 117, 151, 201, 74, 20, 246, 168,
116, 42, 200, 150, 21, 75, 169, 247, 182, 232,
10, 84, 215, 137, 107, 53};
void main(void)
{
/* setup stuff */
//****************** Setup comparators module ************************
CMCON = 0x07;
CVRCON = 0x00;
//****************** Set port direction ******************************
PORTA = 0; // Initialize PORTA by setting output data latches
PORTB = 0; // Initialize PORTB by setting output data latches
PORTC = 0; // Initialize PORTB by setting output data latches
PORTD = 0; // Initialize PORTB by setting output data latches
PORTE = 0; // Initialize PORTB by setting output data latches
TRISA = 0x17; // Value used to initialize data direction
// Set RA<00010111> (0-output, 1-input)
TRISB = 0xFF; // Value used to initialize data direction
// Set RB<11111111> (0-output, 1-input)
TRISC = 0xD8; // Value used to initialize data direction
// Set RC<11011000> (0-output, 1-input)
TRISD = 0xF0; // Value used to initialize data direction
// Set RD<11110000> (0-output, 1-input)
TRISE = 0x07; // Value used to initialize data direction
// Set RE<00000111> (0-output, 1-input)
//****************** Setup timer modules *****************************
TMR1L = Timer1L; // Load TMR1L and TMR1H value
TMR1H = Timer1H;
T1CON = 0x01; // Set T1CON reg B<00000001> Timer1 input internal, on
T2CON = 0x00; // Timer2 off
CCP1CON = 0x00; // CCP1 off
CCP2CON = 0x00; // CCP2 off
//****************** Setup SSP module (I2C) **************************
SSPCON = 0x00; // <00000000>
SSPCON2 = 0x00; // <00000000>
SSPSTAT = 0x00; // <00000000>
SSPADD = 0x00; // 49dec - 100kHz
//****************** Setup ADC module ********************************
ADCON0 = 0xC0; // <11000000>
ADCON1 = 0x06; // <00000110>
//****************** Setup UART module *******************************
TXSTA = 0x24; // <00100100>
RCSTA = 0x90; // <10010000>
SPBRG = 0x81; // 129 (9600N1)
//****************** Setup interrupt *********************************
OPTION = 0x08; // Set OPTION reg B<00001000> prescaler 1:1 to WDT
PIE1 = 0x31; // Set PIE1 reg B<00110001> Timer1 overflow interrupt enable
PIE2 = 0x00; // Set PIE2 reg B<00000000> All off
INTCON = 0xD0; // Set INTCON reg B<11010000> GIE and PEIE enable
//****************** End setup peripheral modules ********************
while(1)
{
CLRWDT(); // Idly kick the dog
if(RX_Complete == 1)
{
SerialLen = SerialIndex;
RX_Complete = 0;
SerialIndex = 0;
RED = 0;
TX_Flag = 1;
TXREG = SerialBuffer[0];
DE = 1;
TXEN = 1;
}
}
}
static void interrupt isr(void)
{
if(TMR1IF)
{
TMR1L = Timer1L; // Load TMR1L and TMR1H value
TMR1H = Timer1H;
if(RX_TMR > 0)
{
--RX_TMR;
}
else
{
if(RX_Flag)
{
RX_Complete = 1;
RX_Flag = 0;
}
}
if(TX_TMR > 0)
{
--TX_TMR;
}
else
{
TXEN = 0;
DE = 0;
GREEN = 0;
}
if(--m10sec == 0)
{
m10sec = 10;
if(flash == 1)
{
flash = 0;
RELAY = 0;
}
else
{
flash = 1;
RELAY = 1;
}
if(--m100sec == 0)
{
m100sec = 100;
Timer();
if(delaycnt > 0)
{
--delaycnt;
}
}
}
TMR1IF = 0; // Clear interrupt flag, ready for next
}
if(TXIF)
{
GREEN = 1;
if(SerialIndex == SerialLen)
{
SerialIndex = 0;
SerialLen = 0;
TX_Flag = 0;
TX_TMR = 20;
}
else
{
TXREG = SerialBuffer[SerialIndex];
++SerialIndex;
}
TXIF = 0;
}
if(RCIF)
{
RED = 1;
RX_TMR = 20;
if(RX_Flag)
{
SerialBuffer[SerialIndex] = RCREG;
++SerialIndex;
if(SerialIndex >= 33)
{
RX_TMR = 0;
RX_Flag = 0;
RX_Complete = 1;
}
}
else
{
if(RX_Complete == 1)
{
RX_Complete = 0;
}
RX_Flag = 1;
SerialIndex=1;
SerialBuffer[0] = RCREG;
}
RCIF = 0;
}
if(INTF)
{
INTF = 0;
}
CLRWDT(); // Clear watchdog timer
}
---------------------------- конец кода ---------------------------------
До встречи,
Alex.
Сабж, собственно...
код ниже, прием пакета и передача его же обратно, _HО_! по RS485 со всеми
вытекающими...
код чего-то глючит, возвращает битый пакет...
-------------------------------- код ------------------------------------
#include <pic.h>
#include "sl.h"
#include "delay.h"
#include "i2c.h"
__CONFIG(WDTEN & PWRTEN & DUNPROT & BOREN & LVPDIS & UNPROTECT & HS & DEBUGDIS
& WRTEN);
#define PWR_ON_PIN RA5 // RA.5 - power to DS1990A
#define PWR_ON_DIR TRISA5
#define ID_DQ_PIN RA4 // RA.4 - DQ for DS1990A
#define ID_DQ_DIR TRISA4
#define Z1 RA0 // Loop zone 1
#define Z2 RA1 // Loop zone 2
#define X10RX RA2 // X.10 RX input
#define X10TX RA3 // X.10 TX output
#define X10ZC RB0 // X.10 Zero crossing input
#define KBD PORTD // Keypad interface
#define RELAY RC0 // Relay output
#define RED RC1 // Bicolor LED red
#define GREEN RC2 // Bicolor LED green
#define DE RC5 // Driver enable output
#define CONFIG RB7 // CONFIG jumper
#define Timer1L 0x88 // TMR1L and TMR1H value
#define Timer1H 0x13 // generates interrupt period 1ms (1kHz)
static unsigned char CRC;
static unsigned char CRC8;
static unsigned char buff[8];
static unsigned char SerialBuffer[32];
static unsigned char SerialIndex;
static unsigned char SerialLen;
static unsigned char ByteCnt;
static unsigned char index;
static unsigned char ADDRESS = 0x1F; // ModBus-X.10 Address (RB2-RB6)
<00xxxxxx>
static unsigned char Year = 0;
static unsigned char Month = 1;
static unsigned char Day = 1;
static unsigned char Hour = 0;
static unsigned char Min = 0;
static unsigned char Sec = 0;
static unsigned char m100sec = 100;
static unsigned char m10sec = 10;
static unsigned char delaycnt = 0;
static unsigned char RX_TMR = 0;
static unsigned char TX_TMR = 0;
static unsigned char DAT[8];
static unsigned char DataLen;
static unsigned char ACK;
static unsigned char SAB;
static unsigned char COMMAND;
static unsigned char CmdLen;
static bit CMD;
static bit PING;
static bit CMDPresent;
static bit DataPresent;
static bit ConfigMode;
static bit KeyPresent = 0;
static bit EndRead;
static bit PassOK;
static bit PassPresent = 0;
static bit TX_Flag = 0;
static bit RX_Flag = 0;
static bit RX_Complete = 0;
static bit flash = 0;
const char mnt[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const char CRCTable[] = { 0, 94, 188, 226, 97, 63, 221, 131, 194, 156,
126, 32, 163, 253, 31, 65, 157, 195, 33, 127,
252, 162, 64, 30, 95, 1, 227, 189, 62, 96,
130, 220, 35, 125, 159, 193, 66, 28, 254, 160,
225, 191, 93, 3, 128, 222, 60, 98, 190, 224,
2, 92, 223, 129, 99, 61, 124, 34, 192, 158,
29, 67, 161, 255, 70, 24, 250, 164, 39, 121,
155, 197, 132, 218, 56, 102, 229, 187, 89, 7,
219, 133, 103, 57, 186, 228, 6, 88, 25, 71,
165, 251, 120, 38, 196, 154, 101, 59, 217, 135,
4, 90, 184, 230, 167, 249, 27, 69, 198, 152,
122, 36, 248, 166, 68, 26, 153, 199, 37, 123,
58, 100, 134, 216, 91, 5, 231, 185, 140, 210,
48, 110, 237, 179, 81, 15, 78, 16, 242, 172,
47, 113, 147, 205, 17, 79, 173, 243, 112, 46,
204, 146, 211, 141, 111, 49, 178, 236, 14, 80,
175, 241, 19, 77, 206, 144, 114, 44, 109, 51,
209, 143, 12, 82, 176, 238, 50, 108, 142, 208,
83, 13, 239, 177, 240, 174, 76, 18, 145, 207,
45, 115, 202, 148, 118, 40, 171, 245, 23, 73,
8, 86, 180, 234, 105, 55, 213, 139, 87, 9,
235, 181, 54, 104, 138, 212, 149, 203, 41, 119,
244, 170, 72, 22, 233, 183, 85, 11, 136, 214,
52, 106, 43, 117, 151, 201, 74, 20, 246, 168,
116, 42, 200, 150, 21, 75, 169, 247, 182, 232,
10, 84, 215, 137, 107, 53};
void main(void)
{
/* setup stuff */
//****************** Setup comparators module ************************
CMCON = 0x07;
CVRCON = 0x00;
//****************** Set port direction ******************************
PORTA = 0; // Initialize PORTA by setting output data latches
PORTB = 0; // Initialize PORTB by setting output data latches
PORTC = 0; // Initialize PORTB by setting output data latches
PORTD = 0; // Initialize PORTB by setting output data latches
PORTE = 0; // Initialize PORTB by setting output data latches
TRISA = 0x17; // Value used to initialize data direction
// Set RA<00010111> (0-output, 1-input)
TRISB = 0xFF; // Value used to initialize data direction
// Set RB<11111111> (0-output, 1-input)
TRISC = 0xD8; // Value used to initialize data direction
// Set RC<11011000> (0-output, 1-input)
TRISD = 0xF0; // Value used to initialize data direction
// Set RD<11110000> (0-output, 1-input)
TRISE = 0x07; // Value used to initialize data direction
// Set RE<00000111> (0-output, 1-input)
//****************** Setup timer modules *****************************
TMR1L = Timer1L; // Load TMR1L and TMR1H value
TMR1H = Timer1H;
T1CON = 0x01; // Set T1CON reg B<00000001> Timer1 input internal, on
T2CON = 0x00; // Timer2 off
CCP1CON = 0x00; // CCP1 off
CCP2CON = 0x00; // CCP2 off
//****************** Setup SSP module (I2C) **************************
SSPCON = 0x00; // <00000000>
SSPCON2 = 0x00; // <00000000>
SSPSTAT = 0x00; // <00000000>
SSPADD = 0x00; // 49dec - 100kHz
//****************** Setup ADC module ********************************
ADCON0 = 0xC0; // <11000000>
ADCON1 = 0x06; // <00000110>
//****************** Setup UART module *******************************
TXSTA = 0x24; // <00100100>
RCSTA = 0x90; // <10010000>
SPBRG = 0x81; // 129 (9600N1)
//****************** Setup interrupt *********************************
OPTION = 0x08; // Set OPTION reg B<00001000> prescaler 1:1 to WDT
PIE1 = 0x31; // Set PIE1 reg B<00110001> Timer1 overflow interrupt enable
PIE2 = 0x00; // Set PIE2 reg B<00000000> All off
INTCON = 0xD0; // Set INTCON reg B<11010000> GIE and PEIE enable
//****************** End setup peripheral modules ********************
while(1)
{
CLRWDT(); // Idly kick the dog
if(RX_Complete == 1)
{
SerialLen = SerialIndex;
RX_Complete = 0;
SerialIndex = 0;
RED = 0;
TX_Flag = 1;
TXREG = SerialBuffer[0];
DE = 1;
TXEN = 1;
}
}
}
static void interrupt isr(void)
{
if(TMR1IF)
{
TMR1L = Timer1L; // Load TMR1L and TMR1H value
TMR1H = Timer1H;
if(RX_TMR > 0)
{
--RX_TMR;
}
else
{
if(RX_Flag)
{
RX_Complete = 1;
RX_Flag = 0;
}
}
if(TX_TMR > 0)
{
--TX_TMR;
}
else
{
TXEN = 0;
DE = 0;
GREEN = 0;
}
if(--m10sec == 0)
{
m10sec = 10;
if(flash == 1)
{
flash = 0;
RELAY = 0;
}
else
{
flash = 1;
RELAY = 1;
}
if(--m100sec == 0)
{
m100sec = 100;
Timer();
if(delaycnt > 0)
{
--delaycnt;
}
}
}
TMR1IF = 0; // Clear interrupt flag, ready for next
}
if(TXIF)
{
GREEN = 1;
if(SerialIndex == SerialLen)
{
SerialIndex = 0;
SerialLen = 0;
TX_Flag = 0;
TX_TMR = 20;
}
else
{
TXREG = SerialBuffer[SerialIndex];
++SerialIndex;
}
TXIF = 0;
}
if(RCIF)
{
RED = 1;
RX_TMR = 20;
if(RX_Flag)
{
SerialBuffer[SerialIndex] = RCREG;
++SerialIndex;
if(SerialIndex >= 33)
{
RX_TMR = 0;
RX_Flag = 0;
RX_Complete = 1;
}
}
else
{
if(RX_Complete == 1)
{
RX_Complete = 0;
}
RX_Flag = 1;
SerialIndex=1;
SerialBuffer[0] = RCREG;
}
RCIF = 0;
}
if(INTF)
{
INTF = 0;
}
CLRWDT(); // Clear watchdog timer
}
---------------------------- конец кода ---------------------------------
До встречи,
Alex.
Site Timeline
- » COM поpт к PC пpиделать дополнительный
- — Next thread in » Microcontrollers (Russian)
-
- » Аккумуляторное питание
- — Previous thread in » Microcontrollers (Russian)
-
- » По моему это гениально
- — Newest thread in » Microcontrollers (Russian)
-
- » Qualcuno si ricorda di JUL?
- — The site's Newest Thread. Posted in » Electronics Hobby (Italian)
-