/*****************************
Servo
******************************
Program by : Magvitron
version : 1.0.0.0 (beta)
Assembled in : winavr
core : mega 8 (8k)
fuse : 8Mhz internal rc
rst disabled
AVR Studio : 4.15.623
GUI Version : 4, 15, 0, 623
AVR Simulator: 1, 0, 2, 1
ATMEGA16 247
date : 12 feb 2013 20.44
*******************************/
/*******************************
Preprovcessor Directive
*******************************/
#define F_CPU 8000000UL
#include<avr/io.h>
#include<util/delay.h>
/******************************/
#define motor PORTB
#define servo PB6
int k;
void servo_to(unsigned int k);
long map(long x, long in_min, long in_max, long out_min, long out_max);
/*******************************
uart definitions
*******************************/
void usart_init();
unsigned int usart_getch();
#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
int main()
{
DDRB=0xff;
while(1)
{usart_init();
unsigned int degree_value,time;
//_delay_ms(1000);
usart_putch('H');
usart_putch('e');
usart_putch('l');
usart_putch('l');
usart_putch('o');
degree_value=usart_getch();
degree_value=(degree_value-48)*10;
usart_putch('D');
usart_putch(':');
convert(degree_value,2);
usart_putch(' ');
for(time=0;time<30;time++)
{
servo_to(degree_value);
}
/*
degree_value=180;
for(degree_value=0;degree_value<180;degree_value +=5)
for(time=0;time<50;time++)
{
servo_to(degree_value);
}
// _delay_ms(30);
for(degree_value=180;degree_value>0;degree_value -=5)
for(time=0;time<50;time++)
{
servo_to(degree_value);
}
// _delay_ms(30);
*/
}
}
void servo_to(unsigned int k)
{
k=50+(k*10);
motor= (1<< servo);
_delay_us(k);
motor = (0<<servo);
_delay_ms(18);
}
long map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
/*******************************
uart thingies
*******************************/
unsigned int usart_getch()
{
while ((UCSRA & (1 << RXC)) == 0);
// Do nothing until data have been received and is ready to be read from UDR
return(UDR); // return the byte
// serial =UDR;
}
void usart_init()
{
UCSRB |= (1<<RXCIE) | (1 << RXEN) | (1 << TXEN); // Turn on the transmission reception ..
// circuitry and receiver interrupt
UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes
UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value..
// into the low byte of the UBRR register
UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value..
// into the high byte of the UBRR register
}
void usart_putch(unsigned char send)
{
while ((UCSRA & (1 << UDRE)) == 0); // Do nothing until UDR is ready..
// for more data to be written to it
UDR = send; // Send the byte
}
/**********************************************
conversions lcd and the uart
**********************************************/
void convert(unsigned int value,unsigned int numb)
{
unsigned char text[numb];
unsigned char t,temp_char;
for(t=1;t<=numb;t++)
{
temp_char = value%10;
value=value/10;
text[t]=temp_char+48;
}
for(t=numb;t>=1;t--)
{
usart_putch(text[t]);//=text[t];
}
}