View previous topic :: View next topic |
Author |
Message |
pd5dj
Joined: 18 Dec 2008 Posts: 51 Location: Avenhorn
|
Posted: Sun Feb 16, 2014 11:17 am Post subject: |
|
|
Hi Kimmi
That is one great example! thanks!
Not only for this cat, but there are some other pieces of code in it, that simplifies my older projects!
Im going to play a bit with this code today.
Next step will be some more complicatied,, that is receiving the the CAT data..
I was thinking to use inputbin for it..
Since there are 5 bytes in total.. i was thinking of this:
Dim RX(5) as byte
inputbin rx(1),5
An interrupt based routine might not be needed because when i send a request command,, i immediatly go in to receive:
Like
PRINTBIN #1, F1;F2;F3;F3;
INPUTBIN #1, RX(1),5
Is this wise?
----- EDIT -----
Yes it was wise i gues, because it works perfectly
Only thing now is how to merge the array into 1 variable.. |
|
Back to top |
|
|
pd5dj
Joined: 18 Dec 2008 Posts: 51 Location: Avenhorn
|
Posted: Sun Feb 16, 2014 8:58 pm Post subject: |
|
|
Ok got the receiving part working..
But im stuck, and i can not find why my program is hanging all the time..
when entering the loop:
The following is happening:
1. read frequency from radio uart0 -> this works
2. display frequency uart0 -> this works
3. read frequency from radio uart1 -> this works
4. display frequency uart1 -> this works
5. wait for 1 second
6. Sending a fixed frequency of 435000000hz -> this works
1. read frequency from radio uart0 -> this works
2. display frequency uart0 -> this works
3. Now the program hangs
Anyone see what i am doing wrong?
reading from uart0 and writing that data to uart1 works perfectly and vica versa..
call read_freq(0)
call send_freq(freqread, 1)
works perfect.
Code: |
$regfile = "m128def.dat"
$crystal = 11059200
$hwstack = 64
$swstack = 64
$framesize = 64
Waitms 100
Config Com1 = 38400 , Synchrone = 0 , Parity = None , Stopbits = 2 , Databits = 8 , Clockpol = 0
Config Com2 = 38400 , Synchrone = 0 , Parity = None , Stopbits = 2 , Databits = 8 , Clockpol = 0
Open "com1:" For Binary As #1
Open "com2:" For Binary As #2
' BUTTONS
Ddrf = &B11111000 'set portc 0 & 1 & 2 input
Read_button Alias Pinf.2
Setfreq_up Alias Pinf.0 'UP +25 Khz
Setfreq_dw Alias Pinf.1 'DOWN -25 Khz
' if display is used
Config Lcd = 20 * 4
Config Lcdpin = Pin , Db4 = Portc.4 , Db5 = Portc.5 , Db6 = Portc.6 , Db7 = Portc.7 , E = Portc.3 , Rs = Portc.2
Cursor Off Noblink
Cls
Waitms 500
Dim Freq1 As Long 'in Hz
Dim Freqread As Long
Dim Freqread_temp As Long
Dim Rx1(5) As Byte
Dim New_freq As Bit 'update bit
Declare Sub Send_freq(byval Freq As Long , Byval Port As Byte) ' UPDATE freq
Declare Sub Disp_freq(byval Freq As Long , Byval Row As Byte) 'UPDATE LCD if used
Declare Sub Read_freq(byval Port As Byte)
Do
Call Read_freq(0) 'read uart0
Locate 1 , 1 : Lcd Freqread ; " " 'display uart0 raw
Call Read_freq(1) 'read uart1
Locate 2 , 1 : Lcd Freqread ; " " 'display uart1 raw
Wait 1
Call Send_freq(435000000 , 1) 'send fixed frequency to uart1
Loop
Close #1
End
'_freq is a long and the frequency value is in Hz
'split freq into 4 bcd pairs
'100/10MHZ 1MHZ/100KHz 10/1KHz 100/10Hz
Sub Send_freq(byval Freq As Long , Byval Port As Byte)
Local Write_command As Byte : Write_command = &H01
Local L As Long
Local F As Byte
Local F1 As Byte
Local F2 As Byte
Local F3 As Byte
Local F4 As Byte
L = Freq / 10000000
F = L
F1 = Makebcd(f)
Freq = Freq Mod 10000000
L = Freq / 100000
F = L
F2 = Makebcd(f)
Freq = Freq Mod 100000
L = Freq / 1000
F = L
F3 = Makebcd(f)
Freq = Freq Mod 1000
L = Freq / 10
F = L
F4 = Makebcd(f) '
'sent to FT-817
If Port = 0 Then
Printbin #1 , F1 ; F2 ; F3 ; F4 ; Write_command ' '100/10MHZ 1MHZ/100KHz 10/1KHz 100/10Hz Command
Elseif Port = 1 Then
Printbin #2 , F1 ; F2 ; F3 ; F4 ; Write_command ' '100/10MHZ 1MHZ/100KHz 10/1KHz 100/10Hz Command
End If
End Sub
' if display is used
Sub Disp_freq(byval Freq As Long , Byval Row As Byte)
Local Str_freq As String * 14
Str_freq = Str(freq)
Str_freq = Format(str_freq , "000.000000") + " Mhz"
If Row = 0 Then
Locate 1 , 1
Elseif Row = 1 Then
Locate 2 , 1
End If
Lcd Str_freq
End Sub
Sub Read_freq(byval Port As Byte)
Local Str_freq As String * 14
Local Readcommand As Byte
Readcommand = &H03 'Read VFO command
If Port = 0 Then
Printbin #1 , 0 ; 0 ; 0 ; 0 ; Readcommand ;
Inputbin #1 , Rx1(1) , 5
Elseif Port = 1 Then
Printbin #2 , 0 ; 0 ; 0 ; 0 ; Readcommand ;
Inputbin #2 , Rx1(1) , 5
End If
Str_freq = Bcd(rx1(1))
Str_freq = Str_freq + Bcd(rx1(2))
Str_freq = Str_freq + Bcd(rx1(3))
Str_freq = Str_freq + Bcd(rx1(4))
Freqread = Val(str_freq)
Freqread = Freqread * 10
End Sub
|
|
|
Back to top |
|
|
kimmi
Joined: 24 Feb 2006 Posts: 1922 Location: Denmark
|
Posted: Mon Feb 17, 2014 1:10 am Post subject: |
|
|
Hi Björn,
try increase stack/frame size around 128
other option is use $timeout for uart RX so you know if its uart or code problem _________________ / Kim |
|
Back to top |
|
|
pd5dj
Joined: 18 Dec 2008 Posts: 51 Location: Avenhorn
|
Posted: Mon Feb 17, 2014 8:18 am Post subject: |
|
|
Hi Thanks
I will try that tonight..
Any idea if my receiving routine is a good way to go?
I receive it in a 5 byte array..
Then converting/merging it to a string variable.
And then copy it to a long variable..
Are there any better alternatives?
I have a feeling it maybe goes wrong here. |
|
Back to top |
|
|
Paulvk
Joined: 28 Jul 2006 Posts: 1257 Location: SYDNEY
|
Posted: Tue Feb 18, 2014 12:47 am Post subject: |
|
|
Hello pd5dj
You should look at the overlay function in bascom it cand break up variables into different sets of bytes
Code: |
Dim G As Word
Dim Twog(2) As Byte At G Overlay
|
This divides G into two single bytes you can do the same with doubles , longs and strings
Regards Paul |
|
Back to top |
|
|
pd5dj
Joined: 18 Dec 2008 Posts: 51 Location: Avenhorn
|
Posted: Sat Mar 01, 2014 2:29 pm Post subject: |
|
|
Hi all
I have been playing and trying lots of things..
But i can not seem to find what goes wrong here..
I have written a little description in the code.
Code: |
'$sim
$regfile = "m128def.dat"
$crystal = 11059200
$hwstack = 128
$swstack = 128
$framesize = 128
Waitms 100
Config Com1 = 4800 , Synchrone = 0 , Parity = None , Stopbits = 2 , Databits = 8 , Clockpol = 0
Config Com2 = 4800 , Synchrone = 0 , Parity = None , Stopbits = 2 , Databits = 8 , Clockpol = 0
Open "com1:" For Binary As #1
Open "com2:" For Binary As #2
' if display is used
Config Lcd = 20 * 4
Config Lcdpin = Pin , Db4 = Portc.4 , Db5 = Portc.5 , Db6 = Portc.6 , Db7 = Portc.7 , E = Portc.3 , Rs = Portc.2
Cursor Off Noblink
Cls
Waitms 500
Config Portf = Input : Toets1 Alias Pinf.0
Dim Freq1 As Long 'in Hz
Dim Readcat1 As Long
Dim Readcat2 As Long
Dim Rx1(5) As Byte
Declare Sub Send_freq(byval Freq As Long , Byval Port As Byte) ' UPDATE freq
Declare Sub Disp_freq(byval Freq As Long , Byval Row As Byte) 'UPDATE LCD if used
Declare Sub Read_freq(byval Port As Byte)
Dim Downlink_freq As Long
Dim Downlink_freq_temp As Long
Dim Uplink_freq As Long
Dim Uplink_freq_temp As Long
Dim Firstboot As Byte
Dim Offset As Long
Dim Plusmin As Byte
Firstboot = 1
'========== START ==========
Do
Call Read_freq(0) 'Reads the downlink frequency
Downlink_freq = Readcat1
If Firstboot = 1 Then
Downlink_freq_temp = Downlink_freq
Firstboot = 0
End If
Locate 1 , 1 : Lcd Downlink_freq
While Toets1 = 1 : Wend
While Toets1 = 0 : Wend
Waitms 500
'========= Here i have a wrong readout after "SETUPLINK ROUTINE" has run for the ! 2nd ! time, the first cycle it works good.. the second it goes wrong, other wise it reads out fine loop after loop
'Normal readout for example: 432112233, but when it goes wrong, it returns a shorter value like: 43211223
'What can be wrong here?
Call Read_freq(1) 'Reads the uplink frequency
Uplink_freq = Readcat2
Locate 2 , 1 : Lcd Uplink_freq
While Toets1 = 1 : Wend
While Toets1 = 0 : Wend
Waitms 500
'"SETUPLINK ROUTINE"
'============= IF This routine has run for the second time,, the readout from above goes wrong..
If Downlink_freq <> Downlink_freq_temp Then 'Checking if the downlink frequency has changed, if so proceed
If Downlink_freq < Downlink_freq_temp Then 'if the downlink freq is lower then the Temp freq, then calculate offset - or +, and set a bit for it.
Offset = Downlink_freq_temp - Downlink_freq
Plusmin = 0
Elseif Downlink_freq > Downlink_freq_temp Then
Offset = Downlink_freq - Downlink_freq_temp
Plusmin = 1
Else
Offset = 0
End If
Locate 3 , 1 : Lcd Offset ; " "
Downlink_freq_temp = Downlink_freq_temp 'update the new temp frequency
While Toets1 = 1 : Wend
While Toets1 = 0 : Wend
Waitms 500
If Plusmin = 0 Then 'Check if the offset was lower or higher, and increase or decrease the new uplink frequency and put it in a new temp variable
Uplink_freq_temp = Uplink_freq - Offset
Elseif Plusmin = 1 Then
Uplink_freq_temp = Uplink_freq + Offset
End If
'clear the original uplink freq variable..
Uplink_freq = 0
While Toets1 = 1 : Wend
While Toets1 = 0 : Wend
Waitms 500
Locate 4 , 1 : Lcd Uplink_freq_temp
Call Send_freq(uplink_freq_temp , 1) 'Send the new uplink frequency to the uplink radio
Readcat1 = 0
Readcat2 = 0
Uplink_freq_temp = 0
While Toets1 = 1 : Wend
While Toets1 = 0 : Wend
Waitms 500
End If
Cls
Loop
Close #2
Close #1
End
' if display is used
Sub Disp_freq(byval Freq As Long , Byval Row As Byte)
Local Str_dispfreq As String * 14
Str_dispfreq = Str(freq)
Str_dispfreq = Format(str_dispfreq , "000.000000") + " Mhz"
If Row = 0 Then
Locate 1 , 1
Elseif Row = 1 Then
Locate 2 , 1
End If
Lcd Str_dispfreq
End Sub
Sub Read_freq(byval Port As Byte)
Local Str_readfreq As String * 14
Local Readcommand As Byte
Local Freqread As Long
Readcommand = &H03 'Read VFO command
If Port = 0 Then
Printbin #1 , 0 ; 0 ; 0 ; 0 ; Readcommand ;
$timeout = 1000000
Inputbin #1 , Rx1(1) , 5
Elseif Port = 1 Then
Printbin #2 , 0 ; 0 ; 0 ; 0 ; Readcommand ;
$timeout = 1000000
Inputbin #2 , Rx1(1) , 5
End If
Str_readfreq = Bcd(rx1(1))
Str_readfreq = Str_readfreq + Bcd(rx1(2))
Str_readfreq = Str_readfreq + Bcd(rx1(3))
Str_readfreq = Str_readfreq + Bcd(rx1(4))
Freqread = Val(str_readfreq)
Freqread = Freqread * 10
If Port = 0 Then
Readcat1 = Freqread
Elseif Port = 1 Then
Readcat2 = Freqread
End If
Freqread = 0
Str_readfreq = ""
End Sub
'_freq is a long and the frequency value is in Hz
'split freq into 4 bcd pairs
'100/10MHZ 1MHZ/100KHz 10/1KHz 100/10Hz
Sub Send_freq(byval Freq As Long , Byval Port As Byte)
Local Write_command As Byte : Write_command = &H01
Local L As Long
Local F As Byte
Local F1 As Byte
Local F2 As Byte
Local F3 As Byte
Local F4 As Byte
L = Freq / 10000000
F = L
F1 = Makebcd(f)
Freq = Freq Mod 10000000
L = Freq / 100000
F = L
F2 = Makebcd(f)
Freq = Freq Mod 100000
L = Freq / 1000
F = L
F3 = Makebcd(f)
Freq = Freq Mod 1000
L = Freq / 10
F = L
F4 = Makebcd(f) '
'sent to FT-817
If Port = 0 Then
Printbin #1 , F1 ; F2 ; F3 ; F4 ; Write_command ' '100/10MHZ 1MHZ/100KHz 10/1KHz 100/10Hz Command
Elseif Port = 1 Then
Printbin #2 , F1 ; F2 ; F3 ; F4 ; Write_command ' '100/10MHZ 1MHZ/100KHz 10/1KHz 100/10Hz Command
End If
End Sub
|
Last edited by pd5dj on Sat Mar 01, 2014 3:08 pm; edited 1 time in total |
|
Back to top |
|
|
kimmi
Joined: 24 Feb 2006 Posts: 1922 Location: Denmark
|
Posted: Sat Mar 01, 2014 2:43 pm Post subject: |
|
|
Hi Björn
did you try a bigger $timeout value ? like $timeout = 5000000
also maby the timing between sent it to small
see this link
Code: | Sub Read_freq(byval Port As Byte)
Local Str_readfreq As String * 14
Local Readcommand As Byte
Local Freqread As Long
Readcommand = &H03 'Read VFO command
If Port = 0 Then
Printbin #1 , 0 ; 0 ; 0 ; 0 ; Readcommand ;
$timeout = 5000000
Inputbin #1 , Rx1(1) , 5
Elseif Port = 1 Then
Printbin #2 , 0 ; 0 ; 0 ; 0 ; Readcommand ;
$timeout = 5000000
Inputbin #2 , Rx1(1) , 5
End If |
_________________ / Kim |
|
Back to top |
|
|
pd5dj
Joined: 18 Dec 2008 Posts: 51 Location: Avenhorn
|
Posted: Sat Mar 01, 2014 3:06 pm Post subject: |
|
|
Hi Kimmi
Thanks,
Yes i have tried that.. no luck
The strange thing is, i can make a loop without any delays..
Like
Do
read_freq(0)
send_freq(freq,1)
Loop
And this works super.. so i am totally confused...
I have forgot to mention that 1 cycle of the routine works.. the second time i goes wrong...
I have re-commented the code above, check it.
sooo strange |
|
Back to top |
|
|
kimmi
Joined: 24 Feb 2006 Posts: 1922 Location: Denmark
|
Posted: Sat Mar 01, 2014 3:59 pm Post subject: |
|
|
hmm
for test try
$hwstack = 254
$swstack = 254
$framesize = 254
see if this works you use alot of long vars _________________ / Kim |
|
Back to top |
|
|
pd5dj
Joined: 18 Dec 2008 Posts: 51 Location: Avenhorn
|
Posted: Sat Mar 01, 2014 4:03 pm Post subject: |
|
|
kimmi wrote: | hmm
for test try
$hwstack = 254
$swstack = 254
$framesize = 254
see if this works you use alot of long vars |
Hmm, no luck
Maybe another approach how i read the uart?, Any suggestions how i can do that?
or is there any command to reset and clear the RX and TX buffer of the uart? |
|
Back to top |
|
|
kimmi
Joined: 24 Feb 2006 Posts: 1922 Location: Denmark
|
Posted: Sat Mar 01, 2014 4:36 pm Post subject: |
|
|
just to be sure Fuse M103C OFF ???
'By default the M128 has the M103 compatibility fuse set _________________ / Kim |
|
Back to top |
|
|
pd5dj
Joined: 18 Dec 2008 Posts: 51 Location: Avenhorn
|
Posted: Sat Mar 01, 2014 4:38 pm Post subject: |
|
|
kimmi wrote: | just to be sure Fuse M103C OFF ???
'By default the M128 has the M103 compatibility fuse set |
Yes, its off |
|
Back to top |
|
|
kimmi
Joined: 24 Feb 2006 Posts: 1922 Location: Denmark
|
Posted: Sat Mar 01, 2014 4:51 pm Post subject: |
|
|
I have no idea why ,
but you can try buffer input
Code: | Config Com1 = 4800 , Synchrone = 0 , Parity = None , Stopbits = 2 , Databits = 8 , Clockpol = 0
Config Com2 = 4800 , Synchrone = 0 , Parity = None , Stopbits = 2 , Databits = 8 , Clockpol = 0
Enable Interrupts
Config Serialin = Buffered , Size = 20
Config Serialin1 = Buffered , Size = 20
Open "com1:" For Binary As #1
Open "com2:" For Binary As #2 |
_________________ / Kim |
|
Back to top |
|
|
pd5dj
Joined: 18 Dec 2008 Posts: 51 Location: Avenhorn
|
Posted: Sat Mar 01, 2014 8:05 pm Post subject: |
|
|
kimmi wrote: | I have no idea why ,
but you can try buffer input
Code: | Config Com1 = 4800 , Synchrone = 0 , Parity = None , Stopbits = 2 , Databits = 8 , Clockpol = 0
Config Com2 = 4800 , Synchrone = 0 , Parity = None , Stopbits = 2 , Databits = 8 , Clockpol = 0
Enable Interrupts
Config Serialin = Buffered , Size = 20
Config Serialin1 = Buffered , Size = 20
Open "com1:" For Binary As #1
Open "com2:" For Binary As #2 |
|
I'm in tears no luck either...
Other approach:
Rewriting the Read Uart routine.
Im now using this code to receive from the uart, and convert it back to a long in HZ
Any idea if this could be done better?
Code: |
Sub Read_freq(byval Port As Byte)
Local Str_readfreq As String * 14
Local Readcommand As Byte
Local Freqread As Long
Readcommand = &H03 'Read VFO command
If Port = 0 Then
Printbin #1 , 0 ; 0 ; 0 ; 0 ; Readcommand ;
$timeout = 1000000
Inputbin #1 , Rx1(1) , 5
Elseif Port = 1 Then
Printbin #2 , 0 ; 0 ; 0 ; 0 ; Readcommand ;
$timeout = 1000000
Inputbin #2 , Rx1(1) , 5
End If
Str_readfreq = Bcd(rx1(1))
Str_readfreq = Str_readfreq + Bcd(rx1(2))
Str_readfreq = Str_readfreq + Bcd(rx1(3))
Str_readfreq = Str_readfreq + Bcd(rx1(4))
Freqread = Val(str_readfreq)
Freqread = Freqread * 10
If Port = 0 Then
Readcat1 = Freqread
Elseif Port = 1 Then
Readcat2 = Freqread
End If
Freqread = 0
Str_readfreq = ""
End Sub
|
|
|
Back to top |
|
|
pd5dj
Joined: 18 Dec 2008 Posts: 51 Location: Avenhorn
|
Posted: Mon Mar 03, 2014 11:58 am Post subject: |
|
|
Ok guys
Im at a point to try a complete different approach about the Receive part..
I want to use the serial interrupt example, that receives byte per byte in..
That is no problem, i used that before..
but now i am using 2x Hardware uart and i use 2 stopbits.. how can i use the following example thats listenens to both uarts with 2 stopbits?
I have started a new topic for this too, maybe usefull for other people too: http://www.mcselec.com/index2.php?option=com_forum&Itemid=59&page=viewtopic&p=63572#63572
Code: |
'--------------------------------------------------------------------
' SERINT.BAS
' (c) 1999-2005 MCS Electronics
' serial interrupt example for AVR
' also look at CONFIG SERIALIN for buffered input routines
'--------------------------------------------------------------------
$regfile = "m88def.dat"
$baud = 19200
$crystal = 8000000
Const Cmaxchar = 20 'number of characters
Dim B As Bit 'a flag for signalling a received character
Dim Bc As Byte 'byte counter
Dim Buf As String * Cmaxchar 'serial buffer
Dim D As Byte
'Buf = Space(20)
'unremark line above for the MID() function in the ISR
'we need to fill the buffer with spaces otherwise it will contain garbage
Bc = 0
Print "Start"
On Urxc Rec_isr 'define serial receive ISR
Enable Urxc 'enable receive isr
Enable Interrupts 'enable interrupts to occur
Do
If B = 1 Then 'we received something
Disable Serial
Print "{" ; Buf ; "}" 'print buffer
Print "BC:" ; Bc 'print character counter
'now check for buffer full
If Bc = Cmaxchar Then 'buffer full
Buf = "" 'clear
Bc = 0 'rest character counter
End If
Reset B 'reset receive flag
Enable Serial
End If
Loop
Rec_isr:
D = Udr 'read UDR only once
Print "*" ' show that we got here
If Bc < Cmaxchar Then 'does it fit into the buffer?
Incr Bc 'increase buffer counter
If D = 13 Then 'return?
Buf = Buf + Chr(0)
Bc = Cmaxchar 'at the end
Else
Buf = Buf + Chr(d) 'add to buffer
End If
' Mid(buf , Bc , 1) = Udr
'unremark line above and remark the line with Chr() to place
'the character into a certain position
'B = 1 'set flag
End If
B = 1 'set flag
Return
|
|
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|