Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

serial port communication and analog input
Goto page 1, 2  Next
 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-ARDUINO
View previous topic :: View next topic  
Author Message
Walt

Bascom Member



Joined: 13 Aug 2009
Posts: 14

blank.gif
PostPosted: Thu May 04, 2017 4:08 pm    Post subject: serial port communication and analog input Reply with quote

Hello
I’m using a Ardunio Mega 328p.
The serial port is used with 19200 Bd for communication to a PC. A additional analog input with a measured voltage will be transmitted to the PC with a” print” command.
The program is not handling the inputs from the serial port
I tried different interrupt command but with no result.
This is the cod snips of a part of my program. Any help will be appreciated
thanks, Walter

Code:

Dim Va As Single                                        'Voltage
Dim Va1 As Single                                     'Voltage1
Dim W1 As Word                                        'ADC Input and Channel specification
Const H1 = 0.0049                                     'Reference
Start Adc                                                     'Analogwandler starten

Enable Interrupts  
Enable Int0                                                 'Interrupt 0 freigeben
On Int0 Label2 Nosave                              'Verweis auf Interrup Rutine "Label2"

 Do                                                    'read RS-232 Input from PC
    If  Ischarwaiting() = 1 Then
        A = Inkey()
        Select Case  Inkey(chr)

                Case  47 : Gosub Dispmessage            'char 47 = " / "
                Case Else : mld = mld + Chr(a)

        End Select
    End If
 Loop
End

'Interrupt rutine for Analog Input
Label2:
   W1 = Getadc(0)                                        'Read first AD Channel
    Va = H1 * W1                                           'Voltage steps * ADC Input
    If Va > VA1  Or Va < Va1 Then
        Print "Uvor " ; Fusing( Va , "#.###"):   ‘transmit via serial port to the PC
        Va1 = Va
    End If
Return
 
Back to top
View user's profile
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 1161
Location: France

france.gif
PostPosted: Thu May 04, 2017 6:20 pm    Post subject: Reply with quote

hi,
try a simple program to test your serial port with the Bascom emulator,
As Uno has usb connector it is easy to use it



you look for the serial port of your uno with the parameters of your computer.
then you can try something like

print 123
print val
print "hellow "

then w"ll test more..

jp
Wink
Back to top
View user's profile Visit poster's website
Walt

Bascom Member



Joined: 13 Aug 2009
Posts: 14

blank.gif
PostPosted: Thu May 04, 2017 8:10 pm    Post subject: Reply with quote

the serial port is working fin until i add the analog input with the interrupt.
Only the analog Input will transmitted to the PC via the print command, the
Serial comunication will be stoped.
Thanks any way,
Walter
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Thu May 04, 2017 9:39 pm    Post subject: Reply with quote

Next time try post code that can be compiled. Small but with every needed stuffs.
Then we can advise you what you forget or what is propably mistake.

From code you attach today I can tell that "Nosave" should be removed from "On Int0 Label2 Nosave"

If you dont understand what Nosave mean - please dont use it Very Happy
You can read about it ofcourse and then use it Very Happy

It is also not clear if you have some pullup on the Int0 for stabilise this pin.

This code is simply to short for diagnose.
Back to top
View user's profile Visit poster's website
Walt

Bascom Member



Joined: 13 Aug 2009
Posts: 14

blank.gif
PostPosted: Fri May 05, 2017 10:38 am    Post subject: serial inteerface with analog input Reply with quote

The complet code of the program

I hope this will be helpfull
Walter
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Fri May 05, 2017 11:46 am    Post subject: Reply with quote

Small tip.
Instead two conditions like this
Code:
If Va > Va1 Or Va < Va1 Then

you can simply
Code:
If Va <> Va1 Then


Im trying to understand so you really want to measure this ADC every 16ms and watch this value in terminal if it differ from previous?
This may be a lot to read Very Happy

You need to do this in interrupt or maybe doing this in the main loop after Timer set its overflow flag will be sufficient?
Then you dont lose any Uart character and you dont get mess after jump to this "menu"
Back to top
View user's profile Visit poster's website
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 1161
Location: France

france.gif
PostPosted: Fri May 05, 2017 5:40 pm    Post subject: Reply with quote

hi,
EDC is right Wink
I think you do too much job in your interrupt,
in an interrupt, I put a flag and in the main loop, if the flag is true I do the job
look at the help
Finally some tips :

* when you use a timer interrupt that occurs each 10 uS for example, be sure that the interrupt code can execute in 10 uS. Otherwise you would loose time.

* it is best to set just a simple flag in the interrupt routine and to determine it's status in the main program. This allows you to use the NOSAVE option that saves stack space and program space. You only have to Save and Restore R24 and SREG in that case.

