Napoli - NaPoLi - nAPoLi - Na - Po - Li

20040513

s1 = serial('COM1', 'BaudRate', 19200);

Abrindo uma porta serial no Matlab + MCU AVR.
"Hoje estive programando um microcontrolador Risc da família Atmel, modelo AT90s2313, é um bichinho pequeno para os dias de hoje, mas já dá pra construir muitos brinquedos interessantes.

Muita atenção agora Gafanhoto Chan !! Treine seu Kung-Fu !!
Projeto de hoje ser "pequeno-microcontrolador" conversa "grande-PC" através de porta serial RS-232 né !!

Primeiramente, utilizando um compilador C, escrevi pacientemente o seguinte código (brincadeira foi quase tudo automático com aplication builder, hahahá):
--------------------------------------------------------------------------------------
ICC-AVR application builder : 5/13/2004 5:04:47 PM
// Target : 2313
// Crystal: 4.0000Mhz

#include
#include

void port_init(void)
{
PORTB = 0x55;
DDRB = 0xFF;
PORTD = 0x00;
DDRD = 0x00;
}

//TIMER1 initialize - prescale:64
// desired value: 1Hz
// actual value: 1.000Hz (0.0%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop timer
TCNT1H = 0x0B; //set count value
TCNT1L = 0xDC;
OCR1H = 0x00; //set compare value
OCR1L = 0x00;
TCCR1A = 0x00;
TCCR1B = 0x03; //start Timer
}

#pragma interrupt_handler timer1_ovf_isr:6
void timer1_ovf_isr(void)
{
//TIMER1 has overflowed
//stop errant interrupts until set up
CLI(); //disable all interrupts



timer1_init();
SEI(); //re-enable interrupts
//all peripherals are now initialized
}

//UART0 initialize
// desired baud rate: 19200
// actual: baud rate:19231 (0.2%)
void uart0_init(void)
{
UCR = 0x00; //disable while setting baud rate
UBRR = 0x0C; //set baud rate
UCR = 0xD8; //enable
}

#pragma interrupt_handler uart0_rx_isr:8
void uart0_rx_isr(void)
{
//uart has received a character in UDR

PORTB = UDR;

}

#pragma interrupt_handler uart0_tx_isr:10
void uart0_tx_isr(void)
{
//character has been transmitted
}

//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer1_init();
uart0_init();

MCUCR = 0x00;
GIMSK = 0x00;
TIMSK = 0x80;
SEI(); //re-enable interrupts
//all peripherals are now initialized
}

//
void main(void)
{
init_devices();
//insert your functional code here...
}
--------------------------------------------------------------------------------------

Agora no prompt do Matlab:

Matlab 6.0.0.88 Release 12
--------------------------------------------------------------------------------------
>> s1 = serial('COM1', 'BaudRate', 19200);
>> fopen(s1) 'isto abre a porta serial p/ podermos então iniciar a comunicação'
'caso haja um erro nesta etapa verifique se nenhum outro software'
'já esteja utilizado a serial.'

>> fwrite(s1,255-2^7) 'isto vai acender o LED7 ligado no PORTB7 do MCU'
>> fclose(s1) 'isto fecha a porta serial no Matlab'
>> s1 'veja que o objeto serial ainda está na memória'

Serial Port Object : Serial-COM1

Communication Settings
Port: COM1
BaudRate: 19200
Terminator: LF

Communication State
Status: closed
RecordStatus: off

Read/Write State
TransferStatus: idle
BytesAvailable: 0
ValuesReceived: 0
ValuesSent: 1

>> s1.BytesAvailable 'o objeto serial tem diversas propriedades muito úteis'
ans = 0
--------------------------------------------------------------------------------------

Chaãaãnnnn!! Gafanhoto vc ainda não preparado para enfrentar mundo externo...
Treine seu Kung-Fu diariamente....