hi there.. im doin a project similiar to (similiar to PR 23 : Multifunction Mobile Robot).
i just want the Xbee Module mode only, since im gonna control my car using skxbee from my pc.. but im using DC motor rather than servo motor.
I need urgent help regarding the programming since im kinda weak in interrupt
below is my idea hw the programming should b:
from hyperterminal(computer)-> press 8 on keyboard
V
SKXBee will transmit (connect to computer using USB)
V
SKXBee(mounted at the car) will receive the data
V
PIC( receive the data by UART)
V
DC motor rotate(car move forward)
hope u guys can help to solve my problem.. thx
- CODE: SELECT_ALL_CODE
// include
#include <pic.h>
// configuration
__CONFIG ( 0x3F32 );
unsigned char data[0] = {0};
unsigned int result;
unsigned int To=0,T=0,TH=0;
unsigned char REC;
unsigned char i=0;
unsigned int us_value (unsigned char mode);
// function prototype
void init(void);
void wireless_xbee(void);
void forward(void);
void stop (void);
void backward (void);
void reverse (void);
void left(void);
void right(void);
unsigned char uart_rec(void);
// interrupt prototype
static void interrupt isr(void)
{
if (TMR0IF) // TMR0 is overflow
{
TMR0IF = 0; // clear flag bit
To +=0x100; // count number of TMR0 overflow ( make it to 16bit TMR)
}
if(RBIF) // there is change bit on RB4-RB7
{
RBIF = 0; // ____
if (RB4) // Rb4 is 1 mean is rising form 0 __|
{
TMR0 = 0; // clear all counter involved, start new count for period of RB4 high
To = 0;
}
// ___
else TH = TMR0 + To; // RB4 is 0 mean is falling form 1 |_____ // save TH, RB4 high period
}
if(RCIF)
{
RCIF = 0; // clear flag bit
if (RCREG == 'R') data[i=0]= RCREG; // check if start byte 'R' is met
else if (RCREG == 100) data[i=0]= RCREG; // check if start byte 'd'(decimal 100) is met
if ((data[0] == 'R'))data [i++] = RCREG; // save the data in data array
if (i>4) i = 4; // if the data array reached max, set the index to 4
}
}
// main function
void main(void)
{
init(); // initiate cnfiguration and initial condition
while(1) // loop
{
wireless_xbee();
}
}
// Initailization
void init()
{
// Tris configuration (input or output)
TRISD = 0x00; //set all PORTD pin as output
// TMR 0 configuation
T0CS = 0;
PSA = 0;
PS2 = 1; // prescale 1:32
PS1 = 1; //
PS0 = 1; //
TMR0IE = 1; // TMR0 Interrupt
TMR0 = 0;
//setup UART
SPBRG = 0x81; //set baud rate to 9600 for 20Mhz
BRGH = 1; //baud rate high speed option
TXEN = 1; //enable transmission
TX9 = 0;
CREN = 1; //enable reception
SPEN = 1; //enable serial port
RX9 = 0;
RCIE = 1; //enable interrupt on eachdata received
// enable all unmasked interrupt
GIE = 1;
PEIE = 1;
TX_PIN = 1;
stop();
}
//Mode 4 : Xbee
// Description : Control the robot using UART ( XBEE or an UART wireless module.
void wireless_xbee (void)
{
while(1) // looping forever
{
if( data[0] == 100) // check if UART start byte is met
{
while(1)
{
if (RCREG =='8') // if character '8' is detected, the robot move forward
{
forward();
}
else if (RCREG == '2') // if character '2' is detected, the robot move backward
{
backward();
}
else if (RCREG == '6') // if character '6' is detected, the robot turn right
{
right();
}
else if (RCREG == '4') // if character '4' is detected, the robot turn left
{
left();
}
else if (RCREG == '5') // if character '5' is detected, then stop the robot
{
stop();
}
else // else then stop the robot
{
stop();
}
}
}
else
{
}
}
}
// Motor control function
// Description : subroutine to set the robot moving direction
void forward ()
{
}
void backward ()
{
}
void left()
{
}
void right()
{
}
void stop()
{
}
// uart function
unsigned char uart_rec(void) //function to wait for a byte receive from uart
{
unsigned char temp;
while(RCIF==0); //wait for data to received
temp=RCREG;
return temp; //return the received data
}