I add :
put the interrupt after the "end "of the program

JP Wink
Back to top
View user's profile Visit poster's website
Walt

Bascom Member



Joined: 13 Aug 2009
Posts: 14

blank.gif
PostPosted: Sat May 06, 2017 4:22 pm    Post subject: Reply with quote

Thank you for all the advices and help.

The duration of the main task of the motor is maximum 10 seconds. I need the analog value also during the main task of the motor.
So I think that there is no way to get the analog value in time.
I’m not familiar with all the possibilities of interrupt handling.

Walter
javascript:emoticon('Sad')
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Sat May 06, 2017 8:47 pm    Post subject: Reply with quote

Try this "slightly" modified code.
I control such devices in another way but I want to make your code alive Very Happy
Not tested but maybe it works Very Happy
Back to top
View user's profile Visit poster's website
Printpix52

Bascom Member



Joined: 18 Jun 2014
Posts: 282
Location: D.F.

mexico.gif
PostPosted: Sun May 07, 2017 3:52 am    Post subject: Re: serial port communication and analog input Reply with quote

Walt wrote:
Hello
I’m using a Ardunio Mega 328p.
The serial port is used with 19200 Bd for communication to a PC. A additional analog input with a measured voltage will be transmitted to the PC with a” print” command.
The program is not handling the inputs from the serial port
I tried different interrupt command but with no result.
This is the cod snips of a part of my program. Any help will be appreciated
thanks, Walter

Code:

Dim Va As Single                                        'Voltage
Dim Va1 As Single                                     'Voltage1
Dim W1 As Word                                        'ADC Input and Channel specification
Const H1 = 0.0049                                     'Reference
Start Adc                                                     'Analogwandler starten

Enable Interrupts  
Enable Int0                                                 'Interrupt 0 freigeben
On Int0 Label2 Nosave                              'Verweis auf Interrup Rutine "Label2"

 Do                                                    'read RS-232 Input from PC
    If  Ischarwaiting() = 1 Then
        A = Inkey()
        Select Case  Inkey(chr)

                Case  47 : Gosub Dispmessage            'char 47 = " / "
                Case Else : mld = mld + Chr(a)

        End Select
    End If
 Loop
End

'Interrupt rutine for Analog Input
Label2:
   W1 = Getadc(0)                                        'Read first AD Channel
    Va = H1 * W1                                           'Voltage steps * ADC Input
    If Va > VA1  Or Va < Va1 Then
        Print "Uvor " ; Fusing( Va , "#.###"):   ‘transmit via serial port to the PC
        Va1 = Va
    End If
Return
 



You forgot to also put these...

Code:
Config Adc = Single , Prescaler = Auto
Back to top
View user's profile
Printpix52

Bascom Member



Joined: 18 Jun 2014
Posts: 282
Location: D.F.

mexico.gif
PostPosted: Sun May 07, 2017 4:13 am    Post subject: Reply with quote

Embarassed Sorry for reading your EDC code but you are very difficult to understand but I make a mistake I will study... Embarassed Wink
Back to top
View user's profile
Printpix52

Bascom Member



Joined: 18 Jun 2014
Posts: 282
Location: D.F.

mexico.gif
PostPosted: Sun May 07, 2017 4:21 am    Post subject: Reply with quote

EDC wrote:
Try this "slightly" modified code.
I control such devices in another way but I want to make your code alive Very Happy
Not tested but maybe it works Very Happy



Embarassed Modified code where? Embarassed
Back to top
View user's profile
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1257
Location: SYDNEY

australia.gif
PostPosted: Sun May 07, 2017 7:47 am    Post subject: Reply with quote

This program reads the value of the 8 ADCs into an array after doing some calculations
connected to the ADCs are 30amp current sensors
It displays the values of current onto the LCD
It also sends the same information to the serial port this is displayed on a PC using a terminal program
So this program is doing I think what you are trying to do

Regards Paul

Code:


'--------------------------------------------------------------------------------
'name                     : 1wireSearch.bas
'micro                    : Mega168p
 'suited for demo          : No    Only due to code size which could be
'reduced by removing print commands!!!!!!!!
 'commercial addon needed  : no
'--------------------------------------------------------------------------------

$regfile = "m168pdef.dat"
$crystal = 8000000
$baud = 9600
$hwstack = 100                                              ' default use 32 for the hardware stack
$swstack = 100                                              'default use 10 for the SW stack
$framesize = 100                                            'default use 40 for the frame space






Config Adc = Single , Prescaler = Auto , Reference = Avcc
'Now give power to the chip
Start Adc

