Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Sending and Receiving Character

Status
Not open for further replies.
Anyone here know how to receiving string from pc to PIC

here is latest code that i get

but it is not code for PIC

Code:
//Le Creusot- France, 21 November 2007

#include <mega16.h>

#include <delay.h>

flash unsigned char string[]={0x80,0xC0,0x40,0x60,0x20,0x30,0x10,0x90};

                     void main(void) {

unsigned char i;  unsigned char data;

UCSRA=0x00; //konfigurasi baud rate 9600bps

UCSRB=0x18;

UCSRC=0x86;

UBRRH=0X00;

UBRRL=0X19;

PORTD=0xFF;

  DDRD=0xF0;    //PD4-PD7 sebagai output

 i=1;

 while (1) {  

while (UCSRA.7) //Apakah ada data baru yang belum dibaca

{

          data=UDR;    

          PORTB=data;   // data dikirim ke Port B

}

if (data==’a’)   //Jika karakter a dikirim,

 {                       // maka motor stepper berputar CW half step

           i--;                         

           if (i==0xFF) i=7;

           PORTD=string[i];

           }

  if(data==’b’)  //Jika karakter b dikirim,

                 {   //motor stepper  berputar CCW half step            

                          …

                 }

               delay_ms(100);                //Untuk bouncing saklar

               PORTD=0;                        //Nilai logika stepper = '0'

     };

}

and Visual Basic Code

for The Transmitter

Code:
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        tampilkanport()

        Dim about As New Mr_Widodo.KomunikasiSerial

        Label3.Text = about.DisplayPembuat

    End Sub

…

Private Sub btnPutarKiri_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPutarKiri.Click

        Dim Serial As New Mr_Widodo.KomunikasiSerial ‘buat objek

        Serial.KirimDataSerial("a", cbPort.Text) ‘Putar kiri

    End Sub

Private Sub btnPutarKanan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim Serial As New Mr_Widodo.KomunikasiSerial

        Serial.KirimDataSerial("b", cbPort.Text) ‘Putar Kanan

    End Sub

End Class

The DLL file is downloadable at internet

just search Mr_Widodo.dll

The C code at above is writen using AVR codevision C

anyone can help me to rewrite the code for PIC ? :)
 
Hi, this is an edited version of the c18 libraries examples from my blog. It takes data from the serial port and makes an array out of it. You can use the Proteus Plug in MPlab and the watch window to see the array.

Code:
//This is a program that displays the use of EUSART with
//Microchips Libraries.It accepts data from the serial port
// and stores it in an array of 10bytes.BR=4800,No Parity,1 Stop Bit
//The EUSART Section is documented on P#70 of C18 Libraries
//Use 181320Basic.DSN

/****************************************************************/
//For Baud Rate calculation refer to Page 137 of datasheet
//Baud Rate=Fosc/[64(n+1)], n=value of SPBRGH:SPBRG register pair
//For a Baud Rate of 4800, we need to find value of SPBRGH:SPBRG register pair
//SPBRGH:SPBRG=((FOSC/Desired Baud Rate)/64) – 1
//			  =((4Mhz/4800)/64)-1
//			  =(833.33/64)-1
//		      =13.02-1
//            =12.02~12
//SPBRGH:SPBRG=12
/****************************************************************/

