Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**************************************/
- /* ARE 2008 */
- /* e-mail: biuro@are.net.pl */
- /* www : are.net.pl */
- /**************************************/
- #define F_CPU 8000000UL // 8 MHz
- #include <util/delay.h>
- #include <string.h>
- #include <avr/io.h>
- #include <stdio.h>
- void delay_ms(int ms)
- {
- volatile long unsigned int i;
- for(i=0;i<ms;i++)
- _delay_ms(1);
- }
- void delay_us(int us)
- {
- volatile long unsigned int i;
- for(i=0;i<us;i++)
- _delay_us(1);
- }
- #define RS PC0
- #define RW PC1
- #define E PC2
- void LCD2x16_init(void)
- {
- PORTC &= ~(1<<RS);
- PORTC &= ~(1<<RW);
- PORTC |= (1<<E);
- PORTD = 0x38; // dwie linie, 5x7 punktow
- PORTC &=~(1<<E);
- _delay_us(120);
- PORTC |= (1<<E);
- PORTD = 0x0e; // wlacz wyswietlacz, kursor, miganie
- PORTC &=~(1<<E);
- _delay_us(120);
- PORTC |= (1<<E);
- PORTD = 0x06;
- PORTC &=~(1<<E);
- _delay_us(120);
- }
- void LCD2x16_clear(void){
- PORTC &= ~(1<<RS);
- PORTC &= ~(1<<RW);
- PORTC |= (1<<E);
- PORTD = 0x01;
- PORTC &=~(1<<E);
- delay_ms(120);
- }
- void LCD2x16_putchar(int data)
- {
- PORTC |= (1<<RS);
- PORTC &= ~(1<<RW);
- PORTC |= (1<<E);
- PORTD = data;
- PORTC &=~(1<<E);
- _delay_us(120);
- }
- void LCD2x16_pos(int wiersz, int kolumna)
- {
- PORTC &= ~(1<<RS);
- PORTC &= ~(1<<RW);
- PORTC |= (1<<E);
- delay_ms(1);
- PORTD = 0x80+(wiersz-1)*0x40+(kolumna-1);
- delay_ms(1);
- PORTC &=~(1<<E);
- _delay_us(120);
- }
- double map(double x, double in_max, double out_max) {// zamienia z jednego przedzialu na drugi, przedzialy od 0
- return x * out_max / in_max;
- }
- int main(void){
- char tmp[17];
- int i;
- double _sp = 0.4 * 400; // narzucona wartosc
- int _h = 0.04 * 400; // histereza
- double _pv = 400; // tutaj bedzie wartosc z potencjometru
- double _e = 0; // blad
- int _cv = 0; // wartosc boolean na diodzie
- ADMUX = 0x40;
- ADCSRA = 0xe0;
- DDRA = 0x00;
- DDRB |= (1<<PB0);
- DDRC = 0x00;
- DDRC |= (1<<PC0) | (1<<PC1) | (1<<PC2) | (1<<PC7);
- DDRD = 0xff;
- PORTC |= (1 << PC3) | (1 << PC4) | (1 << PC5) | (1 << PC6);
- PORTD = 0x00;
- delay_ms(200);
- LCD2x16_init();
- LCD2x16_clear();
- delay_ms(400);
- while(1) { // glowna petla
- _pv = map((double)ADC, 1023, 400);
- _e = _sp - _pv;
- if(_e > _h/2) { // dioda zalezna od bledu
- _cv = 1;
- } else if (_e < -_h/2) {
- _cv = 0;
- }
- // wyswietlacz
- LCD2x16_pos(1,1);
- sprintf(tmp,"Z=%3dC A=%03d.%1dC ", (int)_sp, (int)_pv, (int)_pv%10);
- for(i=0;i < 16;i++) {
- LCD2x16_putchar(tmp[i]);
- }
- LCD2x16_pos(2,1);
- sprintf(tmp,"H=%2iC E=%+04d.%1dC ", _h, (int)_e, (unsigned int)_e%10);
- for(i=0;i < 16;i++) {
- LCD2x16_putchar(tmp[i]);
- }
- if( !(PINC & (1<<PC3))) { // przyciski
- _sp = 0.5 * 400;
- }
- if( !(PINC & (1<<PC4))) {
- _sp = 0.4 * 400;
- }
- if( !(PINC & (1<<PC5))) {
- _h = 0.04 * 400;
- }
- if( !(PINC & (1<<PC6))) {
- _h = 0.1 * 400;
- }
- if(_cv == 1) { // dioda
- PORTB |= (1 << PB0);
- } else {
- PORTB &= ~(1 << PB0);
- }
- delay_ms(500);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement