mady 0 Posted April 14, 2011 Share Posted April 14, 2011 Hello. I have a new problem,if somebody can help me.How can I change the frequency to a msp430g2231 from 1MHz to 4,8 or 16MHz? I want to use 19200 baudrate and I understand that this is the problem... Thanks. Code example: /****************************************************************************** * Half Duplex Software UART on the LaunchPad * * Description: This code provides a simple Bi-Directional Half Duplex * Software UART. The timing is dependant on SMCLK, which * is set to 1MHz. The transmit function is based off of * the example code provided by TI with the LaunchPad. * This code was originally created for "NJC's MSP430 * LaunchPad Blog". * * Author: Nicholas J. Conn - http://msp430launchpad.com * Email: webmaster at msp430launchpad.com * Date: 08-17-10 ******************************************************************************/ #include "msp430g2231.h" #include "stdbool.h" #include "stdint.h" #define TXD BIT1 // TXD on P1.1 #define RXD BIT2 // RXD on P1.2 #define Bit_time 104 // 9600 Baud, SMCLK=1MHz (1MHz/9600)=104 #define Bit_time_5 52 // Time for half a bit. unsigned char BitCnt; // Bit count, used when transmitting byte unsigned int TXByte; // Value sent over UART when Transmit() is called unsigned int RXByte; // Value recieved once hasRecieved is set bool isReceiving; // Status for when the device is receiving bool hasReceived; // Lets the program know when a byte is received // Function Definitions void Transmit(uint8_t); void Enter(void); void main(void) { char response[2]={0}; char transmitted=1; int i=0; WDTCTL = WDTPW + WDTHOLD; // Stop WDT BCSCTL1 = CALBC1_1MHZ; // Set range DCOCTL = CALDCO_1MHZ; // SMCLK = DCO = 1MHz P1SEL |= TXD; P1DIR |= TXD; P1IES |= RXD; // RXD Hi/lo edge interrupt P1IFG &= ~RXD; // Clear RXD (flag) before enabling interrupt P1IE |= RXD; // Enable RXD interrupt TACCR0 = 30; // Delay to allow Ref to settle TACCTL0 |= CCIE; // Compare-mode interrupt. TACTL = TASSEL_2 | MC_1; isReceiving = false; // Set initial values hasReceived = false; __bis_SR_register(GIE); // interrupts enabled\ while(1) { if (transmitted) { //Transmit(response[0]); //transmit AT command and wait for OK response //Transmit('a'); // Transmit('T'); // Transmit('I'); // Transmit('1'); /* Transmit('D'); //call to number, it works good(is just for to send to phone) Transmit('+'); Transmit('4'); Transmit('2'); Transmit('1'); Transmit('9'); Transmit('0'); Transmit('4'); Transmit('8'); Transmit('6'); Transmit('7'); Transmit('1'); Transmit('9'); Transmit('4'); Transmit(';'); Transmit(0x0D);*/ Enter(); transmitted=0; // __bis_SR_register(CPUOFF + GIE); } if (hasReceived) // If the device has recieved a value { hasReceived = false; // Clear the flag response=RXByte+1; //i++; __bis_SR_register(CPUOFF + GIE); transmitted=1; //i=0; Transmit(RXByte+1); } //if (~hasReceived) // Loop again if another value has been received // __bis_SR_register(CPUOFF + GIE); // LPM0, the ADC interrupt will wake the processor up. This is so that it does not // endlessly loop when no value has been Received. } } void Enter() { Transmit(0x0A); Transmit(0x0D); } // Function Transmits Character c void Transmit(uint8_t c) { TXByte=c; while(isReceiving); // Wait for RX completion CCTL0 = OUT; // TXD Idle as Mark TACTL = TASSEL_2 + MC_2; // SMCLK, continuous mode BitCnt = 0xA; // Load Bit counter, 8 bits + ST/SP CCR0 = TAR; // Initialize compare register CCR0 += Bit_time; // Set time till first bit TXByte |= 0x100; // Add stop bit to TXByte (which is logical 1) TXByte = TXByte << 1; // Add start bit (which is logical 0) CCTL0 = CCIS0 + OUTMOD0 + CCIE; // Set signal, intial value, enable interrupts while ( CCTL0 & CCIE ); // Wait for previous TX completion } // Port 1 interrupt service routine #pragma vector=PORT1_VECTOR __interrupt void Port_1(void) { isReceiving = true; P1IE &= ~RXD; // Disable RXD interrupt P1IFG &= ~RXD; // Clear RXD IFG (interrupt flag) TACTL = TASSEL_2 + MC_2; // SMCLK, continuous mode CCR0 = TAR; // Initialize compare register CCR0 += Bit_time_5; // Set time till first bit CCTL0 = OUTMOD1 + CCIE; // Dissable TX and enable interrupts RXByte = 0; // Initialize RXByte BitCnt = 0x9; // Load Bit counter, 8 bits + ST } // Timer A0 interrupt service routine #pragma vector=TIMERA0_VECTOR __interrupt void Timer_A (void) { if(!isReceiving) { CCR0 += Bit_time; // Add Offset to CCR0 if ( BitCnt == 0) // If all bits TXed { TACTL = TASSEL_2; // SLK, timer off (for power consumption) CCTL0 &= ~ CCIE ; // Disable interrupt } else { CCTL0 |= OUTMOD2; // Set TX bit to 0 if (TXByte & 0x01) CCTL0 &= ~ OUTMOD2; // If it should be 1, set it to 1 TXByte = TXByte >> 1; BitCnt --; } } else { CCR0 += Bit_time; // Add Offset to CCR0 if ( BitCnt == 0) { TACTL = TASSEL_2; // SMCLK, timer off (for power consumption) CCTL0 &= ~ CCIE ; // Disable interrupt isReceiving = false; P1IFG &= ~RXD; // clear RXD IFG (interrupt flag) P1IE |= RXD; // enabled RXD interrupt if ( (RXByte & 0x201) == 0x200) // Validate the start and stop bits are correct { RXByte = RXByte >> 1; // Remove start bit RXByte &= 0xFF; // Remove stop bit hasReceived = true; } __bic_SR_register_on_exit(CPUOFF); // Enable CPU so the main while loop continues } else { if ( (P1IN & RXD) == RXD) // If bit is set? RXByte |= 0x400; // Set the value in the RXByte RXByte = RXByte >> 1; // Shift the bits down BitCnt --; } } } Quote Link to post Share on other sites
P!-Ro 4 Posted April 14, 2011 Share Posted April 14, 2011 First you need to buy that 4 or 8 or 16mhz crystal, because the msp doesn't have one built in. Have you done this yet? Quote Link to post Share on other sites
mady 0 Posted April 14, 2011 Author Share Posted April 14, 2011 I have a 12 MHz crystal. Quote Link to post Share on other sites
mady 0 Posted April 14, 2011 Author Share Posted April 14, 2011 I can work with 12 MHz if it can be made.... Quote Link to post Share on other sites
nobody 47 Posted April 14, 2011 Share Posted April 14, 2011 1) You do not need a 12MHz crystal. Use 32kHz crystal added to your Launchpad. 2) Is it possible that you do not need a crystal at all. You have two options: - Calibrate your msp430g2231 (instructions is issued here in the forum) - Use of uncalibrated frequency and manually adjust and change BCSCTL1 and/or DCOCTL. In any case, I suggest you read slau144h.pdf Chapter 5 (pages 273-287). Quote Link to post Share on other sites
none 7 Posted April 14, 2011 Share Posted April 14, 2011 Beware, AFAIK the Launchpad only supports 9600 baud. Quote Link to post Share on other sites
zeke 693 Posted April 15, 2011 Share Posted April 15, 2011 Hey Guys, Take a look at this thread for all the nitty gritty details on how to setup the DCO constants. These parts can run at 16MHz no problem. These parts can also do better than 9600 baud. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.