Simulátor sústavy 1. rádu: Rozdiel medzi revíziami
Zo stránky SensorWiki
Bez shrnutí editace |
Bez shrnutí editace |
||
| Riadok 34: | Riadok 34: | ||
<tabs> | <tabs> | ||
<tab name=" | <tab name="main.c"><syntaxhighlight lang="c++" style="background: LightYellow;"> | ||
#define F_CPU 16000000UL | |||
#define BAUD 9600 | |||
#include <avr/io.h> | #include <avr/io.h> | ||
#include <avr/interrupt.h> | |||
#include <stdint.h> | |||
#include <stdio.h> | |||
#include "uart.h" | |||
#define U 200 // vstup 2.00 V | |||
#define SCALE 1000L // interna mierka | |||
#define TIMER_OCR 155 // Timer 10 ms | |||
volatile uint8_t timer = 0; | |||
uint8_t counter = 0; // pocitadlo | |||
int32_t y_s = 0; // vystup systemu | |||
// Timer - 10 ms | |||
static void timer_init(void) | |||
{ | |||
TCCR2A = (1 << WGM21); // vynulovanie casovaca | |||
TCCR2B = (1 << CS22) | (1 << CS21) | (1 << CS20); // prescaler 1024 | |||
OCR2A = TIMER_OCR; // porovnavacia hodnota | |||
TIMSK2 = (1 << OCIE2A); // povolenie prerusenia | |||
} | |||
ISR(TIMER2_COMPA_vect) | |||
{ | |||
timer = 1; // vystup casovaca | |||
} | |||
// Simulacia systemu 1. radu | |||
static uint16_t filter_step(uint16_t u) | |||
{ | |||
y_s += ((int32_t)u * SCALE - y_s) / 10; // rovnica pre vypocet | |||
uint16_t y = (uint16_t)((y_s + SCALE / 2) / SCALE); // prevod na centivolty | |||
return y; | |||
} | |||
int main(void) | int main(void) | ||
{ | { | ||
uart_init(); | |||
timer_init(); | |||
sei(); // globalne povolenie preruseni | |||
char buf [20]; | |||
while (1) | |||
{ | |||
if (timer) | |||
{ | |||
timer = 0; | |||
counter ++; | |||
if (counter>=5) // simulacia sa vyhodnocuje kazdych 50ms | |||
{ | |||
counter = 0; | |||
uint16_t y = filter_step(U); // simulacia systemu | |||
// Formatovanie vystupu pre SerialPlot | |||
snprintf(buf,sizeof(buf),"%u,%u\n",U,y); | |||
uart_puts(buf); // odoslanie cez UART | |||
} | |||
} | |||
} | |||
return 0; | |||
} | } | ||
</syntaxhighlight ></tab> | </syntaxhighlight ></tab> | ||
<tab name=" | <tab name="uart.c"><syntaxhighlight lang="c++" style="background: LightYellow;"> | ||
#define F_CPU 16000000UL | |||
#define BAUD 9600 | |||
#include <avr/io.h> | #include <avr/io.h> | ||
#include <util/setbaud.h> | |||
#include "uart.h" | |||
void uart_init( void ) | |||
{ | |||
UBRR0H = UBRRH_VALUE; | |||
UBRR0L = UBRRL_VALUE; | |||
#if USE_2X | |||
UCSR0A |= _BV(U2X0); | |||
#else | |||
UCSR0A &= ~(_BV(U2X0)); | |||
#endif | |||
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */ | |||
UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */ | |||
} | |||
void uart_putc(char c) | |||
{ | |||
loop_until_bit_is_set(UCSR0A, UDRE0); /* Wait until data register empty. */ | |||
UDR0 = c; | |||
} | |||
void uart_puts(const char *s) | |||
{ | |||
while(*s) | |||
{ | |||
uart_putc(*s); | |||
s++; | |||
} | |||
} | |||
char uart_getc(void) { | |||
loop_until_bit_is_set(UCSR0A, RXC0); /* Wait until data exists. */ | |||
return UDR0; | |||
} | |||
</syntaxhighlight ></tab> | </syntaxhighlight ></tab> | ||
</tabs> | </tabs> | ||
Verzia z 19:28, 7. jún 2026
Záverečný projekt predmetu MIPS / LS2026 - Matúš Prokop
Zadanie
Mojou úlohou bolo vytvoriť program pracujúci v reálnom čase simulujúci systém Y(s)/U(s)=K/(s*T+1), K=1 [-] a T = 0,5 [sek]. Na vykreslenie priebehu použite SerialPlot. Rozsahy u(t) a y(t) sú 0 až 5V. Rozlíšenie 0.01V. Vykreslite prechodovú charakteristiku odpovedajúcu vstupnej hodnote 2.00V. Použite celočíselnú aritmetiku.

Literatúra:
Analýza a opis riešenia
Opíšte sem čo a ako ste spravili, ak treba, doplňte obrázkami... Podrobne opíšte použité komponenty (okrem základnej dosky s ATmega328P procesorom), pridajte linky na datasheety alebo opis obvodu.

Nezabudnite doplniť schému zapojenia! V texte by ste mali opísať základné veci zo zapojenia, samotná schéma nie je dostačujúci opis.

Algoritmus a program
Algoritmus programu využíva toto a toto, základné funkcie sú takéto a voláma ich tuto... Výpis kódu je nižšie...
#define F_CPU 16000000UL
#define BAUD 9600
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdint.h>
#include <stdio.h>
#include "uart.h"
#define U 200 // vstup 2.00 V
#define SCALE 1000L // interna mierka
#define TIMER_OCR 155 // Timer 10 ms
volatile uint8_t timer = 0;
uint8_t counter = 0; // pocitadlo
int32_t y_s = 0; // vystup systemu
// Timer - 10 ms
static void timer_init(void)
{
TCCR2A = (1 << WGM21); // vynulovanie casovaca
TCCR2B = (1 << CS22) | (1 << CS21) | (1 << CS20); // prescaler 1024
OCR2A = TIMER_OCR; // porovnavacia hodnota
TIMSK2 = (1 << OCIE2A); // povolenie prerusenia
}
ISR(TIMER2_COMPA_vect)
{
timer = 1; // vystup casovaca
}
// Simulacia systemu 1. radu
static uint16_t filter_step(uint16_t u)
{
y_s += ((int32_t)u * SCALE - y_s) / 10; // rovnica pre vypocet
uint16_t y = (uint16_t)((y_s + SCALE / 2) / SCALE); // prevod na centivolty
return y;
}
int main(void)
{
uart_init();
timer_init();
sei(); // globalne povolenie preruseni
char buf [20];
while (1)
{
if (timer)
{
timer = 0;
counter ++;
if (counter>=5) // simulacia sa vyhodnocuje kazdych 50ms
{
counter = 0;
uint16_t y = filter_step(U); // simulacia systemu
// Formatovanie vystupu pre SerialPlot
snprintf(buf,sizeof(buf),"%u,%u\n",U,y);
uart_puts(buf); // odoslanie cez UART
}
}
}
return 0;
}
#define F_CPU 16000000UL
#define BAUD 9600
#include <avr/io.h>
#include <util/setbaud.h>
#include "uart.h"
void uart_init( void )
{
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
#if USE_2X
UCSR0A |= _BV(U2X0);
#else
UCSR0A &= ~(_BV(U2X0));
#endif
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */
UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */
}
void uart_putc(char c)
{
loop_until_bit_is_set(UCSR0A, UDRE0); /* Wait until data register empty. */
UDR0 = c;
}
void uart_puts(const char *s)
{
while(*s)
{
uart_putc(*s);
s++;
}
}
char uart_getc(void) {
loop_until_bit_is_set(UCSR0A, RXC0); /* Wait until data exists. */
return UDR0;
}
Pridajte sem aj zbalený kompletný projekt, napríklad takto (použite jednoznačné pomenovanie, nemôžeme mať na serveri 10x zdrojaky.zip:
Zdrojový kód: zdrojaky.zip
Overenie
Ako ste overili funkciu, napríklad... Na používanie našej aplikácie stačia dve tlačítka a postup používania je opísaný v sekcii popis riešenia. Na konci uvádzame fotku hotového zariadenia.

Video: