Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i have to make a code to send a data from visual to AVR to MCU and show it on 8 LED 7 segment display.

This is my C# code, right now i can't check, if is it working so can you tell me, if is it possible to send data like this?
1st code




And if is here someone, who can do Assembly language could you give me an advice, how to solve the avr problem? (I have to use ATmega128, AVR studio 4 and assembly language - it is school project) I need to recieve data from serial port from Visual read what is that (if 0,1...9) and then show it on display. I am not sure if the initialization is right and i dont know, how to read that data and put them to r18 to compare.
2nd code

Thank you for advice.

What I have tried:

C#
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 test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string a;
        private void button1_Click(object sender, EventArgs e)
        {
                       

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

            if (a == "0")
            {
                int b = 0b00000000;
                string c;
                c = b.ToString();

                port.Open();
                port.Write(c);
                port.Close();
                textBox1.Clear();
            }

             else if (a == "1")
            {
                int b = 0b00000001;
                string c;
                c = b.ToString();

                port.Open();
                port.Write(c);
                port.Close();
                textBox1.Clear();
            }

            else if (a == "2")
            {
                int b = 0b00000010;
                string c;
                c = b.ToString();

                port.Open();
                port.Write(c);
                port.Close();
                textBox1.Clear();
            }
            else if (a == "3")
            {
                int b = 0b00000011;
                string c;
                c = b.ToString();

                port.Open();
                port.Write(c);
                port.Close();
                textBox1.Clear();
            }
            else if (a == "4")
            {
                int b = 0b00000100;
                string c;
                c = b.ToString();

                port.Open();
                port.Write(c);
                port.Close();
                textBox1.Clear();
            }
            else if (a == "5")
            {
                int b = 0b00000101;
                string c;
                c = b.ToString();

                port.Open();
                port.Write(c);
                port.Close();
                textBox1.Clear();
            }
            else if (a == "6")
            {
                int b = 0b00000110;
                string c;
                c = b.ToString();

                port.Open();
                port.Write(c);
                port.Close();
                textBox1.Clear();
            }
            else if (a == "7")
            {
                int b = 0b00000111;
                string c;
                c = b.ToString();

                port.Open();
                port.Write(c);
                port.Close();
                textBox1.Clear();
            }
            else if (a == "8")
            {
                int b = 0b0001000;
                string c;
                c = b.ToString();

                port.Open();
                port.Write(c);
                port.Close();
                textBox1.Clear();
            }
            else if (a == "9")
            {
                int b = 0b0001001;
                string c;
                c = b.ToString();

                port.Open();
                port.Write(c);
                port.Close();
                textBox1.Clear();
            }
        }
    }
}


.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                  //i am not sure, if this initialization is right

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     
// i dont know where can i read the recieved data, i would copy/move to r18 and then compare

    
           
nula:                                                 ; if data = combination -> jump to display
             cpi      R18, 0b00000000
			 brne jedna           
             jmp       A0                              
jedna:
             cpi      R18, 0b00000001
			 brne dva
             jmp       A1
dva:
             cpi       R18, 0b00000010
			 brne tri
             jmp       A2
tri:
             cpi       R18, 0b00000011
			 brne ctyri
             jmp       A3
ctyri:
             cpi       R18, 0b00000100
			 brne pet
             jmp       A4
pet:
             cpi      R18, 0b00000101
			 brne sest
             jmp       A5
 
sest:
             cpi       R18, 0b00000110
			 brne sedm
             jmp       A6
sedm:
             cpi       R18, 0b00000111
			 brne osm
             jmp       A7
osm:
             cpi       R18, 0b00001000
			 brne devet
             jmp       A8
devet:
             cpi       R18, 0b00001001
			 brne exit
             jmp       A9
exit:
           
             reti



A1:

LDI R17, 0b01100000   ;number
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
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900