Differences

This shows you the differences between two versions of the page.

Link to this comparison view

pm:prj2026:andrei.batasev:dorian.gilca [2026/05/25 09:42]
dorian.gilca
pm:prj2026:andrei.batasev:dorian.gilca [2026/05/25 09:55] (current)
dorian.gilca
Line 22: Line 22:
  
 **Block Diagram:** **Block Diagram:**
 +
 +
 {{:​pm:​prj2026:​andrei.batasev:​schema_bloc.jpg?​300|}} {{:​pm:​prj2026:​andrei.batasev:​schema_bloc.jpg?​300|}}
  
Line 36: Line 38:
  
 **Electrical Schematic:​** **Electrical Schematic:​**
 +
 +
 +
 {{:​pm:​prj2026:​andrei.batasev:​schema_electrica.jpg?​300|}} {{:​pm:​prj2026:​andrei.batasev:​schema_electrica.jpg?​300|}}
  
Line 41: Line 46:
 ===== Software Design ===== ===== Software Design =====
  
 +<code c>
 +#include <​avr/​io.h>​
 +#include <​avr/​interrupt.h>​
 +#include <​util/​delay.h>​
 +#include <​stdio.h>​
 +
 +#define PM_BAUD 9600
 +
 +#define TRIG_PIN PC0
 +#define ECHO_PIN PC1
 +
 +#define LATCH_PIN PB4
 +#define CLK_PIN ​  PD4
 +#define EN_PIN ​   PD7
 +#define DATA_PIN ​ PB0
 +
 +#define M1_A 2
 +#define M1_B 3
 +#define M2_A 1
 +#define M2_B 4
 +
 +volatile uint16_t timer_val = 0;
 +volatile uint8_t echo_done = 0;
 +
 +static int _usart0_putchar(char c, FILE *stream) {
 +    if (c == '​\n'​) _usart0_putchar('​\r',​ stream);
 +    while (!(UCSR0A & (1<<​UDRE0)));​
 +    UDR0 = c;
 +    return 0;
 +}
 +
 +static FILE USART0_stdout = FDEV_SETUP_STREAM(_usart0_putchar,​ NULL, _FDEV_SETUP_WRITE);​
 +
 +void usart_init() {
 +    UBRR0H = (unsigned char)(103>>​8);​ //br9600
 +    UBRR0L = (unsigned char)103;
 +    UCSR0B = (1<<​RXEN0) | (1<<​TXEN0);​ //act transmisia
 +    UCSR0C = (1<<​USBS0) | (3<<​UCSZ00);​ //8b
 +    stdout = &​USART0_stdout;​
 +}
 +
 +void shift_out_data(uint8_t data) {
 +    PORTB &= ~(1<<​LATCH_PIN); ​
 +    ​
 +    for(int i=0; i<8; i++) { //pt fiecare bit
 +        PORTD &= ~(1<<​CLK_PIN);​
 +        if(data & (1 << (7-i))) {
 +            PORTB |= (1<<​DATA_PIN);​ //punem val d
 +        } else {
 +            PORTB &= ~(1<<​DATA_PIN);​
 +        }
 +        PORTD |= (1<<​CLK_PIN);​ //ridicam si coboram pin clock
 +    }
 +    ​
 +    PORTB |= (1<<​LATCH_PIN); ​
 +}
 +
 +void motors_init() {
 +    DDRB |= (1<<​LATCH_PIN) | (1<<​DATA_PIN) | (1<<​PB3);​
 +    DDRD |= (1<<​CLK_PIN) | (1<<​EN_PIN) | (1<<​PD3);​
 +
 +    PORTD &= ~(1<<​EN_PIN);​
 +
 +    TCCR2A = (1<<​COM2A1) | (1<<​COM2B1) | (1<<​WGM21) | (1<<​WGM20);​
 +    TCCR2B = (1<<​CS22);​
 +    ​
 +    OCR2A = 0; // punem viteza in registrii
 +    OCR2B = 0;
 +
 +    shift_out_data(0);​
 +}
 +
 +void set_motor_dir(int m1_fwd, int m1_rev, int m2_fwd, int m2_rev) {
 +    uint8_t shift_data = 0;
 +    if (m1_fwd) shift_data |= (1<<​M1_A);​
 +    if (m1_rev) shift_data |= (1<<​M1_B);​
 +    if (m2_fwd) shift_data |= (1<<​M2_A);​
 +    if (m2_rev) shift_data |= (1<<​M2_B);​
 +    shift_out_data(shift_data);​
 +}
 +
 +void sonar_init() {
 +    DDRC |= (1<<​TRIG_PIN);​ //pc0 output
 +    DDRC &= ~(1<<​ECHO_PIN); ​
 +
 +    TCCR1A = 0;
 +    TCCR1B = (1<<​CS11);​
 +
 +    PCICR |= (1<<​PCIE1);​
 +    PCMSK1 |= (1<<​PCINT9);​
 +}
 +
 +ISR(PCINT1_vect) {
 +    if (PINC & (1<<​ECHO_PIN)) {
 +        TCNT1 = 0;
 +    } else {
 +        timer_val = TCNT1;
 +        echo_done = 1;
 +    }
 +}
 +
 +int get_distance() {
 +    echo_done = 0;
 +    ​
 +    PORTC &= ~(1<<​TRIG_PIN);​
 +    _delay_us(2);​
 +    PORTC |= (1<<​TRIG_PIN);​
 +    _delay_us(10);​
 +    PORTC &= ~(1<<​TRIG_PIN);​
 +
 +    uint32_t timeout = 60000;
 +    while(!echo_done && timeout > 0) {
 +        timeout--;
 +    }
 +
 +    if(timeout == 0) return 400;
 +
 +    return timer_val / 116;
 +}
 +
 +int map_speed(int x, int in_min, int in_max, int out_min, int out_max) {
 +    return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
 +}
 +
 +int main() {
 +    usart_init();​
 +    motors_init();​
 +    sonar_init();​
 +    ​
 +    sei();
 +    printf("​ACC System Bare Metal Started\n"​);​
 +
 +    int safe_dist = 25;
 +    int max_dist = 85;
 +
 +    while(1) {
 +        int dist = get_distance();​
 +        if(dist == 0 || dist > 400) dist = 400;
 +
 +        printf("​Dist:​ %d cm\n", dist);
 +
 +        if (dist > max_dist) {
 +            set_motor_dir(0,​ 0, 0, 0);
 +            OCR2A = 0;
 +            OCR2B = 0;
 +        } else if (dist > safe_dist) {
 +            int speed = map_speed(dist,​ safe_dist, max_dist, 115, 220);
 +            set_motor_dir(1,​ 0, 1, 0);
 +            OCR2A = speed;
 +            OCR2B = speed;
 +        } else {
 +            set_motor_dir(0,​ 0, 0, 0);
 +            OCR2A = 0;
 +            OCR2B = 0;
 +        }
 +
 +        _delay_ms(50);​
 +    }
 +    return 0;
 +}
 +</​code>​
 **Development Environment:​** **Development Environment:​**
   * VS Code with PlatformIO (using pure C and register-level programming).   * VS Code with PlatformIO (using pure C and register-level programming).
Line 72: Line 238:
  
 ===== Download ===== ===== Download =====
-//code//+{{:​pm:​prj2026:​andrei.batasev:​dorian_gilca.zip|Project Archive - Source Code and PlatformIO Configuration}}
  
 ===== Journal ===== ===== Journal =====
pm/prj2026/andrei.batasev/dorian.gilca.1779691349.txt.gz · Last modified: 2026/05/25 09:42 by dorian.gilca
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0