Serial comm using pic16f877a....

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Here it is...........

Rich (BB code):
include <pic.h>                // pic specific identifiers 
#define _XTAL_FREQ  20000000        // Xtal speed 
__CONFIG(0x3F71);                // Config bits             // Required prototypes.. each function needs to be declared         // if called BEFORE definition.

unsigned char  HSerin(void);
 void HSerout(unsigned char ch),
 HSerinit(void);   void main(void)                        // program entry  
   {     
int index = 0;  
   unsigned char ch;  
   ADCON1 = 0x6;                    // Analogue off    
                     HSerinit();      
  __delay_ms(150);    
 while(1)                        // endless Loop       
  {         ch = HSerin();                // wait for a character          HSerout(ch);                // Echo back           }     }   void HSerinit()    
 {     TRISC = 0b10000000;                    // should ideally be set  
   SPBRG = 129;                    // 20Mhz xtal 9600 BAUD    
 TXSTA = 0x24;                    // TXEN and BRGH   
  RCSTA = 0x90;                    // SPEN and CREN    
 }  
 void HSerout(unsigned char ch)     {  
   while(!TXIF);                    // Wait for module to finish   
  TXREG = ch;                        // ready to send   
  }
 unsigned char HSerin()    
{     while(!RCIF);                    // Wait for a character    
 return RCREG;                    // return character     
  }
 
Here it is...........
Can you send a character out in an endless manner first? That way it's easier to know if you are able to get the serial port working.

And plus, on the "unsigned char ch" part, make it "unsigned char ch = 'a'".

It sends an 'a', instead of 0, which means "null".

Rich (BB code):
include <pic.h>                // pic specific identifiers 
#define _XTAL_FREQ  20000000        // Xtal speed 
__CONFIG(0x3F71);                // Config bits             // Required prototypes.. each function needs to be declared         // if called BEFORE definition.

unsigned char  HSerin(void);
 void HSerout(unsigned char ch),
 HSerinit(void); 

 void main(void)                        // program entry  
   {     
   int index = 0;  
   unsigned char ch = 'a';         // <- LOOK HERE.
   ADCON1 = 0x6;                    // Analogue off    
   HSerinit();      
  __delay_ms(150);    
 while(1)                        // endless Loop       
  {           HSerout(ch);                // Echo back     
  }
 
 } 


 void HSerinit()    
 {     
   TRISC = 0b10000000;                    // should ideally be set  
   SPBRG = 129;                    // 20Mhz xtal 9600 BAUD    
   TXSTA = 0x24;                    // TXEN and BRGH   
   RCSTA = 0x90;                    // SPEN and CREN    
 }  

 void HSerout(unsigned char ch)     {  
   while(!TXIF);                    // Wait for module to finish   
  TXREG = ch;                        // ready to send   
  }

 unsigned char HSerin()    
 {     while(!RCIF);                    // Wait for a character    
 return RCREG;                    // return character     
  }
So, you will see "aaaaaaaaaaaaa....." in the terminal, continuously without stopping.
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
OK, it is working but few error are there i don't know them...
it is working now thanks...
but i found some problem it display 'a' also but some times other default character , why??
 

Attachments

Can you add a delay in between sending the 'a', likE:

Rich (BB code):
while(1)                        // endless Loop       
  {           HSerout(ch);                // Echo back  
   __delay_ms(250);                     // Delay Code here 
  }
 
 }
 
Have you get the 'a's consistently first? If you can get those things right, then you can proceed to troubleshooting other stuff. No point tackling all things at one go.

If you have the schematics here, put them up again. Do you have a power supply already connected to the system? It's better to power up the system using a seperate supply than the USB.

Edit: In the RealTerm, you can try selecting "none" for the "Hardware Flow Control" too.
 
Last edited:
One more thing, in the RealTerm, you can try selecting "none" for the "Hardware Flow Control" too. Try and see whether the 'a's are sent consistently.
 
Check your circuit. You must not short these Rx and Tx lines. I tested the entire identical thing over in front of me, it's working correctly without interruptions.
 
I'm not using a USB power source. I have a board which powers from the AC adapter. Again, check the circuit for possible short-circuits or very loose connections.
 
Do you have the the picture and the documents of the usb module? You can either power the system using the said usb module, or using a constant DC supply. Do not connect both together.

Use an AC adapter with a voltage converter. The 9V battery can run out very, very fast and may give unpredictable results when the juice becomes less.
 
Don't short them. There's nothing much if you short these pins. Keep these seperate.

Have you tried, or at least think about connecting the system to a constant +5V supply? Not battery.

One wire from Tx pin on the PIC16F microcontroller to the Rx pin on the USB module will be enough, if you power the system with a constant supply.
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Don't short them. There's nothing much if you short these pins. Keep these seperate.
I don't know why the usb module is not working now???
Have you tried, or at least think about connecting the system to a constant +5V supply? Not battery.
after changing it tpo 12V 4A adapter and using 7805 on board i think usb module is damaged!!
 
Top