#include<p18f1320.h>// Include files and definitions for the Processor
#include<usart.h>// Include function definitions for the USART library
#pragma config OSC=INTIO2,WDT=OFF,LVP=OFF// Configuration settings for the 18f1320.
unsigned char readbuffer[10];//Number of characters to be read
unsigned char readpointer=0;//Pointer for the characters
void chk_isr(void);// ISR handler
void hi_prioriint(void)// Hi priority interrupt vector handler
{
	_asm
	GOTO chk_isr
	_endasm
}
#pragma interrupt chk_isr
void chk_isr(void)
{
	unsigned char startcode;
	if(PIR1bits.RCIF==1)
	{	
		if(PIR1bits.RCIF==1)// If any thing received go ahead.
		{	
		readbuffer[readpointer]=ReadUSART();//Read the character recieved and save in startread
		putrsUSART("\n Character added \n");// Send confirmation of the character being added.
		}
	    readpointer++;	
		PIR1bits.RCIF==0;
	}
}
void main(void)
{	
	TRISBbits.TRISB1=0;// Set TX pin as output
	TRISA=0x00;
	OSCCON=0b01100000; //Internal 4MHZ Oscillator
    ADCON1=0x7F;//Make all ports Digital	
	OpenUSART(USART_TX_INT_OFF &// Initialize USART, Transmit interrupt off
			  USART_RX_INT_ON &// Receive interrupt ON
			  USART_ASYNCH_MODE & // Use USART in asynchronous mode 
			  USART_EIGHT_BIT &// Eight bit data
			  USART_CONT_RX &// Enable continous receiving
			  USART_BRGH_LOW,// Do not use baud rate multiplication
			  12);// For a baud rate of 9600 value of SPBRGH:SPBRG register pair
	INTCONbits.PEIE=1;// Enable peripheral interrupts
	INTCONbits.GIEH=1;// Enable all interrupts
	putrsUSART ("\nType in characters to store as a string");
	while(1);// Stay here(wait for interrupts to occur)
CloseUSART();// Disable USART Module.
}

Use 18fBasic.DSN from my blog to test it.
 
Hi, this is an edited version of the c18 libraries examples from my blog. It takes data from the serial port and makes an array out of it. You can use the Proteus Plug in MPlab and the watch window to see the array.

Code:
//This is a program that displays the use of EUSART with
//Microchips Libraries.It accepts data from the serial port
// and stores it in an array of 10bytes.BR=4800,No Parity,1 Stop Bit
//The EUSART Section is documented on P#70 of C18 Libraries
//Use 181320Basic.DSN

/****************************************************************/
//For Baud Rate calculation refer to Page 137 of datasheet
//Baud Rate=Fosc/[64(n+1)], n=value of SPBRGH:SPBRG register pair
//For a Baud Rate of 4800, we need to find value of SPBRGH:SPBRG register pair
//SPBRGH:SPBRG=((FOSC/Desired Baud Rate)/64) – 1
//			  =((4Mhz/4800)/64)-1
//			  =(833.33/64)-1
//		      =13.02-1
//            =12.02~12
//SPBRGH:SPBRG=12
/****************************************************************/

#include<p18f1320.h>// Include files and definitions for the Processor
#include<usart.h>// Include function definitions for the USART library
#pragma config OSC=INTIO2,WDT=OFF,LVP=OFF// Configuration settings for the 18f1320.
unsigned char readbuffer[10];//Number of characters to be read
unsigned char readpointer=0;//Pointer for the characters
void chk_isr(void);// ISR handler
void hi_prioriint(void)// Hi priority interrupt vector handler
{
	_asm
	GOTO chk_isr
	_endasm
}
#pragma interrupt chk_isr
void chk_isr(void)
{
	unsigned char startcode;
	if(PIR1bits.RCIF==1)
	{	
		if(PIR1bits.RCIF==1)// If any thing received go ahead.
		{	
		readbuffer[readpointer]=ReadUSART();//Read the character recieved and save in startread
		putrsUSART("\n Character added \n");// Send confirmation of the character being added.
		}
	    readpointer++;	
		PIR1bits.RCIF==0;
	}
}
void main(void)
{	
	TRISBbits.TRISB1=0;// Set TX pin as output
	TRISA=0x00;
	OSCCON=0b01100000; //Internal 4MHZ Oscillator
    ADCON1=0x7F;//Make all ports Digital	
	OpenUSART(USART_TX_INT_OFF &// Initialize USART, Transmit interrupt off
			  USART_RX_INT_ON &// Receive interrupt ON
			  USART_ASYNCH_MODE & // Use USART in asynchronous mode 
			  USART_EIGHT_BIT &// Eight bit data
			  USART_CONT_RX &// Enable continous receiving
			  USART_BRGH_LOW,// Do not use baud rate multiplication
			  12);// For a baud rate of 9600 value of SPBRGH:SPBRG register pair
	INTCONbits.PEIE=1;// Enable peripheral interrupts
	INTCONbits.GIEH=1;// Enable all interrupts
	putrsUSART ("\nType in characters to store as a string");
	while(1);// Stay here(wait for interrupts to occur)
CloseUSART();// Disable USART Module.
}

Use 18fBasic.DSN from my blog to test it.
hi

thanks for your example

i will take a look :)
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top