hy guys,
i'm working on a new project that involves an ATMEGA 8 and a sharp ir senzor.i'm trying to read data from the senzor and then transmit that data to my pc on a hyperterminal ussing max232.I really dont know whats wrong with my code.when i connect the board to my pc on the the hiperterminal doesnt show numebers, it shows all sorts of simbols sometimes even smily faces.
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include <inttypes.h>
#include<util/delay.h>
#define F_OSC 1000 /* oscillator-frequency in Hz */
#define UART_BAUD_RATE 2400
#define UART_BAUD_CALC(UART_BAUD_RATE,F_OSC) ((F_OSC)/((UART_BAUD_RATE)*16l)-1)
unsigned int ii = 0 ;
unsigned int y = 0;
unsigned int val;
#ifndef F_CPU
//definim viteza cpu-ului daca nu e def
#define F_CPU 1000000
#endif
#define BAUDRATE 2400 //setam baud rate
//calcul Usart Baud Rate Register UBRR
#define UBRRVAL ((F_CPU/(BAUDRATE*16UL))-1)
SIGNAL (SIG_OVERFLOW1){
ii=ii+1;
if (ii==40) {
if(y==0){
PORTD |= 0b01000000;
ii=0;
y=1;
}
else {
PORTD &= 0b10100000;
ii=0;
y=0;
}
}
}
void initializare(){
ADCSRA|= 0b11100000; //
ADMUX |= 0b01000000; //
SREG |= 0b10000000; //global interrupt enable
DDRB |= 0b00000010; //activare portul PB1
DDRD |= 0b11100000; //activare porturi PD5 (rosu), PD6 (galben), PD7 (verde)
TIMSK |= 0b00000101; //Timer/Counter Interrupt Mask Register
}
void USART_Init()
{
//Setam baud rate
UBRRL=UBRRVAL; //bit inferior
UBRRH=(UBRRVAL>>8); //bit sup
//Setam data frame format: asynchronous mode,no parity, 1 stop bit, 8 bit size
UCSRC=(1<<URSEL)|(0<<UMSEL)|(0<<UPM1)|(0<<UPM0)|
(0<<USBS)|(0<<UCSZ2)|(1<<UCSZ1)|(1<<UCSZ0);
//Enable Transmitter and Receiver
UCSRB=(1<<RXEN)|(1<<TXEN);
}
void trimite_USART(int Temp)
{int a[3];
for(int i=2;i>=0;i--)
{
a[i]=Temp%10;
Temp=Temp/10;
}
for(int i=0;i<=2;i++)
{
while (!(UCSRA&(1<<UDRE))){};
UDR=a[i];
}
}
int activare()
{
int temperatura=0;
uint16_t adc_valoare=0;
adc_valoare=ADCL;
adc_valoare += (ADCH<<8);
temperatura=(ADCH256+ADCL)*4.8/1024;
return temperatura;
}
int main(void) {
USART_Init(); // init USART
int Temperatura;
initializare();
while (1) {
Temperatura=activare();
trimite_USART(Temperatura);
_delay_ms(2000);
}
}