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;
}
}
}