View previous topic :: View next topic |
Author |
Message |
Bufalo
Joined: 27 Jan 2017 Posts: 12
|
Posted: Fri Jan 27, 2017 8:18 am Post subject: Serial Port (Atmega8) |
|
|
Hello kind fellows, hope u can help,
What im trying is:
The FT232 apeears in my OS and im trying to send data with self-written software to Atmega8, which should receive incoming data and turn the LED on.
I checked my Rx port with Oscilloscope, and it shows that data is coming, but controller doesnt understand it, and i confued with "INPUT" command, how can i use it with toggling ports? If anyone can correct my code. Huge Thanks in Further.
the code im trying to getting work included:
Code: | $regfile = "m8Adef.dat" ' specify the used micro
$crystal = 4000000 ' used crystal frequency
$baud = 9600 ' Rx port( thougth this we receive a data) **** Also baud rate is the same with Pc program***
Config Portd.0 = Input ' LED
Config Portd.2 = Output
Do
Set Portd.2
If Pind.0 = 1 Then ' if we getting a data on RX port, then toggle the LED
Toggle Portd.2
Waitms 100
End If
Loop
End |
(BASCOM-AVR version : 2.0.7.8 )
Last edited by Bufalo on Fri Jan 27, 2017 8:21 am; edited 1 time in total |
|
Back to top |
|
|
Bufalo
Joined: 27 Jan 2017 Posts: 12
|
Posted: Fri Jan 27, 2017 8:20 am Post subject: Re: Serial Port (Atmega8) |
|
|
Sosrry, duplicated a post by a mistake |
|
Back to top |
|
|
P_Santos
Joined: 07 Jul 2011 Posts: 114
|
Posted: Fri Jan 27, 2017 11:32 am Post subject: |
|
|
Hello
I not see on your code where you open a serial port
Regards
P_santos |
|
Back to top |
|
|
Bufalo
Joined: 27 Jan 2017 Posts: 12
|
Posted: Fri Jan 27, 2017 11:41 am Post subject: |
|
|
P_Santos wrote: | Hello
I not see on your code where you open a serial port
Regards
P_santos |
Hello Santos,
Thx for yout reply.
I tried use "config com"
But BASCOM says that controller doesnt support UART.( what is not true Cuz i did the same with the same controller a year ago, but forget)
Can u explain how to open a port, please? |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Fri Jan 27, 2017 12:01 pm Post subject: |
|
|
You don`t need to Open anything because port will be open with settings configured in IDE.
But you must know than normal/iddle state of RX pin is High (1) so your code pushes micro to stay and wait those 100ms again and again.
This happens because Transmitter for example USB<>USART dongle keep line high in iddle and pull it down when want to sent something.
Code: | If Pind.0 = 1 Then ' This is always true
Waitms 100 'and when uC wait here you can miss incomming data
End If |
|
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Fri Jan 27, 2017 12:10 pm Post subject: |
|
|
it is totally wrong. you can best read the help and the samples.
here is something:
Code: | $regfile = "m8Adef.dat" ' specify the used micro
$crystal = 4000000 ' used crystal frequency
$baud = 9600 ' Rx port( thougth this we receive a data) **** Also baud rate is the same with Pc program***
Config Portd.2 = Output
dim b as byte
Do
b=waitkey()
toggle portd.2
Waitms 100
Loop |
_________________ Mark |
|
Back to top |
|
|
Bufalo
Joined: 27 Jan 2017 Posts: 12
|
Posted: Fri Jan 27, 2017 12:36 pm Post subject: |
|
|
Mark and EDC, thank you a lot.
As i understood i dont need to configure any ports in code and use "waitkey" command.
(Btw, what about sending data should it be on bytes or i can send any value like for example string or integrer to make port toggle?)
I will try it, and will let you know does it help or not. |
|
Back to top |
|
|
Bufalo
Joined: 27 Jan 2017 Posts: 12
|
Posted: Mon Jan 30, 2017 8:25 am Post subject: |
|
|
albertsm wrote: | it is totally wrong. you can best read the help and the samples.
here is something:
Code: | $regfile = "m8Adef.dat" ' specify the used micro
$crystal = 4000000 ' used crystal frequency
$baud = 9600 ' Rx port( thougth this we receive a data) **** Also baud rate is the same with Pc program***
Config Portd.2 = Output
dim b as byte
Do
b=waitkey()
toggle portd.2
Waitms 100
Loop |
|
Thanks Mark, it works
Now im wonder how to use this code with a sending data at the same time, i want to click a button on a program, LED is toggle and MK sending data that i programmed:
Code: | $regfile = "m8Adef.dat" ' specify the used micro
$crystal = 4000000 ' used crystal frequency
$baud = 9600 ' Rx port( thougth this we receive a data) **** Also baud rate is the same with Pc program***
Config Portd.2 = Output
dim b as byte
dim a as byte
dim b1 as byte
a = 1
b1 = 10
Do
b=waitkey()
toggle portd.2
Print "print variable a " ; A
Print Hex(b1)
Waitms 100
Loop |
|
|
Back to top |
|
|
Bufalo
Joined: 27 Jan 2017 Posts: 12
|
Posted: Mon Jan 30, 2017 2:30 pm Post subject: |
|
|
any help, please? |
|
Back to top |
|
|
kimmi
Joined: 24 Feb 2006 Posts: 1922 Location: Denmark
|
Posted: Mon Jan 30, 2017 2:38 pm Post subject: |
|
|
Code: | $regfile = "m8Adef.dat"
$crystal = 4000000
$hwstack = 32
$swstack = 32
$framesize = 32
$baud = 9600
Led_sent Alias Portd.2
Config Led_sent = Output
Led_1 Alias Portd.3
Config Led_1 = Output
Led_2 Alias Portd.4
Config Led_2 = Output
Led_3 Alias Portd.5
Config Led_3 = Output
Button_1 Alias Pinb.1
Config Pinb.1 = Input
Portb.1 = 1
Button_2 Alias Pinb.2
Config Pinb.2 = Input
Portb.2 = 1
Button_3 Alias Pinb.3
Config Pinb.3 = Input
Portb.3 = 1
dim b as byte
Const Led_off = 0
Const Led_on = 1
Do
B = Inkey()
If B <> 0 Then
' sent 0 or 1 for led_1 off on
If B = "0" Then Led_1 = Led_off
If B = "1" Then Led_1 = Led_on
' sent 2 or 3 for led_2 off on
If B = "2" Then Led_2 = Led_off
If B = "3" Then Led_2 = Led_on
' sent 4 or 5 for led_3 off on
If B = "4" Then Led_3 = Led_off
If B = "5" Then Led_3 = Led_on
Waitms 100
Else
' connect a button between portb.1/2/3 and GND
If Button_1 = 0 Then
Led_sent = Led_on
Print "WHAT YOUR CMD IS HERE" ;
Bitwait Button_1 , Set
Led_sent = Led_off
Elseif Button_2 = 0 Then
Led_sent = Led_on
Print "WHAT YOUR CMD IS HERE";
Bitwait Button_2 , Set
Led_sent = Led_off
Elseif Button_3 = 0 Then
Led_sent = Led_on
Print "WHAT YOUR CMD IS HERE" ;
Bitwait Button_3 , Set
Led_sent = Led_off
Else
End If
End If
Loop
|
_________________ / Kim |
|
Back to top |
|
|
Bufalo
Joined: 27 Jan 2017 Posts: 12
|
Posted: Tue Jan 31, 2017 1:52 am Post subject: |
|
|
Kimmi,
Thank you for your reply and help.
I receive data by clicking a button, but i receive only some random numbers (picture attached, btw this problem is not on pc side, cuz i checked it with emulator, i receive string values well).
I little bit changed your code, beczuse i have only 1 button and 2 leds.
As i understood code - Program waiting when we send a data, for turn led1 we need to send "1"(its string type, right?)
If we not send a data, and press a button on mk, we send data from mk to PC program, and "WHAT YOUR CMD IS HERE" should be shown on.
In this code, we need to push a button on mk to send on PC.
i wonder how to make receive work with sending data from PC at the same time, like: click program PC button> send byte> receive number.
http://imgur.com/a/79Pbm
Code: | $regfile = "m8Adef.dat"
$crystal = 4000000
$hwstack = 32
$swstack = 32
$framesize = 32
$baud = 9600
Led_sent Alias Portd.2
Config Led_sent = Output
Led_1 Alias Portd.3
Config Led_1 = Output
Button_1 Alias Pinc.5
Config Pinc.5 = Input
Portc.5 = 1
Dim B As Byte
Const Led_off = 0
Const Led_on = 1
Do
B = Inkey()
If B <> 0 Then
' sent 0 or 1 for led_1 off on
If B = "0" Then Led_1 = Led_off
If B = "1" Then Led_1 = Led_on
' sent 2 or 3 for led_2 off on
Else
' connect a button between portb.1/2/3 and GND
If Button_1 = 0 Then
Led_sent = Led_on
Print "WHAT YOUR CMD IS HERE" ;
Bitwait Button_1 , Set
Led_sent = Led_off
Else
End If
End If
Loop
|
|
|
Back to top |
|
|
kimmi
Joined: 24 Feb 2006 Posts: 1922 Location: Denmark
|
Posted: Tue Jan 31, 2017 2:32 am Post subject: |
|
|
Quote: | Print "WHAT YOUR CMD IS HERE" ; |
_________________ / Kim |
|
Back to top |
|
|
Bufalo
Joined: 27 Jan 2017 Posts: 12
|
Posted: Tue Jan 31, 2017 3:05 am Post subject: |
|
|
kimmi wrote: | Quote: | Print "WHAT YOUR CMD IS HERE" ; |
|
Thanks for reply,
Instead of "5" i receive "650683"
Puzzled here
Is "5" Byte value 5 or Ascii character 5 ? |
|
Back to top |
|
|
Bufalo
Joined: 27 Jan 2017 Posts: 12
|
Posted: Tue Jan 31, 2017 10:10 am Post subject: |
|
|
kimmi wrote: | Quote: | Print "WHAT YOUR CMD IS HERE" ; |
|
Hello , thanks for help Kimmi,
the problem with wrong data was in wrong crystal MHZ. Now i receive everything well, But now i dont understand why my string data( same as byte) command isnt do anythibg to my MK. I try to make work this:
Code: | Do
B = Inkey()
If B <> 0 Then
' sent 0 or 1 for led_1 off on
If B = "0" Then Led_1 = Led_off
If B = "1" Then Led_1 = Led_on
Waitms 100
private void button1_Click(object sender, EventArgs e) // Here i send a byte to MK
{
serialPort1.Write("1");
} |
Im trying to send a command to my MK, by clicking a button on PC program, and if the recevied value equals "1", my LED will turn on. As i said nothing happens, any help please? |
|
Back to top |
|
|
Bufalo
Joined: 27 Jan 2017 Posts: 12
|
Posted: Tue Jan 31, 2017 10:22 am Post subject: |
|
|
Whats strange that if i do "Waitkey()" command on a Bascom Code. My data from WFA sending well, and i can toggle LED
Code: | $regfile = "m8Adef.dat" ' specify the used micro
$crystal = 4000000 ' used crystal frequency
$baud = 9600 ' Rx port( thougth this we receive a data) **** Also baud rate is the same with Pc program***
Config Portd.2 = Output
dim b as byte
Do
b=waitkey()
toggle portd.2
Waitms 100
Loop |
But when i try to do it with "InKey()" it doesnt work, also i cant just use "WaitKey()" cuz then sending operation from MK will not work.
Code: | B = Inkey()
If B <> 0 Then
' sent 0 or 1 for led_1 off on
If B = "0" Then Led_1 = Led_off
If B = "1" Then Led_1 = Led_on
Waitms 100 |
|
|
Back to top |
|
|
|