You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
5.6 KiB

  1. #include <msp430fr6989.h>
  2. //GPIO definitions
  3. #define CSBdefinition P1SEL1 |= BIT4 // Configure UCA0STE (slave select)
  4. #define CLKdefinition P1SEL1 |= BIT5 // Configure UCA0CLK
  5. #define SIMOdefinition P2SEL0 |= BIT0 // Configure UCA0SIMO/TXD
  6. //Output configuration
  7. #define DDRP1 P1DIR = 0x78; // 0111 1000
  8. #define DDRP2 P1DIR |= BIT0;
  9. /////////////////////////////////////////
  10. #define RST_0 P1OUT &= ~BIT3
  11. #define RST_1 P1OUT |= BIT3
  12. #define CSB_0 P1OUT &= ~BIT4
  13. #define CSB_1 P1OUT |= BIT4
  14. #define RS_0 P1OUT &= ~BIT6
  15. #define RS_1 P1OUT |= BIT6
  16. #define SI_0 P2OUT &= ~BIT0
  17. #define SI_1 P2OUT |= BIT0
  18. // Global variables
  19. unsigned char text1[] = {"Hi :D "};
  20. unsigned char text2[] = {"It's done! "};
  21. unsigned int position;
  22. //**************************
  23. // MSP430F59xx Demo - eUSCI_A0, SPI 4-Wire Master
  24. //
  25. // Description: SPI master send data to SPI slave using 4-wire mode.
  26. // ACLK = 32.768kHz, MCLK = SMCLK = DCO ~1MHz. BRCLK = ACLK/2
  27. //
  28. //
  29. // MSP430FR6989
  30. // -----------------
  31. // /|\| XIN|-
  32. // | | | 32KHz Crystal
  33. // --|RST XOUT|-
  34. // | |
  35. // | P2.0|-> Data Out (UCA0SIMO)
  36. // | |
  37. // | P1.3|-> RST LCD
  38. // | P1.6|-> RS LCD
  39. // | P1.5|-> Serial Clock Out (UCA0CLK)
  40. // | P1.4|-> Slave Select (UCA0STE)
  41. // | |
  42. //
  43. // William Goh
  44. // Texas Instruments Inc.
  45. // April 2014
  46. // Built with IAR Embedded Workbench V5.60 & Code Composer Studio V6.0
  47. //**************************
  48. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  49. //Function initialization
  50. void initLCD();
  51. void initSPI();
  52. void display();
  53. void wData(unsigned char d);
  54. void wCommand(unsigned char c);
  55. volatile unsigned char TXData;
  56. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  57. void main(void)
  58. {
  59. WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
  60. PM5CTL0 &= ~LOCKLPM5; // Disable the GPIO power-on default high-impedance mode to activate
  61. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  62. //Pin configuration
  63. CSBdefinition; // Configure UCA0STE (slave select)
  64. CLKdefinition; // Configure UCA0CLK
  65. SIMOdefinition; // Configure UCA0SIMO/TXD
  66. PJSEL0 |= BIT4 | BIT5; // For XT1
  67. DDRP1;
  68. DDRP2;
  69. initSPI();
  70. initLCD();
  71. __delay_cycles(50);
  72. display();
  73. __delay_cycles(50);
  74. while(1){
  75. }
  76. }//main
  77. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  78. void initSPI(void){
  79. // Configure USCI_A0 for SPI operation
  80. UCA0CTLW0 = UCSWRST; // **Put state machine in reset**
  81. UCA0CTLW0 |= UCSSEL__SMCLK; // Choose SMLCK (1MHz)
  82. UCA0BR0 = 0x04;//4 // Set prescaler to 8 to get SCLK=125kHz
  83. UCA0BR1 = 0;
  84. UCA0MCTLW = 0;
  85. // By default -> 4-pin, 8-bit SPI master
  86. UCA0CTLW0 |= UCMST; // Master mode
  87. UCA0CTLW0 |= UCSYNC; // Synchronous mode
  88. UCA0CTLW0 |= UCCKPL; // Clock polarity high
  89. UCA0CTLW0 |= UCMSB; // MSB first (Most Significant Bit)
  90. UCA0CTLW0 |= UCMODE_2; // 4-pin SPI with UCxSTE active high
  91. UCA0CTLW0 |= UCSTEM; // Generate the enable signal for a 4-wire slave
  92. UCA0CTLW0 &= ~UCSWRST; // **Initialize USCI state machine**
  93. UCA0IE |= UCTXIE; // Enable USCI_A0 TX interrupt
  94. UCA0IFG &= ~UCTXIFG;
  95. }
  96. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  97. //Initialize the LCD
  98. void initLCD(){
  99. RST_0; //reset
  100. __delay_cycles(2);
  101. RST_1; //end reset
  102. __delay_cycles(20);
  103. wCommand(0x30); //wake up
  104. __delay_cycles(2);
  105. wCommand(0x30); //wake up
  106. wCommand(0x30); //wake up
  107. wCommand(0x39); //function set
  108. wCommand(0x14); //internal osc frequency
  109. wCommand(0x56); //power control
  110. wCommand(0x6D); //follower control
  111. wCommand(0x70); //contrast
  112. wCommand(0x0C); //display on
  113. wCommand(0x06); //entry mode
  114. wCommand(0x01); //clear
  115. __delay_cycles(100);
  116. }
  117. void wCommand(unsigned char c){
  118. CSB_0; //CSB active
  119. RS_0; //Command/Instruction
  120. UCA0TXBUF = c;
  121. CSB_1; //CSB disabled
  122. __delay_cycles(200);
  123. }
  124. void wData(unsigned char d){
  125. CSB_0; //CSB active
  126. RS_1; //Data
  127. UCA0TXBUF = d;
  128. CSB_1; //CSB disabled
  129. }
  130. void display(){
  131. int a;
  132. wCommand(0x80);
  133. for(a=0; a<16; a++){
  134. wData(text1[a]);
  135. }
  136. wCommand(0xC0);
  137. for(a=0; a<16; a++){
  138. wData(text2[a]);
  139. }
  140. }