Pic16876A and USART not able to trasmit data

Status
Not open for further replies.

tonyyy

Member level 1
Joined
Dec 26, 2012
Messages
38
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Visit site
Activity points
1,663
Hello guys. I am using this code (hitech c) for sending and receving data with virtual terminal (proteus). When I type "1" and "2" when
the terminal has a focus I can proprely turn on/off a led on a portB. But the command "b" doesn't return a the string HHHHH. THe baund
is the same 9600..What's wrong? Any help? Thanks

------------------------------
USART


#ifndef _SERIAL_H_
#define _SERIAL_H_

#define BAUD 9600
#define FOSC 20000000L
#define NINE 0 /* Use 9bit communication? FALSE=8bit */

#define DIVIDER ((int)(FOSC/(16UL * BAUD) -1))
#define HIGH_SPEED 1

#if NINE == 1
#define NINE_BITS 0x40
#else
#define NINE_BITS 0
#endif

#if HIGH_SPEED == 1
#define SPEED 0x4
#else
#define SPEED 0
#endif

#define RX_PIN TRISC7
#define TX_PIN TRISC6

/* Serial initialization */
#define INIT_COMMS()\
RX_PIN = 1;\
TX_PIN = 1;\
SPBRG = DIVIDER;\
RCSTA = (NINE_BITS|0x90);\
TXSTA = (SPEED|NINE_BITS|0x20)


void putch(unsigned char);
unsigned char getch(void);
unsigned char getche(void);

#endif

---------------------------

#include <stdio.h>
#include <htc.h>
#include "usart.h"

/* A simple demonstration of serial communications which
* incorporates the on-board hardware USART of the Microchip
* PIC16Fxxx series of devices. */

void putch(unsigned char byte)
{
while(!TXIF) //wait until TXIF is high
continue;
TXREG = byte; //put byte into Transmit Register
}


unsigned char
getch() {
/* retrieve one byte */
while(!RCIF) /* set when register is not empty */
continue;
return RCREG;
}

unsigned char
getche(void)
{
unsigned char c;
putch(c = getch());
return c;
}

void init_enable()
{
BRGH = 1; /* high baud rate */
SYNC = 0; /* asynchronous */
SPEN = 1; /* enable serial port pins */
CREN = 1; /* enable reception */
SREN = 0; /* no effect */
TXIE = 0; /* disable tx interrupts */
RCIE = 0; /* disable rx interrupts */
TX9 = 0; /* 8- or 9-bit transmission */
RX9 = 0; /* 8- or 9-bit reception */
TXEN = 1; /* enable the transmitter */
RCIE =1;
PEIE =1;
}



void main(void){
char rxbyte;

TRISB = 0;
PORTB = 0;
INTCON=0;
INIT_COMMS(); // set up the USART - settings defined in usart.h
init_enable();
while(1){
rxbyte= getch();
unsigned char input;
switch(rxbyte)
{
case '1':
{
PORTB=0xFF;
}
break;

case '2':
{
PORTB=0x00;
}
break;

case 'b':
{
putch("HHHHH");
}
break;

default :
break;

}
}
}
 

Your "putch" routine expects a byte, not a string (as "HHHHH").
putch('H'); // will work ...
 

After any interrupt occurs and its flag (like TMR0IF, RCIF, TXIF) is set then you have to clear it so that interrupt can happen again.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void putch(unsigned char byte) 
{
while(!TXIF) //wait until TXIF is high
TXIF = 0;
continue; 
TXREG = byte; //put byte into Transmit Register 
}
 
 
unsigned char getch() {/* retrieve one byte */
while(!RCIF)    /* set when register is not empty */
RCIF = 0;
continue;
return RCREG;   
}

 

Hi. Thanks a lot for your relplies.
I had to change in H in h and I was able to send the char. The only one thing now is that if I use Virtual Terminal it works like a charm, while connecting
COMPIM to TX,RX with Tera Term VT I am not able to send/receive data. I have created a pair connection COM2 for Proteus and COM5 for Tera Term. RC6 is connected
to RXD and RC7 to TXD. The serial data match, 9600, 1 bit stop, no parity..seem ok. Noboday has experience with compim component?

- - - Updated - - -

Sorry. It works now. RXD -> RC7, TXD-> RC6.
 

Recently it has been answered about how to send/receive data between Proteus and PC VT softwares. Search for Proteus and COMPIM in edaboard. Someone used a virtual port mapper to send/receive data between Proteus UART and PC Hyperterminal.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…