0% found this document useful (0 votes)
429 views5 pages

MSP430G2553 Uart Header

The document describes a UART communication module for an MSP430 microcontroller that uses the Timer_A peripheral to implement the UART functionality at 9600 baud without directly accessing the TX and RX pins. It initializes the Timer_A and port pins for UART, transmits data by outputting the individual bits using Timer_A compare modes, and receives data by capturing the incoming bits and reading them from the Timer_A capture register. It includes functions for initializing the UART, transmitting data, printing strings and numeric values, and the interrupt handlers for transmitting and receiving bits.

Uploaded by

Robert Katona
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
429 views5 pages

MSP430G2553 Uart Header

The document describes a UART communication module for an MSP430 microcontroller that uses the Timer_A peripheral to implement the UART functionality at 9600 baud without directly accessing the TX and RX pins. It initializes the Timer_A and port pins for UART, transmits data by outputting the individual bits using Timer_A compare modes, and receives data by capturing the incoming bits and reading them from the Timer_A capture register. It includes functions for initializing the UART, transmitting data, printing strings and numeric values, and the interrupt handlers for transmitting and receiving bits.

Uploaded by

Robert Katona
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

//******************************************************************************

// MSP430G2xx2 Demo - Timer_A, Ultra-Low Pwr UART 9600 Echo, 32kHz ACLK
//
//
lpsei:
//
__enable_interrupt();
//UART-hoz megszakits bekapcsolsa
//
UART_init();
//
//
Adat fogadsa:
//
__bis_SR_register(LPM0_bits); //itt addig vr amig nem jn karakter
//
//
adat=rxBuffer; //adat unsigned char tipus vltozba lementjk a karak
tert
//
//
UART_tx(rxBuffer); // ez az echo
//
//
Szveg kiiratsa:
//
UART_print("\r\nUzenetet megkaptam!\r\n");
//
//
Adat kiiratsa:
//
UART_print2(adat); +-9999 max rtkkel
//
UART_print_float(adat); +-999 max rtkkel, 2 tizedes jegyig kiirja
//
// Description: Use Timer_A CCR0 hardware output modes and SCCI data latch
// to implement UART function @ 9600 baud. Software does not directly read and
// write to RX and TX pins, instead proper use of output modes and SCCI data
// latch are demonstrated. Use of these hardware features eliminates ISR
// latency effects as hardware insures that output and input bit latching and
// timing are perfectly synchronised with Timer_A regardless of other
// software activity. In the Mainloop the UART function readies the UART to
// receive one character and waits in LPM3 with all activity interrupt driven.
// After a character has been received, the UART receive function forces exit
// from LPM3 in the Mainloop which configures the port pins (P1 & P2) based
// on the value of the received byte (i.e., if BIT0 is set, turn on P1.0).
// ACLK = TACLK = LFXT1 = 32768Hz, MCLK = SMCLK = default DCO
// //* An external watch crystal is required on XIN XOUT for ACLK *//
//
//
MSP430G2xx2
//
----------------//
/|\|
XIN|//
| |
| 32kHz
//
--|RST
XOUT|//
|
|
//
| CCI0B/TXD/P1.1|-------->
//
|
| 9600 8N1
//
| CCI0A/RXD/P1.2|<-------//
// D. Dang
// Texas Instruments Inc.
// December 2010
// Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10
//******************************************************************************

//-----------------------------------------------------------------------------// Hardware-related definitions


//-----------------------------------------------------------------------------#define UART_TXD 0x02
// TXD on P1.1 (Timer0_A.OUT0)
#define UART_RXD 0x04
// RXD on P1.2 (Timer0_A.CCI1A)

//-----------------------------------------------------------------------------// Conditions for 9600 Baud SW UART, SMCLK = 1MHz


//-----------------------------------------------------------------------------#define UART_TBIT_DIV_2
(1000000 / (9600 * 2))
#define UART_TBIT
(1000000 / 9600)
//-----------------------------------------------------------------------------// Global variables used for full-duplex UART communication
//-----------------------------------------------------------------------------unsigned int txData;
// UART internal variable for TX
unsigned char rxBuffer;
// Received UART character
//-----------------------------------------------------------------------------// Function prototypes
//------------------------------------------------------------------------------

