Pic to Pic Usart
I hate microcontrollers!
Here is what I seem to find after I finally programmed the chip in the correct port (DIP 18B).
It seems like the RCREG port is not being cleared and so USART_Data_Ready is always testing true and so it always enters that loop and assigns "i" the variable that is in that register.
The code will work but you have to send another character to clear out the RCREG. I will also try to port the code to Pro C and see what happens.
Here is what I seem to find after I finally programmed the chip in the correct port (DIP 18B).
It seems like the RCREG port is not being cleared and so USART_Data_Ready is always testing true and so it always enters that loop and assigns "i" the variable that is in that register.
The code will work but you have to send another character to clear out the RCREG. I will also try to port the code to Pro C and see what happens.
Code: Select all
unsigned char i;
unsigned char rxnum;
void main() {
//INTCON.GIE = 0; // Disable interrupts
CMCON = 7; //Declare Variables
VRCON = 0;
PORTA = 0; //Init. PORTA
PORTB = 0; //& PORTB
TRISA = 0x00;
TRISB = 0x00;
//TRISB.F1 = 1;
//rxnum = 0;
Usart_Init(2400); //Establish USART at 2400bps
PORTB.F5 = 1;
Delay_ms(1000);
PORTB.F5 = 0;
Delay_ms(1000);
while(1){
if(Usart_Data_Ready){
i = Usart_Read();
// i = RCREG;
// Usart_Write(i); // write data back to computer
}
if( i == 65){
PORTB.F4 = 1; // Flash light if "A" is received
Delay_ms(1000);
PORTB.F4 = 0;
Usart_Write(i);
i = 0;
}
if( i == 66){
PORTB.F5 = 1; // Flash light if "A" is received
Delay_ms(1000);
PORTB.F5 = 0;
Usart_Write(i);
i = 0;
}
} // end while
} // end main
-
- Posts: 2780
- Joined: 25 Dec 2008 15:22
- Location: Scotland
This is killing me, I cant get anywhere. Ive hooked the receiver to the Terminal and I get no response what so ever. I send the values separately and together and I get no response. I figured I would clear the RCREG at the end of the loop, but im just kinda shooting from the hip. Any other insight into this pain in the butt?
Thanks!
Thanks!
Code: Select all
unsigned short i, rxdata[3];
unsigned int rxnum;
void main() {
INTCON.GIE = 0; // Disable interrupts
cmcon = 7; //Declare Variables
VRCON = 0;
TRISA = 0; //Set PORTA as output
TRISB = 0; //Set PORTB as output
TRISB.F1 = 1; //Set PORTB bit 1 as input
PORTA = 0xFF; //Init. PORTA
PORTB = 0;
PORTB.F4 = 1;
PORTB.F5 = 1; //& PORTB
Usart_Init(2400); //Establish USART at 2400bps
while(1){
if(Usart_Data_Ready()){
i = Usart_Read();
}
if( i == 65){
rxnum = 0;
while( rxnum < 3){
if(Usart_Data_Ready()){
rxdata[rxnum] = Usart_Read();
rxnum++;
}
if((rxdata[1] == 66) && (rxdata[2] == 35)){
PORTA = rxdata[0];}
//Delay_ms(1000); // Longer delay so you dont miss anything
}}
if ( rxdata[0] == 239) {
PORTB.F4 = 0;
//Delay_ms(1000); // Longer delay so you dont miss anything
//PORTA = 0XFF;
//PORTB.F4 = 1;
}
if ( rxdata[0] == 223) {
PORTB.F5 = 0;
//Delay_ms(1000);
//PORTA = 0XFF; // Longer delay so you dont miss anything
//PORTB.F5 = 1;
}
Delay_ms(1000);
PORTA = 0XFF; // Longer delay so you dont miss anything
PORTB.F5 = PORTB.F4 = 1;
RCREG = 0;
}
}
- nikola.kostic
- mikroElektronika team
- Posts: 433
- Joined: 11 Aug 2009 12:12
Tanky321,
Try adding Delay_ms(100); after Usart_Init(2400);.
Try adding Delay_ms(100); after Usart_Init(2400);.
Code: Select all
...
Usart_Init(2400); //Establish USART at 2400bps
Delay_ms(100);
...
Im a little confused as to what your saying? Im not sure which delay your talking about.drdoug wrote:It looks like you have a 1 second delay after you 1 byte of the uart which wouldkeep you from receiving any other characters.
I'll try to play with your code again later.
I ended up getting my USB to RS232 converter and I tried the 100mS delay, but it didnt help either.
Thanks
Consider this modification
What happened when you tried the code I sent?
Code: Select all
...
if ( rxdata[0] == 223) {
PORTB.F5 = 0;
//Delay_ms(1000);
//PORTA = 0XFF; // Longer delay so you dont miss anything
//PORTB.F5 = 1;
}
// Delay_ms(1000); // you'll miss a lot of data if you wait 1 second here
PORTA = 0XFF; // Longer delay so you dont miss anything
PORTB.F5 = PORTB.F4 = 1;
RCREG = 0;
}
}