Skip to main content

Featured Post

Product Design of a 4 wheel differential drive robot

Use Matlab to read serial data

 

Matlab is one big tool which you can play with.. Smile 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

Popular posts from this blog

Plymouth theme for Ubuntu

Bored of having the same boot animation screen again and again? There are plenty of Plymouth themes available out there. This is a theme I created by slicing some cool gif files I found online. You can download the theme and find the installation steps on my github account. ( https://github.com/krishnan793/PlymouthTheme-Cat )  The theme is created for Ubuntu 16.04. But this can be installed on previous versions with slight modification. If you have a slow computer then you can watch the whole animation loops. (The VM I used to record the screen was fast though. :))

Getting started with GPIO (Testing GPIO with a multimeter)

There are 65 possible GPIO pins that you can control. Some of the GPIO pins are normally configured to different functions. Let's play with one of them. Go to the beaglebone website and figure out the header pin corresponding to gpio23. The gpio23 is mapped to the 13th header pin. In this tutorial we are testing the gpio23 output using a multimeter (Do not connect an LED directly to BBB IO pin). First go to following directory cd /sys/class/gpio / Then make gpio23 available echo 23 > export A new directory will be created. Open gpio directory. cd gpio23 The direction of IO will be normally set as in (input).  To make it as output, echo out > direction Then set the pin to high using echo 1 > value Measure the voltage at pin 13 of P8 header. If its 3.3 V then you are on the right track. Pin 1 and 2 will be ground. You can connect the negative terminal of multimeter to pin 1 or 2. To set output as zero, echo 0 > value After finished wit...

AT Commands

AT commands are used for communicating with a modem. AT is the abbreviation of ATtention commands. Every command start with AT. Some of the useful AT commands are listed below. You can call a number, attend a call, hang a call, send sms, read sms, and the list goes on with the features of a particular provider. Some commands are specific to some models. GSM modems can be bought and can be integrated to Micro-controllers or directly to PC. Following list may be helpful to hobbyist or others working on GSM modems for project works. AT The response will be OK. This means that the modem is up and ready. ATD94xxxxxxxx This command will initiate a voice call to the no 94xxxxxxxx. The semicolon indicates that it is a voice call. If you want to run any USSD codes like *123# for balance enquirey, do the same without semicolon. ATD*123# Other Call control commands Command Description ATA Answer command ATD Dial command ATH Hang up call   Sending an SMS...