avr128 온도센서 예제

#include <avr/io.h>
#include "delay.h"
#include <stdio.h>

void Putch(char);

int main()
{
char str[20];
char *pStr ;

int adc;
int temp;        

ADMUX = 0X40;                //ADC1 단극성 입력, ADC1
ADCSRA = 0XE7;                //ADEN = 1, ADFR =1, ADSC =1, 128분주
delay_ms(5);

//통신
UCSR0A = 0x0;
UCSR0B = 0b00001000; //TXEN0 송신 인에이블
UCSR0C = 0b00000110; //비동기 모드 ,패리트(x),정지1비트,8비트,클럭 극성
                 //   0  0        00                 011        0
UBRR0H = 0;             //16MHz 일때 BAUD = 9600    
UBRR0L = 103;

delay_ms(100);

while(1)
{


    adc = (int) ADCL +((int)ADCH << 8);    //ADC 변환값 읽기
    temp = adc*100.0/204.8;
    
    pStr = str;
    sprintf(str, "%d\n\r" , temp);
    

    while(*pStr)
    {
        Putch(*pStr++ );  //문자열 전송
    
    }
    delay_ms(5000);
}
}

void Putch(char data)
{    
    
        while(!(UCSR0A & 0x20));   //송신 버퍼 UDR0이 비어 있어 새로운 데이터를 받을
        UDR0 = data;               //준비가 되었을 때 set 됨.
        
}  

by honam | 2009/12/03 09:48 | avr | 트랙백 | 덧글(0)

 

QT QextSerialPort 를 이용한 avr128 통신

port = new QextSerialPort("COM1");

        port->setBaudRate(BAUD9600);
        port->setDataBits(DATA_8);
        port->setParity(PAR_NONE);
        port->setStopBits(STOP_1);
        port->setFlowControl(FLOW_OFF);


bool res = false;
res = port->open(QextSerialPort::ReadWrite);

if(res)
QMessageBox::information(this, "honam", "con ok!!");

else
QMessageBox::information(this, "honam", "con Error!!");




QMessageBox::information(this, "honam", "con test!!");


port->putChar('a');
port->putChar('b');
port->putChar('c');
port->putChar('d');

port->close();
---------------------------------------avr -------------------------------
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>

#include "delay.h"

void Putch(char);

char Getch();

int main()
{
int led=0;

 DDRA=0X0FF;

// PORTA=0X00;
 UCSR0A =0X0;
 UCSR0B = 0B00010000; 
 UCSR0C = 0B00000110;
 UBRR0H=0;        
 UBRR0L=103; // 16MHz 에서 9600

 delay_ms(1000);

 while(1)
 {
 led=Getch();
  delay_ms(1000);

  switch(led)
  {
           case 'a':
            PORTA=0x01;
            delay_ms(500);
            break;
        case 'b':
            PORTA=0x02;
            delay_ms(500);
        break;
        case 'c':
            PORTA=0x04;
            delay_ms(500);
            break;
        case 'd':
            PORTA=0x08;
            delay_ms(500);
            break;
        case 'e':
            PORTA=0x10;
            delay_ms(500);
            break;
        case 'f':
            PORTA=0x20;
            delay_ms(500);
            break;
        case 'g':
            PORTA=0x00;
            delay_ms(500);
            break;
    }
 

  delay_ms(1000);
 }
}

char Getch(void)
{
 while(!(UCSR0A & 0x80));
 return UDR0;
}

by honam | 2009/12/02 11:55 | QT | 트랙백 | 덧글(0)

 

◀ 이전 페이지다음 페이지 ▶