#define F_CPU 12000000 #include #include #include // 指定された時間だけ待つ void delay_ms(uint16_t w){ while (w-->0) _delay_ms(1); } /////////////////////////// LCD Functions ///////////////////////////// #define LCD_PORT PORTD #define LCD_SIG_RS 0x10 #define LCD_SIG_E 0x20 #define LCD_CMD_CLEAR 0x01 volatile void lcd_wait(void) { _delay_us(40); } // 8ビット出力 void lcd_out(unsigned char d) { LCD_PORT = d | LCD_SIG_E; // output and enable E lcd_wait(); LCD_PORT &= ~LCD_SIG_E; // output and disable E lcd_wait(); } // LCD初期化 void lcd_init(void) { // 3回 00000011を出力 lcd_out(0x03); _delay_ms(5); lcd_out(0x03); _delay_ms(2); lcd_out(0x03); lcd_wait(); // ファンクションセット lcd_out(0x02); // 4ビットモードに lcd_out(0x2); lcd_out(0x8); lcd_wait(); // 00001100 表示オン/カーソルOFF/ブリンクOFF lcd_out(0x0); lcd_out(0xc); // 00000110 エントリーモードセット lcd_out(0x0); lcd_out(0x6); } // データ出力 void lcd_write(unsigned char d) { lcd_out(((d >> 4)&0x0f) | LCD_SIG_RS); // 上位4ビット lcd_out((d&0x0f) | LCD_SIG_RS); // 下位4ビット lcd_wait(); } // 表示クリア void lcd_clear(void) { LCD_PORT = 0; lcd_out(0); lcd_out(LCD_CMD_CLEAR); _delay_ms(2); } void lcd_setpos(int x,int y) { y &= 0x1; // 2行のみ LCD_PORT = 0; lcd_out(0x08 | (y << 2) ); lcd_out(x); _delay_ms(1); } static int lcd_line = 0; static int lcd_clearflag = 0; // char送信 void lcd_putchar(char c) { if (c=='\n') { lcd_line++; lcd_clearflag = 1; } else if (c=='\f') { lcd_clear(); lcd_line = 0; } else { if (lcd_clearflag) { lcd_setpos(0,lcd_line); lcd_clearflag = 17; while (--lcd_clearflag) lcd_write(' '); lcd_setpos(0,lcd_line); } lcd_write((unsigned char)c); } } // 文字列出力 void lcd_puts(char *s) { while (*s) lcd_putchar(*s++); } // 数値送信 void lcd_puti(long d) { char s[8]; int l=0,sgf=0; s[sizeof(s)-1]='\0'; if (d<0) { d=-d; sgf=1; } do{ s[sizeof(s)-2-l]=d%10+'0'; l++; } while(d/=10); if (sgf) { s[sizeof(s)-2-l]='-'; l++; } lcd_puts(s+(sizeof(s)-1-l)); } /////////////////////////////////////////////////////////////////////// void pwm_init() { TCNT0 = 0x00; //カウンタ初期化 TCCR0A = (1<