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.

Wireless control.........

Status
Not open for further replies.
Ritesh!! The port operations on RD 0...3 are not working correctly... I think there is a read-modify-write issue as this part

C:
	if(ch=='a'){
		RD0=1;
		RD1=0;
		__delay_ms(2);
	}

Switches on RD0 but then switches it off again when RD1 = 0; is called...


Also if I send ascii 101 to the system it reads it fine then it will sit waiting for one of the control codes..


You WILL need to make a better serial communication... The other thing is once you send a control code... Afer 2 mS you then turn it off again..
 
Hi,

i have corrected the problem...
Code:
ch=Hserin();
if(ch=='a'){
RD0=1;
RD1=0;
RD2=0;
RD3=0;

}
if(ch=='b'){
RD0=0;
RD1=1;
RD2=0;
RD3=0;

}
if(ch=='c'){
RD2=1;
RD3=0;
RD0=0;
RD1=0;

}
if(ch=='d'){
RD2=0;
RD3=1;
RD0=0;
RD1=0;

}
if(ch=='e'){
RD1=1;
RD3=1;
RD0=0;
RD2=0;

}
if(ch=='f'){
RD0=1;
RD2=1;
RD1=0;
RD3=0;

}
if(ch=='g'){
RD0=0;
RD1=0;
RD2=0;
RD3=0; 
}
if(ch=='h'){
RD0=0;
RD1=1;
RD2=1;
RD3=0; 
}
if(ch=='i'){
RD0=1;
RD1=0;
RD2=0;
RD3=1; 
}
 
One of you posts.... You were wanting to learn interrupts!! This is the BEST way to service the serial port....

Every time a character arrives on the port it is placed in a queue... Then it doesn't matter what control / data is sent or when.... I'm going to write a simple interrupt routine that services the serial port and return the data in a more organised way....

When you program in C you are keeping the "top down" approach inherent with assembly...

Repetitive chunks of code is a waste of valuable resources.....

I'll post a small program that will show you a better way of what you need..
 
Just messing... I came up with this..

C:
#include <htc.h>
#include <stdlib.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000

void interrupt ISR(void); 
unsigned char input[10];			// Transmision buffer
void  HSerinit(void);
unsigned char Hserin(void);			// transmission functions
int getInt(unsigned char* str);		// a sort of atoi
int rxdata,inchar;							// incomming data store
unsigned char rxcommand;			// incomming command store

void main(void)
	{
	unsigned char portbuf;
	PR2 = 0b11111001 ;
	T2CON = 0b00000100 ;   
	CCP1CON = 0b00111100;                  
	ADCON1 = 0x6;                 
	HSerinit();  
	TRISD=0x00;
 	
	PORTD = 0x0; 
	__delay_ms(502);
	PORTD=0x5;       // for testing hardware it is working till here..
	__delay_ms(502);
	PORTD =0xA;
	__delay_ms(502);
	PORTD = 0x0;
	PEIE = 1;
	GIE = 1;
	while(1)
		{
   		CCPR1L = (unsigned char)rxdata ;// set PWM
		portbuf = 0;
 		switch(rxcommand)
			{
			case ('a'):
				portbuf += 1;
 				break;
			case ('b'):
				portbuf += 2;
				break;
			case ('c'):
				portbuf += 4;
				break;
			case ('d'):
				portbuf += 8;
				break;
			case ('e'):
				portbuf += 10;
				break;
			case ('f'):
				portbuf += 5;
				break;
			default:
				portbuf = 0;
			}	
		PORTD = portbuf;
		__delay_ms(2);
		}
	}
 
 
void interrupt ISR()
	{
	int x;
	if(OERR)
		{
		CREN = 0;
		CREN = 1;
		return;
		}
  	input[inchar] = RCREG;
	if(input[inchar] == 0x0D)
		{
		if(inchar == 1)
			{
			if(input[inchar-1] > 0x39)
				rxcommand = input[inchar-1];
			}
		else
			rxdata = getInt(input);	
		for(x = 0;x<10;x++)
			input[x] = 0;
		inchar = 0;
		return;
		}
	inchar++;	
	}

int getInt(unsigned char* str) // sort of atoi
	{
	int result = 0;
	while(*str !=0x0D)		// until we see carriage return
		{
		if(*str >0x29 && *str < 0x3A)	// Is it a number
			{
			result*=10;
			result+= *str - 0x30;		// convert to int
			}
		str++;
		}
	return result;		// all done
	}
 

 
void HSerinit(void)
	{
    TRISC = 0x80;                    // TX was an input!
    SPBRG = 129;                    // 20Mhz xtal 9600 BAUD
    TXSTA = 0x24;                    // TXEN and BRGH
    RCSTA = 0x90;                    // SPEN and CREN
	RCIE = 1;
	}

Seems to work fine..
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top