View previous topic :: View next topic |
Author |
Message |
Holzwurm56
Joined: 03 Dec 2012 Posts: 16
|
Posted: Sun Feb 02, 2014 6:01 pm Post subject: Problem with serial data transfer |
|
|
Hi ,
I made a circuit with a ATMega8 to measure temperatur and oxygen , then I show the data on a lcd , this works .
Now I want to send the data via serial uart to an other ATMega8 and show the data then on a lcd .
The LCD shows the comments but then data's are scrap . Does someone see my mistake ? I found some post about serial receiving but I do not understand them .
Transmit unit
Code: |
' $sim ' Auskommentieren für das echte Programm
$regfile = "m8def.dat"
$crystal = 1000000
$hwstack = 32
$swstack = 32
$framesize = 40
$Baud = 4800
Config Watchdog = 2048
Start Watchdog
'config input / output
Ddrc = &B11000000
Ddrb = &B00000110
Config Portd.6 = Input
Config Portd.7 = Input
Jumper1 Alias Pind.6
Jumper2 Alias Pind.7
Taster2 Alias Pinc.2
Taster3 Alias Pinc.3
Taster4 Alias Pinc.4
Taster5 Alias Pinc.5
'Pullup
Portd.7 = 1
Portd.6 = 1
Portc.2 = 1
Portc.3 = 1
Portc.4 = 1
Portc.5 = 1
Config Adc = Single , Prescaler = Auto , Reference = Internal
Dim X As Word , Unt As Byte , Ob As Byte , Y As Word
Dim Temp As Byte
Dim Ee_unt As Eram Byte , Ee_ob As Eram Byte
Dim Xstr as String * 4 , Ystr as String * 4
Unt = Ee_unt
Ob = Ee_ob
Start Adc
Do
' Anzeige Abgastemperatur
Y = Getadc(1)
Waitms 100
' Normalbetrieb Sauerstoffmessung
X = Getadc(0)
X = X / 4
If X < Unt Then
Portb.2 = 1
Else
Portb.2 = 0
End If
If X < Ob Then
Portb.1 = 1
Else
Portb.1 = 0
End If
'Übergabe an serielles Display
Ystr = STR(Y)
Xstr = STR(X)
Ystr = Format(Ystr , "000")
Xstr = Format(Xstr , "00,0")
Print Y ,"............." ,X
'enter setup Sauerstoffgrenzwert
If Jumper1 = 0 Then
Locate 1 , 1
Lcd "Schaltpkt1=" ; Unt ; " "
Locate 2 , 1
Lcd "Schaltpkt2=" ; Ob ; " "
Do
Reset Watchdog
'Taster Schaltpunkt 1 aufwärts
If Taster2 = 0 Then
Waitms 30
Unt = Unt + 1
Waitms 100
End If
If Unt > 150 Then Unt = 150
'Taster Schaltpunkt 1 abwärts
If Taster3 = 0 Then
Waitms 30
Unt = Unt - 1
Waitms 100
End If
If Unt < 1 Then Unt = 1
'Taster Schaltpunkt 2 aufwärts
If Taster4 = 0 Then
Waitms 30
Ob = Ob + 1
Waitms 100
End If
If Ob > 150 Then Ob = 150
'Taster Schaltpunkt2 abwärts
If Taster5 = 0 Then
Waitms 30
Ob = Ob - 1
Waitms 100
End If
If Ob < 1 Then Ob = 1
Print Ob ,"........" , Unt
Waitms 100
' exit setup and save to eeprom
Loop Until Jumper1 = 1
Ee_unt = Unt
Ee_ob = Ob
End If
Reset Watchdog
' Ende Einstellung Sauerstoffgrenzwert
' ************************************
Loop
End
|
Receiving unit
Code: |
' $sim ' Auskommentieren für das echte Programm
$regfile = "m8def.dat"
$crystal = 1000000
$hwstack = 32
$swstack = 32
$framesize = 40
$Baud = 4800
Config Watchdog = 2048
Start Watchdog
On URXC OnRxD
Enable URXC
Enable Interrupts
DDRB = &B11111111
DDRC = &B11111110
DDRD = &B11111111
Config Lcdpin = Pin , Rs = PortC.3 , E = PortC.1 , Db4 = PortB.5 , Db5 = PortB.4 , Db6 = PortB.3 , Db7 = PortB.2
Config Lcd = 20 * 4
Cursor Off Noblink
Dim Wert as String * 20
Dim Neustart as Byte
Locate 1,1
LCD "ANZEIGE ABGASWERTE"
Waitms 1000
CLS
Do
Locate 1,1
Lcd "ABGAS- SAUER-"
Locate 2,1
Lcd "TEMPERATUR STOFF"
Locate 4,1
Lcd "GRAD PROZENT"
Locate 3,1
If Len(Wert) > 19 Then
Lcd Wert
End If
Neustart = Neustart + 1
If Neustart > 10 Then
Goto Neustarten
End If
Reset Watchdog
Neustarten:
Waitms 1000
Loop
OnRxD:
Wert = Wert + Chr(UDR)
Return
End
|
Many thanks for every answer
Hans
(BASCOM-AVR version : 2.0.7.7 ) |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Sun Feb 02, 2014 8:13 pm Post subject: |
|
|
Print Y ,"............." ,X
Code: | use Print Y ; "............." ;X ;
the last ; for suppressing chr(13) + chr(10)
|
_________________ Mark |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Sun Feb 02, 2014 8:15 pm Post subject: |
|
|
you should add some synchronization as well.
print "#";"............." ;X
In the receiver :
b=udr ' read value
if b="#" then 'sync
wert=""
else
wert=wert+ chr(b)
end if
b is a byte which is needed since once you read UDR you can not read it again without being sure it has the same value. _________________ Mark |
|
Back to top |
|
|
Holzwurm56
Joined: 03 Dec 2012 Posts: 16
|
Posted: Mon Feb 03, 2014 8:14 pm Post subject: |
|
|
Hi Mark Alberts ,
many thanks for your answer , I change my code like the first post . Your second post I do not understand , I change the code in the receiving MC but I don't know the change in the transmit MC . The variable Y is not more in the print command . On the lcd I can see the data from x and y , but I cannot control in what line or colum they are displayed .
Many thanks for every answer
Hans |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Mon Feb 03, 2014 10:32 pm Post subject: |
|
|
hi, i do not give complete samples, i just try to get you on your way.
the Y i was missing. my point : when you start sending data like 123------456 123----456 ,how can you be sure that the receiver know what the start is? some data might never arrive because receiver is powered later for example.
So you need to have a simple protocol.
# means start of data , in your receiver you keep waiting till you get this character !
, can be delimiter for multiple values like x and y :
I would not send all the ------ since you can easily add that in the receiver. send as little data as is needed is best.
so send would send :
#123,456#123,456
here you see, you do not know the end ! so you need a trailer as well like *
so you can send : #123,456*
or #123,456*#123,456*#123,456*
IN the receiver you need to build some logic using a simple state machine:
dim bState as byte
ISR :
b=udr 'get the character
select case b ' depending on the state
case 0 : 'no header yet so wait for #
if b="#" then ' ok we got the header
bstate=1 'next state
sX="" : sY="" 'clear strings
end if
case 1 : 'we only get here when the header was received so all data is X value until we get a ,
if b= "," then 'check the comma
bstate=2 'next state
else ' this means we receive data for X
sX=sX+chr(b) 'store in string
end if
case 2 ' this means the comma was received and now we receive data for Y till we get a *
if b="*" then 'end
lcd sX
lowerline lcd sY
bstate=0 'reset state
else 'still data for Y
sY=sY+ chr(b)
end if
end select
return
try to follow the flow in the isr. maybe by using the simulator. _________________ Mark |
|
Back to top |
|
|
Holzwurm56
Joined: 03 Dec 2012 Posts: 16
|
Posted: Wed Feb 05, 2014 11:14 am Post subject: |
|
|
Hi Mark Alberts ,
many thanks for your answer . In small steps I think I learn the principles of serial communication . I found no infos about the order " ISR : " . Is it the same as " OnRxd: " ? The State Machine is still a great mistery for me .
Thanks
Hans |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Thu Feb 06, 2014 11:23 pm Post subject: |
|
|
the ISR: is just the serial interrupt.
a state machine is an effective way of controlling different events and stages. A state machine has a limited number of states. And it does not do a thing unless a new state is detected.
Like a traffic light. This could be done using a state machine as well.
for example the light is green, then it goes orange and then red. after red, it becomes green again (it depends on the country).
so you could program it like this :
dim state as byte
do
select case state
case 0 : 'nothing done yet so we set all leds off
green=0 : red=0 : yellow=0
state =1
case 1 : 'the first state would be green
green=1 : state = 2
case 2 : wait 3 : state =3 'wait some time
case 3 : orange=1 'state=4
case 4 : wait 1 : state=5
case 5 : green=0:orange=0: red=1
state =6
case 6 : wait 20 'seems red is always long
state= 7
case 7 : red=0 :
state=1 ' again
end select
loop
now this is very simple. but we cut the logic in small pieces/states. and each state is unique. it is clear which state will be next. _________________ Mark |
|
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
|
|