View previous topic :: View next topic |
Author |
Message |
gabriel.moreano.epn
Joined: 03 Sep 2013 Posts: 12
|
Posted: Fri Jul 04, 2014 6:20 pm Post subject: Help with USART |
|
|
Hello everyone my name is Gabriel, I have a problem with USART communication, I need to connect two atmega and that they have the same temperature value read from a sensor, I can send a character (print chr) and read in the atmega 2 with a inkey () but I need to know if you could send and read numbers with decimals, eg 37.5, the programs I'm using both atmegas is:
ATMEGA2
'1 Configuracion Principal
$regfile = "m164pdef.dat"
$crystal = 8000000
$baud = 9600
'2 Configuracion de Puertos
Ddra = &B00000000
Porta = &B00000000
'3 Configuracion de Perifericos
Config Com1 = 9600 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
Config Lcd = 16 * 2
Config Lcdbus = 4
Config Lcdpin = Pin , Db4 = Portb.0 , Db5 = Portb.1 , Db6 = Portb.2 , Db7 = Portb.3 , E = Portb.4 , Rs = Portb.5
Cls
Cursor Off
Config Adc = Single , Prescaler = Auto
Start Adc
'4 Definicion de Variables
Dim Temp As Word , Temp1 As Word
Dim Temperatura As Single , Temperatura1 As Byte
Dim Tempcabina As Single , Tempcabina1 As Byte
Dim A As Single
A = 100 / 1024
'5 Programa Principal
Do
Temp = Getadc(0)
Waitms 150
Temp1 = Getadc(1)
Waitms 150
Temperatura = Temp * A
Tempcabina = Temp1 * A
Temperatura1 = Temperatura
Tempcabina1 = Tempcabina
Print Chr(temperatura1)
Print Chr(tempcabina1)
Locate 1 , 1
Lcd "temp: " ; Fusing(temperatura , "##.#")
Locate 2 , 1
Lcd "tcabina: " ; Tempcabina1
Loop
ATMEGA 2
'1 Configuracion Principal
$regfile = "m164pdef.dat"
$crystal = 8000000
'2 Configuracion de Puertos
Ddra = &B00000000
Porta = &B00000000
'3 Configuracion de Perifericos
Config Com1 = 9600 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
Config Serialin = Buffered , Size = 4
Enable Interrupts
Config Lcd = 16 * 2
Config Lcdbus = 4
Config Lcdpin = Pin , Db4 = Portb.0 , Db5 = Portb.1 , Db6 = Portb.2 , Db7 = Portb.3 , E = Portb.4 , Rs = Portb.5
Cls
Cursor Off
'4 Definicion de Variables
Dim Temperatura(3) As Byte , Auxtemp As Byte
Dim Tempcabina As Byte , Auxtempcabina As Byte
Dim Aux As Byte , Aux1 As Byte
Dim Oxigeno As Byte
Dim Cardiaco As Byte
Dim Humedad As Byte
Dim Settemp As Byte
Dim Sethumedad As Byte
Oxigeno = 93
Cardiaco = 65
Humedad = 55
Settemp = 37
Sethumedad = 55
'5 Programa Principal
Do
Aux = Ischarwaiting()
If Aux = 1 Then
Temperatura(1) = Inkey()
Temperatura(2) = Inkey()
Temperatura(3) = Inkey()
If Temperatura(1) > 0 Then
Auxtemp = Temperatura(1)
Else
End If
Else
End If
Aux1 = Ischarwaiting()
If Aux1 = 1 Then
Tempcabina = Inkey()
If Tempcabina > 0 Then
Auxtempcabina = Tempcabina
Else
End If
Else
End If
Locate 1 , 1
Lcd "temp: " ; Auxtemp ; " "
Locate 2 , 1
Lcd "tcabina: " ; Auxtempcabina ; " "
Print Auxtemp
Print Auxtempcabina
Print Oxigeno
Print Cardiaco
Print Humedad
Print Settemp
Print Sethumedad
Loop
(BASCOM-AVR version : 2.0.7.7 ) |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Fri Jul 04, 2014 9:39 pm Post subject: |
|
|
First -> Made shortcut to Wiki http://wiki.mcselec.com/bavr/BASCOM-AVR_Help
Easier to read than help pdf. Read : PRINT, FUSING, ISCHARWAITING, INPUT, INKEY, WAITKEY and ECHO. All with examples there.
Then You can think... You can print String like
Code: | Print Fusing(s , "#.##") 'prints 123.46 | and directly display it on second LCD as string..
or You can multiply by 10 and send as Word..and many other ways.
I made little PC program that decode this string and display parameters on desktop (it can send commands in other side too)
Code: | Tekst_pwm = Str(x)
Tekst_t = Str(odczyt)
Tekst_tryb = Str(tryb)
Tekst_pilnuj = Str(pilnuj)
Tekst_hist = Str(hist)
Message = Tekst_tryb + " " + Tekst_t + " " + Tekst_pwm + " " + Tekst_pilnuj + " " + Tekst_hist + " "
Print Message |
Please Log In if You want see picture. |
|
Back to top |
|
|
gabriel.moreano.epn
Joined: 03 Sep 2013 Posts: 12
|
Posted: Fri Jul 04, 2014 11:38 pm Post subject: |
|
|
Might you give me a code example of how to read the number with decimals in atmega, the shipping is no problem but reading |
|
Back to top |
|
|
JC
Joined: 15 Dec 2007 Posts: 585 Location: Cleveland, OH
|
Posted: Sat Jul 05, 2014 7:16 pm Post subject: |
|
|
I don't know if you are running a simulation, or on real hardware.
Your schematics for the power and ground connections are NOT the same on the two micros.
Note that Vcc AND AVcc must be tied to V+. You have not shown the connection on the second micro.
Note that BOTH grounds on the M164 need to be tied to ground.
You show ARef tied to Vcc on one micro. Do NOT do this. This can damage the micro.
You can connect ARef to a cap to ground.
JC |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Sat Jul 05, 2014 9:29 pm Post subject: |
|
|
Here You have a tested one.
Code: | $regfile = "attiny2313a.dat"
$crystal = 8000000
$baud = 38400
Config Serialin = Buffered , Size = 10
Cls
Lcd "String "
Dim S As String * 5
Enable Interrupts
Do
If Ischarwaiting() = 1 Then
Input S
Locate 1 , 8 : Lcd S
End If
Loop
End |
I test it moment before post this so its okay, and working. Note that in Terminal You must send Enter because Input waiting for it. You can use Waitkey Inkey, Do what You want, just test. Note that Ischarwaiting need Enable Interrupts.
Maybe Ischarwaiting You can use like that:
Code: |
While Ischarwaiting >0
Input S
Wend
|
Try use Fusing only once:
Code: |
Temperatura1 = Fusing(temperatura , "##.#")
Lcd Temperatura1
Print Temperatura1
|
|
|
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
|
|