ATtiny13を使用したプログラマブル・ディバイダ
提供:泣かないでゆり子
2016年2月11日 (木) 18:31時点における市川ゆり子 (トーク | 投稿記録)による版 (ページの作成:「==C言語コード== <pre> →* DeviderCounter.c * * Created: 2016/02/11 1:00:41 * Author: 市川ゆり子: /* Programmable Divider by tiny13 + DIP SW --Fuses-...」)
C言語コード
/* * DeviderCounter.c * * Created: 2016/02/11 1:00:41 * Author: 市川ゆり子 */ /* Programmable Divider by tiny13 + DIP SW --Fuses-- HIGH: 0xFD LOW:0x7A SPIEN * BODLEVEL 1V8 SUT_CKSEL INTRCOSC_9MHZ6_14CK_64MS --PIN Connection-- 1 RESET: N/C 2 PB3: DIP SW bit 4 3 PB4: DIP SW bit 2 4 GND: GND 5 OC0A: Divider signal out 6 PB1: DIP SW bit 1 7 T0: Divider signal in 8 Vcc: +5V */ #include <avr/io.h> char readDIPsw(); int main(void) { //Peripheral initialize. DDRB = 0b00100001; //DDRB0,5 is out. other is in. PORTB = 0b00011010; //PORTB1,3.4 pull up. OCR0A = (unsigned int) 0; //init Counter Compare Register TCCR0A = (1 << COM0A0) + (1 << WGM01); //OC0A pin Toggle, CTC counter mode. TCCR0B = (1 << CS01) + (1 << CS02); //Counter clock source T0 pin (external). while(1) { OCR0A = 5 * readDIPsw(); //Read DIP SW and set it to Compare Register. } } //Read DIP SW state and return it. char readDIPsw() { char bit0 = ~(PINB >> PINB1) & 0x01; //Read PINB1 state. char bit1 = ~(PINB >> PINB3) & 0x01; //Read PINB3 char bit2 = ~(PINB >> PINB4) & 0x01; //Read PINB4 return (bit2 << 2) | (bit1 << 1) | bit0; //Joining bits. }