Hi Idris,
The codes are as follows:
- CODE: SELECT_ALL_CODE
#include <htc.h>
#include "lcd.h"
#include "system.h"
#include "uart.h"
// Configuration
//==========================================================================
__CONFIG ( 0x2FF2 ); // Configuration for PIC16F887 at 2007
__CONFIG ( 0x3FFF ); // Configuration for PIC16F887 at 2008
void delay(unsigned int ui_value)
{
while (ui_value-- > 0)
{
__delay_ms(1); // macro from HI-TECH compiler which will generate 1ms delay base on value of _XTAL_FREQ in system.h
}
}
// main function
//==========================================================================
void main(void)
{
PORTA = 0; // Clear Port
PORTB = 0;
PORTC = 0;
PORTD = 0;
TRISA = 0b00000000; // set PORTA as INPUT
TRISB = 0b00000011; // set PORTB<7:2> as OUTPUT , PORTB<1:0> as INPUT
TRISC = 0b11000000;
TRISD = 0b00000000; // set PORTD as output
ANSELH = 0; // SET PORTB as DIGITAL I/O for PIC16F887
uart_initialize(); // Initialise UART
delay(500);
lcd_initialize(); // Initialise LCD
// send signal to Hyperterminal to tell the SK40C is ready
lcd_putstr("Ready To Receive & Transmit!!");
delay(2000);
lcd_clear();
uart_putstr("Ready To Receive & Transmit!!");
delay(1200);
// receive data from HyperTerminal by pressing PC keyboard
while (1)
{
unsigned int data,i = 0;
for (i=0 ; i<32 ; i++) // LCD have 32 display spaces
{
if(i == 15)lcd_2ndline(); // if more then 0x0F,jump to 2nd line
else if (i == 31)lcd_home(); // if more then 0x4F, jump to 1st line
uc_uart_receive(); // UART receive data
data = RCREG; // receive data from uart and save at data
send_lcd_data(1,data); // data the data to LCD display
}
}
}
While in the uart.c are some codes as follows:
- CODE: SELECT_ALL_CODE
#include <htc.h>
#include "system.h"
#include "uart.h"
/*******************************************************************************
* PUBLIC FUNCTION: uart_initialize
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Initialize the UART module.
*
*******************************************************************************/
void uart_initialize(void)
{
unsigned char dummy = 0;
BRG16 = 0;
SYNC = 0;
TX9 = 0;
RX9 = 0;
BRGH = 1; // Select high speed baud rate.
SPBRG = 129; // Configure the baud rate.
SPEN = 1; // Enable serial port.
CREN = 1; // Enable reception.
TXEN = 1; // Enable transmission.
TXIE = 0;
RCIE = 1;
dummy = RCREG; // dummy read, to clear the receive buffer
dummy = RCREG;
}
/*******************************************************************************
* PUBLIC FUNCTION: uart_transmit
*
* PARAMETERS:
* ~ uc_data - The data that we want to transmit.
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* This function will transmit one byte of data using UART. This is a blocking
* function, if the transmit buffer is full, we will wait until the
* data in the buffer has been sent out before we move in the new data.
*
*******************************************************************************/
void uart_transmit(unsigned char uc_data)
{
// Wait until the transmit buffer is ready for new data.
while (TXIF == 0);
// Transmit the data.
TXREG = uc_data;
}
/*******************************************************************************
* PUBLIC FUNCTION: uc_uart_receive
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ The data received from the UART.
*
* DESCRIPTIONS:
* This function will receive one byte of data using UART. This is a blocking
* function because if the receive buffer is empty, we will wait until the
* data is received.
*
*******************************************************************************/
unsigned char uc_uart_receive(void)
{
// If there is overrun error...
if (OERR == 1) {
// Clear the flag by disable and enable back the reception.
CREN = 0;
CREN = 1;
}
// Wait until there is data available in the receive buffer.
while (RCIF == 0);
// Return the received data.
return RCREG;
}
/*******************************************************************************
* PUBLIC FUNCTION: uart_putstr
*
* PARAMETERS:
* ~ csz_string - The null terminated string to transmit.
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Transmit a string using the UART.
*
*******************************************************************************/
void uart_putstr(const char* csz_string)
{
// Loop until the end of string.
while (*csz_string != '\0') {
uart_transmit(*csz_string);
// Point to next character.
csz_string++;
}
}
Luckily I have discovered to ensure the communication between the MCU and my PC. Each of them can send and receive strings now. But there is another problem. I cannot remove my ICSP programmer from the board and PCeven though I am not using it. I have to keep them connected. Otherwise, the MCU acan only receive the strings from ny PC while the MCU cannot send any string to my PC through the UC00A.
How can this even happening? The pins are not even related to each other. To program the MCU, it needs RB6 and RB7 ports. While for the UART, it will only needs RC6 and RC7.
Can you suggest me something?