hEY..I'M using ctron sk40B..and im really dummies and new using this
Could someone teach me how to transfer data from my PIC to PC using rs232..
I'm really want to learn it..Someone,help me please!!
//=============================================================================
// Filename: Example_uart.c
//-----------------------------------------------------------------------------
// Compiled using MPLAB-C18 v3.34 student edition
//=============================================================================
// Company : Cytron Technologies, Malaysia
// Revision : 1.00
// Date : 28 Dec 2009
//=============================================================================
#include <p18f4520.h>
#include "xlcd.h"
#include "delays.h"
#include "usart.h"
//=============================================================================
// Configuration Bits
//=============================================================================
#pragma config OSC = HS // HS oscillator
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor disabled
#pragma config IESO = OFF // Oscillator Switchover mode disabled
#pragma config PWRT = OFF // PWRT disabled
#pragma config BOREN = OFF // Brown-out Reset disabled in hardware and software
#pragma config WDT = OFF // WDT disabled (control is placed on the SWDTEN bit)
#pragma config MCLRE = ON // MCLR pin enabled; RE3 input pin disabled
#pragma config PBADEN = OFF // PORTB<4:0> pins are configured as digital I/O on Reset
#pragma config CCP2MX = PORTC // CCP2 input/output is multiplexed with RC1
#pragma config LVP = OFF // Single-Supply ICSP disabled
#pragma config XINST = OFF // Extended Instruction Set
//=============================================================================
// Define Pins
//=============================================================================
#define led1 LATBbits.LATB6 //Active High
#define led2 LATBbits.LATB7
#define sw1 PORTBbits.RB0 //Active Low
#define sw2 PORTBbits.RB1
//=============================================================================
// Function Prototypes
//=============================================================================
void Delay_1msX (unsigned int miliseconds);
void Delay_100msX (unsigned int msec);
unsigned char uart_rec(void) //receive uart value
{
unsigned char rec_data;
while(PIR1bits.RCIF==0); //wait for data
rec_data=RCREG;
return rec_data; //return the data received
}
void uart_send(unsigned char data)
{
while(TXSTAbits.TRMT==0); //only send the new data after
TXREG=data; //the previous data finish sent
}
void uart_str(const rom char *s)
{
while(*s)uart_send(*s++);
}
//=============================================================================
// Global Variables
//=============================================================================
unsigned int i, t;
unsigned char data_tx, data_rx;
//=============================================================================
// Main Program
//=============================================================================
void main ()
{
//set I/O input output
TRISB = 0b00000011; //Configure PORTB I/O direction
TRISC = 0b11000000; //Configure PORTC I/O direction
TRISD = 0b00000000; //Configure PORTD I/O direction
PORTB = 0;
PORTC = 0;
PORTD = 0;
//-------------------------------------------------------------------------
// Configure External LCD
//-------------------------------------------------------------------------
OpenXLCD( EIGHT_BIT & LINES_5X7 );
ClearXLCD(); //Clear display
led1=1; //Turn on led1
//-------------------------------------------------------------------------
// Configure UART
//-------------------------------------------------------------------------
OpenUSART (USART_TX_INT_OFF & //Transmit interrupt off
USART_RX_INT_OFF & //Receive interrupt off
USART_ASYNCH_MODE & //Asynchronous mode - UART
USART_EIGHT_BIT & //8-bit transmit/receive
USART_CONT_RX & //Continuous reception
USART_BRGH_HIGH, //High baudrate
129); //9600 baudrate at 20MHz frequency
// baudUSART (BAUD_IDLE_CLK_HIGH &
// BAUD_8_BIT_RATE & //
// BAUD_WAKEUP_OFF &
// BAUD_AUTO_OFF);
led2=1; //Turn on led2
while(1)
{
//-------------------------------------------------------------------------
// UART Transmit
//-------------------------------------------------------------------------
SetCurXLCD(0); //Set cursor to line 1, position 0
putrsXLCD("Transmitted:"); //Display LCD
data_tx='O'; //Assign data
while(BusyUSART()); //Check if Usart is busy or not
putcUSART(data_tx); //Send data through UART
putcXLCD(data_tx); //Display data sent
data_tx='K'; //Assign data
while(BusyUSART()); //Check if Usart is busy or not
putcUSART(data_tx); //Send data through UART
putcXLCD(data_tx); //Display data sent
//-------------------------------------------------------------------------
// UART Receive
//-------------------------------------------------------------------------
SetCurXLCD(20); //Set cursor to line 2, position 0
putrsXLCD("Received :"); //Display LCD
while(PIR1bits.RCIF==0); //Data ready?
data_rx=getcUSART(); //Assign received data to variable
while(BusyUSART()); //Check if Usart is busy or not
putcUSART(data_rx); //Echo the data in Hyperterminal
putcXLCD(data_rx); //Display data received from UART
Delay_100msX(20); //Delay for 2 seconds
ClearXLCD(); //Clear LCD
}
}//End of main
//=============================================================================
// Subroutines
//=============================================================================
/********************************************************************
* Function Name: Delay_1msX *
* Return Value: void *
* Parameters: miliseconds: amount of delay *
* Description: This routine generates various delays *
* needed by xlcd functions. *
* For delay of 1ms (18F4550 running at 48MHz) *
* Cycles = (TimeDelay * Fosc) / 4 *
* Cycles = (1ms * 48MHz) / 4 *
* Cycles = 12,000 *
* Since call of function also takes some *
* instruction cycles, the exact value to get *
* 1ms delay is less than 12,000. *
********************************************************************/
void Delay_1msX (unsigned int miliseconds)
{
t=0;
while(t<miliseconds)
{
Delay1KTCYx(11);
Delay10TCYx(96);
Nop();
Nop();
Nop();
Nop();
Nop();
t++;
}
}//End of Delay_1msX
/********************************************************************
* Function Name: Delay_100msX *
* Return Value: void *
* Parameters: miliseconds: amount of delay *
* Description: This routine generates various delays *
* needed by xlcd functions. *
* For delay of 100ms *
* (18F4550 running at 48MHz) *
* Cycles = (TimeDelay * Fosc) / 4 *
* Cycles = (100ms * 48MHz) / 4 *
* Cycles = 1,200,000 *
* Since call of function also takes some *
* instruction cycles, the exact value to get *
* 1ms delay is less than 1,200,000. *
********************************************************************/
void Delay_100msX (unsigned int msec)
{
t=0;
while(t<msec)
{
Delay10KTCYx(119);
Delay1KTCYx(9);
Delay10TCYx(96);
t++;
}
}//End of Delay_100msX
#include <p18f4520.h>
sarra WROTE:So How to modify it?I take several days just for doing these simple code.
Hey,do you have ym or any instant message?I really need your help
Users browsing this forum: No registered users and 3 guests