본문 바로가기

만들기 / making/sensing workshop

Day 5: Ultrasonic Sensor

Day 5: Ultrasonic Sensor

인터럽트를 쓰지 않았을 때 비효율 적인 연산이 일어남을 보여주는 소스

#include <avr/io.h>
#include <util/delay.h>


#define LED 5
#define output_low(port,pin) port &= ~(1<<pin)
#define output_high(port,pin) port |= (1<<pin)
#define set_input(portdir,pin) portdir &= ~(1<<pin)
#define set_output(portdir,pin) portdir |= (1<<pin)


#define FOSC 16000000
#define BAUD 9600
#define UBRR FOSC/16/BAUD-1

void init_USART0(unsigned int baud)
{
UBRR0 = baud; // Set Baudrate
UCSR0C = (3<<UCSZ00); // Character Size 8 bit
UCSR0B |= _BV(RXEN0) | _BV(TXEN0); // Receiver and Transmitter Enable
}


void setRXCIE_USART0()
{
UCSR0B |= _BV(RXCIE0);
}


unsigned char receive_1byte_USART0(void)
{
loop_until_bit_is_set(UCSR0A, RXC0);
return UDR0;
}


void transmit_1byte_USART0(unsigned char data)
{
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = data;
}



void transmit_str_USART0(char *str)
{
while (*str != 0) {
transmit_1byte_USART0(*str);
*str++;
}
}



void transmit_4digit_USART0(int num)
{
unsigned char temp;
int digit = 1000;


while (digit != 0) {
temp = num / digit;
transmit_1byte_USART0('0'+temp);
num -= (digit*temp);
digit /= 10;
}
}



int main()
{
set_output(DDRC, LED);
init_USART0(UBRR); // initialize USART0


while (1) {
if ('a' == receive_1byte_USART0()) {
transmit_str_USART0("Hello World!\r\n");
output_high(PORTC, LED);
_delay_ms(1000);

output_low(PORTC, LED);
_delay_ms(1000);


}
}


return 0;
}

초음파 센서 연결도

초음파센서는 hagisonic.com 사의  HG-M40DC 센서를 사용합니다.


MCU Code

//-----------------------------------------------------------------------------
//  ATmega168
//  Internal 16 MHz RC Osc.
//  (1/16M = 62.5 ns)
//-----------------------------------------------------------------------------

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "hhjjj168.h"

#define LED 5

#define output_low(port,pin) port &= ~(1<<pin)
#define output_high(port,pin) port |= (1<<pin)
#define set_input(portdir,pin) portdir &= ~(1<<pin)
#define set_output(portdir,pin) portdir |= (1<<pin)

#define TCNT1_InitVal   0  // set timer its initial value

volatile unsigned int timeunit = 0;

#define FOSC 16000000
#define BAUD 9600
#define UBRR FOSC/16/BAUD-1

volatile char flag = 0;




ISR(INT0_vect)
{
if (flag)
{
timeunit= TCNT1;
flag = 0;
}

}
/*
ISR(TIMER1_OVF_vect)
{
    TCNT1 = TCNT1_InitVal;  // Clear Timer/Counter1

   
}

*/

void extintInit() 

  EICRA = (1<<ISC01 | 1<<ISC00); //EXT Triggers on rising edge of PCINT0 
  EIMSK = (1<<INT0); //Enables NT0 
  


void extINT0enable()
{
EIMSK = (1<<INT0); //Enables NT0
}

void extINT0disable()
{
EIMSK = 0x00; //Disalbes NT0
}
void init_Timer1()
{
    TCCR1B = 0x00;  // Stop Timer/Counter1
    TCNT1 = TCNT1_InitVal;  // Initial Value of Timer/Counter1
    //TIMSK1 = _BV(TOIE1);    // Timer/Counter1 Overflow Interrupt Enable
}

//-----------------------------------------------------------------------------
//  Main
//-----------------------------------------------------------------------------
int main()
{
set_input(DDRD,PD2);
    set_output(DDRC,PC5);

init_Timer1();
init_USART0(UBRR); //initialize USART0

extintInit();
sei();          // Global Interrupt Enable

output_low(PORTC, PC5);
_delay_ms(100);
flag = 0;
    while (1) 
{

float temp;
unsigned int distance ;
output_high(PORTC, PC5);
TCNT1=0;
TCCR1B = 0x03; // Start Timer/Counter1 clk/64 62.5nsX64=4us timer1 overflow in 65536 * 4us = 262.144ms
_delay_ms(2);
output_low(PORTC, PC5);
flag = 1;
_delay_ms(30);
temp = ((float)timeunit*1.36-300)/2;

transmit_digit_USART0((unsigned int)timeunit-400);
transmit_1byte_USART0('\n');

    }

    return 0;
}


Processing Code

import processing.serial.*;
Serial myPort;  // Create object from Serial class
String myString = null;
int cnt = 0;
void setup() 
{
  //size(200, 200);
  
  // List all the available serial ports
  println(Serial.list());
  String portName = Serial.list()[1];
  myPort = new Serial(this, portName, 9600);
}

void draw() 
{

  while (myPort.available() > 0) 
  {  
    myString = myPort.readStringUntil('\n');
    if (myString != null) 
    {
      println(myString);
      
    }
  }
}

'만들기 / making > sensing workshop' 카테고리의 다른 글

audio sensor MCU code  (0) 2009.08.31
Day 7: Audio Sensor  (0) 2009.08.31
ATmega168 USART  (0) 2009.08.31
Day 4: InfraRed Sensor  (0) 2009.08.31
ATmega168 ADC 3  (0) 2009.08.31