CMSP-MAX

From Curuxa

CMSP-MAX

This Module allows communication between the microcontroller on a Main Board and a computer using the serial port.

If your computer doesn't have a serial port, you can buy a cheap USB-to-serial converter.

Contents

Electronic circuit

Bill of materials:

  • Perforated board of at least 8x9 holes. Individual pads, not copper strips
  • 2x data connector
  • 1x MAX232
  • 4x 1uF electrolytic capacitor
  • 1x 100nF ceramic capacitor
  • 1x Female DB-9 connector (for cable, not for PCB)
  • 1x 270Ω resistor
  • some wire
Schematic
Layout: top view
Layout: bottom view

Wiring

CMSP-MAX Wiring

On the female DB-9 connector, pin 2 is called RXD, pin 3 is TXD and pin 5 is GND.

MB_TX and MB_RX data connectors must be connected to the pins TX and RX of the microcontroller you are using (see its datasheet or the schematic of your Main Board).

The three wires next to the label Serial Port must be connected to the indicated pins on a female DB-9 connector, which will be then plugged to your computer. If you don't have a serial port on your computer, you can use a cheap USB-to-serial adapter.


How it works

This Module is based on a MAX232 integrated circuit, which adapts the digital signals from the microcontroller to the high-voltage and negative voltages used by RS-232 (serial port). The real, digital communications are handled by software using the internal UART module of the microcontroller.

Construction

CMSP-MAX CMSP-MAX CMSP-MAX CMSP-MAX CMSP-MAX CMSP-MAX

Test

To read and write to the serial port you can use Hyperterminal on Windows, GtkTerm on Linux...

Once you can communicate with the computer properly, it's recommended to write your own computer programs to send and receive specific signals. There is an example at the project page of this robot controlled from a PC.

Sending data (Hello World)

/*==================================================================
* Sample program for CMSP-MAX (transmission-only) using MBP40
*
* This program sends consecutive numbers through the serial port.
* It's the most simple way to check for proper transmission from the
* microcontroller to the computer.
*
* Connect MB_TX from CMSP-MAX to TX/CK (pin 25) on MBP40
*
* http://curuxa.org
*=================================================================*/
 
#include <MBP40.h> 
 
#define OSC_8MHz
#include <Delays.h>
 
ConfigBits1(_CP_OFF & _DEBUG_OFF & _FCMEN_OFF  & _IESO_OFF & _CPD_OFF & _LVP_OFF & _BOR_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTOSCIO);
 
//Writes a byte to the serial port
void WriteSP(unsigned int8 value){
	//wait for previous transmission to end
	while(!TXIF){
		//do nothing
	}
	TXREG=value;
	Delay10us();
}
 
void main() {
	unsigned int8 i=13;
 
	SetIntosc8MHz();
	AdcDisable();
 
	//setup UART, transmission
	//Baud rate timer: SPBRG=((Fosc/(16 * 9600) -1))
	//Baud rate = Fosc/(16*(SPBRG+1))
	//real resulting baud rate for Fosc=8MHz: 9615.384615 bauds
	// SPRG=51, 
	SPBRGH=0;
	SPBRG=51;
	BRGH=1; //high speed
	TXEN=1; // (register TXSTA) enable transmitter
	SYNC=0; // (register TXSTA) asynchronous operation
	SPEN=1; // (register RCSTA) set TX/CK as output
	//...by default: 8 bits, no interrupts
 
	WriteSP('h');
	WriteSP('e');
	WriteSP('l');
	WriteSP('l');
	WriteSP('o');
 
	while(1){
		WriteSP(i);
		i++;
	}
}


Sending and receiving data

/*==================================================================
* Sample program for CMSP-MAX using MBP40
*
* This program reads a byte from the serial port and sends back three bytes:
* 1- The same byte as received
* 2- The received byte plus one
* 3- The received byte plus two
* For example, if you send a 57 (single byte) from the computer, it will
* send back a 57, 58 and 59.
*
* Connect MB_TX from CMSP-MAX to TX/CK (pin 25) on MBP40
* Connect MB_RXfrom CMSP-MAX to RX/DT (pin 26) on MBP40
*
* http://curuxa.org
*=================================================================*/
 
#include <MBP40.h> 
 
#define OSC_8MHz
#include <Delays.h>
 
ConfigBits1(_CP_OFF & _DEBUG_OFF & _FCMEN_OFF  & _IESO_OFF & _CPD_OFF & _LVP_OFF & _BOR_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTOSCIO);
 
//Writes a byte to the serial port
void WriteSP(unsigned int8 value) {
	//wait for previous transmission to end
	while(!TXIF) {
		//do nothing
	}
	TXREG=value;
	Delay10us();
}
 
//Reads a byte from the serial port. If there is no data on the receiver buffer, wait indefinitely for the next incoming byte
unsigned int8 ReadSPWait(){
	unsigned int8 dummy;
	while(!RCIF) {
		if (OERR) {
			TXEN=0;
			TXEN=1;
			CREN=0;
			CREN=1;
		}
		if (FERR) {
			dummy=RCREG;
			TXEN=0;
			TXEN=1;
		}
	}
	return RCREG;
}
 
//Reads a byte from the serial port. If there is no data on the receiver buffer, return 0xFF
unsigned int8 ReadSP() {
	if(RCIF) return RCREG;
	else return 0xFF;
}
 
void main() {
	unsigned int8 i=0;
 
	SetIntosc8MHz();
	AdcDisable();
 
	//setup UART
	//Baud rate timer: SPBRG=((Fosc/(16 * 9600) -1))
	//Baud rate = Fosc/(16*(SPBRG+1))
	//real resulting baud rate for Fosc=8MHz: 9615.384615 bauds
	// SPRG=51, 
	SPBRGH=0;
	SPBRG=51;
	BRGH=1; //high speed
	CREN=1; //enable receiver
	TXEN=1; // (register TXSTA) enable transmitter
	SYNC=0; // (register TXSTA) asynchronous operation
	SPEN=1; // (register RCSTA) set TX/CK as output, RX/DT as input
	//...by default: 8 bits, no interrupts
 
	WriteSP('\n');
	WriteSP('i');
	WriteSP('n');
	WriteSP('i');
	WriteSP('t');
 
	while(1) {
		i=ReadSP();
		if(i!=0xFF) {
			WriteSP(i);
			i++;
			WriteSP(i);
			i++;
			WriteSP(i);
		}
	}
}

See also

PCBot is a simple robot controlled from a computer. The computer communicates with the microcontroller on the Main Board using a CMSP-MAX.