MAX3140 SPI UART with RS-485/RS422 on MBED

Please forgive my piecemeal approach to this posting. I am finishing up some details but thought it might be of interest to those who are using several/many mbeds in a party-line configuration. Schematics and details to follow soon.

 

 

//Test Code - The circular buffer is not complete (will run off the end) but it does work at a full 155Kbaud, I am also using an Interrupt based recieve and will add a transmit buffer empty check soon.

#include "mbed.h"

SPI spi(p5,p6,p7);
DigitalOut cs(p8);
InterruptIn Max3140Data(p9);

Serial pc(USBTX, USBRX);

int ret = 0;
char input_character = 0;
char output_character = 0;
char input_buffer[16384];
int input_buffer_pointer = 0;
int output_buffer_pointer = 0;

void gotit() {

cs = 0; 
ret = 0;
ret = spi.write(0x0000);
cs = 1;
input_character = char(ret & 0x7F);
input_buffer[input_buffer_pointer++] = input_character;
//pc.printf("%c",input_character);
}

void Max3140_Write(char output_byte){
ret = 0;
cs = 0;
ret = spi.write(0x8000 | output_byte);
cs = 1;
wait(0.0001);
}


int main(){

spi.format(16,0);
spi.frequency(1000000);

Max3140Data.mode(PullUp);   
Max3140Data.fall(&gotit);

wait(0.5);

Max3140_Write('H');
Max3140_Write('e');
Max3140_Write('l');
Max3140_Write('l');
Max3140_Write('o');
Max3140_Write('\r');
Max3140_Write('\n');   

ret = 0;
cs = 0;
ret = spi.write(0xC400);
cs = 1;
wait(0.1);
//pc.printf("\r\nWRITE CONFIGURATION register = 0x%04X\r\n ", ret);

cs = 0;
ret = spi.write(0x4000);
cs = 1;
//pc.printf("\r\nREAD CONFIGURATION register = 0x%04X\r\n", ret);
wait(0.1);

// Try to read some data

ret = 0;
cs = 0;
ret = spi.write(0x8600);
cs = 1;

wait(0.1);

//cs = 0;
//ret = spi.write(0x0000);
// cs = 1;

pc.printf("at loop\r\n");
input_buffer_pointer = output_buffer_pointer = 0;
for(;;){      
if(input_buffer_pointer > output_buffer_pointer){
//pc.printf("%d %d\r\n", input_buffer_pointer, output_buffer_pointer);
output_character = input_buffer[output_buffer_pointer++];
pc.printf("%c", output_character);
}
}

}

 

 

rs485


1 comment

03 Aug 2022

i have tried this MAX 3140  RS 485 exmple  and transmite data  succesfully from device to pc but i am not receive any data  when we write data on pc and try to receive on device . so how we can receive data from pc(teraterm console) to device.

You need to log in to post a comment