All pastes #1875029 Raw Edit

hd44780-rs232 avr interface

public c v1 · immutable
#1875029 ·published 2010-05-31 20:33 UTC
rendered paste body
#define F_CPU 8000000L #include <stdlib.h>#include <stdio.h>#include <string.h>#include <avr/io.h>#include <avr/interrupt.h>#include <avr/pgmspace.h>#include <util/delay.h>#include <avr/wdt.h> #define T0_INIT                256-250#define tbi(PORT,BIT)        PORT^=_BV(BIT)  #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))#include "uart.h"#include "lcd.h"#define USMODE_IDLE 0#define USMODE_PARAM1 1#define USMODE_PARAM2 2#define US_IDLE 255#define US_CMODE 254#define US_CPU 253#define US_LINE1 252#define US_LINE2 251#define US_PROGRESSBAR 250#define US_BACKLIGHT 249#define US_REBOOT 248#define US_SETMODE 247#define US_SETCGMODE 246#define US_SETRATE 245//#define US_KEEP_CON 244#define US_SOUNDH 243#define US_SOUNDL 242#define US_SOUNDLEVEL 241#define CG_ICONS1 0#define CG_VIS 1#define MAX_MENU 2#define KEY_MENU 6#define KEY_SET 7#define C10STEP 10static const PROGMEM unsigned char znaczki[] ={	0xe,0x11,0x1b,0x1f,0x1f,0x1f,0xe,0x0, // HD	0x0,0x1c,0x2, 0x19,0x5, 0x15,0x0,0x0, // RSS	0x1,0x3, 0x6, 0xf, 0x6, 0xc,0x18,0x0, // winamp	0x0,0x8, 0x1d,0x19,0x1f,0xe,0x0, 0x0, // internet	0xc,0x12,0x12,0xc, 0x4, 0x2,0x2, 0x0  // img};static const PROGMEM unsigned char nutka[] ={	0x2,0x3,0x2,0xe,0x1e,0xc,0x0};unsigned char cpuusg[8]= {	0x0,0x0, 0x0, 0x0, 0x0, 0x0, 0x0,0x0};static const PROGMEM unsigned char levels[64]={	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x1F, 	0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x1F, 0x1F, 	0x00, 0x00, 0x00, 0x00, 0x1F, 0x1F, 0x1F, 0x1F, 	0x00, 0x00, 0x00, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 	0x00, 0x00, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 	0x00, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 	0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F};void redraw(void);void SetBacklight(char);void print(const char *str);void make_bar(int perc, unsigned char* buffer);void refresh_cgram(void);void reboot(void);char cpu_usage=0;int usart_mode=USMODE_IDLE;int usart_cmd=US_IDLE;char *a;char *line1;int line1p=0;char *line2="                ";int line2p=0;unsigned char rate=8;char hd_busy=0;unsigned char backlight=255;volatile unsigned char clk=0;volatile unsigned char clk2=0;volatile unsigned char sndsample=32;volatile unsigned int snd_freq=0;//unsigned int timeout=0;char cgmode=CG_ICONS1;int main(void){    lcd_init(LCD_DISP_ON);	refresh_cgram();	//DDRD = 0x0; //PD6 i PD7 jako wejście	//PORTD = 0xFF; 	USART_Init(MYUBRR); 		line1="      ...       ";	line2="                ";		TIMSK = _BV(TOIE0);        // włącz obsługę przerwań T/C0	TCNT0 = T0_INIT;         // wartość początkowa T/C0	TCCR0 = _BV(CS00);			//TIMSK |= _BV(TOIE1);        // włącz obsługę przerwań T/C1	TCNT1 = 0xFF00;        // wartość początkowa T/C1	TCCR1A = 0x00;        // włącz tryb czasomierza T/C1	TCCR1B = _BV(CS10);		sei();                // włącz obsługę przerwań	DDRC|=1<<7;		PORTC=255;	_delay_ms(333);	PORTC=0;	DDRB = _BV(1) | _BV(2) | _BV(3) | _BV(4);	backlight=255;			usart_str("boot");    for (;;) {		        if(clk2%rate == 0)		{			redraw();			hd_busy=0;		}		hd_busy |= ((PIND>>5) & 1) ^ 1 ;		if(clk2==0) UDR='p';    }}void redraw(void){	refresh_cgram();	lcd_gotoxy(0,0);	lcd_puts(line1);	lcd_gotoxy(0,1);	lcd_puts(line2);	//lcd_gotoxy(15,0);	//lcd_putc(hd_busy==1? 0 :' ');}void SetBacklight(char state){	if(state==1) PORTC |= 1 << 7;	else PORTC &= 0xFF ^ (1 << 7);}void print(const char *str){	usart_str(str);}char chear=USMODE_IDLE;SIGNAL(SIG_UART_RECV) { 	char chr = UDR;		if(chear==USMODE_IDLE ){		chear=chr;		if(chear == US_LINE2)			line2p=0;					if(chear == US_LINE1)			line1p=0;		if(chear==US_REBOOT) {			reboot();		}	}else{		if(chear == US_CPU){						cpu_usage=chr;			make_bar(cpu_usage,cpuusg);			chear=USMODE_IDLE;		}		if(chear == US_SETMODE){			lcd_init(chr);			redraw();			chear=USMODE_IDLE;		}		if(chear == US_SETCGMODE){			cgmode=chr;			chear=USMODE_IDLE;		}		if(chear == US_LINE1){			if(chr==US_IDLE) chear=USMODE_IDLE;			else line1[line1p++]=chr;					}						if(chear == US_LINE2){			if(chr==US_IDLE) chear=USMODE_IDLE;			else line2[line2p++]=chr;		}						if(chear == US_PROGRESSBAR){			for(int i=0; i<16; i++) {				line2[i]= i < chr*0.16 ? 0xFF : 0x20;			}			chear=USMODE_IDLE;		}		if(chear == US_BACKLIGHT){			backlight=chr;			chear=USMODE_IDLE;		}		if(chear == US_SETRATE){			rate=chr;			chear=USMODE_IDLE;		}		if(chear == US_SOUNDH){			snd_freq &= 0x00FF;			snd_freq |= chr<<8;			chear=USMODE_IDLE;			if(snd_freq==0) cbi(TIMSK,TOIE1);			else sbi(TIMSK,TOIE1);		}		if(chear == US_SOUNDL){			snd_freq &= 0xFF00;			snd_freq |= chr;			chear=USMODE_IDLE;			if(snd_freq==0) cbi(TIMSK,TOIE1);			else sbi(TIMSK,TOIE1);		}		if(chear == US_SOUNDLEVEL){			sndsample = chr<<1;			PORTB=0;			chear=USMODE_IDLE;		}		/*if(chear == US_KEEP_CON){			timeout=0;			line1="";			chear=USMODE_IDLE;		}*/	}		if(chr==192) reboot();	}void reboot(){	print("REBOOT");	cli(); //irq's off	wdt_enable(WDTO_15MS); //wd on,15ms	while(1); //loop }void make_bar(int perc, unsigned char* buffer) {	int black_lines = perc*0.08;	for(int i=0; i<8; i++) {		buffer[7-i] = (i<black_lines) ? 0x1f : (i%2==1 ? 0x11 : 0x00);	}}void refresh_cgram() {	lcd_command(_BV(LCD_CGRAM));			if(cgmode==CG_ICONS1)	{		for(unsigned char i=0; i<24; i++)		{			lcd_data(pgm_read_byte_near(&znaczki[i]));		}		for(unsigned char i=0; i<8; i++)		{			lcd_data(cpuusg[i]);		}		if( clk2<128 )			lcd_data(0x00);		for(unsigned char i=0; i<7; i++)		{			lcd_data(pgm_read_byte_near(&nutka[i]));		}		if( clk2>=128)			lcd_data(0x00);					for(unsigned char i=24; i<40; i++)		{			lcd_data(pgm_read_byte_near(&znaczki[i]));		}	}	else if(cgmode==CG_VIS) {		for(unsigned char i=0; i<64; i++)		{			lcd_data(pgm_read_byte_near(&levels[i]));		}	}	lcd_gotoxy(0,0);	}SIGNAL (SIG_OVERFLOW1){	TCNT1=snd_freq;	PORTB ^= sndsample;}SIGNAL (SIG_OVERFLOW0){	TCNT0 = T0_INIT;                // przeładuj timer 0	clk++;	if(clk==0){		clk2++;		/*timeout++;		if(timeout>1000) reboot();*/	}	if(clk > backlight) PORTC |= 1 << 7;	else PORTC &= 0xFF ^ (1 << 7);	}