'With STOP ADC, you can remove the power from the chip
'Stop Adc
Dim Buf(10) As String * 6
Dim I As Byte
Dim S As String * 5
Dim B As String * 1
Dim X As Integer
Dim M As Single
Dim Y As Word
Dim W As Word , Channel As Byte
Dim K As Byte
Dim L As Byte


Config Pinb.2 = Output                                      'LCD back light  on my board

' Config 1wire = Portc.4                                     'use this pin

Config Lcdpin = Pin , Db4 = Portd.5 , Db5 = Portd.4 , Db6 = Portd.3 , Db7 = Portd.2 , E = Portb.0 , Rs = Portb.1

       I = 1
      Set Portb.2                                           'turn back light on

 Config Lcd = 16x2

 L = 0
Y = 512
'open channel for output
'Open "comd.2:9600,8,n,1" For Output As #1



'Now open a pin for input
'Open "comd.3:9600,8,n,1" For Input As #2
'since there is no relation between the input and output pin
'there is NO ECHO while keys are typed
'For I = 1 To 10
' Print #1 , "Number"
' Wait 1
'Next I
Cls
Locate 1 , 1
Lcd "line 1"
Locate 2 , 1
Lcd "line 2"


Do
  Channel = 0

 'W = Getadc(channel)
 For K = 1 To 8
     Channel = K - 1
     W = 0
     M = 0
   For I = 1 To 8                                           'This will add 16 ADC samples together
   W = W + Getadc(channel)
   Next I
   Shift W , Right , 3


     'Print "Channel " ; Channel ; " value " ; W
     Select Case W

     Case Is > 512
     X = W - 512
     M = X / 20
     B = "+"
     S = B + Fusing(m , "##.##")

     Buf(k) = S

     Case Is < 512
     X = 512 - W
     M = X / 20
     B = "-"
     S = B + Fusing(m , "##.##")

     Buf(k) = S

     End Select
     'Print #1 , B ; S

     'Print B ; S
 Next
   Wait 5

  Select Case L
    Case 0

     Cls
     Locate 1 , 1
     Lcd "1=" ; Buf(1) ; "  2=" ; Buf(2)
     Locate 2 , 1
     Lcd "3=" ; Buf(3) ; "  4=" ; Buf(4)
     Incr L

    Case Is > 0

     Cls
     Locate 1 , 1
     Lcd "5=" ; Buf(5) ; "  6=" ; Buf(6)
     Locate 2 , 1
     Lcd "7=" ; Buf(7) ; "  8=" ; Buf(8)

     L = 0
   Print
   For K = 1 To 8
     Print K ; ">" ; Buf(k) ; "   ";
   Next



  End Select


Loop
End

'The new M163 has options for the reference voltage
'For this chip you can use the additional param :
'Config Adc = Single , Prescaler = Auto, Reference = Internal
'The reference param may be :
'OFF      : AREF, internal reference turned off
'AVCC     : AVCC, with external capacitor at AREF pin
'INTERNAL : Internal 2.56 voltage reference with external capacitor ar AREF pin

'Using the additional param on chip that do not have the internal reference will have no effect.
 
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Sun May 07, 2017 8:40 am    Post subject: Reply with quote

Because it was me who advise @Walt to attach whole its code, I also take the effort to understand how it works before Very Happy

I think this is the code for controlling stepmotor module like A4988 or similar where you must set direction, enable and then generate clock signal for every step.

So original program works like this: If command was received then program jump to the first subroutine and display decoded command. From this subroutine it jump again to another subroutine "Motor".
In this subroutine direction and motor_enable was set and in "Loop Until needed steps are done" program stay in it with ~3ms period for every step.
So program can stay in those loops even for 10sec Very Happy

For making this job more difficult @Walt enable Interrupt and read ADC in it every 16ms with, horrible I think, math with Single Very Happy

So in my "mod" I left original @Walt descriptions for the variables Very Happy
Program should behave like original but it is "nonblocking" and, after another modification, should allow for more commands even "at work". For example, motor stop etc.

Remember that original program generate clock signal for long period.

Have a nice day Very Happy
Back to top
View user's profile Visit poster's website
Walt

Bascom Member



Joined: 13 Aug 2009
Posts: 14

blank.gif
PostPosted: Sun May 07, 2017 11:46 am    Post subject: Reply with quote

EDC
Thank you for the file. You are an expert.
Everything is working fine except the analog transmission to the pc.
If the analog value transmission is running, the control of the motor is not possible.
If I mark the print command ( ‘Print "Uvor " ; Format(helpstr , "0.000") ) so that there is no data transfer of the command to the PC, the motor control is ok
Walter
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-ARDUINO All times are GMT + 1 Hour
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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