0% found this document useful (0 votes)
3 views

lab7

The document is a code snippet for controlling an HXT900 servo using an ATmega328PB microcontroller. It sets up Timer 0 for PWM output on pin PD5 and cycles through four predefined servo positions. The code continuously updates the servo position with a delay of 100 milliseconds between each change.

Uploaded by

rowen prather
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

lab7

The document is a code snippet for controlling an HXT900 servo using an ATmega328PB microcontroller. It sets up Timer 0 for PWM output on pin PD5 and cycles through four predefined servo positions. The code continuously updates the servo position with a delay of 100 milliseconds between each change.

Uploaded by

rowen prather
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

#define F_CPU 16000000UL

#include <avr/io.h>

#include <util/delay.h>

static uint8_t posit[4]={0,45,-45,45}; //position of servo

int main ( void )

{//https://docplayer.net/100038445-This-about-using-an-atmega328pb-xplained-mini-to-drive-an-
hxt900-servo-with-a-16-bit-timer-see-link-1-at-the-end-of-this-document.html

// CONNECT THE SERVO https://www.avrfreaks.net/forum/tut-atmega328pb-8-bit-timer-servo-pwm-


hxt900

DDRD |= (1 << DDD5); //Set pin PD5 as output for OC0B

// Timer 0 setup pwm

TCCR0A = 0x21; // Set OCR2B Table 18-8 in Datasheet. & Table 18-9.

TCCR0B = 8; // Set WMG0[2]; tABLE 18-9

TCNT0 = 0; // clear counter

OCR0A = 156; // 20 ms period = 20000 us.

OCR0B = 4; //

TCCR0B |= 5; //set clock to divide by 1024 and start tALBE 18-10

while (1)

for (uint8_t ii = 0;ii < 4;ii++) // cycle through the positions low - middle -

//high - middle - low

OCR0B = posit[ii]; //sets the new position

_delay_ms(100);

You might also like