uart atmega16

Chcete si postavit robota, ale tak nějak nevíte kudy do toho?
jur3c

Re: uart atmega16

Příspěvek od jur3c »

teraz mam trosku iny problem mam kod

Kód: Vybrat vše

/*************************************************************************
Title:    example program for the Interrupt controlled UART library
Author:   Peter Fleury <pfleury@gmx.ch>   http://jump.to/fleury
File:     $Id: test_uart.c,v 1.5 2012/09/14 17:59:08 peter Exp $
Software: AVR-GCC 3.4, AVRlibc 1.4
Hardware: any AVR with built-in UART, tested on AT90S8515 at 4 Mhz

DESCRIPTION:
          This example shows how to use the UART library uart.c

*************************************************************************/
#include <stdlib.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>

#include "uart.h"


/* define CPU frequency in Mhz here if not defined in Makefile */
#ifndef F_CPU
#define F_CPU 16000000UL
#endif

/* 9600 baud */
#define UART_BAUD_RATE      9600      


int main(void)
{ DDRC=0;
  DDRD=124;
  PORTC=255;
  OCR1A=255;
  OCR1B=255; 
  TCCR1A = (1<<COM1A1) | (1<<COM1B1) |(1<<WGM10);
  TCCR1B = (1<<WGM12) | (1<<CS10);
  DDRA=3;
  DDRB=252;
  PORTB=255;
  PORTA=3;
  int a=1;
  int b=1;
  int j=1;
  int d=1;
  int e=1;
  int f=1;
  int g=1;
  int h=1;
  int poz=1;
  int poza=1;
  int pozb=1;
    unsigned int c;
    char buffer[7];
    int  num=134;

    
    /*
     *  Initialize UART library, pass baudrate and AVR cpu clock
     *  with the macro 
     *  UART_BAUD_SELECT() (normal speed mode )
     *  or 
     *  UART_BAUD_SELECT_DOUBLE_SPEED() ( double speed mode)
     */
    uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) ); 
    
    /*
     * now enable interrupt, since UART library is interrupt controlled
     */
    sei();
    
    /*
     *  Transmit string to UART
     *  The string is buffered by the uart library in a circular buffer
     *  and one character at a time is transmitted to the UART using interrupts.
     *  uart_puts() blocks if it can not write the whole string to the circular 
     *  buffer
     */
    uart_puts("String stored in SRAM\n");
    
    /*
     * Transmit string from program memory to UART
     */
    uart_puts_P("String stored in FLASH\n");
    
        
    /* 
     * Use standard avr-libc functions to convert numbers into string
     * before transmitting via UART
     */     
    itoa( num, buffer, 10);   // convert interger into string (decimal format)         
    uart_puts(buffer);        // and transmit string to UART

    
    /*
     * Transmit single character to UART
     */
    uart_putc('\r');
    
    for(;;)
    {
        /*
         * Get received character from ringbuffer
         * uart_getc() returns in the lower byte the received character and 
         * in the higher byte (bitmask) the last receive error
         * UART_NO_DATA is returned when no data is available.
         *
         */
        c = uart_getc();
        if ( c & UART_NO_DATA )
        {
            /* 
             * no data available from UART 
             */
        }
        else
        {
            /*
             * new data available from UART
             * check for Frame or Overrun error
             */
            if ( c & UART_FRAME_ERROR )
            {
                /* Framing Error detected, i.e no stop bit detected */
                uart_puts_P("UART Frame Error: ");
            }
            if ( c & UART_OVERRUN_ERROR )
            {
                /* 
                 * Overrun, a character already present in the UART UDR register was 
                 * not read by the interrupt handler before the next character arrived,
                 * one or more received characters have been dropped
                 */
                uart_puts_P("UART Overrun Error: ");
            }
            if ( c & UART_BUFFER_OVERFLOW )
            {
                /* 
                 * We are not reading the receive buffer fast enough,
                 * one or more received character have been dropped 
                 */
                uart_puts_P("Buffer overflow error: ");
            }
			switch (c) //Which ASCII character was received?
    {
        case '!':   
                    break;
         
        case 'U':   {PORTD=120;PORTA=1;}
                    break; 
 
        case 'C':   {PORTD=112;PORTA=0;}
                    break; 
 
        case 'D':   {PORTD=116;PORTA=2;}
                    break; 
 
        case 'L':   {PORTD=116;PORTA=1;}
                    break;
 
        case 'R':   {PORTD=120;PORTA=2;}
                    break;
 
        case 'a':   {PORTD=112;PORTA=1;}
                    break;
 
        case 'e':   {PORTD=120;PORTA=0;}
                    break;
		case 'b':   {if (bit_is_clear(PINC,0) && bit_is_clear(PINC,1) && bit_is_clear(PINC,2) && bit_is_clear(PINC,3) && !bit_is_clear(PINC,4) && !bit_is_clear(PINC,5) && !bit_is_clear(PINC,6) && !bit_is_clear(PINC,7))
    {PORTB=0; PORTD=120;PORTA=1;}
   else if (bit_is_clear(PINC,0) && bit_is_clear(PINC,1) && bit_is_clear(PINC,2) && !bit_is_clear(PINC,3) && !bit_is_clear(PINC,4) && !bit_is_clear(PINC,5) && !bit_is_clear(PINC,6) && !bit_is_clear(PINC,7))
   {PORTB=16; PORTD=112;PORTA=1;}
   else if (bit_is_clear(PINC,0) && bit_is_clear(PINC,1) && !bit_is_clear(PINC,2) && !bit_is_clear(PINC,3) && !bit_is_clear(PINC,4) && !bit_is_clear(PINC,5) && !bit_is_clear(PINC,6) && !bit_is_clear(PINC,7))
   {PORTB=16; PORTD=112;PORTA=1;}
   else if (bit_is_clear(PINC,0) && !bit_is_clear(PINC,1) && !bit_is_clear(PINC,2) && !bit_is_clear(PINC,3) && !bit_is_clear(PINC,4) && !bit_is_clear(PINC,5) && !bit_is_clear(PINC,6) && !bit_is_clear(PINC,7))
   {PORTB=16; PORTD=116;PORTA=1;}
   else if (!bit_is_clear(PINC,0) && !bit_is_clear(PINC,1) && !bit_is_clear(PINC,2) && !bit_is_clear(PINC,3) && !bit_is_clear(PINC,4) && !bit_is_clear(PINC,5) && !bit_is_clear(PINC,6) && !bit_is_clear(PINC,7))
   {PORTB=16; PORTD=116;PORTA=1;}
   else if (bit_is_clear(PINC,0) && bit_is_clear(PINC,1) && bit_is_clear(PINC,2) && bit_is_clear(PINC,3) && bit_is_clear(PINC,4) && !bit_is_clear(PINC,5) && !bit_is_clear(PINC,6) && !bit_is_clear(PINC,7))
   {PORTB=8; PORTD=120;PORTA=0;}
   else if (bit_is_clear(PINC,0) && bit_is_clear(PINC,1) && bit_is_clear(PINC,2) && bit_is_clear(PINC,3) && bit_is_clear(PINC,4) && bit_is_clear(PINC,5) && !bit_is_clear(PINC,6) && !bit_is_clear(PINC,7))
   {PORTB=8; PORTD=120;PORTA=0;}
   else if (bit_is_clear(PINC,0) && bit_is_clear(PINC,1) && bit_is_clear(PINC,2) && bit_is_clear(PINC,3) && bit_is_clear(PINC,4) && bit_is_clear(PINC,5) && bit_is_clear(PINC,6) && !bit_is_clear(PINC,7))
   {PORTB=8; PORTD=120;PORTA=2;}
   else if (bit_is_clear(PINC,0) && bit_is_clear(PINC,1) && bit_is_clear(PINC,2) && bit_is_clear(PINC,3) && bit_is_clear(PINC,4) && bit_is_clear(PINC,5) && bit_is_clear(PINC,6) && bit_is_clear(PINC,7))
   {PORTB=8; PORTD=120;PORTA=2;}
	else PORTB=24;
	}
	                break;
        case 'c': { PORTD=120;PORTA=1;
   if (!bit_is_clear(PINC,7)) a=1;
   else a=0;
   if (!bit_is_clear(PINC,6)) b=1;
   else b=0;
   if (!bit_is_clear(PINC,5)) j=1;
   else j=0;
   if (!bit_is_clear(PINC,4)) d=1;
   else d=0;
   if (!bit_is_clear(PINC,3)) e=1;
   else e=0;
   if (!bit_is_clear(PINC,2)) f=1;
   else f=0;
   if (!bit_is_clear(PINC,1)) g=1;
   else g=0;
   if (!bit_is_clear(PINC,0)) h=1;
   else h=0;
   poza=(a*0+1000*b+2000*j+3000*d+4000*e+5000*f+6000*g+7000*h);
   pozb=(a+b+j+d+e+f+g+h);
   poz=poza/pozb;
   if (poz==0 || poz<1000)  {OCR1A = 255;  OCR1B = 0;}
   else if (poz==1000 || poz<2000)  {OCR1A = 255;  OCR1B = 200;}
   else if (poz==2000 || poz<3000)  {OCR1A = 255;  OCR1B = 227;}
   else if (poz==3000 || poz<4000)  {OCR1A = 255;  OCR1B = 255;}
   else if (poz==4000 || poz<5000)  {OCR1A = 227;  OCR1B = 255;}
   else if (poz==5000 || poz<6000)  {OCR1A = 200;  OCR1B = 255;}
   else if (poz==6000 || poz<7000 || poz==7)  {OCR1A = 0;  OCR1B = 255;}
   

	} 
	                break;
        case 'd':   {if (bit_is_clear(PINB,0) && bit_is_clear(PINB,1) && bit_is_clear(PIND,7))
     {PORTD=116;PORTA=2;}//dozadu
    else if (bit_is_clear(PINB,0) && !bit_is_clear(PINB,1) && !bit_is_clear(PIND,7))
     {PORTD=120;PORTA=2;}//doprava
	 else if (bit_is_clear(PINB,0) && !bit_is_clear(PINB,1) && bit_is_clear(PIND,7))
     {PORTD=120;PORTA=0;}//mierne doprava
    else if (!bit_is_clear(PINB,0) && bit_is_clear(PINB,1) && !bit_is_clear(PIND,7)) 
     {PORTD=116;PORTA=1;}//dolava
	 else if (!bit_is_clear(PINB,0) && bit_is_clear(PINB,1) && bit_is_clear(PIND,7)) 
     {PORTD=112;PORTA=1;}//mierne dolava
    else if (!bit_is_clear(PINB,0) && !bit_is_clear(PINB,1) && bit_is_clear(PIND,7)) 
     {PORTD=120;PORTA=1;}//dopredu
     else
     {PORTD=112;PORTA=0;}//stoji
    }
                    break;
		default:    {PORTD=112;PORTA=0;}
                    break; //Character unknown to my routine, discard character
  
    }
         

 
 
            /* 
             * send received character back
             */
            uart_putc( (unsigned char)c );
        }
    }
    
}
a tie programy co mam pod b ,c a d chcem aby robil lenze ja viem ze je to zle pretoze ono mi to urobi len jeden krat cize otazka je ako to spravim aby to slo kym neposlem iny symbol napr. Y
Pirx
Příspěvky: 181
Registrován: 24 úno 2013, 16:29
Kontaktovat uživatele:

Re: uart atmega16

Příspěvek od Pirx »

Učebnice jazyka C 1.díl 6.v. Herout, Pavel

Napriklad zde:
http://honzaujep.wz.cz/Ucebnice%20jazyk ... Herout.pdf

Nebo si ji kup, coz je lepsi.
Soldering fumes make you stronger!
jur3c

Re: uart atmega16

Příspěvek od jur3c »

no teraz to mam nejak takto ale aj tak ked spustim ten program tak ho nemozem zastavit resp. prepnut na iny

Kód: Vybrat vše

#include <stdlib.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>

#include "uart.h"


/* define CPU frequency in Mhz here if not defined in Makefile */
#ifndef F_CPU
#define F_CPU 16000000UL
#endif

/* 9600 baud */
#define UART_BAUD_RATE      9600      


int main(void)
{ DDRC=0;
  DDRD=124;
  PORTC=255;
  OCR1A=255;
  OCR1B=255; 
  TCCR1A = (1<<COM1A1) | (1<<COM1B1) |(1<<WGM10);
  TCCR1B = (1<<WGM12) | (1<<CS10);
  DDRA=3;
  DDRB=252;
  PORTB=255;
  PORTA=3;
  int a=1;
  int b=1;
  int j=1;
  int d=1;
  int e=1;
  int f=1;
  int g=1;
  int h=1;
  int poz=1;
  int poza=1;
  int pozb=1;
    unsigned int c;
    char buffer[7];
    int  num=134;

    
    /*
     *  Initialize UART library, pass baudrate and AVR cpu clock
     *  with the macro 
     *  UART_BAUD_SELECT() (normal speed mode )
     *  or 
     *  UART_BAUD_SELECT_DOUBLE_SPEED() ( double speed mode)
     */
    uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) ); 
    
    /*
     * now enable interrupt, since UART library is interrupt controlled
     */
    sei();
    
    /*
     *  Transmit string to UART
     *  The string is buffered by the uart library in a circular buffer
     *  and one character at a time is transmitted to the UART using interrupts.
     *  uart_puts() blocks if it can not write the whole string to the circular 
     *  buffer
     */
    uart_puts("String stored in SRAM\n");
    
    /*
     * Transmit string from program memory to UART
     */
    uart_puts_P("String stored in FLASH\n");
    
        
    /* 
     * Use standard avr-libc functions to convert numbers into string
     * before transmitting via UART
     */     
    itoa( num, buffer, 10);   // convert interger into string (decimal format)         
    uart_puts(buffer);        // and transmit string to UART

    
    /*
     * Transmit single character to UART
     */
    uart_putc('\r');
    
    for(;;)
    {
        /*
         * Get received character from ringbuffer
         * uart_getc() returns in the lower byte the received character and 
         * in the higher byte (bitmask) the last receive error
         * UART_NO_DATA is returned when no data is available.
         *
         */
        c = uart_getc();
        if ( c & UART_NO_DATA )
        {
            /* 
             * no data available from UART 
             */
        }
        else
        {
            /*
             * new data available from UART
             * check for Frame or Overrun error
             */
            if ( c & UART_FRAME_ERROR )
            {
                /* Framing Error detected, i.e no stop bit detected */
                uart_puts_P("UART Frame Error: ");
            }
            if ( c & UART_OVERRUN_ERROR )
            {
                /* 
                 * Overrun, a character already present in the UART UDR register was 
                 * not read by the interrupt handler before the next character arrived,
                 * one or more received characters have been dropped
                 */
                uart_puts_P("UART Overrun Error: ");
            }
            if ( c & UART_BUFFER_OVERFLOW )
            {
                /* 
                 * We are not reading the receive buffer fast enough,
                 * one or more received character have been dropped 
                 */
                uart_puts_P("Buffer overflow error: ");
            }
			switch (c) //Which ASCII character was received?
    {
        case '!':   
                    break;
         
        case 'U':   {PORTD=120;PORTA=1;}
                    break; 
 
        case 'C':   {PORTD=112;PORTA=0;}
                    break; 
 
        case 'D':   {PORTD=116;PORTA=2;}
                    break; 
 
        case 'L':   {PORTD=116;PORTA=1;}
                    break;
 
        case 'R':   {PORTD=120;PORTA=2;}
                    break;
 
        case 'a':   {PORTD=112;PORTA=1;}
                    break;
 
        case 'e':   {PORTD=120;PORTA=0;}
                    break;
		case 'b':   do {if (bit_is_clear(PINC,0) && bit_is_clear(PINC,1) && bit_is_clear(PINC,2) && bit_is_clear(PINC,3) && !bit_is_clear(PINC,4) && !bit_is_clear(PINC,5) && !bit_is_clear(PINC,6) && !bit_is_clear(PINC,7))
    {PORTB=0; PORTD=120;PORTA=1;}
   else if (bit_is_clear(PINC,0) && bit_is_clear(PINC,1) && bit_is_clear(PINC,2) && !bit_is_clear(PINC,3) && !bit_is_clear(PINC,4) && !bit_is_clear(PINC,5) && !bit_is_clear(PINC,6) && !bit_is_clear(PINC,7))
   {PORTB=16; PORTD=112;PORTA=1;}
   else if (bit_is_clear(PINC,0) && bit_is_clear(PINC,1) && !bit_is_clear(PINC,2) && !bit_is_clear(PINC,3) && !bit_is_clear(PINC,4) && !bit_is_clear(PINC,5) && !bit_is_clear(PINC,6) && !bit_is_clear(PINC,7))
   {PORTB=16; PORTD=112;PORTA=1;}
   else if (bit_is_clear(PINC,0) && !bit_is_clear(PINC,1) && !bit_is_clear(PINC,2) && !bit_is_clear(PINC,3) && !bit_is_clear(PINC,4) && !bit_is_clear(PINC,5) && !bit_is_clear(PINC,6) && !bit_is_clear(PINC,7))
   {PORTB=16; PORTD=116;PORTA=1;}
   else if (!bit_is_clear(PINC,0) && !bit_is_clear(PINC,1) && !bit_is_clear(PINC,2) && !bit_is_clear(PINC,3) && !bit_is_clear(PINC,4) && !bit_is_clear(PINC,5) && !bit_is_clear(PINC,6) && !bit_is_clear(PINC,7))
   {PORTB=16; PORTD=116;PORTA=1;}
   else if (bit_is_clear(PINC,0) && bit_is_clear(PINC,1) && bit_is_clear(PINC,2) && bit_is_clear(PINC,3) && bit_is_clear(PINC,4) && !bit_is_clear(PINC,5) && !bit_is_clear(PINC,6) && !bit_is_clear(PINC,7))
   {PORTB=8; PORTD=120;PORTA=0;}
   else if (bit_is_clear(PINC,0) && bit_is_clear(PINC,1) && bit_is_clear(PINC,2) && bit_is_clear(PINC,3) && bit_is_clear(PINC,4) && bit_is_clear(PINC,5) && !bit_is_clear(PINC,6) && !bit_is_clear(PINC,7))
   {PORTB=8; PORTD=120;PORTA=0;}
   else if (bit_is_clear(PINC,0) && bit_is_clear(PINC,1) && bit_is_clear(PINC,2) && bit_is_clear(PINC,3) && bit_is_clear(PINC,4) && bit_is_clear(PINC,5) && bit_is_clear(PINC,6) && !bit_is_clear(PINC,7))
   {PORTB=8; PORTD=120;PORTA=2;}
   else if (bit_is_clear(PINC,0) && bit_is_clear(PINC,1) && bit_is_clear(PINC,2) && bit_is_clear(PINC,3) && bit_is_clear(PINC,4) && bit_is_clear(PINC,5) && bit_is_clear(PINC,6) && bit_is_clear(PINC,7))
   {PORTB=8; PORTD=120;PORTA=2;}
	else PORTB=24;
	} while(c=='b');
	                break;
        case 'c': do { PORTD=120;PORTA=1;
   if (!bit_is_clear(PINC,7)) a=1;
   else a=0;
   if (!bit_is_clear(PINC,6)) b=1;
   else b=0;
   if (!bit_is_clear(PINC,5)) j=1;
   else j=0;
   if (!bit_is_clear(PINC,4)) d=1;
   else d=0;
   if (!bit_is_clear(PINC,3)) e=1;
   else e=0;
   if (!bit_is_clear(PINC,2)) f=1;
   else f=0;
   if (!bit_is_clear(PINC,1)) g=1;
   else g=0;
   if (!bit_is_clear(PINC,0)) h=1;
   else h=0;
   poza=(a*0+1000*b+2000*j+3000*d+4000*e+5000*f+6000*g+7000*h);
   pozb=(a+b+j+d+e+f+g+h);
   poz=poza/pozb;
   if (poz==0 || poz<1000)  {OCR1A = 255;  OCR1B = 0;}
   else if (poz==1000 || poz<2000)  {OCR1A = 255;  OCR1B = 200;}
   else if (poz==2000 || poz<3000)  {OCR1A = 255;  OCR1B = 227;}
   else if (poz==3000 || poz<4000)  {OCR1A = 255;  OCR1B = 255;}
   else if (poz==4000 || poz<5000)  {OCR1A = 227;  OCR1B = 255;}
   else if (poz==5000 || poz<6000)  {OCR1A = 200;  OCR1B = 255;}
   else if (poz==6000 || poz<7000 || poz==7)  {OCR1A = 0;  OCR1B = 255;}
   

	} while(c=='c');
	                break;
        case 'd':   do {if (bit_is_clear(PINB,0) && bit_is_clear(PINB,1) && bit_is_clear(PIND,7))
     {PORTD=116;PORTA=2;}//dozadu
    else if (bit_is_clear(PINB,0) && !bit_is_clear(PINB,1) && !bit_is_clear(PIND,7))
     {PORTD=120;PORTA=2;}//doprava
	 else if (bit_is_clear(PINB,0) && !bit_is_clear(PINB,1) && bit_is_clear(PIND,7))
     {PORTD=120;PORTA=0;}//mierne doprava
    else if (!bit_is_clear(PINB,0) && bit_is_clear(PINB,1) && !bit_is_clear(PIND,7)) 
     {PORTD=116;PORTA=1;}//dolava
	 else if (!bit_is_clear(PINB,0) && bit_is_clear(PINB,1) && bit_is_clear(PIND,7)) 
     {PORTD=112;PORTA=1;}//mierne dolava
    else if (!bit_is_clear(PINB,0) && !bit_is_clear(PINB,1) && bit_is_clear(PIND,7)) 
     {PORTD=120;PORTA=1;}//dopredu
     else
     {PORTD=112;PORTA=0;}//stoji
    } while(c=='d');
                    break;
		default:    {PORTD=112;PORTA=0;}
                    break; //Character unknown to my routine, discard character
  
    }
         

 
 
            /* 
             * send received character back
             */
            uart_putc( (unsigned char)c );
        }
    }
    
}
AlesH
Příspěvky: 323
Registrován: 25 úno 2013, 09:18

Re: uart atmega16

Příspěvek od AlesH »

Problém toho posledního kódu je zřejmě v tom, že např. smyčka s while(c=='b') po zadání 'b' nikdy neskončí, protože proměnná "c" se v té smyčce nemůže změnit.

Předpokládám, že "c = uart_getc();" nečeká na příjem znaku z UARTu, ale i když na UARTu nic není, tak projde dál. V takovém případě je jednou z možností řešení uložit si načtený znak do jiné proměnné, např. "x" (jen v případě, že z UARTu přišel nějaký znak) a v další části hlavní smyčky se pak rozhodovat podle toho "x" pomocí switch(x) nebo třeba if (x=='b') ... else if (x=='c') ... else if (atd.). Pak hlavní smyčka poběží nepřetržitě a akce přiřazená k poslednímu přijatému znaku se bude provádět opakovaně.
Naposledy upravil(a) AlesH dne 28 bře 2013, 21:22, celkem upraveno 1 x.
Pirx
Příspěvky: 181
Registrován: 24 úno 2013, 16:29
Kontaktovat uživatele:

Re: uart atmega16

Příspěvek od Pirx »

AlesH píše: Předpokládám, že "c = uart_getc();" nečeká na příjem znaku z UARTu, ale i když na UARTu nic není, tak projde dál.
Myslim, ze je to presne tak, protoze "if ( c & UART_NO_DATA )" zjevne testuje vyskyt prijateho znaku.
Snad bych jenom doplnil, ze je vhodne oddelit vykonnou cast kodu od ridici (prikazy z UART) a ve vykonne casti kodu menit jeji chovani podle zmeny stavu, ktery zavisi na prijatem znaku. Vhodna konstrukce pro zacatecnika je treba "switch/case" a nebo lepe konecny automat (FSM).
Soldering fumes make you stronger!
Uživatelský avatar
Dex
Administrátor
Příspěvky: 1519
Registrován: 16 úno 2013, 14:26

Re: uart atmega16

Příspěvek od Dex »

Ta knihovna používá přerušení a ukládá znaky do bufferu. Takže je pouze potřeba otestovat jestli v bufferu něco je a pokud ano tak s tím něco provést.

Bohužel ten kód je napsán tak "prasácky", že se v něm nedokážu rychle zorientovat a na hlubší rozbor se mi jaksi nedostává času.

Chtělo by to alespoň nahlédnout do té knížky, která byla doporučena a psát to trochu "normálně".
"all your robots are belong to us"
robodoupe.cz
jur3c

Re: uart atmega16

Příspěvek od jur3c »

tak som to teda oddelil aby to nebolo v podmienke prijatia znaku a ide to v pohode diky moc
petr-kubac
Příspěvky: 96
Registrován: 24 úno 2013, 15:43
Bydliště: Frydek - Mistek
Kontaktovat uživatele:

Re: uart atmega16

Příspěvek od petr-kubac »

Při vší úctě k autorovi jeho kód je prasárna, před kterou blednou i mé Assemblerové zdrojáky

Příklad

Kód: Vybrat vše

{if (bit_is_clear(PINC,0) && bit_is_clear(PINC,1) && bit_is_clear(PINC,2) && bit_is_clear(PINC,3) && !bit_is_clear(PINC,4) && !bit_is_clear(PINC,5) && !bit_is_clear(PINC,6) && !bit_is_clear(PINC,7))
Znamená, že tyto podmínky platí všechy zároveň

PIN C Bit 0 je 0
PIN C Bit 1 je 0
PIN C Bit 2 je 0
PIN C Bit 3 je 0
PIN C Bit 4 není 0 - tedy je 1
PIN C Bit 5 není 0 - tedy je 1
PIN C Bit 6 není 0 - tedy je 1
PIN C Bit 7 není 0 - tedy je 1

Takže bych řekl že celá ta prasáckost se dá napsat jako

if ( PINC == 0b11110000 )

A když to vezmeme řádek po řádce tak můžeme klidně pokračovat ve stejném stylu.
Dokonce bych řekl, že napsat to tak jak to má autor je výrazně pracnější než napsat to správně

nebo jsem se spletl ?
"The best computer language is a solder" - "Nejlepší programovací jazyk je pájka" - Bob Pease
http://petr-kubac.blog.cz/
jur3c

Re: uart atmega16

Příspěvek od jur3c »

určite to je prasarna ale to boli prvé programy kde som veľa prepisoval :D som to nechával v takom tvare ale to sa este veľa krat prepise
Uživatelský avatar
Dex
Administrátor
Příspěvky: 1519
Registrován: 16 úno 2013, 14:26

Re: uart atmega16

Příspěvek od Dex »

Ale kdepak to není "přepisováním" to je tím, že napsat nějaký kód, který jde přeložit, neznamená, že umíš programovat.

Pochop, že slušně napsaný kód je hrozně užitečný když je třeba potřeba najít chybu.

Komentáře tam máš prakticky jen ty co už jsou v tom příkladu ze kterého jsi vyšel. Každý začátečník na tuhle poznámku odpoví, že to má teprve rozepsaný a komentáře dopíše až to bude hotový. Tak to je taky chyba. Měl by jsi si přečíst trochu teorie. Možná pak zjistíš, že to všechno nejsou jenom "kecy" ale že to opravdu funguje.

Mimochodem Petr si narozdíl ode mne našel čas přepsat tu tvou "mega-turbo-podmínku" do normálního tvaru, kde je mi hned jasné o co jde.

Myslíme to s tebou dobře tak si nech poradit :)
"all your robots are belong to us"
robodoupe.cz
Odpovědět