PDA

View Full Version : Wireless serial communication question



Ahmadabuomar
- 20th December 2009, 17:59
HI EVERY ONE

i m trying to control a robotic arm with wireless serial communication between two PICs. the sender pic takes and input from a joustick potintiometers and send instructios to the receiving pic. there should be 7 bytes to send using serin command. a preamble byte $55 and 6 control bytes.

i established the serial communication between the two pic successfully. and i tested the received data with lcd. everything works great.

the problem is, when i wrote a PWM routine in the receiving pic, i used TMR0 interrupts. now when i try to reveive data and do PWM. the pic doesnt work.

i tried to figure out whats wrong but i failed

can u help me?

here are the codes. i didnt add potentiometers reading to sender pic. i just tried to test it with numbers.


INCLUDE "MODEDEFS.BAS"

S1 VAR BYTE
S3 VAR BYTE
S4 VAR BYTE
S6 VAR BYTE
S7 VAR BYTE
DC VAR BYTE
DC1 VAR BYTE
DC2 VAR BYTE

CNT VAR BYTE
S1 = 255
S3 = 3
S4 = 44
S6 = 10
S7 = 20
DC = 77


TRISB = 0
PAUSE 500

MAIN:
PORTB.0 = 1
PAUSE 1000
PORTB.0 = 0
PAUSE 1000
PORTB.0 = 1
PAUSE 1000
PORTB.0 = 0
PAUSE 1000




DATASEND:

SEROUT 7,T2400,[$55,S1,S3,S4,S6,S7,DC]
PAUSEUS 2

GOTO DATASEND



the receiver pic code is here


'************************************************* ***************
'* Name : W_ROB_REC.PBP *
'* Author : [AHMAD ABU OMAR] *
'* Notice : Copyright (c) 2009 [AHMAD ABU OMAR] *
'* : All Rights Reserved *
'* Date : 16/12/2009 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
'************************************************* ***************
'* DIFINTIONS
'************************************************* ***************
define osc 48
INCLUDE "MODEDEFS.BAS"
'************************************************* ***************
'* VARIABLES
'************************************************* ***************
S1 VAR BYTE ' first servo motor position
S2 VAR BYTE
S3 VAR BYTE
S4 VAR BYTE
S5 VAR BYTE
S6 VAR BYTE
S7 VAR BYTE
DC VAR BYTE
DC1 VAR BYTE ' right DC motor of mobile base
DC2 VAR BYTE ' left DC motor of mobile base

OLD VAR BYTE ' a counter variable


dc1 = 0
DC2 = 0
S1 = 1
S2 = 255
S3 = 1
S4 = 1
S5 = 255
S6 = 1
S7 = 1


'************************************************* ***************
'* MAIN PROGRAM START
'************************************************* ***************
INTCON2 = $F5 ' DISBALE PORTB PULL-UPS, SET TRM0 INTERRUPT TO HIGH PERIORITY
INTCON3 = $C0 ' DISABLE EXTERNAL INTERRUPTS PERIORITY
RCON.7 = 0 ' DISABLE PERIORITY LEVELS ON INTERRUPTS
PIE1 = 0 ' DISABLE ALL PREPHIRAL INTERRUPTS
PIE2 = 0
PIR1 = 0
PIR2 = 0
ADCON1 = 15 ' ALL DIGITAL I/O
TRISA = 0
TRISB = %10000000 ' RB7 is input, used to reveive serial data
TRISC = 0
LATA = 0
LATB = 0
LATC = 0

INTCON = %00100000 ' ENABLE TIMER0 INTERRUPTS
T0CON = %00000101 ' SET PRESCALAR 64 FOR TMR0, APROX 5us PER INCREMENT
' AND TMR0 WORKING IN 16 BIT MODE
TMR0L = $60 ' SET A BALANCE FOR 20ms PERIOD BEFORE INTERRUPT
TMR0H = $F0
ON INTERRUPT GOTO PW
INTCON = %10100000
T0CON.7 = 1
MAIN:
WHILE 1
PAUSEUS 1

GOSUB RECEIVE
WEND






'************************************************* ***************
'* PW: INTERRUPT SERVICE ROUTINE
'************************************************* ***************
DISABLE
PW:
PORTB =%01111111
OLD = 0
TMR0L = 0
PAUSEUS 800
s2 = 255 - s1
s5 = 255 - s4
FOR OLD = 0 TO 255
IF S1<= 0 THEN PORTB.0 = 0:S1 = 1
IF S2<= 0 THEN PORTB.1 = 0:S2 = 1
IF S3<= 0 THEN PORTB.2 = 0:S3 = 1
IF S4<= 0 THEN PORTB.3 = 0:S4 = 1
IF S5<= 0 THEN PORTB.4 = 0:S5 = 1
IF S6<= 0 THEN PORTB.5 = 0:S6 = 1
IF S7<= 0 THEN PORTB.6 = 0:S7 = 1
S1 = S1 - 1
S2 = S2 - 1
S3 = S3 - 1
S4 = S4 - 1
S5 = S5 - 1
S6 = S6 - 1
S7 = S7 - 1

WHILE TMR0L = OLD
WEND

NEXT OLD
TMR0L = $EF ' SET A BALANCE FOR 18ms PERIOD BEFORE INTERRUPT
TMR0H = $F1
INTCON = %10100000
T0CON.7 = 1 ' TIMER0 ON
RESUME
ENABLE

'************************************************* ***************
'* SUBROUTINE RECEIVE
'************************************************* ***************
RECEIVE:


serIN PORTB.7,T2400,[$55],S1,S3,S4,S6,S7,DC
PAUSEUS 1
RETURN



i m using PIC 16f628a on sending end and 18f2550 on the reciving end

crystals used on both sides are 4 MHz

rsocor01
- 20th December 2009, 23:17
When I have used SEROUT or SERIN with interrupts I always use them when the interrupts are disable. Serial communications slow down your program a lot. It takes sometime time for SEROUT and SERIN to execute, so for me is better to disable the interrupts first.

Robert

Ahmadabuomar
- 21st December 2009, 01:15
THX FOR YOUR REPLY

robert, i m sending 7 bytes with a baud rate of 2400 . this means 240 byte per second will be recived, that is 0.24 byte per millisecond.

since i m doing a PWM signal, the off time is 18 millisecond ( this is what i can use since it is out of ISR ).

if i disabled the interrupt, and let serin take its time, it may take 7*(1/0.24) = 29 ms. this time i cant afford, since the PWM signal must be 50 Hz ( 20ms) and there is only 18ms free in this period.

got what i mean?

i ll try to recive only two bytes with ur suggestion, should i disable interrupts by writing 'disable' before serin command? and write 'enable' after it?

i ll try what you suggeted

rsocor01
- 21st December 2009, 03:49
Yes, that is how I do it. I put the SERIN and SEROUT between the disable and enable block. Maybe somebody else here can give you more suggestions.

Robert