After all there is more question poping up then answers i decide to upload the pdf where is everything writen, like the wire, motor type and what we can and can't use. Here is the link pdf format of the assignment
So i am working on a project for the school, it is first time for me to work with Microcontrollers. I and i got stuck with the rest of my assignment, so i hope a good soul can help me out with this.
So my code for now is this
#include <stdio.h>
#include "dev/uart.h"
#include <util/delay.h>
#include <avr/io.h>
#include "dev/adc.h"
#define LED_RED (1 << PE5)
#define LED_YELLOW (1 << PE3)
#define BUTTON (1 << PF2)
#define SENSOR (1 << PF3)
#define PUSH (PINF & (BUTTON))
#define TILT (!(PINF & (SENSOR)))
#define JOYSTICK_X (1 << PF0)
#define JOYSTICK_Y (1 << PF1)
typedef struct
{
uint16_t x;
uint16_t y;
} inputVector;
typedef struct
{
int16_t x;
int16_t y;
} convertedVector;
void joystick_init()
{
adc_init(0x03);
}
convertedVector joystick_status()
{
inputVector newVector;
convertedVector vector;
newVector.x = adc_get(0);
newVector.y = adc_get(1);
inputVector max;
max.x = 896;
max.y = 902;
inputVector min;
min.x = 8;
min.y = 9;
vector.x = 0;
vector.y = 0;
if(newVector.x >= 498 && newVector.x <= 518)
{
vector.x = 0;
}
else if(newVector.x < 498)
{
vector.x = ((newVector.x * 100) / 901) - 100;
}
else
{
int16_t half = 896/2;
vector.x = ((newVector.x * 50) / half);
}
if(newVector.y >= 492 && newVector.y <= 512)
{
vector.y = 0;
}
else if(newVector.y < 492)
{
vector.y = ((newVector.y * 100) / 901) - 100;
}
else
{
int16_t half = 901/2;
vector.y = ((newVector.y * 50) / half);
}
printf("x: %d, y: %d\n", vector.x, vector.y);
return vector;
}
int main(void) {
uart_init(9600);
//uint8_t counter = 0;
DDRE |= (LED_RED); /* OUTPUT */
DDRE |= (LED_YELLOW);
DDRF &= ~(BUTTON); /* INPUT */
DDRF &= ~(SENSOR);
/* JOYSTICK */
DDRF &= ~(JOYSTICK_X); /* INPUT */
DDRF &= ~(JOYSTICK_Y);
joystick_init();
while (1){
if(!PUSH && !TILT)
{
PORTE |= (LED_RED);
PORTE |= (LED_YELLOW);
_delay_ms(50);
PORTE = 0;
_delay_ms(50);
}
if(PUSH && !TILT)
{
PORTE |= (LED_RED);
_delay_ms(50);
PORTE = 0;
_delay_ms(50);
}
if(!PUSH && TILT)
{
PORTE |= (LED_YELLOW);
_delay_ms(50);
PORTE = 0;
_delay_ms(50);
}
if(PUSH && TILT)
{
PORTE |= (LED_RED);
_delay_ms(500);
PORTE = 0;
PORTE |= (LED_YELLOW);
_delay_ms(500);
PORTE = 0;
}
joystick_status();
//_delay_ms(100);
}
return 0;
}
What it does now it reads the input values from joystick and it can be (x or y) min: 8 and max: 900, but we need to map it to [-100,100] this works but not perfect, so if someone can help to do the mapping better i would be grateful, but that is not the main point of my question.
But my real question is how i can programm the MCU to generate PWM signals ?
I need to implant those 3 functions :
- servo_init(), to initialize the timer/counter2;
- servo_left(int v), to set the angular velocity of the left wheel;
- servo_right(int v), to set the angular velocity of the right wheel;
There are four registers to be set during the initialization: TCCR2A, TCCR2B, OCR2A, OCR2B.So what they mean ?
The two functions servo_le f t(intv) and servo_right(intv) set the corresponding Output Compare Register to control the pulse duration of the output control signal. The input parameter v of each function is an integer in the range [−100, 100] whose absolute value is proportional to the angular velocity of the corresponding servomotor and whose sign defines the direction of rotation. As overall, these functions map the input range [−100, 100] to the output range [1.0, 2.0] ms which corresponds to the pulse duration of the control signal, thus the angular velocity and rotation direction of the servomotor. Notice that, as the two servomotors are symmetrically mounted, one of the two functions has to invert the direction of rotation.
Right wheel : PortB, pin 4, Output Compare Register : OC2A, TinkerKit Conn: O1
Left wheel : PortH, pin 6, Output Compare Register : OC2B, TinkerKit Conn: O2
I spend all day on the internet finding anything that would help out, but i didn't have any luck.