BasicMicroUK - Forums

www.basicmicro.co.uk
It is currently Fri Jul 20, 2018 1:48 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: Beginner HSERIN trouble
PostPosted: Mon Dec 03, 2012 7:25 pm 
Offline
Citizen

Joined: Mon Dec 03, 2012 6:58 pm
Posts: 6
Hi All,
I'm very new to using MBasic and I am abit stuck. I have a Rover robot with four servos, I have the Rover running in Auto mode on its own however I want to add an override that will allow manual control. I need the board to continue normal operation until the override command is given over the serial rs232 port. I cant allow a delay in normal operation so the standard SerIN command isnt really suited but the HSerIn is. Can someone explain to me why the following code doesnt read or print back to the COM port. I am using a BasicAtomPro BAP 28.

Code:
;   Declare Global Constrants
;-----------------------------------------------------------------------------
MaxStrSize   Con   20       ;String length

;   Declare Global Varriables
;-----------------------------------------------------------------------------

Sbuffer      var Byte(30)   ; Buffer varriable to store input serial data
InByte      var Byte      ; Used to read one byte at a time
j         var Byte      ; Counter

;   Run Once - Setup
;-----------------------------------------------------------------------------

j=0;
low p0
low p1

Clear               ; Clear serial buffer
EnableHSerial1         ; Enable UART1
SetHSerial1 H9600       ; Sets up UART1 for 9600bps


;   Main Loop
;-----------------------------------------------------------------------------

Main
   j=0;
   HSerStat   4,Auto_Mode      ; If no new Byte return to Auto Mode
   HSerStat   3,Check_Serial   ; If new Byte on Serial go to Check Serial sub
   Return_Point            ; Return point for sub routine
   Pause 20               ; Slight delay before next loop cycle
   
Goto Main

;   Auto_Mode subroutine
;-----------------------------------------------------------------------------

Auto_Mode
   ; Detail not included for this example
   HSerOut 1,[13,"Auto Mode running"];      ; Print out to show in sub
   Pause 100                        ; Delay before returning back
gosub Return_Point      


;   Read In RS232 Input Buffer
;-----------------------------------------------------------------------------
Check_Serial

   HSerIn 1,[InByte]      ; Save Single Byte
   Sbuffer(j)=InByte      ; Add byte to string
   j=j+1            
   
   If InByte = 13   Then   ; If CR detected
      HSerOut 1,[13,"Serial Command Recieved"]
      GoSub Check_string   ; Go to Check_String
      Check_SIn_Return   ; Function return point
   EndIf
   HSerStat   3,Check_Serial   ; Loop back if more characters

gosub Return_Point


;   Check RS232 String data
;-----------------------------------------------------------------------------
Check_SIn

      SerOut s_out,i9600,[13,"Complete Line Recieved: ",Str SBuffer,30]
      ; Does not reach this point

gosub Check_SIn_Return
;-----------------------------------------------------------------------------



I may even be doing this all wrong. Like I said I am pretty new to this so any help you can provide would be great Thanks


Top
 Profile  
 
 Post subject: Re: Beginner HSERIN trouble
PostPosted: Tue Dec 04, 2012 5:20 am 
Offline
Site Admin
User avatar

Joined: Thu Mar 01, 2001 7:00 pm
Posts: 1316
Location: Temecula, CA
whenever you aren't getting what you expect simplify your code. Write a simple program that just outputs serial data like this:

sethserial1 h9600 ;enablehserial isn't needed anymore

main
hserout ["testing",13]
pause 100
goto main

If this works then move on to adding in hserin commands. Also its easier to use an hserin command with a 0 timeout than to use the hserstat command to see if there is a byte waiting.

temp var byte

hserin nodata,0,[temp]
;do something with temp here
nodata

The program will jump to the nodata label if there is no data waiting to be handled. If there is a byte it will be put in temp and you can add commands to do something with it before the nodata label.

_________________
Tech Support
Basic Micro - Robotic Technology Evolved


Top
 Profile  
 
 Post subject: Re: Beginner HSERIN trouble
PostPosted: Tue Dec 04, 2012 6:43 pm 
Offline
Citizen

Joined: Mon Dec 03, 2012 6:58 pm
Posts: 6
I did try the following code
Code:
setHserial1 H9600, H8databits, Hnoparity, h1stopbits;
Pause 100

