Pic to Pic Usart

General discussion on mikroC.
Author
Message
Tanky321
Posts: 40
Joined: 07 Dec 2008 15:29

#31 Post by Tanky321 » 21 Aug 2009 20:06

Thank you very much, I really appreciate all of your help!!

Tanky321
Posts: 40
Joined: 07 Dec 2008 15:29

#32 Post by Tanky321 » 26 Aug 2009 22:46

I've been messing around with this some more, and havent gotten any where. the only thing i can think is that my device flags are not set right.


What I have set is:


WDT_OFF

MCLRE_OFF

INTOSC_OSC_NOCLKOUT


Im not sure if Im missing anything, or if I shouldnt have one of these checked off.


Thanks

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

#33 Post by drdoug » 27 Aug 2009 06:07

I found a 648A to play with and I'll try to squeeze it in tomorrow.

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

#34 Post by drdoug » 27 Aug 2009 15:16

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.

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

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

#35 Post by Mince-n-Tatties » 27 Aug 2009 15:24

drdoug wrote:I hate microcontrollers!
don't be hatin :wink:

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

#36 Post by drdoug » 27 Aug 2009 15:25

I just ported the code over to Pro C and had the same problem so I think it may be a compiler issue. I do not have time to look through the ams now but it may be worth starting a trouble ticket (or I am just missing something obvious which I have already done several times this AM).

Tanky321
Posts: 40
Joined: 07 Dec 2008 15:29

#37 Post by Tanky321 » 27 Aug 2009 18:25

This did in fact end up working, im going to try and see if I can figure anything out with my actual code. Il post the results.



Thanks again!

Tanky321
Posts: 40
Joined: 07 Dec 2008 15:29

#38 Post by Tanky321 » 28 Aug 2009 16:56

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!

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;


}
}

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

#39 Post by drdoug » 30 Aug 2009 20:33

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.

User avatar
nikola.kostic
mikroElektronika team
Posts: 433
Joined: 11 Aug 2009 12:12

#40 Post by nikola.kostic » 03 Sep 2009 14:25

Tanky321,

Try adding Delay_ms(100); after Usart_Init(2400);.

Code: Select all

...
Usart_Init(2400);                  //Establish USART at 2400bps
Delay_ms(100);
...

Tanky321
Posts: 40
Joined: 07 Dec 2008 15:29

#41 Post by Tanky321 » 07 Sep 2009 16:19

Thanks for the replies! Il be trying this soon, I ended up installing Ubuntu on my desktop forgetting that I needed it for the UART terminal! I ended up ordering a USB to RS232 connector, so I will let you guys know how I make out soon.

Tanky321
Posts: 40
Joined: 07 Dec 2008 15:29

#42 Post by Tanky321 » 16 Sep 2009 02:18

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.
Im a little confused as to what your saying? Im not sure which delay your talking about.

I ended up getting my USB to RS232 converter and I tried the 100mS delay, but it didnt help either.

Thanks

Tanky321
Posts: 40
Joined: 07 Dec 2008 15:29

#43 Post by Tanky321 » 21 Sep 2009 13:15

Anyone?

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

#44 Post by drdoug » 21 Sep 2009 14:02

Consider this modification

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;  


}
}
What happened when you tried the code I sent?

Tanky321
Posts: 40
Joined: 07 Dec 2008 15:29

#45 Post by Tanky321 » 21 Sep 2009 21:39

Im not sure which code you mean? Did you send an email or PM or something?


Thanks

Post Reply

Return to “mikroC General”