UART

Started by wonghongyik December 20, 2008
Hello everybody,

I was trying to write a UART program to send out few bytes of data by
using PIC16F877A, CCS C compiler, MPLAB IDE, MPLAB SIM. The problem of
my program is the code fail to transmit out the data to pin RC6 of
Port C. So far i am just use MPLAB SIM to simulate my program and
using Watch window to monitor all the register and symbol. To simulate
the program i was pressing the "step into" button, and the first 2
bytes of data was load to TXREG and TSR but after that not able to
load the third bytes of data beacuse TXIF flag is not set, this
showing that TXREG was still full and the data look like "jam"
already. from the Watch window the Port C does not show any
value(0x00), i also try to press "animate" and let the program run for
few minutes but still no data was show on Port C. Anybody can help to
solve the problem or give some suggestion?

Below is my program,

#include <16F877A.h>

//***** Function prototype ******//
void init(void);
void transmit (void);
void transmit_empty (void);

//*****variable declaration******//
unsigned char TXSTA,RCSTA,SPBRG,INTCON,PIR1,PIE1,TXREG,PORTC,TRISC;
int count;

//****** define sfr address *****//
#byte TXSTA = 0x98
#byte RCSTA = 0x18
#byte SPBRG = 0x99
#byte PIE1 = 0x8c
#byte INTCON = 0x8b
#byte TXREG = 0x19
#byte RCREG = 0x1A
#byte PIR1 = 0x0c
#byte PORTC = 0x07
#byte TRISC = 0x87

char ucIPOD_packet_origin[2][7]={
{0xff,0x55,0x03,0x02,0x00,0x01,0xfa}, /* " play/pause " */
{0xff,0x55,0x03,0x02,0x00,0x08,0xf3} /* " fwd " */
};

/********************************************************************
FUNCTION : main()
DESCRIPTION : main loop
INPUT : Null
OUTPUT : Null
CREATE :
DATE : 07/10/08
********************************************************************/
void main()
{
init ();
transmit ();
while (1)
{
}
}
/********************************************************************
FUNCTION : init()
DESCRIPTION : sfr initialization
INPUT : Null
OUTPUT : Null
CREATE :
DATE : 07/10/08
********************************************************************/
void init ()
{
PORTC = 0x00; //0b 0000 0000 //Clear Port C

TRISC = 0xFF; //0b 1111 1111 //Enable USART Transmitter

TXSTA = 0x24; //0b 0010 0100 //Transmit Enable //SYNC = 0,
Asynchronous Mode

RCSTA = 0x90; //0b 1001 0000 //Receive Enable //Serial Port Enable

SPBRG = 0x40; //0b 0100 0000 //SPBRG = 64, Baudrate = 19.231KBAUD,
High Baud Rate

INTCON = 0x00; //0b 0000 0000 //Disable Global and Peripheral Interrupt

PIR1 = 0x10; //0b 0001 0000 //USART transmit/Receive Interrupt Flag bit

PIE1 = 0x00; //0b 0000 0000; //Individual enable bits for the
peripheral interrupts.

count = 0;
}
/********************************************************************
FUNCTION : transmit()
DESCRIPTION : uart transmission
INPUT : Null
OUTPUT : Null
CREATE :
DATE : 07/10/08
********************************************************************/
void transmit ()
{
while (1) //Keep on looping
{
if (PIR1 == 0x10) //Check TXIF flag
{ //If TXREG is empty

TXREG = ucIPOD_packet_origin[0][count];//Load data

count++; //Counter increment
}
}
}

I am really no idea why such condition happen, everything look like
set properly but it is not run.
Is it my simualtion methods is wrong or my code was having problem?

Any suggestion is appreciate.

Thanks