Main
      HSerOut 1,["Test",13]
      Pause 1000
Goto Main

Which should keep pumping out "Test" every second but I am still not getting anything on the Laptop COM line (with same settings as above). There is a basic understanding with the HSerial stuff I must be missing here. I do receive over the COM line when I use SerOut, so is it a correct pin issue?


Top
 Profile  
 
 Post subject: Re: Beginner HSERIN trouble
PostPosted: Tue Dec 04, 2012 7:37 pm 
Offline
Master

Joined: Tue Nov 21, 2006 5:34 pm
Posts: 662
How is your bap28 connected to a PC? On these chips the hardware serial port is on io pins 14 and 15 as ttl level signals. These are not same pins as rs232 signal used to program...

Kurt


Top
 Profile  
 
 Post subject: Re: Beginner HSERIN trouble
PostPosted: Tue Dec 04, 2012 8:13 pm 
Offline
Citizen

Joined: Mon Dec 03, 2012 6:58 pm
Posts: 6
I have the BAP28 on a Bot Board 2 with a 9 pin RS232 connector between the board and laptop.
I understand that I need to treat the functions differently in terms of pins but I'm not sure how to go about that in my case.
Does the HSerIn buffer the data from the RS232 (same as I use to program viva BotBoard2 I/O board) connected to the P14 (Rx) of the BAP28 ?


Top
 Profile  
 
 Post subject: Re: Beginner HSERIN trouble
PostPosted: Tue Dec 04, 2012 10:06 pm 
Offline
Site Admin
User avatar

Joined: Thu Mar 01, 2001 7:00 pm
Posts: 1316
Location: Temecula, CA
p14 and p15 are not connected to the rs-232 on the bot board. You have to connect a max232 or equivilent circuit to talk to a PC serial port. Its completely seperate from the db-9 on the bot board. Those pins uses software serial to communicte.

serout s_out,i9600,["data"] ;example sending serial data out the programming pins of the module.

_________________
Tech Support
Basic Micro - Robotic Technology Evolved


Top
 Profile  
 
 Post subject: Re: Beginner HSERIN trouble
PostPosted: Tue Dec 04, 2012 10:34 pm 
Offline
Citizen

Joined: Mon Dec 03, 2012 6:58 pm
Posts: 6
Ah ok so the Software serial runs using pins 1 (sout) and 2 (sin) of the BAP28 which is also used for programming and connected to the DB9 port on the BotBoard.
Where as the Hardware serial stores/writes the buffer using pins 14 (Rx) and 15 (Tx). Correct ?
This is bad news for me, I was hoping to interrupt the auto behavior (of my robot) with a command string over the PC serial port.


Top
 Profile  
 
 Post subject: Re: Beginner HSERIN trouble
PostPosted: Wed Dec 05, 2012 12:52 am 
Offline
Master

Joined: Tue Nov 21, 2006 5:34 pm
Posts: 662
A long time ago, I experimented with a way to send a command string from the PC to a BAP28, knowing that data received on an IO pin such as S_IN is only received if the program is in a SERIN command. One experiment I did was using an interrupt on IRQ0, which was a big hack. I don't appear to have any of this code sitting around anymore. But there are some posts up on the Lynxmotion forum about this, including:
http://www.lynxmotion.net/viewtopic.php ... hilit=IRQ0

And I am not sure this would be a good thing to try to understand if you are a beginner.

Kurt


Top
 Profile  
 
 Post subject: Re: Beginner HSERIN trouble
PostPosted: Tue Dec 11, 2012 5:17 pm 
Offline
Citizen

Joined: Mon Dec 03, 2012 6:58 pm
Posts: 6
Thanks Kurt
I had a look at that thread and your right it looks abit of an advance hack for my level. But what I did manage to get from it is you use the IRQ0 interrupt to detect incoming packets and you process them in the software. However the IRQ0 is on pin 14 of the BAP28 so that wouldn't suit my needs at the moment. I did something like this before in C were I used the interrupts to detect incoming bytes and detect when a buffer was full.

