View previous topic :: View next topic |
Author |
Message |
Walt
Joined: 13 Aug 2009 Posts: 14
|
Posted: Thu May 04, 2017 4:08 pm Post subject: serial port communication and analog input |
|
|
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 |
|
|
Duval JP
Joined: 22 Jun 2004 Posts: 1161 Location: France
|
Posted: Thu May 04, 2017 6:20 pm Post subject: |
|
|
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
|
|
Back to top |
|
|
Walt
Joined: 13 Aug 2009 Posts: 14
|
Posted: Thu May 04, 2017 8:10 pm Post subject: |
|
|
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 |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Thu May 04, 2017 9:39 pm Post subject: |
|
|
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
You can read about it ofcourse and then use it
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 |
|
|
Walt
Joined: 13 Aug 2009 Posts: 14
|
Posted: Fri May 05, 2017 10:38 am Post subject: serial inteerface with analog input |
|
|
The complet code of the program
I hope this will be helpfull
Walter |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
|
Back to top |
|
|
Duval JP
Joined: 22 Jun 2004 Posts: 1161 Location: France
|
Posted: Fri May 05, 2017 5:40 pm Post subject: |
|
|
hi,
EDC is right
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 |
|
Back to top |
|
|
Walt
Joined: 13 Aug 2009 Posts: 14
|
Posted: Sat May 06, 2017 4:22 pm Post subject: |
|
|
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('') |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Sat May 06, 2017 8:47 pm Post subject: |
|
|
Try this "slightly" modified code.
I control such devices in another way but I want to make your code alive
Not tested but maybe it works |
|
Back to top |
|
|
Printpix52
Joined: 18 Jun 2014 Posts: 282 Location: D.F.
|
Posted: Sun May 07, 2017 3:52 am Post subject: Re: serial port communication and analog input |
|
|
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 |
|
|
Printpix52
Joined: 18 Jun 2014 Posts: 282 Location: D.F.
|
Posted: Sun May 07, 2017 4:13 am Post subject: |
|
|
Sorry for reading your EDC code but you are very difficult to understand but I make a mistake I will study... |
|
Back to top |
|
|
Printpix52
Joined: 18 Jun 2014 Posts: 282 Location: D.F.
|
Posted: Sun May 07, 2017 4:21 am Post subject: |
|
|
EDC wrote: | Try this "slightly" modified code.
I control such devices in another way but I want to make your code alive
Not tested but maybe it works |
Modified code where? |
|
Back to top |
|
|
Paulvk
Joined: 28 Jul 2006 Posts: 1257 Location: SYDNEY
|
Posted: Sun May 07, 2017 7:47 am Post subject: |
|
|
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 |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Sun May 07, 2017 8:40 am Post subject: |
|
|
Because it was me who advise @Walt to attach whole its code, I also take the effort to understand how it works before
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
For making this job more difficult @Walt enable Interrupt and read ADC in it every 16ms with, horrible I think, math with Single
So in my "mod" I left original @Walt descriptions for the variables
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 |
|
Back to top |
|
|
Walt
Joined: 13 Aug 2009 Posts: 14
|
Posted: Sun May 07, 2017 11:46 am Post subject: |
|
|
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 |
|
|
|