Created
January 12, 2025 10:26
-
-
Save akira345/0e0cb5f2e51ceb4e56ae2e01850ed00c to your computer and use it in GitHub Desktop.
AT90S2313 avr-gcc code sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AT90S2313ビルド方法 | |
## -Wall : 詳細な警告を全て表示するようにする | |
## -Os : バイナリサイズが小さくなるよう最適化をする | |
## -mmcu=<Name> : プログラムを動かすマイコンを指定 | |
# ビルド | |
avr-gcc -Os -Wall -mmcu=at90s2313 main.c -o test.elf | |
# elfからhexファイル作成 | |
avr-objcopy -I elf32-avr -O ihex test.elf test.hex | |
# メモリ使用量を調べる | |
avr-size -C --mcu=at90s2313 test.elf |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <avr/io.h> | |
#include <avr/interrupt.h> | |
#include <avr/eeprom.h> | |
#include <avr/pgmspace.h> | |
// クロック周波数 | |
#define F_CPU 10000000UL | |
#include <util/delay.h> | |
// EEPROMアドレス定義 | |
#define EEPROM_TEST_ADDRESS 0x00 | |
// フラッシュメモリに格納するデータ | |
const char flash_message[] PROGMEM = "Flash memory test successful!\n"; | |
// EEPROMに保存するデータ | |
uint8_t eeprom_test_data = 123; | |
// UART初期化(AT90S2313用) | |
void uart_init() { | |
#define BAUD 9600 | |
#include <util/setbaud.h> | |
UBRR = UBRR_VALUE; // ボーレート設定(8ビットレジスタ) | |
UCR = (1<<RXCIE)|(1<<TXCIE)|(1<<RXEN)|(1<<TXEN); // 送受信有効化 | |
} | |
// UART送信(AT90S2313対応) | |
void uart_transmit(char data) { | |
while (!(USR & (1 << UDRE))); // 送信バッファが空くまで待機 | |
UDR = data; // データを送信 | |
while (!(USR & (1 << TXC))); // 送信完了を待機 | |
USR |= (1 << TXC); // TXCフラグをクリア | |
} | |
// UARTで文字列送信 | |
void uart_transmit_string(const char *str) { | |
while (*str) { | |
uart_transmit(*str++); | |
} | |
} | |
/* | |
// SRAMテスト関数 | |
void test_sram() { | |
uint8_t sram_test[10]; // SRAMに10バイト確保 | |
uart_transmit_string("SRAM Test: Writing Data...\n"); | |
for (uint8_t i = 0; i < 10; i++) { | |
sram_test[i] = i; // データ書き込み | |
} | |
uart_transmit_string("SRAM Test: Verifying Data...\n"); | |
for (uint8_t i = 0; i < 10; i++) { | |
if (sram_test[i] != i) { | |
uart_transmit_string("SRAM Test Failed!\n"); | |
return; | |
} | |
} | |
uart_transmit_string("SRAM Test Passed!\n"); | |
} | |
*/ | |
// EEPROMテスト関数 | |
void test_eeprom() { | |
uart_transmit_string("EEPROM Test: Writing Data...\n"); | |
eeprom_write_byte((uint8_t *)EEPROM_TEST_ADDRESS, eeprom_test_data); // データ書き込み | |
_delay_ms(10); // 書き込み完了待ち | |
uint8_t data = eeprom_read_byte((uint8_t *)EEPROM_TEST_ADDRESS); // データ読み出し | |
uart_transmit_string("EEPROM Test: Verifying Data...\n"); | |
if (data == eeprom_test_data) { | |
uart_transmit_string("EEPROM Test Passed!\n"); | |
} else { | |
uart_transmit_string("EEPROM Test Failed!\n"); | |
} | |
} | |
// LED状態を保持する変数 | |
volatile uint8_t led_state = 0; | |
// 外部割り込み処理 | |
ISR(INT0_vect) { | |
led_state = !led_state; // LEDの状態を反転 | |
if (led_state) { | |
PORTB |= (1 << PB0); // LED点灯 | |
uart_transmit_string("External Interrupt Triggered: LED ON\n"); | |
} else { | |
PORTB &= ~(1 << PB0); // LED消灯 | |
uart_transmit_string("External Interrupt Triggered: LED OFF\n"); | |
} | |
} | |
// タイマー1のハンドラ | |
// 外部割り込み処理(AT90S2313用) | |
ISR(TIMER1_COMP1_vect) { | |
PORTB ^= (1 << PB1); // PB1のLEDを反転 | |
// uart_transmit_string("Timer Interrupt Triggered!\n"); | |
} | |
// UART受信割り込み処理 | |
ISR(UART_RX_vect) { | |
char received = UDR; // 受信データ取得 | |
uart_transmit(received); // エコーバック送信 | |
} | |
// 外部割り込みの初期化 | |
void init_external_interrupt() { | |
MCUCR = (1 << ISC01); // INT0立ち下がりエッジでトリガー | |
GIMSK = (1 << INT0); // INT0割り込み有効化 | |
} | |
// タイマー1の初期化(AT90S2313対応) | |
void init_timer1() { | |
// タイマー1設定: CTCモード、分周1024 | |
// AT90S2313ではCTCモードはTimer1-B でないと使えない | |
TCCR1B |= (1 << CTC1); // CTCモード | |
TCCR1B |= (1 << CS12) | (1 << CS10); // プリスケーラ1024 | |
OCR1A = (F_CPU / 1024) - 1; // 1秒間隔 | |
TIMSK |= (1 << OCIE1A); // コンペアマッチA割り込み許可 | |
} | |
/* | |
// フラッシュメモリからデータを読み出して送信 | |
void test_flash_memory() { | |
char buffer[50]; // フラッシュメモリから読み出すデータを一時保存するバッファ | |
strcpy_P(buffer, flash_message); // フラッシュメモリからデータをコピー | |
uart_transmit_string("Reading Flash Memory:\n"); | |
uart_transmit_string(buffer); // UARTで送信 | |
} | |
*/ | |
int main() { | |
// GPIO設定 | |
DDRB |= (1 << PB0) | (1 << PB1); // PB0, PB1を出力に設定 | |
PORTD |= (1 << PD2); // PD2(スイッチ)をプルアップ | |
// UART初期化 | |
uart_init(); | |
// 外部割り込みとタイマーの初期化 | |
init_external_interrupt(); | |
init_timer1(); | |
// グローバル割り込み有効化 | |
sei(); | |
// 起動メッセージを送信 | |
// uart_transmit_string("System Initialized\n"); | |
// フラッシュメモリテスト | |
// test_flash_memory(); | |
// SRAMとEEPROMのテスト | |
// test_sram(); | |
test_eeprom(); | |
while (1) { | |
// メインループは特に処理なし(割り込みで動作) | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment