Transporting data from Visual to AVR (atmega128)

Go To Last Post
20 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am sorry if i this question is here twice, but i think i have not posted the first one.

Hi, please can you help me? I am AVR begginer and i have to do a code for transporting "something" from Visual Studio to AVR (show it on 8 Led 7 Segments display)

 

What it should do:

When i press a keybord (0-9) the number must show on display.

It should contains autonegation (not necessary. i will try to do it after i will solve the problem with transporting)

It should monitor errors. (not necessary. i will try to do it after i will solve the problem with transporting)

 

Microcontroller: ATMega128

AVR Studio 4

 

Please i need an advice how to initialize it for transporting data. I've tried to find an initialization, but i found only pieces.

Initialization is all i need, i will somehow do the rest, but the main thing is to show something.

I can give you more info.

 

 

My C# code is like: I got textbox there, i will press the keybord (0-9) and the by button i will send it to SerialPort (for now it's silly code, i will rework that later)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;

namespace seriovalinka
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        byte[] a = { 0b11100000, 0b11100001, 0b11100010, 0b11100011, 0b11100100, 0b11100101, 0b11100110, 0b11100111, 0b11101000, 0b11101001 };  // bytes for combination for display

        SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);    

        private void btnx_Click(object sender, EventArgs e)
        {
            int b = int.Parse(tb1.Text);
            if (b >= 0 && b <= 9)

                {

                port.Open();
                port.Write(a, b, 1);
                port.Close();
                tb1.Clear();
                btnx.Visible = false;
            }
            else {  }
        }

        private void tb1_TextChanged(object sender, EventArgs e)
        {
            string pomoc;
            pomoc = tb1.Text;

            if (pomoc.Contains("1"))
            {
                btnx.Visible = true;
            }
            else if (pomoc.Contains("2"))
            {
                btnx.Visible = true;
            }
            else if (pomoc.Contains("3"))
            {
                btnx.Visible = true;
            }
            else if (pomoc.Contains("4"))
            {
                btnx.Visible = true;
            }
            else if (pomoc.Contains("5"))
            {
                btnx.Visible = true;
            }
            else if (pomoc.Contains("6"))
            {
                btnx.Visible = true;
            }
            else if (pomoc.Contains("7"))
            {
                btnx.Visible = true;
            }
            else if (pomoc.Contains("8"))
            {
                btnx.Visible = true;
            }
            else if (pomoc.Contains("9"))
            {
                btnx.Visible = true;
            }
            else if (pomoc.Contains("0"))
            {
                btnx.Visible = true;
            }
            else { tb1.Text = ""; }

        }
    }
}

AVR code is harder at least for me. My friend have me and advice but i dont think it is really working. 

4

.Nolist
.include"m128def.inc"
.LIST
.CSEG

ldi UCSR0B, 0b00011000;initialization what i have found that is necessary to do (i've tried to do it, but nor sure if it's right)
ldi UCSR0C, 0b00000011; -||-
ldi UCSRnA, 0b00000010; -||-

ldi r22,LOW(RAMEND)  ;initialization atmega128 ports
out SPL,r22
ldi r22,HIGH(RAMEND)
out SPH,r22
ldi r22, 0xff
out DDRD, r22
out DDRB, r22
out PORTD, r22
out PORTB, r22

def:          ;this initialization was my friend's advice, but i think it's not right
             mov      R0, 0b00000000
             mov      R1, 0b11111111
             mov      R2, 0b11111111
             mov      R3, 0b11111111
             mov      R4, 0b00000000
             mov      R5, 0b00000000
             mov      TMOD,0b00100001
             mov      SCON,0b01010000
             mov      TH1, 0b11111101
             mov      IE, 0b10011010
             mov      IP,0b00010000
             mov      TH0,high(62535)
             mov      TL0,low(62535)
             mov      TCON, 0b01010000       

test:
             cpi      ri, $           ; were all data transported?
             mov      R7, SBUF       ; data ->acc
             clr         ri         ; for another data transport
             mov      R5, #01d
             reti

nula:                                                 ; if data = combination -> jump to display
             cpi      R7, 0b11100000, jedna
             jmp       A0
jedna:
             cpi      R7, 0b11100001, dva
             jmp       A1
dva:
             cpi       R7, 0b11100010, tri
             jmp       A2
tri:
             cpi       R7, 0b11100011, ctyri
             jmp       A3
ctyri:
             cpi       R7, 0b11100100, pet
             jmp       A4
pet:
             cpi      R7, 0b11100101, sest
             jmp       A5

sest:
             cpi       R7, 0b11100110, sedm
             jmp       A6
sedm:
             cpi       R7, 0b11100111, osm
             jmp       A7
osm:
             cpi       R7, 0b11101000, devet
             jmp       A8
devet:
             cpi       R7, 0b11101001, exit
             jmp       A9
exit:
             mov      TH0,#high(62535)
             mov      TL0,#low(62535)
             reti

A1:

LDI R17, 0b01100000   ;number
LDI R18, 0b11110011      ;position
OUT PORTD, R18
OUT PORTB, R17

rjmp test

A2:

LDI R17, 0b11011010
LDI R18, 0b11110011
OUT PORTD, R18
OUT PORTB, R17

rjmp test

A3:

LDI R17, 0b11110010
LDI R18, 0b11110011
OUT PORTD, R18
OUT PORTB, R17

rjmp test

A4:

LDI R17, 0b01100110
LDI R18, 0b11110011
OUT PORTD, R18
OUT PORTB, R17

rjmp test

A5:

LDI R17, 0b10110110
LDI R18, 0b11110011
OUT PORTD, R18
OUT PORTB, R17

rjmp test

A6:

LDI R17, 0b00111110
LDI R18, 0b11110011
OUT PORTD, R18
OUT PORTB, R17

rjmp test

A7:

LDI R17, 0b11100000
LDI R18, 0b11110011
OUT PORTD, R18
OUT PORTB, R17

rjmp test

A8:

LDI R17, 0b11111110
LDI R18, 0b11110011
OUT PORTD, R18
OUT PORTB, R17

rjmp test

A9:

LDI R17, 0b11100110
LDI R18, 0b11110011
OUT PORTD, R18
OUT PORTB, R17

rjmp test

A0:

LDI R17, 0b11111100
LDI R18, 0b11110011
OUT PORTD, R18
OUT PORTB, R17

rjmp test

 

Last Edited: Sun. Nov 12, 2017 - 06:51 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As it seems you are new to assembly programming I would suggest to start with a couple of simple programs.

 

interestingly that you use an antique processor like the Mega128... school project? or self learning with school materials?

I have not used the Meag128 for a long time, but I do recall things might go horribly wrong when you set the fuse bites incorrect, so hope for school project were that is handled for you.

Perhaps some other freak can enlighten on that when you do start from scratch and also have to set the fuses.

 

first I would start with reading the datasheet, you seem to have copied an initialization sequence from somewhere (stack initialization)

 

I would suggest to first play a bit with the leds individually. So make a small program that turns the LEDs all off

then a second program that turns them all on, followed by one that has the LED blink with a certain interval.

this way you get some basic programming feeling and later on you have a debugging interface to your disposal (normally one starts with just 1 led, but hey you got 7 in one go so have 7 stages of information for debugging.

 

with the last step you can also see if the processor is running at the speed you think it is running at.

with uart failure that is the most common problem. You think the processor is running at frequency X but actually it is running at frequency Y.......

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Why assembly language on the AVR?

'This forum helps those who help themselves.'

 

pragmatic  adjective dealing with things sensibly and realistically in a way that is based on practical rather than theoretical consideration.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:
Why assembly language on the AVR?

Yes - why?

 

meslomp wrote:
As it seems you are new to assembly programming I would suggest to start with a couple of simple programs.

When new to anything, it always makes sense to start simple!

 

"Learn to walk before trying to run" - as they say!

 

Here are some simple steps: http://www.avrfreaks.net/comment...

 

interestingly that you use an antique processor like the Mega128...

And an antique IDE like AVR Studio 4 !

 

surprise

 

first I would start with reading the datasheet

Absolutely - that is the fundamental requirement of any microcontroller project!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

mckrek7 wrote:
I am AVR begginer

So welcome to the wonderful world of AVR!

 

Do you have any experience with any other microcontrollers? Or with programming in general?

 

and i have to do a code

Why do you "have to"?

 

As already noted, is this a school project/assignment?

 

for transporting "something" from Visual Studio to AVR (show it on 8 Led 7 Segments display)

An important thing to understand is that these are really 2 entirely separate projects:

 

  1. To Visual Studio, the AVR is irrelevant.
    Your VS code will simply send to some standard PC port - it neither knows nor cares what might be attached to that port in the outside world.
    Therefore your VS project can be defined as: "Send something to some port".
     
  2. To the AVR, Visual Studio is irrelevant.
    Your AVR code will simply receive from some AVR port - it neither knows nor cares what might be attached to that port in the outside world.

    Therefore your AVR project can be defined as: "Receive something from some port".

 

 

This is important to understand, as it allows you to break down the project into manageable chunks.

 

A common beginner's mistake is to try to do everything all at once, then just switch it all on and hope it all works.

But, when it doesn't work, you don't know if that's because the sender isn't sending properly, or the receiver isn't receiving properly, or your connections are faulty - or a combination of the lot!

 

See: http://www.avrfreaks.net/comment...

 

 

Since you are using serial ports, the way to test the separate parts of your project is to use a Terminal (or, more likely nowadays, some terminal emulator software):

 

  1. Check that the terminal receives the expected stuff from the VS code.
  2. Use the terminal to send stuff to the AVR - and check that it responds correctly.

 

When you have the 2 parts working separately, then - and only then - bring them together.

 

 

As already noted, you can apply the above strategy to further break down your AVR project:

  1. Code to manage the LEDs
  2. Code to receive from the serial port.

 

Both of these are standard, basic microcontroller exercises - so there should be plenty of examples & tutorials to look at.

 

eg, this very site has a Tutorials section: http://www.avrfreaks.net/forums/tutorials

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

The simplest C program to initalise a UART is probably something like:

void uart_init() {
    UCSR0B = (1 << TXEN0) | (1 << RXEN0);
    UBRR0L = 51; // 9600 on 8MHz
}

void uart_sendchar(char data) {
    while (!(UCSR0A & (1 << UDRE0)));
    UDR0 = data;
}

int main(void) {
    uart_init();
    while(1) {
        uart_sendchar('A');
    }
}

So in Asm that would be:

    .Nolist
    .include"m128def.inc"
    .LIST
    .CSEG

    ldi r22,LOW(RAMEND)  ;initialization atmega128 ports
    out SPL,r22
    ldi r22,HIGH(RAMEND)
    out SPH,r22

    rcall uart_init

    ldi r24, 'A'
loop:
    rcall uart_sendchar
    rjmp loop

uart_init:
    ldi r16, (1 << TXEN0) | (1 << RXEN0)
    out UCSR0B, r16
    ldi r16, 51
    out UBRR0L, r16; // 9600 on 8MHz
    ret

uart_sendchar:
; enter with char to send in R24
    sbis UCSR0A, UDRE0
    rjmp uart_sendchar ; wait until UDRE set
    out UDR0, r24
    ret

BTW suggest you don't write code like this:

ldi UCSR0B, 0b00011000;initialization what i have found that is necessary to do (i've tried to do it, but nor sure if it's right)
ldi UCSR0C, 0b00000011; -||-
ldi UCSRnA, 0b00000010; -||-

For anyone (and that includes you in a few weeks) to know what this is doing you would have to get out a copy of the datasheet and lookup what those two bits in UCSR0B are. Code like this is easier to read if you use something like:

ldi UCSR0B, (1 << RXEN0) | (1 << TXEN0) ;initialization what i have found that is necessary to do (i've tried to do it, but nor sure if it's right)
ldi UCSR0C, (1 < <UCSZ00 | (1 << UCPOL0); -||-
ldi UCSRnA, (1 << U2X0); -||-

That now is clearer about what bits are being set. HOWEVER this code is complete nonsense. You cannot use LDI like that. The first parameter of an LDI opcode can only be a register number between R0 and R31. You cannot LDI to a peripheral registers like UCSR0B in a single AVR opcode. This is a RISC processor and what can be achieved in a single opcode is quite limited so this code would be something more like:

ldi r16, (1 << RXEN0) | (1 << TXEN0) ;initialization what i have found that is necessary to do (i've tried to do it, but nor sure if it's right)
out UCSR0B, r16
ldi r16, (1 < <UCSZ00 | (1 << UCPOL0); -||-
out UCSR0C, r16
ldi r16, (1 << U2X0); -||-
out UCSRnA, r16

which is "closer" but it is still wrong because there is no register called UCSRnA. When the datasheet talks about registers with lower case 'n' in the name it intends you replace 'n' with the number of the UART you are trying to program. So UCSR0A or UCSR1A. So the code would actually be:

ldi r16, (1 << U2X0); -||-
out UCSR0A, r16

If you had tried to build the code you presented you presumably already saw several errors such as this one and it saying "there is no such thing as UCSRnA"? Also you will have seen errors because you tried to us UCSR0B etc in LDI instructions.

 

Any way the C and then Asm I showed at the top of this is the very most basic stuff you need to make the UART work. Replace the 51 in it with the right number for baud rate and CPu clock frequency you plan to use.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Welcome to the Forum.

 

You mentioned that you are a beginner with AVTR's, so some additional information is needed for others to help you.

 

1)  Is this a school project?

2)  Can you use a high level language, (C, for example)?

3)  Why have you selected the M128, it is an older model?

 

As mentioned above, you need to break the project down into smaller pieces, each of which can then be accomplished, and finally put the entire project together.

 

For the AVR part of the project, I'd suggest getting an Arduino Uno, for example.

Inexpensive ones can be had for < $3 USD.

It has a modern micro, and includes a USB interface, which you will find useful for the connection to the PC part of your project.

It also has a (USB) power supply for the micro, a reset push button, one LED, and all of the by-pass capacitors, etc., to make the micro run properly.

 

It also has lots of I/O pins, to which you can connect a bunch of LEDs.

 

On the AVR side you could start by receiving the incoming command from the PC and just turn on one of several LEDs.

(Press "1" on PC --> LED #1 turns on, Press "2" on PC --> LED #2 turns on...).

 

Once all of this works, then think about driving a 7-segment display, which is doable, but not trivial for a beginner.

That requires interrupts and setting up a timer/counter in CTC mode, as well as a fair amount of hardware to drive the 7-Segment display, (unless it has a built in I2C interface...).

 

A small Arduino Nano is shown below.

Just plug in the USB cable and the hardware part of the project is done, at least for the first steps in this project!

 

JC

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

DocJC wrote:
driving a 7-segment display, which is doable, but not trivial for a beginner.

That requires interrupts and setting up a timer/counter in CTC mode, as well as a fair amount of hardware to drive the 7-Segment display

I think you must be assuming a multi-digit, multiplexed 7-segment display here?

 

Driving a single 7-segment digit requires none of the above!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mckrek7 wrote:
My C# code is like:

            string pomoc;
            pomoc = tb1.Text;

            if (pomoc.Contains("1"))
            {
                btnx.Visible = true;
            }
            else if (pomoc.Contains("2"))
            {
                btnx.Visible = true;
            }
            else if (pomoc.Contains("3"))
            {
                btnx.Visible = true;
            }
            else if (pomoc.Contains("4"))
            {
                btnx.Visible = true;
            }
            else if (pomoc.Contains("5"))
            {
                btnx.Visible = true;
            }
            else if (pomoc.Contains("6"))
            {
                btnx.Visible = true;
            }
            else if (pomoc.Contains("7"))
            {
                btnx.Visible = true;
            }
            else if (pomoc.Contains("8"))
            {
                btnx.Visible = true;
            }
            else if (pomoc.Contains("9"))
            {
                btnx.Visible = true;
            }
            else if (pomoc.Contains("0"))
            {
                btnx.Visible = true;
            }
            else { tb1.Text = ""; }

Does C# really not have a simple isdigit() function - as 'C' does ??

 

surprise

 

http://www.cplusplus.com/reference/cctype/isdigit/

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, thanks for all your responses. Yes it is school project, we have to use atmega128, we have to use AVR studio4 and Yes we have to do it with assembly language. (like we are also programming on 15-20 years old computers :/...). I will try to do something with your advices. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, I suppose asm can support comments (and ignore them) and macros, easier to read and to detect typos... .

What might be interesting would be that magical binary numbers be macros : you, your friend(s) and maybe your teacher would be happier, in the long term.

Another interesting feature would be that one knows how PORT B, say, is connected to a 7 segment display /digit and have some description of your hardware (say PORTB, 0 is connected to digit a....)    (if this is part of your source, you might want to  reuse it and adapt it: maybe your teacher, if -s-he reads code,  would be appealed with the idea of making code easier to read/reuse/adapt). BTW : it is recommended, but not compulsory,  comments are in American, in France (I do not know how they should be written in Czech Republic or Slovakia or...)...

 

Edited : is jmp an unconditional jump? Then, what is the use of comparing? (would be faster without comparing? a lot of lines would disappear with the same logic) I am very ignorant w/r assembly.

Last Edited: Mon. Nov 13, 2017 - 06:04 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

dbrion0606 wrote:
is jmp an unconditional jump?
He appears to have written the assembler as if using the opcodes of some other type of macro. He's using things like:

             cpi      R7, 0b11100000, jedna
             jmp       A0
jedna:

which uses some weird 3 parameter form of CPI that an AVR simply does not have. On the micro he took this from one can only guess that the "CPI like" instruction there was something like

 

CPI input1, input2, destination_if_compare_true/flase

 

Again AVR is RISC and does not offer complex/compound instructions like this. I'm guessing he wants to use something more like:

             cpi      R7, 0b11100000 
             brne     jedna
             jmp      A0
jedna:

though it's not clear to me if it's intended to be BRNE or BREQ ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks you Clawson (I was really puzzled : it looked like a switch ; maybe, if asm is array aware, he should have computed an index -with bound checking- and deduced R17, -R18 seems constant, if numbers are valid ones -or -s-he should have used c and activated the option --save-temps)

 

The way it is wrtitten, it can be optimised a lot, making it faster...

 

OTOH, if -s-he wants to use a conditional switch, there are 10 typos to fix... and it cannot be a mouse operation...

 

BTW  : '0' -nula- is transported, if R7 does not vary, as  1110 0000 == 0xd0 0xe0; ASCII value of '0' is, according to python,   ord('0') == 48 == 0x30 == 0b 0011 0000. I am afraid , if it is an error, it has been copied and pasted to every digit.

 

Does asm support quoted characters ('0'.... '9')? That would make things easier .

Perhaps C# should support quoted characters, too....

 

PC is not likely to find out a digit as code is written (or my optical nerve is in a very lousy state from following all the jumps)....

 

Edited : what would happen if serial port is not "com1" -say, one bought a new PC, with "only" USB plugs?
 

Last Edited: Mon. Nov 13, 2017 - 07:46 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
On the micro he took this from one can only guess that the "CPI like" instruction there was something like

 

CPI input1, input2, destination_if_compare_true/flase

 

Something like this, perhaps: http://www.8052.com/51cjne

 

Which might also explain:

mckrek7 wrote:

             mov      R7, SBUF       ; data ->acc  

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The assembly code would appear to be a horrible mashup of AVR and 8051.

 

Never was a code comment so true...

 

;this initialization was my friend's advice, but i think it's not right

'This forum helps those who help themselves.'

 

pragmatic  adjective dealing with things sensibly and realistically in a way that is based on practical rather than theoretical consideration.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, I suppose OP should change friends. I do not know whether teachers read code, and OP might add many other, subject  related,  comments.... (if it does not wotk, because of time constraints/unluck), comments can help fixing it...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

If you're going to copy a friend's homework, you need to be sure that the friend knows what he's doing ...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, so if the initialization is right could it be like this?

.Nolist
.include"m128def.inc"
.LIST
.CSEG



ldi r22,LOW(RAMEND)  ;initialization atmega128 ports
out SPL,r22
ldi r22,HIGH(RAMEND)
out SPH,r22
ldi r22, 0xff
out DDRD, r22
out DDRB, r22
out PORTD, r22
out PORTB, r22

rcall uart_init                  

ldi r24, 'A'
loop:
    rcall uart_sendchar
    rjmp loop

uart_init:
    ldi r16, (1 << TXEN0) | (1 << RXEN0)
    out UCSR0B, r16
    ldi r16, 103
    out UBRR0L, r16; // 9600 on 16MHz
    ret

uart_sendchar:

    sbis UCSR0A, UDRE0
    rjmp uart_sendchar 
    out UDR0, r24 
    ret     




    
           
nula:                                                 
             cpi      R24, 0b00000000   //r24 or r16?
			 brne jedna           
             jmp       A0                              
jedna:
             cpi      R24, 0b00000001
			 brne dva
             jmp       A1
dva:
             cpi       R24, 0b00000010
			 brne tri
             jmp       A2
tri:
             cpi       R24, 0b00000011
			 brne ctyri
             jmp       A3
ctyri:
             cpi       R24, 0b00000100
			 brne pet
             jmp       A4
pet:
             cpi      R24, 0b00000101
			 brne sest
             jmp       A5
 
sest:
             cpi       R24, 0b00000110
			 brne sedm
             jmp       A6
sedm:
             cpi       R24, 0b00000111
			 brne osm
             jmp       A7
osm:
             cpi       R24, 0b00001000
			 brne devet
             jmp       A8
devet:
             cpi       R24, 0b00001001
			 brne exit
             jmp       A9
exit:
           
             reti



A1:

LDI R17, 0b01100000   ;show number on display
LDI R18, 0b11110011      ;position
OUT PORTD, R18
OUT PORTB, R17

rjmp delay1

A2:

LDI R17, 0b11011010 
LDI R18, 0b11110011
OUT PORTD, R18
OUT PORTB, R17

rjmp delay1

A3:

LDI R17, 0b11110010 
LDI R18, 0b11110011
OUT PORTD, R18
OUT PORTB, R17

rjmp delay1

A4:

LDI R17, 0b01100110 
LDI R18, 0b11110011
OUT PORTD, R18
OUT PORTB, R17

rjmp delay1

A5:

LDI R17, 0b10110110 
LDI R18, 0b11110011
OUT PORTD, R18
OUT PORTB, R17

rjmp delay1

A6:

LDI R17, 0b00111110 
LDI R18, 0b11110011
OUT PORTD, R18
OUT PORTB, R17

rjmp delay1

A7:

LDI R17, 0b11100000 
LDI R18, 0b11110011
OUT PORTD, R18
OUT PORTB, R17

rjmp delay1

A8:

LDI R17, 0b11111110 
LDI R18, 0b11110011
OUT PORTD, R18
OUT PORTB, R17

rjmp delay1

A9:

LDI R17, 0b11100110 
LDI R18, 0b11110011
OUT PORTD, R18
OUT PORTB, R17

rjmp delay1

A0:

LDI R17, 0b11111100 
LDI R18, 0b11110011
OUT PORTD, R18
OUT PORTB, R17

rjmp delay1




delay1:
 ldi  r21, 2
    ldi  r19, 160
    ldi  r20, 146
L1: dec  r20
    brne L2
    dec  r19
    brne L2
    dec  r21
    brne L2
    rjmp PC+1


/*    just trying to make it easier
*start:
*
*ldi zl, low(2*num2) 
*ldi zh, high(2*num2)
*ldi xl, low(2*num1)
*ldi xh, high(2*num1)
*ldi r17, 10
*ldi r19, 10
*
*A:
*cpi r22, r18
*brne posun
*jmp zobrazeni
*
*posun:
*
*lpm r22, Z+ //Do r22 načtu Z a posune se o hodnotu dále
*lpm r23, X+ //Do r22 načtu Z a posune se o hodnotu dále
*dec r169*dec r17
*
*ret
*
*zobrazeni: 
*
*LDI R18, 0b11110011
*OUT PORTD, R18
*OUT PORTB, R23
*rjmp delay1
*
*delay1:
*ldi  r21, 2
*    ldi  r19, 160
*    ldi  r20, 146
*L1: dec  r20
*    brne L2
*    dec  r19
*    brne L2
*    dec  r21
*    brne L2
*   rjmp start
*
*
*
*num1:.DB 0b11111100, 0b01100000, 0b11011010, 0b11110010, 0b01100110, 0b10110110, 0b00111110, 0b11100000, 0b11111110, 0b11100110
*
*num2:.DB 0b00000000, 0b00000001, 0b00000010, 0b00000011, 0b00000100, 0b00000101, 0b00000110, 0b00000111, 0b00001000, 0b00001001 
*/

The initailazition looks like sending data, but i need receiving data (i trust you), but are the data in r16 or r24? And the "2nd" part of code is just i am trying to make it easier - more sophisticated.

I am asking because i can't try it right now, i will try it tomorrow and it will be the last chance to try it on MCU then i have to give it to him, so could you tell me, if this will possibly work or how can i put the data from serial port to a register? Thank you

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mckrek7 wrote:
i need receiving data

 

Excuse me, but who needs receiving data?

you?

PC?

Avr?

 

This simple fact should be put into comments(if you want to reuse this code; if you want people try to help you, switching from other topics; if your teacher likes comments)

 

I am afraid your PC sends data (in binary : non ASCII) and, according to the value, avr activates some segments -linked to PORTB- and a given position -part of PORTD-... -> avr needs  to receive.

 

Did you look at (explains a lot of things and is very useful)  http://www.avrfreaks.net/forum/tut-soft-using-usart-serial-communications?page=all

 

There is a simple 2 liners piece of C :

while ((UCSRA & (1 << RXC)) == 0) {}; // Do nothing until data have been received and is ready to be read from UDR
volatile byte ReceivedByte = UDR; // Fetch the received byte value into the variable "ReceivedByte"

Using Clawson's hint, you can get it converted it into asm, and adapted.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

mckrek7:

 

I have sent you a Private Message.