This shows you the differences between two versions of the page.
pm:prj2025:eradu:alexandru.ionita03 [2025/05/25 16:55] alexandru.ionita03 [Download] |
pm:prj2025:eradu:alexandru.ionita03 [2025/05/25 20:15] (current) alexandru.ionita03 [Bibliografie/Resurse] |
||
---|---|---|---|
Line 60: | Line 60: | ||
{{:pm:prj2025:eradu:alexandru-andrei_ionita_pm_circuit.png?700|}} | {{:pm:prj2025:eradu:alexandru-andrei_ionita_pm_circuit.png?700|}} | ||
- | ===== Software Design ===== | + | ===== Cod ===== |
+ | <note warning> | ||
+ | #include <avr/io.h> \\ | ||
+ | #include <avr/interrupt.h> \\ | ||
+ | #include <util/delay.h> \\ | ||
+ | #define LED_PIN PB7 \\ | ||
+ | #define BUTTON_PIN PD2 \\ | ||
+ | #define BUZZER_PIN PH4 \\ | ||
+ | void uart_init() { \\ | ||
+ | UBRR0H = 0; \\ | ||
+ | UBRR0L = 103; \\ | ||
+ | UCSR0B = (1 << RXEN0) | (1 << TXEN0); \\ | ||
+ | UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); \\ | ||
+ | } \\ | ||
+ | void uart_tx(char c) { \\ | ||
+ | while (!(UCSR0A & (1 << UDRE0))); \\ | ||
+ | UDR0 = c; \\ | ||
+ | } \\ | ||
+ | void uart_print(const char *s) { \\ | ||
+ | while (*s) uart_tx(*s++); \\ | ||
+ | } \\ | ||
+ | volatile uint8_t btn_flag = 0; \\ | ||
+ | ISR(INT0_vect) { btn_flag = 1; } \\ | ||
+ | void pwm_init() { \\ | ||
+ | DDRB |= (1 << PB4); \\ | ||
+ | TCCR2A = (1 << COM2A1) | (1 << WGM20); \\ | ||
+ | TCCR2B = (1 << CS21); \\ | ||
+ | OCR2A = 0; \\ | ||
+ | } \\ | ||
+ | void adc_init() { \\ | ||
+ | ADMUX = (1 << REFS0); \\ | ||
+ | ADCSRA = (1 << ADEN) | (1 << ADPS2); \\ | ||
+ | } \\ | ||
+ | uint16_t adc_read(uint8_t ch) { \\ | ||
+ | ADMUX = (ADMUX & 0xF0) | (ch & 0x0F); \\ | ||
+ | ADCSRA |= (1 << ADSC); \\ | ||
+ | while (ADCSRA & (1 << ADSC)); \\ | ||
+ | return ADC; \\ | ||
+ | } \\ | ||
+ | void spi_init() { \\ | ||
+ | DDRB |= (1 << PB2) | (1 << PB1) | (1 << PB0); \\ | ||
+ | SPCR = (1 << SPE) | (1 << MSTR); \\ | ||
+ | PORTB |= (1 << PB0); \\ | ||
+ | } \\ | ||
+ | uint8_t spi_transfer(uint8_t data) { \\ | ||
+ | SPDR = data; \\ | ||
+ | while (!(SPSR & (1 << SPIF))); \\ | ||
+ | return SPDR; \\ | ||
+ | } \\ | ||
+ | void twi_init() { \\ | ||
+ | TWSR = 0; \\ | ||
+ | TWBR = 72; \\ | ||
+ | TWCR = (1 << TWEN); \\ | ||
+ | } \\ | ||
+ | void twi_start() { \\ | ||
+ | TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN); \\ | ||
+ | while (!(TWCR & (1 << TWINT))); \\ | ||
+ | } \\ | ||
+ | void twi_stop() { \\ | ||
+ | TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO); \\ | ||
+ | } \\ | ||
+ | uint8_t twi_write(uint8_t data) { \\ | ||
+ | TWDR = data; \\ | ||
+ | TWCR = (1 << TWINT) | (1 << TWEN); \\ | ||
+ | while (!(TWCR & (1 << TWINT))); \\ | ||
+ | return (TWSR & 0xF8); \\ | ||
+ | } \\ | ||
+ | void setup() { \\ | ||
+ | DDRB |= (1 << LED_PIN); \\ | ||
+ | DDRH |= (1 << BUZZER_PIN); \\ | ||
+ | DDRD &= ~(1 << BUTTON_PIN); \\ | ||
+ | PORTD |= (1 << BUTTON_PIN); \\ | ||
+ | uart_init(); \\ | ||
+ | EICRA |= (1 << ISC01); \\ | ||
+ | EIMSK |= (1 << INT0); \\ | ||
+ | sei(); \\ | ||
+ | pwm_init(); \\ | ||
+ | adc_init(); \\ | ||
+ | spi_init(); \\ | ||
+ | twi_init(); \\ | ||
+ | uart_print("Ceas multi-lab\r\n"); \\ | ||
+ | } \\ | ||
+ | void delay_ms(uint16_t ms) { \\ | ||
+ | while(ms--) _delay_ms(1); \\ | ||
+ | } \\ | ||
+ | void loop() { \\ | ||
+ | PORTB |= (1 << LED_PIN); \\ | ||
+ | delay_ms(300); \\ | ||
+ | PORTB &= ~(1 << LED_PIN); \\ | ||
+ | delay_ms(300); \\ | ||
+ | uart_print("Salut!\r\n"); \\ | ||
+ | if (btn_flag) { \\ | ||
+ | uart_print("Buton INT0!\r\n"); \\ | ||
+ | PORTH |= (1 << BUZZER_PIN); \\ | ||
+ | delay_ms(100); \\ | ||
+ | PORTH &= ~(1 << BUZZER_PIN); \\ | ||
+ | btn_flag = 0; \\ | ||
+ | } \\ | ||
+ | static uint8_t duty = 0; \\ | ||
+ | OCR2A = duty; \\ | ||
+ | duty += 32; \\ | ||
+ | uint16_t val = adc_read(0); \\ | ||
+ | char buf[32]; \\ | ||
+ | float temp = val * (5.0 / 1023.0) * 20; \\ | ||
+ | snprintf(buf, sizeof(buf), "Temp: %.1f C\r\n", temp); \\ | ||
+ | uart_print(buf); \\ | ||
+ | PORTB &= ~(1 << PB0); \\ | ||
+ | uint8_t resp = spi_transfer(0xAA); \\ | ||
+ | PORTB |= (1 << PB0); \\ | ||
+ | twi_start(); \\ | ||
+ | uint8_t status = twi_write(0xD0); \\ | ||
+ | if (status == 0x18) uart_print("RTC gasit!\r\n"); \\ | ||
+ | else uart_print("RTC lipsa!\r\n"); \\ | ||
+ | twi_stop(); \\ | ||
+ | delay_ms(1000); \\ | ||
+ | } \\ | ||
+ | int main(void) { \\ | ||
+ | setup(); \\ | ||
+ | while (1) { \\ | ||
+ | loop(); \\ | ||
+ | } \\ | ||
+ | } \\ | ||
- | <note tip> | ||
- | Descrierea codului aplicaţiei (firmware): | ||
- | * mediu de dezvoltare (if any) (e.g. AVR Studio, CodeVisionAVR) | ||
- | * librării şi surse 3rd-party (e.g. Procyon AVRlib) | ||
- | * algoritmi şi structuri pe care plănuiţi să le implementaţi | ||
- | * (etapa 3) surse şi funcţii implementate | ||
</note> | </note> | ||
- | ===== Rezultate Obţinute ===== | ||
- | <note tip> | + | <note> |
- | Care au fost rezultatele obţinute în urma realizării proiectului vostru. | + | Lab 0 (GPIO): pinii pentru buzzer, display 8 segmente |
- | </note> | + | |
- | ===== Concluzii ===== | + | Lab 1 (UART): Serial Monitor + SoftwareSerial pentru Bluetooth HC-05, primești/setezi alarmă/radio de la PC sau app BT. |
- | ===== Download ===== | + | Lab 3 (Timere, PWM): PWM pe buzzer, toggling cu millis(). |
- | <note warning> | + | Lab 5 (SPI): Citire fisiere SD Card |
- | #include <Wire.h> \\ | + | |
- | #include <LiquidCrystal_I2C.h> \\ | + | |
- | #include <RTClib.h> \\ | + | |
- | #include <TM1637Display.h> \\ | + | |
- | #include <SoftwareSerial.h> \\ | + | |
- | #include <RDA5807M.h> \\ | + | |
- | #define BUZZER_PIN 7 \\ | + | Lab 6 (I2C): RTC DS3231, LCD, Radio RDA5807M – toate pe I2C. |
- | #define TM1637_DIO 4 \\ | + | </note> |
- | #define TM1637_CLK 5 \\ | + | ===== Rezultate Obţinute ===== |
- | #define BUTTON_PIN 2 \\ | + | |
- | #define POT_PIN A7 \\ | + | |
- | #define BT_RX 8 \\ | + | |
- | #define BT_TX 9 \\ | + | |
- | LiquidCrystal_I2C lcd(0x27, 16, 2); \\ | + | <note tip> |
- | RTC_DS3231 rtc; \\ | + | În urma dezvoltării proiectului „Ceas multifuncțional”, am obținut următoarele rezultate: |
- | TM1637Display display(TM1637_CLK, TM1637_DIO); \\ | + | |
- | SoftwareSerial btSerial(BT_RX, BT_TX); \\ | + | |
- | RDA5807M radio; \\ | + | |
+ | Funcționalitate completă hardware: Toate modulele hardware au fost conectate și testate — ceas RTC, afișaj LCD I2C, display TM1637, buzzer de alarmă, modul radio FM și MP3 player cu amplificator. | ||
- | volatile bool alarmTriggered = false; \\ | + | Afișaj ora și dată: Ceasul afișează în timp real ora și data atât pe LCD I2C, cât și pe display-ul TM1637 cu 4 cifre. |
- | int alarmHour = 8, alarmMinute = 0; \\ | + | |
- | bool alarmActive = false; \\ | + | |
- | unsigned long lastBeepTime = 0; \\ | + | |
- | bool buzzerState = false; \\ | + | |
- | void IRAM_ATTR handleButton() { \\ | + | Alarmă sonoră: Sistemul permite programarea unei alarme, care activează buzzer-ul la ora setată și afișează un mesaj pe LCD. |
- | alarmActive = false; \\ | + | |
- | digitalWrite(BUZZER_PIN, LOW); \\ | + | |
- | lcd.setCursor(0, 1); \\ | + | |
- | lcd.print("Alarma oprita! "); \\ | + | |
- | } \\ | + | |
- | void setup() { | + | Redare audio: Am reușit să comut între două surse audio (radio FM sau MP3 player), semnalul fiind amplificat și, opțional, redat pe difuzor. |
- | pinMode(BUZZER_PIN, OUTPUT); | + | |
- | pinMode(BUTTON_PIN, INPUT_PULLUP); | + | |
- | attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), handleButton, FALLING); | + | Interfață Bluetooth: Utilizatorul poate seta ora alarmei, poate schimba frecvența radio sau melodia de pe MP3 player direct dintr-o aplicație pe telefon, folosind modulul Bluetooth HC-05. |
- | Serial.begin(9600); | + | Cod modular, utilizare registre: Pentru testare și validare, am implementat funcționalitățile atât cu biblioteci, cât și direct pe registre AVR, pentru a ilustra cunoștințele teoretice și practice dobândite la fiecare laborator. |
- | btSerial.begin(9600); | + | |
- | Wire.begin(); | + | Testare și prezentare: Proiectul a fost testat pe breadboard și poate fi demonstrat live, atât cu, cât și fără unele componente (ex: difuzor), arătând fluxul semnalului și interacțiunea dintre module. |
- | lcd.init(); lcd.backlight(); | + | </note> |
- | rtc.begin(); | + | |
- | display.setBrightness(0x0f); | + | |
- | radio.init(); | + | |
- | radio.setFrequency(10390); | + | |
- | lcd.setCursor(0, 0); | + | ===== Concluzii ===== |
- | lcd.print("Ceas multi-lab!"); | + | |
- | } | + | |
- | void loop() { | + | Proiectul a validat atât integrarea hardware a componentelor electronice diverse, cât și implementarea software pe platforma Arduino/AVR, acoperind temele principale din laboratoarele de microcontrolere: GPIO, UART, întreruperi, ADC, PWM, SPI și I2C, precum și comunicarea wireless și interfața cu utilizatorul. |
- | DateTime now = rtc.now(); | + | |
- | int hh = now.hour(); | + | |
- | int mm = now.minute(); | + | |
- | int ss = now.second(); | + | |
- | int timeVal = hh * 100 + mm; | ||
- | display.showNumberDecEx(timeVal, 0b01000000, true); | ||
- | if (Serial.available()) { | ||
- | String cmd = Serial.readStringUntil('\n'); | ||
- | handleCommand(cmd); | ||
- | } | ||
- | if (btSerial.available()) { | ||
- | String cmd = btSerial.readStringUntil('\n'); | ||
- | handleCommand(cmd); | ||
- | } | ||
- | if (alarmActive) { | ||
- | unsigned long t = millis(); | ||
- | if (t - lastBeepTime > 300) { | ||
- | buzzerState = !buzzerState; | ||
- | analogWrite(BUZZER_PIN, buzzerState ? 180 : 0); | ||
- | lastBeepTime = t; | ||
- | } | ||
- | lcd.setCursor(0, 1); | ||
- | lcd.print("ALARMA! "); | ||
- | } else { | ||
- | digitalWrite(BUZZER_PIN, LOW); | ||
- | } | ||
- | lcd.setCursor(0, 0); | ||
- | lcd.printf("%02d:%02d:%02d T:%.1f ", hh, mm, ss, temp); | ||
- | if (!alarmActive && hh == alarmHour && mm == alarmMinute && ss == 0) { | + | ===== Bibliografie/Resurse ===== |
- | alarmActive = true; | + | |
- | } | + | |
- | delay(50); | ||
- | } | ||
- | void handleCommand(String cmd) { \\ | + | ==== Software ==== |
- | cmd.trim(); \\ | + | <note> |
- | if (cmd.startsWith("ALARM ")) { \\ | + | Arduino Documentation & Tutorials |
- | int sep = cmd.indexOf(':'); \\ | + | https://www.arduino.cc/en/Guide/HomePage |
- | if (sep > 0) { \\ | + | |
- | alarmHour = cmd.substring(6, sep).toInt(); \\ | + | |
- | alarmMinute = cmd.substring(sep+1).toInt(); \\ | + | |
- | Serial.println("Setat alarma la " + String(alarmHour) + ":" + String(alarmMinute)); \\ | + | |
- | btSerial.println("Setat alarma la " + String(alarmHour) + ":" + String(alarmMinute)); \\ | + | |
- | } \\ | + | |
- | } else if (cmd.startsWith("RADIO ")) { \\ | + | |
- | float freq = cmd.substring(6).toFloat() / 100; \\ | + | |
- | radio.setFrequency(freq); \\ | + | |
- | Serial.println("Radio: " + String(freq, 2)); \\ | + | |
- | btSerial.println("Radio: " + String(freq, 2)); \\ | + | |
- | } \\ | + | |
- | } \\ | + | |
- | </note> | + | PlatformIO Documentation |
+ | https://docs.platformio.org/ | ||
- | ===== Jurnal ===== | ||
- | <note tip> | + | Librăria “avr-libc” și manualul de programare AVR |
- | Puteți avea și o secțiune de jurnal în care să poată urmări asistentul de proiect progresul proiectului. | + | https://www.nongnu.org/avr-libc/user-manual/index.html |
</note> | </note> | ||
+ | ==== Hardware: ==== | ||
- | ===== Bibliografie/Resurse ===== | ||
<note> | <note> | ||
- | Listă cu documente, datasheet-uri, resurse Internet folosite, eventual grupate pe **Resurse Software** şi **Resurse Hardware**. | + | ATmega2560 Datasheet |
+ | https://ww1.microchip.com/downloads/en/DeviceDoc/ATmega2560-Data-Sheet-DS40002211A.pdf | ||
+ | |||
+ | Optimus Digital – Fise tehnice module | ||
+ | |||
+ | Modul RTC DS3231 - https://www.optimusdigital.ro/ro/altele/1102-modul-cu-ceas-in-timp-real-ds3231.html | ||
+ | |||
+ | Modul MP3 YX5200 - https://www.optimusdigital.ro/ro/audio-altele/2146-modul-de-redare-mp3-in-miniatura.html | ||
+ | |||
+ | Modul Radio RDA5807M - https://www.optimusdigital.ro/ro/wireless-radio-fm/745-modul-radio-fm-rda5807m.html | ||
+ | |||
+ | Modul Bluetooth HC-05 - https://www.optimusdigital.ro/ro/wireless-bluetooth/153-modul-bluetooth-master-slave-hc-05-cu-adaptor.html | ||
</note> | </note> | ||
<html><a class="media mediafile mf_pdf" href="?do=export_pdf">Export to PDF</a></html> | <html><a class="media mediafile mf_pdf" href="?do=export_pdf">Export to PDF</a></html> | ||