PS regulátor
Zo stránky SensorWiki
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include <stdio.h>
#include "pomocne.h"
FILE mystdout_uart = FDEV_SETUP_STREAM(sendchar, NULL, _FDEV_SETUP_WRITE);
int main(void)
{
UCSR0B = 0x00; // Disable RxD and TxD for UART0
// Inicializacia
// Nie vsetky registre su rozpisane; doplnte
inituart(); // inicializacia UARTU
adc_init(); // inicializacia ADC
timer1_init(); // inicializacia Timer1: PWM
timer2_init(); // inicializacia Timer2: 1ms vzorky su zakladom
stdout = &mystdout_uart;
//printf("\x1B[1;1HAD [4]=");
sei(); // Enable ALL interrupts
do{
if(vypis){ // Vypis sa realizuje kazdych 100 ms
vypis=0;
//printf("\x1b[2;1H%04x %04d %04d %04d %3d.%02d ",plnenie,iii,w_zel,reg_odch_n,(y_reg/100),(y_reg%100));
printf("%d\r",y_reg/2);
}
} while(1);
}
K programu potrebujete aj knižnicu pomocne.h s pomocnými funkciami:
// Premenne:
extern int K_reg;
extern int Ti_reg;
extern volatile long m_P_s,m_P_n,m_I_s,m_I_n;
extern volatile int m_reg;
extern int w_zel;
extern int w_zel1,w_zel2;
extern volatile int reg_odch_n;
extern volatile int reg_odch_s;
volatile unsigned char plnenie;
extern volatile unsigned int y_reg;
extern int T_v; // 110ms=109 20ms=19
extern volatile unsigned char t_out_spust_prev;
extern volatile unsigned char t_100ms;
extern volatile unsigned int iii;
extern volatile unsigned char vypis;
// Hlavne funkcie:
extern void PS_reg(void);
extern void adc_init(void);
extern void timer1_init(void);
extern void timer2_init(void);
extern void timer2_ovf_isr(void);
// Pomocne funkcie:
extern void inituart(void);
extern int sendchar(char c, FILE *stream);
// Following calculation assumes that F_CPU is assigned in 'Project/Options'
#define BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / (BAUDRATE * 16UL))) - 1)
/* definitions for UART control */
// Valid for ATmega328
#define BAUD_RATE_REG UBRR0 //
#define UART_STATUS_REG UCSR0A // OK, checked (but also control reg.)
#define UART_CONTROL_REG_B UCSR0B // OK, checked
#define UART_CONTROL_REG_C UCSR0C // Added for mode setting
#define ENABLE_TRANSMITTER_BIT TXEN0 // OK, checked
#define ENABLE_RECEIVER_BIT RXEN0 // OK, checked
#define DATA_REGISTER_EMPTY_BIT UDRE0 // Added, for possible speed-up
#define TRANSMIT_COMPLETE_BIT TXC0 // OK, checked
#define RECEIVE_COMPLETE_BIT RXC0 // OK, checked
#define UART_DATA_REG UDR0 // OK, checked
#define SETBIT(VAR,BIT) ((VAR)|=(1<<(BIT)))