Matlab is one big tool which you can play with.. Matlab can be configured to read serial data that can be send through a microcontroller. Here is the code for Matlab to read a single line and print. Assume that you are sending ascii character values through microcontroller separated by new line.
Here is the Matlab code to receive the data.
1: obj1 = instrfind('Type', 'serial', 'Port', 'COM13', 'Tag', '');
2: % Create the serial port object if it does not exist
3: % otherwise use the object that was found.
4: if isempty(obj1)
5: obj1 = serial('COM13');
6: else
7: fclose(obj1);
8: obj1 = obj1(1)
9: end
10: obj1.BaudRate=4800;
11: set(obj1, 'Parity', 'even');
12: set(obj1, 'StopBits', 2.0);
13: fopen(obj1);
14: data1=fscanf(obj1, '%s');
15: data1=fscanf(obj1, '%s'); %Assume that you are sending characters and 8 bit binary no.s
16: data1; %Use a=fread(obj1); if it's not charecter
17: fclose(obj1);
18: delete(obj1);
19: clear obj1;
Here is the code for AVR Microcontroller to send ‘1024’ through serial port.
1: /*
2: * Ploting_ADC_Values.c
3: *
4: * Created: 22-10-2013 09:44:54
5: * Author: ANANTHAKRISHNAN U S
6: */
7:
8:
9:
10: #define F_CPU 1000000UL
11: #define FOSC 1000000
12: #define BAUD 4800
13: #define MYUBRR FOSC/16/BAUD-1
14:
15: #include <avr/io.h>
16: #include <util/delay.h>
17: #include <stdio.h>
18:
19: static int uart_putchar(char c, FILE *stream);
20: static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,_FDEV_SETUP_WRITE);
21:
22:
23: void USART_Init( unsigned int ubrr);
24: static int uart_putchar(char c, FILE *stream);
25: char uart_getchar(FILE *stream);
26:
27: int main(void)
28: {
29: USART_Init(MYUBRR); //Initializing USART
30: stdout = &mystdout;
31: int i =1024;
32: while(1)
33: {
34: printf("%d\n",i);
35: _delay_ms(50);
36: }
37: }
38:
39: void USART_Init( unsigned int ubrr)
40: {
41: /*Set baud rate */
42: UBRR0H = (unsigned char)(ubrr>>8);
43: UBRR0L = (unsigned char)ubrr;
44: //Enable receiver and transmitter */
45: UCSR0B = (1<<TXEN0) | (1<<RXEN0);
46: /* Set frame format: 8data, 2stop bit */
47: UCSR0C = (1<<USBS0)|(3<<UCSZ00)|(1<<UPM01);//Asynchronous 8bit data Even parity 2 stop bit
48: }
49:
50: static int uart_putchar(char c, FILE *stream)
51: {
52: if (c == '\n') {
53: uart_putchar('\r', stream);
54: }
55: // Wait for empty transmit buffer */
56: while ((UCSR0A & (1 << UDRE0)) == 0) {};
57: // Put data into buffer, sends the data */
58: UDR0 = c;
59: }
60:
61:
62: char uart_getchar(FILE *stream) {
63:
64: while ((UCSR0A & (1 << RXC0)) == 0) {};
65: /* Wait until data exists. */
66: return UDR0;
67: }
Comments
Post a Comment