Skip to content

12. Output Devices

It should be an interesting week , where I will be testing operating a Servo motor .

In the group Assignment we worked on aa DC motor .

Design

As usual , KiCad

Routing

Gimp

Get GCode with FabModules

Design files Here

Milling

Run using OpenBuilds

A nicely finished board

Soldering

A walk in the park , quite long to be honest ,

Programming

#include <avr/io.h>
#include <util/delay.h>

#define output(directions,pin) (directions |= pin) // set port direction for output
#define set(port,pin) (port |= pin) // set port pin
#define clear(port,pin) (port &= (~pin)) // clear port pin
#define pin_test(pins,pin) (pins & pin) // test for port pin
#define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set
#define position_delay() _delay_ms(1000)

#define PWM_port PORTA
#define PWM_pin (1 << PA7)
#define PWM_direction DDRA

#define Button_port PORTB
#define Button_direction DDRB
#define Button_pins PINB
#define Button_pin (1 << PB2)

int main(void) {
   //
   // main
   //
   // set clock divider to /1
   //
   CLKPR = (1 << CLKPCE);
   CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
   //
   // set up timer 1
   //
   TCCR1A = (1 << COM1A1) | (0 << COM1A0); // clear OC1A on compare match
   TCCR1B = (0 << CS12) | (1 << CS11) | (0 << CS10) | (1 << WGM13); // prescaler /8, phase and frequency correct PWM, ICR1 TOP
   ICR1 = 25000; // 20 ms frequency
   //
   // set PWM pin to output
   //
   clear(PWM_port, PWM_pin);
   output(PWM_direction, PWM_pin);

   // enable pullup on button pin
   set(Button_port, Button_pin);

   while (1) 
   {

     if (!pin_test(Button_pins,Button_pin))
     {
        OCR1A = 1875;
        position_delay();
      }
     else
     {
         OCR1A = 1250;
        position_delay();
     }
  }
}

Set as a small angle , because that’s what I needed .


Last update: July 1, 2021