msp430

Loading thread data ...

Ну ты шутник. Камень какой? Есть аппаратный UART на борту или нет?

на сайте, где лежит родной софт (т.е. на сайте тексасных инструментов) есть очень много application notes, в том числе и UART. В том числе и программный.

С уважением, Сергей Борщ

Reply to
Sergey A. Borshch
Reply to
Dmitry Olshansky

Дима, приветствую!

Ну тогда вот:

bit_defs.h: Мне удобнее пользоваться осмысленными мнемониками имен битов и их комбинаций

#define USART_CTL 0 #define USART_TCTL 1 #define USART_RCTL 2 #define USART_MOD 3 #define USART_BR0 4 #define USART_BR1 5 #define USART_RXBUF 6 #define USART_TXBUF 7 #ifdef __IAR_SYSTEMS_ICC #define USART0(SFR) *((unsigned char*)&UCTL0_+SFR) #define USART1(SFR) *((unsigned char*)&UCTL1_+SFR) #else #ifdef __IAR_SYSTEMS_ASM #define USART0(SFR) UCTL0_+SFR #define USART1(SFR) UCTL1_+SFR #endif #endif /* Register UCTLx (0x0070,0x0078) */ #define _SWReset SWRST #define _AddressMode MM #define _NormalMode 0x00 #define _Sync SYNC #define _Async 0x00 #define _Feedback LISTEN #define _NoFeedback 0x00 #define _8bit CHAR #define _7bit 0x00

#define _TwoStop SPB #define _OneStop 0x00 #define _Even PEV #define _Odd 0x00 #define _Parity PENA #define _NoParity 0x00

/* Register UTCTLx (0x0071,0x0079) */ #define _TxEmpty TXEPT

#define UartUCLKI 0x00 #define UartACLK USSEL0 #define UartSMCLK USSEL1

Terminal.h: #define CR "\r\n"

#ifdef RS232_USART0 #define RS232 USART0 #define RS232RxVector [UART0RX_VECTOR] #define RS232TxVector [UART0TX_VECTOR] #define RS232TxIntEnable() IE1 |= UTXIE0 #define RS232TxIntDisable() IE1 &= ~UTXIE0 #define RS232RxIntEnable() IE1 |= URXIE0 #define RS232RxIntDisable() IE1 &= ~URXIE0 #else #ifdef RS232_USART1 #define RS232 USART1 #define RS232RxVector [UART1RX_VECTOR] #define RS232TxVector [UART1TX_VECTOR] #define RS232TxIntEnable() IE2 |= UTXIE1 #define RS232TxIntDisable() IE2 &= ~UTXIE1 #define RS232RxIntEnable() IE2 |= URXIE1 #define RS232RxIntDisable() IE2 &= ~URXIE1 #else #error Not UART module defined! #endif #endif

UART.c: #include <msp430x14x.h>

#include <bit_defs.h>

#include <stdio.h>

#define OSC 8000000L //Hz

#define UARTBaudRate 2400 #define MCLK (OSC) #define SMCLK (OSC/2) #define USARTCLK MCLK //NOT SMCLK, F149 bug, see errata

#define RS232_USART0 #include "Terminal.h"

#define RxBufferSize 32 // size = 2^x #define TxBufferSize 16 // size = 2^x

unsigned char RxBuffer[RxBufferSize]; // Rx queue unsigned char TxBuffer[TxBufferSize]; // Tx queue unsigned char RxHead,RxTail,TxHead,TxTail; // queue indexes

void UARTInit (void) { RS232(USART_BR0)= USARTCLK / UARTBaudRate; RS232(USART_BR1)= (USARTCLK / UARTBaudRate)>>8; RS232(USART_MOD)= 0; RS232(USART_CTL)=_Async|_NoFeedback|_8bit|_OneStop|_NoParity|_NormalMode; RS232(USART_TCTL)= UartSMCLK | _TxEmpty; TxHead=TxTail=RxHead=RxTail=0;

#ifdef RS232_USART0 ME1 = URXE0 | UTXE0; P3SEL |= (1<<5) | (1<<4); #else ME2 = URXE1 | UTXE1; P3SEL |= (1<<7) | (1<<6); #endif RS232RxIntEnable(); }

void interrupt UARTTxVector Tx232(void) { RS232(USART_TXBUF) = TxBuffer[TxTail++]; // get byte from queue TxTail &= TxBufferSize - 1; // overwrap tail index

if(TxTail == TxHead) RS232TxIntDisable(); // Nothing more to transmit }

void interrupt UARTRxVector Rx232 (void) { RxBuffer[RxHead++] = RS232(USART_TXBUF); // store received byte to queue RxHead &= RxBufferSize-1; // overwrap head index }

int putch(int Symbol) { while(((TxHead - TxTail) & (TxBufferSize-1)) == (TxBufferSize-1)); // buffer full, wait if(Symbol >= ' ') TxBuffer[TxHead++] = Symbol; // place symbol to queue else TxBuffer[DebugHead++] = '.'; // replace symbol with "."

TxHead &= TxBufferSize-1; // overwrap head index RS232TxIntEnable(); return (1); }

int getch(void) { int Symbol; while(RxHead == TxTail); // Rx queue empty, wait Symbol = RxBuffer[RxTail++]; // get received symbol RxTail &= RxBufferSize-1; // overwrap tail index return (Symbol); }

void main (void) { printf("Hello! Tipa test UART 2400"CR); for(;;);

}

Если делать передачу без буфера и прерываний, то можно примерно так:

int putch(int Symbol) { while(!RS232(USART_TCTL) & _TxEmpty); // alredy transmitting, wait RS232(USART_TXBUF) = Symbol; // send symbol return (1); }

С уважением, Сергей Борщ

Reply to
Sergey A. Borshch

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.