void UART_init(void)
{
DCOCTL = 0x00;
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
P1OUT
P1SEL
P1DIR
P1DIR

|=
|=
|=
&=

BIT1 |BIT2;
UART_TXD |UART_RXD;
UART_TXD;
~UART_RXD;

TACCTL0 = OUT;
TACCTL1 = SCS + CM1 + CAP + CCIE;
TACTL = TASSEL_2 + MC_2;

// Set DCOCLK to 1MHz

// Timer function for TXD/RXD pins

// Set TXD Idle as Mark = '1'


// Sync, Neg Edge, Capture, Int
// SMCLK, start in continuous mode

}
//-----------------------------------------------------------------------------// Outputs one byte using the Timer_A UART
//-----------------------------------------------------------------------------void UART_tx(int byte)
{
while (TACCTL0 & CCIE);
TACCR0 = TAR;
TACCR0 += UART_TBIT;
TACCTL0 = OUTMOD0 + CCIE;
txData = byte;
txData |= 0x100;
txData <<= 1;
}

//
//
//
//
//
//
//

Ensure last char got TX'd


Current state of TA counter
One bit time till first bit
Set TXD on EQU0, Int
Load global variable
Add mark stop bit to TXData
Add space start bit

//-----------------------------------------------------------------------------// Szveg kiiratsa


//-----------------------------------------------------------------------------void UART_print(char *string)
{
while (*string) {
UART_tx(*string++);
}

}
void UART_print2(int data)
{
int a,b,c,d;
if(data<0.0){
UART_tx(0x2D);
data=-data;
}

//negativ jel kdja

a=data/1000;
b=(data-a*1000)/100;
c=(data-a*1000-b*100)/10;
d=data-a*1000-b*100-c*10;
a+=48;
b+=48;
c+=48;
d+=48;
UART_tx(a);
UART_tx(b);
UART_tx(c);
UART_tx(d);
}
void UART_print_float(float data) //vals szm kiiratsa kt tizedes jegyel
{
int a,b,c,d,e;
int data_i;
//egsz rsznek a trolsra
float remain;
//tizedes pont utnni rsz trolsra
if(data<0.0){
UART_tx(0x2D);
data=-data;
}

//negativ jel kdja

data_i=(unsigned int)data;
remain=(data-(double)data_i)*100;
a=(unsigned int)data/100;
b=((unsigned int)data-a*100)/10;
c=(unsigned int)data-a*100-b*10;
d=(unsigned int)remain/10;
e=(unsigned int)remain-d*10;

//szzasok szma
//tizesek szma
//egyesek szma
//tizedesek szma
//szzadosok szma

a+=48;
b+=48;
c+=48;
d+=48;
e+=48;
UART_tx(a);
UART_tx(b);
UART_tx(c);
UART_tx(0x2E);
UART_tx(d);
UART_tx(e);
}

//pont kdja

//-----------------------------------------------------------------------------// Timer_A UART - Transmit Interrupt Handler


//-----------------------------------------------------------------------------#pragma vector = TIMER0_A0_VECTOR
__interrupt void Timer_A0_ISR(void)
{
static unsigned char txBitCnt = 10;
TACCR0 += UART_TBIT;
if (txBitCnt == 0) {
TACCTL0 &= ~CCIE;
txBitCnt = 10;
}
else {
if (txData & 0x01) {
TACCTL0 &= ~OUTMOD2;
}
else {
TACCTL0 |= OUTMOD2;
}
txData >>= 1;
txBitCnt--;
}

//
//
//
//

Add Offset to CCRx


All bits TXed?
All bits TXed, disable interrupt
Re-load bit counter

// TX Mark '1'
// TX Space '0'

}
//-----------------------------------------------------------------------------// Timer_A UART - Receive Interrupt Handler
//-----------------------------------------------------------------------------#pragma vector = TIMER0_A1_VECTOR
__interrupt void Timer_A1_ISR(void)
{
static unsigned char rxBitCnt = 8;
static unsigned char rxData = 0;
switch (__even_in_range(TA0IV, TA0IV_TAIFG)) { // Use calculated branching
case TA0IV_TACCR1:
// TACCR1 CCIFG - UART RX
TACCR1 += UART_TBIT;
// Add Offset to CCRx
if (TACCTL1 & CAP) {
// Capture mode = start bit edg
e
TACCTL1 &= ~CAP;

// Switch capture to compare mo

de
TACCR1 += UART_TBIT_DIV_2;
}
else {
rxData >>= 1;
if (TACCTL1 & SCCI) {

// Point CCRx to middle of D0

// Get bit waiting in receive l

atch
rxData |= 0x80;
}
rxBitCnt--;
if (rxBitCnt == 0) {
rxBuffer = rxData;
rxBitCnt = 8;
TACCTL1 |= CAP;

//
//
//
//

All bits RXed?


Store in global variable
Re-load bit counter
Switch compare to capture mo

de
__bic_SR_register_on_exit(LPM0_bits); // Clear LPM0 bits fr
om 0(SR)
}
}
break;

}
}
//------------------------------------------------------------------------------

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy