SK40B..&rs 232..transfer data from pic to pc

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

SK40B..&rs 232..transfer data from pic to pc

Postby sarra » Sat Jan 22, 2011 12:44 pm

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!!
sarra
Newbie
 
Posts: 9
Joined: Sat Jan 22, 2011 12:36 pm

Re: SK40B..&rs 232..transfer data from pic to pc

Postby ABSF » Sat Jan 22, 2011 2:47 pm

Are the Serial hardware from your SK40B and PC already set up? If not, the following will apply.

You may download the SK40C manual and under section 6.2, there is a description on how to connect UART to PC via RS232. Since your SK40B has RS232 built-in, you dont need to buy the USB-UART interface. Just connect your RS232 to PC and setup according to the procedure in the section.

The firmware for 16F877A and 18F4520 are also provided under sample program zip files. Just unzip and programs for the required UART files according to the PIC you use. The source codes in C are also provided so just tailor it to your need.

What kind of data are you going to send to your PC? You need to describe in more detail so members here can help.


Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: SK40B..&rs 232..transfer data from pic to pc

Postby sarra » Sat Jan 22, 2011 4:16 pm

Actually i develop IR sensor circuit.
I do some algorithm for button detection.
Then i make some program to PIC to turn on the LED for each button.
Then i want to send the data to PC and i want to used VISUAL C#/ or visual basic as my GUI to mention that button correct is true or not.
Is it clear my explanation?
And im really blur how to do it
Thanks for your help:)
sarra
Newbie
 
Posts: 9
Joined: Sat Jan 22, 2011 12:36 pm

Re: SK40B..&rs 232..transfer data from pic to pc

Postby shiyan » Sat Jan 22, 2011 11:25 pm

well, the push button detection and LED activation is quite basic, many example out there, even Cytron have many DIY projects using this as basic input and indicator. Sending data from PIC to computer is using UART peripheral and transmit it, you should be able to get the sample source code too. I think SK40C have sample code on this. Anyway, I would strongly suggest you to understand what is UART, as I am sure you will ask in future discussion :mrgreen:

Good luck and do update the progress here.
User avatar
shiyan
Amateur
 
Posts: 189
Joined: Wed Jun 09, 2010 10:59 am

Re: SK40B..&rs 232..transfer data from pic to pc

Postby sarra » Mon Jan 24, 2011 6:56 am

help me doing these tutorial..
is it the code still same for me if im using pic18f452 others than change the type of PIC
PIC 18F4520 to PIC18F452
why i try these tutorial,its failed
CODE: SELECT_ALL_CODE
//=============================================================================
// 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
sarra
Newbie
 
Posts: 9
Joined: Sat Jan 22, 2011 12:36 pm

Re: SK40B..&rs 232..transfer data from pic to pc

Postby ABSF » Mon Jan 24, 2011 7:40 am

I dont think you can directly use the code without modifications if you changed the PIC from 4520 to 452.

Without going through the datasheet of 4520 & 452. First mistake I spotted might be:

#include <p18f4520.h>


You cannot use the header file for 18f4520 for your 18f452. The SFRs may be different. Even though the PIC core is the same 4520 may have extra things like USB support and the hardware pins for UART might be different from 452.

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: SK40B..&rs 232..transfer data from pic to pc

Postby sarra » Mon Jan 24, 2011 7:46 am

For your information,i want to transfer data like these..these is my algorithm

i have 5button and 5 led
when i put 1 of the button at infrared sensor circuit,i had programmed my PIC18 to turn on one of the LED.
This already done

So now,
it means i have 5 output..
and i want to send these 5 output to computer..
sarra
Newbie
 
Posts: 9
Joined: Sat Jan 22, 2011 12:36 pm

Re: SK40B..&rs 232..transfer data from pic to pc

Postby sarra » Mon Jan 24, 2011 8:47 am

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
sarra
Newbie
 
Posts: 9
Joined: Sat Jan 22, 2011 12:36 pm

Re: SK40B..&rs 232..transfer data from pic to pc

Postby robosang » Tue Jan 25, 2011 9:59 pm

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


If you don take the time to understand it, it will take you several months just waiting for other help. All of us been through that process, everyone learn.

ya... i have a link which is master in everything http://www.google.com :mrgreen:
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm

Re: SK40B..&rs 232..transfer data from pic to pc

Postby ober » Wed Jan 26, 2011 9:05 pm

Hi, basically allen is correct, try to modify the code from beginning. Change the header file as a start, if you got error, please show us the error in order for us to know what is going on. Step by step.
Ober Choo
Cytron Technologies Sdn Bhd
www.cytron.com.my
User avatar
ober
Moderator
 
Posts: 1486
Joined: Wed Apr 15, 2009 1:03 pm

Next

Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 3 guests

cron