|
|
View previous topic :: View next topic |
Author |
Message |
prafulla.prolific
Joined: 05 Jun 2013 Posts: 10 Location: Mumbai
|
Help for PIC to PIC Communication using RS485 |
Posted: Mon Jun 24, 2013 2:04 am |
|
|
I am having problem in RS485 communication I have written my code with the help of example but i have problem. My hardware is ok having PIC 16F873A, 6 Keys & Max 485 IC .
my code as
Code: |
#include<rs485.c>
#define RS485_ID OUR_RS485_ID
//------------------------------------------------------------------------------
#define MINIMUM_FIRING_ENGLE 150
#define MAXIMUM_FIRING_ENGLE 240
#define CH1_ON 0x01
#define CH1_OFF 0x02
#define CH2_ON 0x03
#define CH2_OFF 0x04
#define CH3_ON 0x05
#define CH3_OFF 0x06
//------------------------------------------------------------------------------
unsigned int1 ch_on;
int8 OUR_RS485_ID = 0;
void initialise_ports(void);
int8 chk_status(void);
unsigned int8 key_data;
unsigned int8 dimming_value;
//------------------------------------------------------------------------------
#int_TBE
void TBE_isr(void)
{
putc(dimming_value);
}
#int_TIMER1
void TIMER1_isr(void)
{
// int8 i;
// if(rs485_get_message(msg, FALSE))
// {
// printf("\n\r%d: ", msg[0]);
// for(i=0; i < msg[1]; ++i)
{
// putc(msg[i+2]);
}
// printf("\n\r");
// }
}
/*
#int_TIMER0
void TIMER0_isr(void)
{
}
*/
//------------------------------------------------------------------------------
void main()
{
unsigned int8 dimming_ch1;
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
SET_TRIS_A(0x3F);
initialise_ports();
// enable_interrupts(INT_TIMER1);
// enable_interrupts(INT_TIMER0);
// enable_interrupts(INT_TBE);
// enable_interrupts(GLOBAL);
key_data=0xff;
while(TRUE)
{
key_data=chk_status();
if(key_data != 0xFF)
{
delay_ms(10);
if(key_data == 0xfe)
{
output_high(PIN_C0);
ch_on=1;
dimming_value=CH1_ON;
enable_interrupts(INT_TBE);
enable_interrupts(GLOBAL);
// disable_interrupts(INT_TBE);
while(! input(PIN_A0));
}
if(key_data == 0xfd)
{
output_low(PIN_C0);
ch_on=0;
dimming_value=CH1_OFF;
enable_interrupts(INT_TBE);
enable_interrupts(GLOBAL);
// disable_interrupts(INT_TBE);
while(! input(PIN_A1));
}
if(key_data == 0xef)
{
output_high(PIN_C1);
dimming_value=CH2_ON;
enable_interrupts(INT_TBE);
enable_interrupts(GLOBAL);
// disable_interrupts(INT_TBE);
while(! input(PIN_A4));
}
if(key_data == 0xdf)
{
output_low(PIN_C1);
dimming_value=CH2_OFF;
enable_interrupts(INT_TBE);
enable_interrupts(GLOBAL);
// disable_interrupts(INT_TBE);
while(! input(PIN_A5));
}
if(key_data == 0xbf)
{
output_high(PIN_C2);
dimming_value=CH3_ON;
enable_interrupts(INT_TBE);
enable_interrupts(GLOBAL);
// disable_interrupts(INT_TBE);
while(! input(PIN_B4));
}
if(key_data == 0x7f)
{
output_low(PIN_C2);
dimming_value=CH3_OFF;
enable_interrupts(INT_TBE);
enable_interrupts(GLOBAL);
// disable_interrupts(INT_TBE);
while(! input(PIN_B5));
}
if(ch_ON)
{
output_high(PIN_C0);
if(key_data == 0xfb)
{
while(!input(PIN_A2));
{
if(dimming_ch1 < 238)
{
dimming_ch1++;
}
else
{
dimming_ch1 = 240;
}
dimming_value=dimming_ch1;
enable_interrupts(INT_TBE);
enable_interrupts(GLOBAL);
// disable_interrupts(INT_TBE);
delay_ms(40);
output_toggle(PIN_C3);
}
}
if(key_data == 0xf7)
{
while(! input(PIN_A3));
{
if(dimming_ch1 > 152)
{
dimming_ch1--;
}
else
{
dimming_ch1 = 150;
}
dimming_value=dimming_ch1;
enable_interrupts(INT_TBE);
enable_interrupts(GLOBAL);
// disable_interrupts(INT_TBE);
delay_ms(40);
output_toggle(PIN_C3);
}
}
}
}
key_data=0xff;
}//end_of_while
}
//------------------------------------------------------------------------------
void initialise_ports(void)
{
output_high(PIN_A0);
output_high(PIN_A1);
output_high(PIN_A2);
output_high(PIN_A3);
output_high(PIN_A4);
output_high(PIN_A5);
output_low(PIN_C0);
output_low(PIN_C1);
output_low(PIN_C2);
output_float(PIN_C3);
output_high(PIN_B4);
output_high(PIN_B5);
}
//------------------------------------------------------------------------------
int8 chk_status(void)
{
int8 temp;
temp=((input_B()<<2) & 0xC0);
temp=((input_A() & 0x3F) | temp);
return temp;
}
//------------------------------------------------------------------------------
void init_variable(void)
{
// MAXIMUM_FIRING_ENGLE;
}
//------------------------------------------------------------------------------
void RS485send(char* s, int8 id)
{
int8 size;
for(size=0; s[size]!='\0'; ++size);
rs485_wait_for_bus(FALSE);
while(!rs485_send_message(id, size, s))
delay_ms(OUR_RS485_ID);
}
//------------------------------------------------------------------------------
|
can any help me please just guide me i will follow guideline. _________________ I m Working as PIC Programmer Fresher |
|
|
prafulla.prolific
Joined: 05 Jun 2013 Posts: 10 Location: Mumbai
|
Re: Help for PIC to PIC Communication using RS485 |
Posted: Mon Jun 24, 2013 3:52 am |
|
|
prafulla.prolific wrote: | I am having problem in RS485 communication I have written my code with the help of example but i have problem. My hardware is ok having PIC 16F873A, 6 Keys & Max 485 IC .
my code as
Code: |
#include<rs485.c>
#define RS485_ID OUR_RS485_ID
//------------------------------------------------------------------------------
#define MINIMUM_FIRING_ENGLE 150
#define MAXIMUM_FIRING_ENGLE 240
#define CH1_ON 0x01
#define CH1_OFF 0x02
#define CH2_ON 0x03
#define CH2_OFF 0x04
#define CH3_ON 0x05
#define CH3_OFF 0x06
//------------------------------------------------------------------------------
unsigned int1 ch_on;
int8 OUR_RS485_ID = 0;
void initialise_ports(void);
int8 chk_status(void);
unsigned int8 key_data;
unsigned int8 dimming_value;
//------------------------------------------------------------------------------
#int_TBE
void TBE_isr(void)
{
putc(dimming_value);
}
#int_TIMER1
void TIMER1_isr(void)
{
// int8 i;
// if(rs485_get_message(msg, FALSE))
// {
// printf("\n\r%d: ", msg[0]);
// for(i=0; i < msg[1]; ++i)
{
// putc(msg[i+2]);
}
// printf("\n\r");
// }
}
/*
#int_TIMER0
void TIMER0_isr(void)
{
}
*/
//------------------------------------------------------------------------------
void main()
{
unsigned int8 dimming_ch1;
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
SET_TRIS_A(0x3F);
initialise_ports();
// enable_interrupts(INT_TIMER1);
// enable_interrupts(INT_TIMER0);
// enable_interrupts(INT_TBE);
// enable_interrupts(GLOBAL);
key_data=0xff;
while(TRUE)
{
key_data=chk_status();
if(key_data != 0xFF)
{
delay_ms(10);
if(key_data == 0xfe)
{
output_high(PIN_C0);
ch_on=1;
dimming_value=CH1_ON;
enable_interrupts(INT_TBE);
enable_interrupts(GLOBAL);
// disable_interrupts(INT_TBE);
while(! input(PIN_A0));
}
if(key_data == 0xfd)
{
output_low(PIN_C0);
ch_on=0;
dimming_value=CH1_OFF;
enable_interrupts(INT_TBE);
enable_interrupts(GLOBAL);
// disable_interrupts(INT_TBE);
while(! input(PIN_A1));
}
if(key_data == 0xef)
{
output_high(PIN_C1);
dimming_value=CH2_ON;
enable_interrupts(INT_TBE);
enable_interrupts(GLOBAL);
// disable_interrupts(INT_TBE);
while(! input(PIN_A4));
}
if(key_data == 0xdf)
{
output_low(PIN_C1);
dimming_value=CH2_OFF;
enable_interrupts(INT_TBE);
enable_interrupts(GLOBAL);
// disable_interrupts(INT_TBE);
while(! input(PIN_A5));
}
if(key_data == 0xbf)
{
output_high(PIN_C2);
dimming_value=CH3_ON;
enable_interrupts(INT_TBE);
enable_interrupts(GLOBAL);
// disable_interrupts(INT_TBE);
while(! input(PIN_B4));
}
if(key_data == 0x7f)
{
output_low(PIN_C2);
dimming_value=CH3_OFF;
enable_interrupts(INT_TBE);
enable_interrupts(GLOBAL);
// disable_interrupts(INT_TBE);
while(! input(PIN_B5));
}
if(ch_ON)
{
output_high(PIN_C0);
if(key_data == 0xfb)
{
while(!input(PIN_A2));
{
if(dimming_ch1 < 238)
{
dimming_ch1++;
}
else
{
dimming_ch1 = 240;
}
dimming_value=dimming_ch1;
enable_interrupts(INT_TBE);
enable_interrupts(GLOBAL);
// disable_interrupts(INT_TBE);
delay_ms(40);
output_toggle(PIN_C3);
}
}
if(key_data == 0xf7)
{
while(! input(PIN_A3));
{
if(dimming_ch1 > 152)
{
dimming_ch1--;
}
else
{
dimming_ch1 = 150;
}
dimming_value=dimming_ch1;
enable_interrupts(INT_TBE);
enable_interrupts(GLOBAL);
// disable_interrupts(INT_TBE);
delay_ms(40);
output_toggle(PIN_C3);
}
}
}
}
key_data=0xff;
}//end_of_while
}
//------------------------------------------------------------------------------
void initialise_ports(void)
{
output_high(PIN_A0);
output_high(PIN_A1);
output_high(PIN_A2);
output_high(PIN_A3);
output_high(PIN_A4);
output_high(PIN_A5);
output_low(PIN_C0);
output_low(PIN_C1);
output_low(PIN_C2);
output_float(PIN_C3);
output_high(PIN_B4);
output_high(PIN_B5);
}
//------------------------------------------------------------------------------
int8 chk_status(void)
{
int8 temp;
temp=((input_B()<<2) & 0xC0);
temp=((input_A() & 0x3F) | temp);
return temp;
}
//------------------------------------------------------------------------------
void init_variable(void)
{
// MAXIMUM_FIRING_ENGLE;
}
//------------------------------------------------------------------------------
void RS485send(char* s, int8 id)
{
int8 size;
for(size=0; s[size]!='\0'; ++size);
rs485_wait_for_bus(FALSE);
while(!rs485_send_message(id, size, s))
delay_ms(OUR_RS485_ID);
}
//------------------------------------------------------------------------------
|
can any help me please just guide me i will follow guideline. |
schematic for circuit as follow.
[img]http://postimg.org/image/3lh0mj333/[/img]
[img][/img] _________________ I m Working as PIC Programmer Fresher |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Mon Jun 24, 2013 5:42 am |
|
|
'it doesn't work'...is not very helpful..
Please breakdown your program into smaller steps!
Get rid of 90% of your code!
You do not need interrupts for this program.
You do not need to set tris either(compiler's very smart,does it for you!
First thing I'd do is to create a 'read keyboard' function. Start with reading the ports and sending data to a PC terminal program to confirm the keys are correct. Then add your 'chk_status' function, test, confirm that your logic is correct, again sending data to a PC terminal program.
You do NOT want to initialise porta pins high. You chose them to be INPUTS and have pullups/switches on them not OUTPUTs.
Same hold true for B4 and B5.
Same problem with C3.
In fact get rid of the entire 'initialise' function.
ther's more 'wrong' but I need to get a coffee....
hth
jay |
|
|
|
|
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
|