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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168 | /*
This program creates an initial map of the robot using a combination of USART and CAN messaging.
/////////////////////////////////
// Create map of the whole robot
// 1. CAN message to node with lowest nodeID = loNODE
// 2. Everyone listens
// 3. loNODE cycles through TxMUX setting Tx = 1;
// 4. loNODE waits for response on each RxMUX;
// 5. If it gets response, loNODE adds responder nodeID to map
// 6. loNODE finished cycling TxMUX; send CAN message confirming finish
// 7. lonNODE = loNODE+1;
// 8. if loNODE exists GOTO 3; else GOTO 9
// 9. DONE
////////////////////////////////
*/
#include "usart.h"
#define BAUD_RATE 2400 //bps.
// #define rxtx_time 64000 // previous 55120 to 65535
#define rxtx_time 55120
int partner[8] = {0,0,0,0,0,0,0,0};
int partnerNum[8] = {0,0,0,0,0,0,0,0};
uint8 USART_search_active = 0;
uint8 USART_timer = 0;
uint8 usart_rx_flag = 0;
void initiateUSART(uint8 usart_Mode)
{
initUSART();
USART_search_active = 1;
USART_timer = 0;
usart_rx_flag = 1;
disable_interrupts(int_timer1);
//disable_interrupts(int_timer3);
set_timer3(rxtx_time);
// set_timer3(55120);
switch(usart_Mode)
{
case 0:
rxSearchRec();
break;
case 1:
rxSearch();
break;
default:
}
enable_interrupts(int_timer1); // don't interrupt until a pulse has started.
}
////////////////////
///Initialize USART
////////////////////
// Setup USART to do 90 khz with 20 Mhz clock using SPBRG = 12;
// Other options used are:
// Asynchronous mode
// Eight bit messages, ninth bit left unused
// Continuous reception
// Set BRGH high to have "fast mode"
// Set interrupts on for both TX and RX
void initUSART(void)
{
TRISB &= 0b00011110; //Enable the B567 to select PORT
//Enable ports B01 to be status outputs
SPBRG = (20000000 / BAUD_RATE)/64 - 1; // Set Baud Rate
TXSTA &= 0b11101111; // Sets of TX USART reg's
TXSTA |= 0b00000100; // High Speed
RCSTAbits_SPEN = 1; // Enables bit output/input set-up
}
void rxSearchRec(void)
{
unsigned char muxPort = 7;
while(USART_search_active) //Try each rx mux port 8 times
{
selectPort(muxPort); // choose the next muxPort
TXSTAbits_TXEN = 1; // Enable TX USART
TXREG = RB_NODEID;
while(TXSTAbits_TRMT == 0){}; //Code now simply waits
//for the transmission to end
TXSTAbits_TXEN = 0; // Disable TX USART
muxPort++; // increment muxPort
if(muxPort > 7) muxPort = 0; // only 7 channels will be used on the muxPort
}
}
void rxSearch(void)
{
unsigned char i = 0;
unsigned char muxPort = 0;
for(i=0; i<8; i++)
{
partner[i]=0; //Initialize the partner slots
partnerNum[i]=0;
}
PORTB &= 0b11111110;
while(USART_search_active)
{
muxPort++; // increment muxPort
if(muxPort > 7) muxPort = 0; // only 6 channels will be used on the muxPort
PORTB |= 0b00000001;
selectPort(muxPort);
listen(muxPort);
neighbor[muxPort] = partner[muxPort];
}
PORTB &= 0b11111110;
}
void nextPort()
{
PORTB += 32;
if(PORTB &= 0b11100000 == 0b11100000)
PORTB &= 0b00011111;
}
void selectPort(uint8 port)
{
uint8 port_temp = 0;
port_temp = port*32;
PORTB &= 0b00011111;
PORTB |= port_temp;
}
void listen(uint8 port)
{
uint8 rcData;
RCSTAbits_CREN = 0;
RCREG = 0;
RCSTAbits_CREN = 1;
while(usart_rx_flag)
{
if(PIR1bits_RCIF)
{
rcData = RCREG;
if(rcData != 0)
{
partnerNum[port]++;
partner[port] = rcData;
}
}
}
usart_rx_flag = 1;
RCSTAbits_CREN = 0;
}
|