• Welcome to LusoRobótica - Robótica em Português.
 

sfr05

Started by tiagosilva510, 29 de May de 2011, 12:38

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

tiagosilva510

e como faço agora pra programar os certos?

senso

Depende do que fizeste.
Não consegues nem ler os fuses?
Tens outro Atmega?
Se sim, faz um programa em que simplesmente toglas um pino, ligas esse pino ao pino Xtal1 do atmega bloqueado que tens, e ligas tambem a massa dos dois em conjunto, depois tentas ler a assinatura, se funcionar é porque tens os fuses errados no que toca a cristal/oscilador externo, se não ler, é porque desligas-te o ISP.
Avr fanboy

tiagosilva510

ja esta a funcionar, obrigado senso

tiagosilva510

agora passei ao passo seguinte.. ja coloquei o triger a funcionar.. estou a trabalhar no echo

#define F_CPU 1000000
#include <stdio.h>
#include <stdlib.h>
#include <avr/io.h>
#include <util/delay.h>

void startRange(void);
unsigned int echo(void);
void setup(void);

int main(void)
{
setup();

//unsigned int range;

while(1)
{
startRange(); // Send a high on the trigger pin to start a ranging
echo();
}
}

void startRange(void)
{
PORTD = (1<<DDD0); // Send trigger pin high
_delay_us(10);     // Wait 10us...
PORTD = (0<<DDD0); // Send trigger pin low
//_delay_us(10);
}

void setup(void)
{
DDRD = 0b00000001; // Port D pin 0 ouput all others input

}


unsigned int echo(void)
{
unsigned int range;
while((PIND = (1<<DDD1))); // Wait for echo pin to go high,  this indicates the start of the incoming pulse
//TCNT0 = 0x00;
//TCCR0B =0b00000100;
while((PIND = (0<<DDD1))); // Wait for echo pin to go low signaling that the pulse has ended
//TCCR0B = 0x08;
range = TCNT1/29; // Read back value in the timer counter register, this number divided by 29 will give us the range in CM

return(range);
}


preciso de ajuda com esses contadores.. pk faz-me muita confusao a maneira de funcionar os contadores.. n percebo muito de como os por a funcionar..

Cumprimentos

tiagosilva510

nng da uma ajuda?

senso

Dá uma leitura no meu tutorial sobre timers:
http://hekilledmywire.wordpress.com/2011/05/28/introduction-to-timers-tutorial-part-6/

É que não configuras nada de nada dos timers e estás á espera que eles como que por milagre funcionem
Avr fanboy

tiagosilva510

vou hoje mesmo ler o teu tutorial :)

agradeço desde ja a ajuda senso ;)

tiagosilva510

nao estou a conseguir gerar uma interrupção com 1micro segundo.. alguem me ajuda? acho que estou a fazer as contas mal

senso

Avr fanboy

tiagosilva510


tcustodio

segundo o código fonte das funções de delay um delay tão pequeno é impossível...

Quote
void delayMicroseconds(unsigned int us)
{
   // calling avrlib's delay_us() function with low values (e.g. 1 or
   // 2 microseconds) gives delays longer than desired.
   //delay_us(us);

#if F_CPU >= 16000000L
   // for the 16 MHz clock on most Arduino boards

   // for a one-microsecond delay, simply return.  the overhead
   // of the function call yields a delay of approximately 1 1/8 us.
   if (--us == 0)
      return;

   // the following loop takes a quarter of a microsecond (4 cycles)
   // per iteration, so execute it four times for each microsecond of
   // delay requested.
   us <<= 2;

   // account for the time taken in the preceeding commands.
   us -= 2;
#else
   // for the 8 MHz internal clock on the ATmega168

   // for a one- or two-microsecond delay, simply return.  the overhead of
   // the function calls takes more than two microseconds.  can't just
   // subtract two, since us is unsigned; we'd overflow.
   if (--us == 0)
      return;
   if (--us == 0)
      return;

   // the following loop takes half of a microsecond (4 cycles)
   // per iteration, so execute it twice for each microsecond of
   // delay requested.
   us <<= 1;
    
   // partially compensate for the time taken by the preceeding commands.
   // we can't subtract any more than this or we'd overflow w/ small delays.
   us--;
#endif

   // busy wait
   __asm__ __volatile__ (
      "1: sbiw %0,1" "\n\t" // 2 cycles
      "brne 1b" : "=w" (us) : "0" (us) // 2 cycles
   );
}


ali pouco depois do início:
Quote
// for a one-microsecond delay, simply return.  the overhead
   // of the function call yields a delay of approximately 1 1/8 us.
   if (--us == 0)
      return;

:/
Tiago Custódio,
- Não me dêem álcool se estiver um piano por perto.

senso

Basta usar um timer de 8 bits, com prescaler a 1 e meter count até 16, mas isso não te dá muito tempo para executar código é usar o modo CTC a fazer o toogle to pino.

Podes fazer delay de 4 instruções ou mais usando o _delay_loop2(unsigned int val)
Avr fanboy

tiagosilva510

o varrimento de leds ja esta feito... agora queria por um buzzer a apitar com maior frequencia qunato menor for a distancia...

#define F_CPU 1000000
#include <stdio.h>
#include <stdlib.h>
#include <avr/io.h>
#include <util/delay.h>
#include<interrupt.h>

unsigned int distancia=0,distancia_1=0;
unsigned char ler_pino=0,contador=800;

int cont=0;
void startRange(void);
unsigned int echo(void);
void setup(void);


/*ISR(TIMER0_COMPA_vect) {
contador--; //decrementa o contador até chegar a 0 (500ms)
if(contador==0) {
contador=800;
PORTD=~PORTD;// vai negar o estado do led, se estiver aceso apaga e vice versa
}
}*/



int main(void)
{
setup();



while(1)
{
startRange(); // Send a high on the trigger pin to start a ranging
distancia=echo();
distancia_1=((distancia*6)/58);

if(distancia_1>=650){
PORTB = 0b00000001;
// OCR0A=170;
}
if(distancia_1<550){
PORTB = 0b00000011;
// OCR0A=150;
}
if(distancia_1<450){
PORTB = 0b00000111;
// OCR0A=120;
}
if(distancia_1<350){
PORTB = 0b00001111;
// OCR0A=100;
}
if(distancia_1<250){
PORTB = 0b00011111;
// OCR0A=70;
}
if(distancia_1<150){
PORTB = 0b00111111;
// OCR0A=40;
}
if(distancia_1<70){
PORTB = 0b01111111;
// OCR0A=15;

}
_delay_ms(300);
}
}



void startRange(void)
{
PORTC =0b00000001 ; // Send trigger pin high
_delay_ms(15);     // Wait 10us...
PORTC = 0b00000000; // Send trigger pin low
}

unsigned int echo(){
unsigned int cont_sonar=0;
ler_pino=PINC&0b00000010;
while(ler_pino!=0b00000010)
ler_pino=PINC&0b00000010;
while(ler_pino!=0b00000000)
{
cont_sonar++;
ler_pino=PINC&0b00000010;
}

return cont_sonar;
}
void setup(void)
{
DDRC = 0b00000001; // Port D pin 0 ouput all others input
DDRB = 0b01111111;
PORTB = 0b00000000;
DDRD = 0b00000001;
TIMSK0=TIMSK0|2; //habilita a flag de interrupção do TC0
SREG=SREG|0X80; //HABILITA AS INTERRUPÇOES GLOBAIS
TCCR0A=0b10000011; //Timer0A
TCCR0B=0b00000100; //prescaler 256
}


tou a tentar fazer isso atravez do OCR0A mas n tou a conseguir.. alguem me ajuda?