Squirrels have fuzzy tails.
0 Members and 1 Guest are viewing this topic.
void SonarInitialize(void){ //Set whether pin is input or output using direction registers //Pin C3 as an input (echo) DDRC &= ~0x08; //Pin B3 as an output (trigger) DDRB |= 0x08;}void ServoInitialize(void){ //Pin B1 as an output (trigger) DDRB |= 0x02;}void ServoScan(void){// Set up a PWM channel on Pin B1 (servo) TCCR1A = 0; ICR1 = 46079; // frequency is every 20ms //configure Timer1 for fast PWM mode vai ICR1 with pre-scaling of 8 TCCR1A = (1 << WGM11); TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS11); // Set PB1 and PB2 as outputs DDRB |= _BV(1) | _BV(2); TCCR1A |= 2 << 6; // enable PWM on port B1 in non-inverted compare mode 2 TCCR1A |= 2 << 4; // enable PWM on port B2 in non-inverted compare mode 2 while (1) { for (OCR1A=200; OCR1A < 5000; OCR1A+=100) { delayMs(1); } LEDBothOn; for (OCR1A=5000; OCR1A > 200; OCR1A-=100) { delayMs(1); } }}void SonarRanging(void){ float Distance_to_obst; uint16_t delay=0; //PRITM0 = 0x00; Timer0 on //PRR = (1 << 5); //TCCR0B = (0 << WGM02)|(0 << WGM01)|(0 << WGM00); //Pin B3 is the trigger: Bring it High //PORTB |= 0x08; PORT_ON(PORTB, 3); //delay 10 us _delay_us(12); //Bring Pin B3 low //PORTB ^= 0x08; PORT_OFF(PORTB, 3); //wait for it to go high do {} while (!(PINC & 0x08)); TCNT0=0x00; TCCR0B = START_CLK_N; do {} while (PINC & 0x08); TCCR0B = STOP_CLK; Distance_to_obst= TCNT0/58; if (1 < Distance_to_obst < 10) { LED1On; delayMs(500); LED1Off; delayMs(500); } }
//PRITM0 = 0x00; Timer0 on //PRR = (1 << 5); //TCCR0B = (0 << WGM02)|(0 << WGM01)|(0 << WGM00);
do {} while (!(PINC & 0x08));
PORT_ON(PORTB, 3);_delay_us(12);PORT_OFF(PORTB, 3);do {} while PORT_IS_OFF(PORTC, 3);TCNT0=0x00;TCCR0B = START_CLK_N; do {} while PORT_IS_ON( PORTC, 3); TCCR0B = STOP_CLK;Distance_to_obst= TCNT0/58;
#define PORT_IS_ON( port_letter, number ) ( port_letter & (1<<number) )#define PORT_IS_OFF( port_letter, number ) (!( port_letter & (1<<number) ))
#include <avr/io.h>#include <util/delay.h>int main(void){ DDRB = 0xff; volatile unsigned int i, EdgeR, EdgeF, eta; i = EdgeR = EdgeF = eta = 0; PORTB = 0x01; _delay_us(10); PORTB = 0x00; DDRB = 0x00; _delay_us(650); TCCR1B = (1<<CS00); while(i == 0) { i = PINB; } EdgeR = TCNT1; while(i != 0) { i = PINB; } EdgeF = TCNT1; eta = EdgeF-EdgeR;return 1;}