Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

request help on connecting computer to microcontroller(atmega8l)

Status
Not open for further replies.

vvarlord

Member level 4
Joined
Mar 15, 2012
Messages
74
Helped
4
Reputation
8
Reaction score
3
Trophy points
1,288
Activity points
1,756
i'm trying to send and recieve data over an serial protocol between micro and computer
i recieved data in micro successfully,but the problem is when i write a program to send data to computer
i use matlab for sending and recieving thorough rs232 & use codevisionavr to programg the micro
after i set the baud rate and com number and opened the port,, i use scanf(//serial port name),,,, but instead of getting what's inteded i get this error: a timeout occured before the terminator was reached.

it will be appreciated if can anyone help.
 

bigdogguru

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,274
Trophy points
1,413
Location
Southwest, USA
Activity points
62,505
The error message you are receiving could be due to several reasons, however without examining your code I would just be guessing.

Can you post both your MATLAB and AVR code?


BigDog
 

jasonc2

Full Member level 4
Joined
Jun 23, 2012
Messages
204
Helped
25
Reputation
52
Reaction score
25
Trophy points
1,328
Location
NYC, USA
Activity points
3,415
Sscanf won't process the data until the terminator character (which you can specify) arrives on the line. Either make sure your device is sending the expected terminator character as necessary, or change the terminator character to something else. That's the easy first thing to check.

Failing that, your next step is to verify that the data is actually being sent to the computer.

If you can, grab a scope and check your RS232 lines for activity. If you see none, check your hardware/wiring/firmware.

Then, grab a terminal emulator like Termite and use it to view the incoming data. If you see none, you'll have some debugging to do (firmware bug, faulty wiring, incorrect baud rate, timing, etc.). If you see data, verify that it is what you expect.

If you see the data in your terminal emulator and it looks like what you are expecting, then the problem lies in your Matlab receiving code. I'm not familiar with Matlab. If you are sure the data is arriving and is in the expected format, check the Matlab documentation, make sure you are using the serial API correctly, see if you can find some samples that work.

Hope that helps,
Jason
 

vvarlord

Member level 4
Joined
Mar 15, 2012
Messages
74
Helped
4
Reputation
8
Reaction score
3
Trophy points
1,288
Activity points
1,756
this is the codevisionavr code,,,,it's simple but i want to start with simple working codes
even this doen't work


Code:
/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.3 Standard
Automatic Program Generator
© Copyright 1998-2011 Pavel Haiduc, HP InfoTech s.r.l.
[url]http://www.hpinfotech.com[/url]

Project : 
Version : 
Date    : 12/18/2012
Author  : PerTic@n
Company : If You Like This Software,Buy It
Comments: 


Chip type               : ATmega8L
Program type            : Application
AVR Core Clock frequency: 1.000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 256
*****************************************************/

#include <mega8.h>

// Alphanumeric LCD functions
#include <alcd.h>

// Standard Input/Output functions
#include <stdio.h>
#include <delay.h>
// Declare your global variables here
int a=0;
unsigned char h,s[];
void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port B initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out 
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 
PORTB=0x00;
DDRB=0xFF;

// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: Off
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 2400
UCSRA=0x00;
UCSRB=0x08;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x19;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC disabled
ADCSRA=0x00;

// SPI initialization
// SPI disabled
SPCR=0x00;

// TWI initialization
// TWI disabled
TWCR=0x00;

// Alphanumeric LCD initialization
// Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTB Bit 7
// RD - PORTB Bit 6
// EN - PORTB Bit 5
// D4 - PORTB Bit 4
// D5 - PORTB Bit 2
// D6 - PORTB Bit 1
// D7 - PORTB Bit 3
// Characters/line: 16
lcd_init(16);

while (1)
      {
      sprintf(s,"%d",a);
      a=a+1;
      lcd_puts(s);
      h=a;
      putchar(h);  
      
      delay_ms(7000);
      
      lcd_clear();}
}
- - - Updated - - -

and the matlab code is simple too:

Code:
s=serial('com6')
set(s,'baudrate',2400)
fopen(s)
fscanf(s)
fclose(s)
- - - Updated - - -

thsnks jason
i'm goin' to check all that and ,will post the results
 
Last edited by a moderator:

jasonc2

Full Member level 4
Joined
Jun 23, 2012
Messages
204
Helped
25
Reputation
52
Reaction score
25
Trophy points
1,328
Location
NYC, USA
Activity points
3,415
I did not look at your code in detail nor do I know if you are having other hardware issues but what stands out to me is your usage of fscanf to read a sequence of raw binary data coming in the port (0x01, 0x02, 0x03, and so on). I will assume that data is in fact arriving at your PC intact.

MATLAB fscanf: http://www.mathworks.com/help/matlab/ref/serial.fscanf.html
MATLAB Terminator: http://www.mathworks.com/help/matlab/matlab_external/terminator.html

The scanf family of functions is generally used for reading ASCII coded text. Although technically according to MATLAB's documentation it should work for you, your usage of it is unusual.

In any case the real problem seems to be exactly what the error message described - fscanf timed out waiting for the terminator character to appear on the line. According to the MATLAB documentation for Terminator, scanf won't complete until the terminator character is read. The default terminator is linefeed (byte value 0x0A), and so scanf won't complete until it sees an 0x0A. In your code, that won't happen for 9 * 7000ms = 63 seconds, which is probably greater than scanf's timeout.

You either need to delimit output bytes with the correct terminator character (e.g. putchar(h) then putchar(0x0A) each time through - beware when h==0x0A however), or read your data using functions designed for binary input. The latter is probably the approach you want given the nature of your data. You may wish to use fread() instead:

http://www.mathworks.com/help/matlab/ref/serial.fread.html

If you *do* wish to use scanf and the terminator as a field deliminator, note that you can change the terminator value if 0x0A is not appropriate for you.

Hope that helps,
Jason
 

vvarlord

Member level 4
Joined
Mar 15, 2012
Messages
74
Helped
4
Reputation
8
Reaction score
3
Trophy points
1,288
Activity points
1,756
i've done the tests you mentioned ,, and i did recieve the expected data in the termite app,,,so i think my matlab code is buggy
i'll try to use fread() , but it give me another error to think about!
"Warning: The specified amount of data was not returned within the Timeout period."
how to set how much data should the fread command obtain from the external file?

- - - Updated - - -

i've found the command's right format to use for my purpose
A = fread(fid, count)
now my first problem is solved
thank you jason,, you've helped me alot
now i've got another question.,,,, it's about matlab:
how i can write a program to recieve a signal from the micro & and procces the data,,,, i want it to be in simulink
i don't know much about matlab so,, simple description would be appreciated
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top