|
|
View previous topic :: View next topic |
Author |
Message |
AdamkT1
Joined: 21 Apr 2007 Posts: 44
|
16F877 & 18F452 communication |
Posted: Tue Jul 10, 2007 7:38 am |
|
|
I am trying to establish a communication link between 16F877 and 18F452, where in the 877 acting as master for sending byte data to 452 acting as the receiver.
The problem is that I cant use the standard I2c routines due to a number of reasons. One of them being 18F452 already using interrupts for other functionalities and being a beginner / student, I wish to keep the things as much simple as possible. Therefore I had to develop my own algorithm.
I tried my best not to bother this forum as much as possible but could not solve the problem on my own. So this is an SOS situation for me.
The code for 877 (The sender ) is :
Code: |
#include <DEFS_877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#bit DTR = PORTD.0 // Data Terminal Ready (Out)
#bit CTS = PORTD.1 // signal from 18F452 ready to receive (Input)
#bit CLK = PORTD.2 // Clock (out)
#bit DO1 = PORTD.3 // Data out (out)
#bit DRD = PORTD.4 // Data Read (in)
#define Hi 1
#define Lo 0
#use fast_io(A)
#use fast_io(B)
//!#use fast_io(c) // cant make this execute in CCS compiler
#use fast_io(D)
void sendByte(int8 data)
{
int8 ctr;
PORTC=data;
DTR=Hi; // Signal being ready for sending.
CLK=Hi;
delay_ms(2);
while (CTS==0); //Wait for CTS to go high.
for(ctr=0; ctr<8; ctr++)
{
output_bit(DO1, shift_left(&data,1,0));
CLK=Lo;
delay_ms(5); // let the pin stabilize
CLK=Hi;
delay_ms(5); //let the pin stabilize
}
DTR=Lo;
CLK=Lo;
delay_ms(5); // delay for EEPROM write cycle
}//end sendByte
void main(void)
{
int8 n1;
SET_TRIS_A(0); // Pin A1,A2 inputs
SET_TRIS_B(0);
SET_TRIS_C(0);
SET_TRIS_D(0x12);
PORTA=0;
PORTB=0;
PORTC=0;
PORTD=0;
delay_ms(500); //let the system to stabilize
while (TRUE)
{
for (n1=10; n1<100;n1++)
{
sendByte(n1);
delay_ms(200); //just to be able to see the port values
}//end for
}//end while
} |
18F452 Code is:
Code: |
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#define Hi 1
#define Lo 0
#bit DTR = PORTD.0 // Data Terminal Ready (in)
#bit CTS = PORTD.1 // signal from 18F452 ready to receive (out)
#bit CLK = PORTD.2 // Clock (in)
#bit DI1 = PORTD.3 // Data out (in)
#bit DRD = PORTD.4 // Data Read (out)
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#use fast_io(D)
int8 getByte(void)
{
int8 ctr,data;
CTS=Hi;
while (DTR==Lo);
while (CLK==Lo);
for(ctr=0; ctr<8; ctr++)
{
while (CLK==Hi); //wait for low edge of CLK
shift_left(&data,1,input(DI1));
while (CLK==Lo); //wait for high edge of CLK
}
CTS=Lo;
return(data);
}//end getByte
void main()
{
int8 data;
SET_TRIS_A(0); // Pin A1,A2 inputs
SET_TRIS_B(0);
SET_TRIS_C(0);
SET_TRIS_D(0x0D);
PORTA=0;
PORTB=0;
PORTC=0;
PORTD=0;
delay_ms(500); //let the system to stabilize
while (TRUE)
{
data= getByte();
PORTC=data;
delay_ms(200);
}//end while
} |
After hours of debugging, what I have learnt so far is that the statement :
output_bit(DO1, shift_left(&data,1,0));
in the for loop of sendByte is not working.
Hardware connection details:
I have just connected the Port D pins of 877 to PortD pins of 452 with out any pullups/pulldowns. |
|
|
Ttelmah Guest
|
|
Posted: Tue Jul 10, 2007 8:02 am |
|
|
The problem is your pin definition.
The 'output_bit' command, requires a pin definition, in the format 'PIN_D3', while your define, had D01, defined as 'PORTD.3'...
Best Wishes |
|
|
AdamkT1
Joined: 21 Apr 2007 Posts: 44
|
|
Posted: Wed Jul 11, 2007 10:13 am |
|
|
Thank you.
It solved my problem. |
|
|
|
|
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
|