📜 ⬆️ ⬇️

MSP430, learn to program and debug hardware (part 3)


Perhaps you are not the first time to sit down for programming your MSP430. If this is not the case, then I recommend reading the previous articles on this topic: part 1 , part 2 .
In this article we will look at a tool for graphical configuration of the periphery of our microcontroller - Grace, get acquainted with the principle of the watchdog and work with the virtual COM-port (through the programmer). A small code sample will traditionally be considered, and all the links necessary for understanding will be provided.



Introduction


In the first article I mentioned Code Composer Studio , which I refused to consider, but recently my attention, thanks to Sokolov AV, attracted one plug-in for it - the Graphical Peripheral Configuration Tool (Grace) .

Every time when it is necessary to configure the next peripheral module MSP430, we are faced with a number of difficulties. These difficulties include ignorance of the list of registers of a particular module, distinctive features of a particular microcontroller, or poor understanding of English documentation. In any of these cases, Grace can be useful.
')

Grace


Download and install this tool together with Code Composer Studio v5 via the following link . You will need to register on the Texas Instruments website.

After installing CCS, select Project → New CCS Project , in Project templates and examples, select Empty Project → Empty Grace (MSP430) Project , in the Device area do not forget to specify a microcontroller from the list.

As soon as the “Grace - Welcome” screen appears, press the Device Overview key. You will see the following picture.

Clicking on a specific device on it, you will proceed to its setting, which can be made in several modes. Modes may vary for different devices, but usually these are Basic User , Power User and Registers . Basic User and Power User are designed to quickly configure the peripheral device intuitively. Registers displays a list of all device registers and allows you to change their values.

Tip: the datasheet for the microcontroller does not always contain complete information about the device of interest, you can usually read about all registers and parameters in the MSP430xxxx Family User's Guide file, which can be downloaded from the Texas Instruments website.

Since I was used to using Workbench for work, after the setup is completed, all configuration data is required to be transferred there. To do this, click Project → Build All , find the directory / src / csl / in the project folder, and all * .c files with the configuration of each device are in it.

Code example


The following code works with a USB-UART bridge embedded in the programmer, the watchdog is also included in the code, but in order.
  1. #include "msp430f2274.h"
  2. #include <string>
  3. void UARTWriteString ( string str ) ;
  4. bool watchdogReset = true ;
  5. void main ( void )
  6. {
  7. WDTCTL = WDT_MRST_32 ; // Watchdog automatically
  8. // restart the system in 32ms.
  9. BCSCTL1 = CALBC1_1MHZ ; // Set the clock frequency of the Basic Clock System.
  10. DCOCTL = CALDCO_1MHZ ; // Set the clock
  11. // frequency Digital Controlled Oscillator.
  12. P3SEL = BIT4 + BIT5 ; // Select the function P1.4 / P1.5 as TXD / RXD for UART.
  13. UCA0CTL1 | = UCSWRST ; // This bit blocks the work of interrupts from the UART and work
  14. // shift register so as not to interfere
  15. // setup (roughly speaking, disables UART).
  16. UCA0CTL1 | = UCSSEL_2 ; // Our UART will work from
  17. // SMCLK (Sub-main clock), that is, from 1MHZ.
  18. UCA0BR0 = 0x68 ; // The frequency divider for SMCLK (1000000/9600).
  19. UCA0BR1 = 0x00 ;
  20. UCA0MCTL = 0x04 ; // Defines the modulation mask.
  21. // This helps minimize errors.
  22. UCA0CTL1 & = ~ UCSWRST ; // Turn the UART back on.
  23. P1DIR | = BIT0 ; // Setup LEDs.
  24. P1DIR | = BIT1 ;
  25. P1OUT & = ~ BIT0 ;
  26. P1OUT & = ~ BIT1 ;
  27. UARTWriteString ( "--- Hi, Habr! ---" ) ;
  28. unsigned char data ;
  29. while ( true )
  30. {
  31. while ( ! ( IFG2 & UCA0RXIFG ) ) // Check that the receive buffer is ready.
  32. if ( watchdogReset )
  33. WDTCTL = WDTPW + WDTCNTCL ; // Reset the watchdog timer to zero.
  34. data = UCA0RXBUF ;
  35. if ( data == 0x01 )
  36. {
  37. UARTWriteString ( "--- Redeem green LED .---" ) ;
  38. P1OUT & = ~ BIT1 ;
  39. }
  40. else if ( data == 0x02 )
  41. {
  42. UARTWriteString ( "--- Ignite the green LED .---" ) ;
  43. P1OUT | = BIT1 ;
  44. }
  45. else if ( data == 0x03 )
  46. {
  47. UARTWriteString ( "--- Switch the state of the red LED .---" ) ;
  48. P1OUT ^ = BIT0 ;
  49. }
  50. else
  51. {
  52. UARTWriteString ( "--- Received data does not match .---" ) ;
  53. watchdogReset = false ;
  54. }
  55. }
  56. }
  57. void UARTWriteString ( string str )
  58. {
  59. int strSize = str. length ( ) ;
  60. for ( int i = 0 ; i < strSize ; i ++ )
  61. {
  62. WDTCTL = WDTPW + WDTCNTCL ; // Reset the watchdog timer to zero.
  63. while ( ! ( IFG2 & UCA0TXIFG ) ) ; // Check the readiness of the send buffer.
  64. UCA0TXBUF = str [ i ] ;
  65. }
  66. }

To work with a COM port on the computer side, we need software, my choice fell on the COM Port Toolkit . What exactly you choose does not matter.

A small video that will allow you to understand what exactly the example does.


Unlike previous articles, I tried to give intelligible comments right in the program code. I see no reason at this time to explain each register used in the code. I just want to draw attention to some aspects.

  1. There is a good online calculator for calculating the UCA0BR0 , UCA0BR1 and UCA0MCTL values.
  2. SMCLK is a signal that comes from an external resonator (if installed) or from DCO using dividers 1, 2, 4 or 8. Used as a clock signal for peripherals.
  3. In the example, watchdog is used not quite as intended, this code only explains how it works. The point is that if your program does not set the WDTCNTCL bit in the WDTCTL register to one in 32ms ( WDT_MRST_32 ), the system will be reset. Watchdog is required to prevent your software from hanging.
  4. The WDTCTL register has 16 bits, the first 8 need to be set in the WDTPW every time you write to it. This is a register protection mechanism against accidental writing in case of software failures.
  5. Description and configuration of the Basic Clock System is a reason for the whole article, while you should understand that the MSP430 has a very flexible system of clock generators, which, in a simplified form, can be configured using Grace.
  6. Using such a debugging method greatly slows down the program as a whole, but it allows you to reliably determine the order of code execution, including in interrupt handlers.


Conclusion


The longer I write, the more difficult and more the material becomes. This article will be the penultimate in the series for beginners.
Next time I will touch on all the questions that I did not have time to answer earlier.

I hope this article has been helpful to you, reader.

Source: https://habr.com/ru/post/137719/


All Articles