For my project I am essentially sending wireless data to an Arduino with a wifi shield, using the arduino to strip out a command from the data, send the command to a MAX232 board, then the command into the BotBoard II ( viva RS232 ) and BAP28 for processing. I am getting abit lost here, am I going about this the right way ? After what Kurt has said I think I should strip my DB9 connector and connect the Rx/Tx to P14/P15, then I should be able to use the hSerIn and hSerOut for the hardware serial commands right ?


Top
 Profile  
 
 Post subject: Re: Beginner HSERIN trouble
PostPosted: Tue Dec 11, 2012 8:36 pm 
Offline
Master

Joined: Tue Nov 21, 2006 5:34 pm
Posts: 662
Actually on Bap28 IRQ0 is on TX/RX IO pins.... If you are looking at the Schematic that is part of the Data Sheet the P14 you are seeing, is shown inside the H8 Chip. This is that actual Processor IO pin that the BAP28s SIN/SOUT io pins actually connect to.

Kurt


Top
 Profile  
 
 Post subject: Re: Beginner HSERIN trouble
PostPosted: Wed Dec 19, 2012 3:33 pm 
Offline
Citizen

Joined: Mon Dec 03, 2012 6:58 pm
Posts: 6
Sorry for the slow response, had to take a break from this project for a week for family stuff.

Yes Kurt your absolutely right I was looking at he H8 chip pins and not the BAP28 pins my mistake sorry. I have had a look round the forums and seen a number of threads from you on the IRQ0 interrupt for serial comms and it does seem the best way to handle my problem. So although it looks abit advanced for my level I still need to use it. I have played around with the code you linked but cant seem to get the interrupt to fire.

Code:
   hostflag = 0             ;  Set Flag Initally   
   PMR1.bit4 = 1           ;  enable pin to IRQ0 interrupt instead of normal I/O
   IEGR1.bit0 = 0          ;  Interrupt IRQ0 on falling edge
   enable irq0
   ienr1.bit0 = 1       
In debug mode it shows the code getting stuck with the Port Mode Register (PMR) assignment and goes no further

Where as if I try using the IRQ0 as a standard interrupt as so
Code:
hostflag var byte
TEMP var byte

;---------------------------------------
Serout s_out,i9600,[0,"Start"]
ENABLE IRQ0INT
ONINTERRUPT IRQ0INT, HANDLE_IRQ0
hostflag =0

;---------------------------------------
Main
   if HostFlag then
      Serout s_out,i9600,[13,"Flag reset"]
      Pause 200000
   else
      Serout s_out,i9600,[13,"CONTINUE"]
      Pause 1000
   endif

GoTO Main

;---------------------------------------
HANDLE_IRQ0:
   SerOut s_out,i9600,[13,"INTERRUPT ACTIVE"]
   Pause 1000
   hostflag=1
resume                  ; and return
The loop runs but the interrupt doesn't fire.

Any thoughts on what I am doing wrong ? And again guys thank you for your patience with this newbie !


Top
 Profile  
 
 Post subject: Re: Beginner HSERIN trouble
PostPosted: Wed Dec 19, 2012 4:00 pm 
Offline
Master

Joined: Tue Nov 21, 2006 5:34 pm
Posts: 662
No problem. We all have different things that come up...

Back when some of these threads were written, Interrupts were enabled by default, whereas later releases of the IDE, interrupts are disabled by default.

So right before Main loop add another line:
Code:
enable

This says enable interrupts and sets the appropriate hardware register...

Kurt


Top
 Profile  
 
 Post subject: Re: Beginner HSERIN trouble
PostPosted: Wed Dec 19, 2012 4:42 pm 
Offline
Master

Joined: Tue Nov 21, 2006 5:34 pm
Posts: 662
I saw PM, but these sort of hang up as the input box is limited to 1 message else it is held...

First thing I should mention, is I never try to debug anything with interrupts using the debug mode... It will often hang!

Also In your interrupt handler, I would first disable having the interrupt hit again, that is in my code example:
PMR1.bit4 = 0 ; restore pin n to normal IO
Also not good to sleep for a second and do serial output as part of interrupt handler. Usually I debug things like this by using one of the three LEDs on the board. That is when I get an interrupt I may turn on the LED or toggle it...

That way if anything else comes from PC it wont continue to interrupt you...

Obviously you will only process it once as you don't reset the conditions.

Kurt


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


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 post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

phpBB SEO