sana_akhtar
Member level 2
- Joined
- Apr 21, 2012
- Messages
- 47
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,286
- Activity points
- 1,606
If you can't open virtual port COM4 in teraterm or putty, it won't open in Hyperterminal either. So something seems to be confused with your tests.I tried two other software, teraterm and putty. Both says that "unable to open port connection". So it means there is some connection problem. Where do I need to do settings, in code or in serial communication(PC) ?
/* File: UART.c
Purpose: To provide UART functionality for PIC uC
*/
#include "Includes.h"
void InitUART(void)
{
TRISC6 = 1; // TX Pin
TRISC7 = 1; // RX Pin
SPBRG = ((_XTAL_FREQ/16)/BAUDRATE) - 1;
BRGH = 1; // Fast baudrate
SYNC = 0; // Asynchronous
SPEN = 1; // Enable serial port pins
CREN = 1; // Enable reception
SREN = 0; // No effect
TXIE = 0; // Disable tx interrupts
RCIE = 1; // Enable rx interrupts
TX9 = 0; // 8-bit transmission
RX9 = 0; // 8-bit reception
TXEN = 0; // Reset transmitter
TXEN = 1; // Enable the transmitter
}
void SendByteSerially(unsigned char Byte) // Writes a character to the serial port
{
while(!TXIF); // wait for previous transmission to finish
TXREG = Byte;
}
unsigned char ReceiveByteSerially(void) // Reads a character from the serial port
{
if(OERR) // If over run error, then reset the receiver
{
CREN = 0;
CREN = 1;
}
while(!RCIF); // Wait for transmission to receive
return RCREG;
}
void SendStringSerially(const unsigned char* st)
{
int i;
for (i=0; st[i]!='\0';i++)
{
while(!TXIF);
SendByteSerially(st[i]);
}
}
#ifndef __UART_H
#define __UART_H
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 4000000 // Hz
#endif
// Comm Setup
#define BAUDRATE 9600 //bps
// 8 bit data mode with one stop bit
// No flow control, no parity bit
//Function Prototypes
void InitUART(void);
void SendByteSerially(unsigned char);
unsigned char ReceiveByteSerially(void);
void SendStringSerially(const unsigned char*);
#endif
#include <htc.h>
#include "uart.h"
#include "Includes.h"
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 4000000
#endif
void main(void)
{
InitUART();
while(1)
{
SendStringSerially("hello world");
}
}
#ifndef __INCLUDES_H
#define __INCLUDES_H
// Define CPU Frequency
// This must be defined, if __delay_ms() or
// __delay_us() functions are used in the code
#define _XTAL_FREQ 4000000
#include <htc.h>
#include "UART.h"
#include "ISR.h"
#endif
#ifndef __ISR_H
#define __ISR_H
void interrupt ISR(void);
#endif
TRISC6 = 0; // TX Pin as output
.....
while(1)
{
SendByteSerially('H');
SendByteSerially('E');
SendByteSerially('L');
SendByteSerially('L');
SendByteSerially('O');
SendByteSerially(13); // CR
SendByteSerially(10); // LF
Delay_ms(500);
}
}
void main(void)
{
InitUART();
while(1)
{
SendStringSerially("hello world");
SendByteSerially(13); // CR
SendByteSerially(10); // LF
Delay_ms(500);
}
}
Which lines? logic-level UART is idling high, so it should be pulled up (if necesary). RS232 is idle low, but don't need pull-down resistors.Pull down UART lines of PIC using 10ks.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?