Hi guys
I'm trying to send 2 different bytes (one at a time) from the slave to the master; different actions must be taken by the slave depending on which byte is received. I wrote some code to confirm that the master is sending the different bytes and it is indeed. I also confirmed that the slave is receiving the different bytes. The problem arises when I modify the code to include the "if statements" to check which byte was received and take appropriate action; this part of the code just doesn't work. I have LED's on RA0 and RA1 to check the results.
Please help!
Code C - [expand ] 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include<htc.h>
#define _XTAL_FREQ 4000000
char UART_Init( const long int baudrate) ;
char UART_TX_Empty( ) ;
char UART_Data_Ready( ) ;
char UART_Read( ) ;
void UART_Read_Text( char * Output, unsigned int length) ;
void UART_Write( char data) ;
void UART_Write_Text( char * text) ;
void main( )
{
int d;
PORTA = 0x00 ;
TRISA = 0x00 ; // Set PORTA as output
// Set all pins as digital
ANSEL = 0x00 ;
ANSELH = 0x00 ;
// Disable comparators
CM1CON0 = 0x00 ;
CM2CON0 = 0x00 ;
CM2CON1 = 0x00 ;
UART_Init( 9600 ) ;
do
{
if ( UART_Data_Ready( ) )
{ d = UART_Read( ) ; }
if ( d == 0xAA )
{ RA0 = 1 ; }
else
if ( d == 0xFF )
{ RA0 = 0 ; }
__delay_ms( 50 ) ;
} while ( 1 ) ;
}
/********** UART.h **************/
char UART_Init( const long int baudrate)
{
ANSEL = 0x00 ;
unsigned int x;
x = ( _XTAL_FREQ - baudrate* 64 ) / ( baudrate* 64 ) ;
if ( x> 255 )
{
x = ( _XTAL_FREQ - baudrate* 16 ) / ( baudrate* 16 ) ;
BRGH = 1 ;
}
if ( x< 256 )
{
SPBRG = x;
SYNC = 0 ;
SPEN = 1 ;
TRISB7 = 1 ;
TRISB5 = 1 ;
CREN = 1 ;
TXEN = 1 ;
return 1 ;
}
return 0 ;
}
char UART_TX_Empty( )
{
return TRMT;
}
char UART_Data_Ready( )
{
return RCIF;
}
char UART_Read( )
{
while ( ! RCIF) ;
return RCREG;
}
void UART_Read_Text( char * Output, unsigned int length)
{
int i;
for ( int i= 0 ; i< length; i++ )
Output[ i] = UART_Read( ) ;
}
void UART_Write( char data)
{
while ( ! TRMT) ;
TXREG = data;
}
void UART_Write_Text( char * text)
{
int i;
for ( i= 0 ; text[ i] != '\0 ' ; i++ )
UART_Write( text[ i] ) ;
}
Last edited by a moderator: Nov 21, 2014