Noob Serial question
Moderators: David Barker, Jerry Messina
Noob Serial question
I have a project I'm working on where I need to poll and then read the data from a attitude sensor and then use the data to control two servos. I have been working on this for awhile but have hit a brick wall with getting the serial data into the chip and storing it so that I can then use it to drive the servos. This is the serial bit I have done.
Public Sub Serial()
USART.SetBaudrate (br19200)
poll = $0E
USART.Write (poll)
USART.ReadByte()
Save(0,Header, R_MSB, R_LSB, P_MSB, P_LSB, Y_MSB, Y_LSB, TT_MSB, TT_LSB, CS_MSB, CS_LSB)
Restore
End Sub
To get the data I need you have to send 0X0E to the sensor and then it sends back the 11 bytes of data. The sensor communicates in RS232 and that canbe done at one of three speeds, 19200, 38400 or 115000. It is currently set to 19200. The chip I am using is the 18F2680 although I have same code for 18F4620.
Any help would be greatly appreciated.
Public Sub Serial()
USART.SetBaudrate (br19200)
poll = $0E
USART.Write (poll)
USART.ReadByte()
Save(0,Header, R_MSB, R_LSB, P_MSB, P_LSB, Y_MSB, Y_LSB, TT_MSB, TT_LSB, CS_MSB, CS_LSB)
Restore
End Sub
To get the data I need you have to send 0X0E to the sensor and then it sends back the 11 bytes of data. The sensor communicates in RS232 and that canbe done at one of three speeds, 19200, 38400 or 115000. It is currently set to 19200. The chip I am using is the 18F2680 although I have same code for 18F4620.
Any help would be greatly appreciated.
Your device is sending back 11 bytes but you are only doing a single read. Try something like this - data will be stored in the byte array:
Code: Select all
Dim x as byte,
attitude_data(11) As byte
Public Sub Serial()
USART.SetBaudrate (br19200)
poll = $0E
USART.Write (poll)
for x = 0 to 10
attitude_data(x) = USART.ReadByte()
next
End Sub
Hi rmteo,
thanx for the quick response, much appreciated. I tried the change but still not working for me. here's the full code I have written. could you look at it and see where else I have probably screwed up.
Cheers stuart
thanx for the quick response, much appreciated. I tried the change but still not working for me. here's the full code I have written. could you look at it and see where else I have probably screwed up.
Code: Select all
Device = 18F2680
Clock = 8
Include "USART.Bas"
Include "convert.bas"
Dim Attitude_data(11) As Byte
Dim Header As Byte
Dim R_MSB As Byte
Dim R_LSB As Byte
Dim P_MSB As Byte
Dim P_LSB As Byte
Dim Y_MSB As Byte
Dim Y_LSB As Byte
Dim TT_MSB As Byte
Dim TT_LSB As Byte
Dim CS_MSB As Byte
Dim CS_LSB As Byte
Dim RawPitch As Float
Dim PitchDeg As Integer
Dim RawRoll As Float
Dim Rolldeg As Integer
Dim i As Byte
Dim poll As Byte
Dim x As Byte
Public Sub Serial()
USART.SetBaudrate (br19200)
poll = $0E
USART.Write (poll)
For x = 0 To 10
Attitude_data(x) = USART.ReadByte()
Next
Save(0,Header, R_MSB, R_LSB, P_MSB, P_LSB, Y_MSB, Y_LSB, TT_MSB, TT_LSB, CS_MSB, CS_LSB)
Restore
End Sub
//********************************************
// centers servo
//********************************************
Public Sub Center()
For i = 0 To 20
High(PORTB.0)
DelayUS(1500)
Low(PORTB.0)
High(PORTB.1)
DelayUS(1500)
Low(PORTB.1)
DelayMS(20)
Next
End Sub
//********************************************
// converts data to degress for Pitch
//********************************************
Public Sub Pitch()
RawPitch = P_MSB*256.0+P_LSB ' Convert to position number
PitchDeg = RawPitch * 360.0/65536.0 ' Convert to degrees
End Sub
//********************************************
// converts data to degrees for Roll
//********************************************
Public Sub Roll()
RawRoll = R_MSB *256.0+R_LSB ' Convert to position number
Rolldeg = RawRoll * 360.0/65536.0 ' Convert to degrees ' store in register
End Sub
//********************************************
// Pitch up routine
//********************************************
Sub pitchup()
For i = 0 To 20
High(PORTB.0)
DelayUS(2400)
Low(PORTB.0)
DelayMS(20)
Next
End Sub
//********************************************
// Pitch down routine
//********************************************
Sub pitchdn()
For i = 0 To 20
High(PORTB.0)
DelayUS(650)
Low(PORTB.0)
DelayMS(20)
Next
End Sub
//********************************************
// Roll Left routine
//********************************************
Sub Rolllft()
For i = 0 To 20
High(PORTB.1)
DelayUS(2400)
Low(PORTB.0)
DelayMS(20)
Next
End Sub
//********************************************
// roll right routine
//********************************************
Sub Rollrght()
For i = 0 To 20
High(PORTB.1)
DelayUS(650)
Low(PORTB.1)
DelayMS(20)
Next
End Sub
//****************************
// Initialise
//****************************
P_MSB = 0
P_LSB = 0
R_MSB = 0
R_LSB = 0
TRISB = 0
PORTB = 0
LATB = 0
TRISC.6 = 0
TRISC.7 = 1
While true
Center
Serial
Pitch
Roll
If PitchDeg >180 Then pitchup ' Angles bigger than 180 are negative
Else pitchdn
EndIf
If Rolldeg >180 Then Rolllft ' Angles bigger than 180 are negative
Else Rollrght
EndIf
Wend
You are using these lines incorrectly - you don't need them:
The array contains the data:
Code: Select all
Save(0,Header, R_MSB, R_LSB, P_MSB, P_LSB, Y_MSB, Y_LSB, TT_MSB, TT_LSB, CS_MSB, CS_LSB)
Restore
Code: Select all
attitude_data(0) ' 1st. data byte
attitude_data(1) ' 2nd. data byte
'
'
'
attitude_data(10) ' 11th. data byte
Thanx again rmteo. I have removed the following lines.
And changed the byte references in the maths sections to attitude_data(1), etc.
But still no go. It centers the servo on power up as it is told to but does little else. Beginning to get frustrating, and that means the more frustrated I get the less I can see my faults in the code. Swordfish is still very new to but I can see that getting on top of it and learning it properly will have great benefits.
Cheers Stuart
Code: Select all
Dim Header As Byte
Dim R_MSB As Byte
Dim R_LSB As Byte
Dim P_MSB As Byte
Dim P_LSB As Byte
Dim Y_MSB As Byte
Dim Y_LSB As Byte
Dim TT_MSB As Byte
Dim TT_LSB As Byte
Dim CS_MSB As Byte
Dim CS_LSB As Byte
Save(0,Header, R_MSB, R_LSB, P_MSB, P_LSB, Y_MSB, Y_LSB, TT_MSB, TT_LSB, CS_MSB, CS_LSB)
Restore
But still no go. It centers the servo on power up as it is told to but does little else. Beginning to get frustrating, and that means the more frustrated I get the less I can see my faults in the code. Swordfish is still very new to but I can see that getting on top of it and learning it properly will have great benefits.
Cheers Stuart
Okay so now I think that my hold up is with my math calcs. The data from the sensor comes out as hex. I need to convert to decimal for the calcs to work. This is the modifed code I have tried but with no luck.
I have declared P_MSB1 & P_LSB1 as bytes and included convert.bas.
This only gives me Incompatable parameter types and too many parameter errors.
Sorry but I need more guidance or hand holding.
Code: Select all
Public Sub Pitch()
Convert.HexToDec(P_MSB, P_MSB1)
Convert.HexToDec(P_LSB, P_LSB1)
RawPitch = P_MSB1 * 256.0 + P_LSB1 ' Convert to position number
PitchDeg = RawPitch * 360.0/65536.0 ' Convert to degrees
End Sub
This only gives me Incompatable parameter types and too many parameter errors.
Sorry but I need more guidance or hand holding.
Stuart, have you been able to program and get a PIC to run a LED blinking test? If not, do it first. Then you can continue with one to operate an R/C servo and get that working.
Your original question was about how to get the serial part working - and I showed you how to do that. I may be wrong but it looks to me like you are trying to do too much at one time. There are just too many things going on to determine why it is not doing what you want it to do.
You need to isolate which parts of your program are not working - then ask a specific question related to that issue.
Your original question was about how to get the serial part working - and I showed you how to do that. I may be wrong but it looks to me like you are trying to do too much at one time. There are just too many things going on to determine why it is not doing what you want it to do.
You need to isolate which parts of your program are not working - then ask a specific question related to that issue.
I agree with the less code at once comment!, do it in little sections with known dummy variables to make sure your calculations work etc.
The usage for conversion is incorrect, this is a way to do it:-
The usage for conversion is incorrect, this is a way to do it:-
Code: Select all
Public Sub Pitch()
Dim P_MSB1 as Byte
Dim P_LSB1 as Byte
P_MSB1=HexToDec(P_MSB)
P_LSB1=HexToDec(P_LSB)
RawPitch = P_MSB1 * 256.0 + P_LSB1 ' Convert to position number
PitchDeg = RawPitch * 360.0/65536.0 ' Convert to degrees
End Sub
Thanx Guys,
I have been testing each section in small bits where I can. I tested the servos by setting them up to do left, right and sweep movements so that I could see how good the movement and code was. I have also tested the serial hardware by using some echo code and hyperterminal.
It is just the bits in between and linking it together where i'm running into some problems. It also doesn,t help when I put myself under a bit of a time crunch to complete this.
Once again thanx for the advice and help and I'll keep working on it. Will let you know how it goes.
cheers Stuart
I have been testing each section in small bits where I can. I tested the servos by setting them up to do left, right and sweep movements so that I could see how good the movement and code was. I have also tested the serial hardware by using some echo code and hyperterminal.
It is just the bits in between and linking it together where i'm running into some problems. It also doesn,t help when I put myself under a bit of a time crunch to complete this.
Once again thanx for the advice and help and I'll keep working on it. Will let you know how it goes.
cheers Stuart
Hi Guys,
still working on this. Have been testing each part of code as suggested. I found a good little terminal program called Docklight which has helped a lot.
I have sent tested the attitude sensor with the term programe by sending the hex command and receive the correct data back. I can send a copy of this hex data to my board and it responds.
But I am not getting the correct hex number sent out from my board. I have tried writing the hex number in several different ways but still no luck.
Still I will keep plugging away and hopefully it will eventually click in place.
Regards Stuart
still working on this. Have been testing each part of code as suggested. I found a good little terminal program called Docklight which has helped a lot.
I have sent tested the attitude sensor with the term programe by sending the hex command and receive the correct data back. I can send a copy of this hex data to my board and it responds.
But I am not getting the correct hex number sent out from my board. I have tried writing the hex number in several different ways but still no luck.
Still I will keep plugging away and hopefully it will eventually click in place.
Regards Stuart
Basically it is jut a modification of my code above.
Thanx Stuart
Code: Select all
Public Sub Serial()
poll = $0E
RCSTA.4=0
RCSTA.4=1
USART.SetBaudrate (br19200)
USART.Write (poll)
For x = 0 To 10
Attitude_data(x) = USART.ReadByte()
Next
End Sub