MSP430 Learning Notes 4-Two timers generate step single frequency tone

Publisher:lcn18560863680Latest update time:2018-04-18 Source: eefocusKeywords:MSP430 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This program is used in the development board to generate sounds of different frequencies. The overall program is relatively simple, mainly using two timers. The code and my comments are as follows.


  1. /********************************************************* 

  2. Program function: Use fixed frequency square wave to drive buzzer, a total of 16 tones; 

  3.           While emitting different tones, the LED lights up to indicate the 

  4.           The number of the current tone (1~16) 

  5. ---------------------------------------------------------- 

  6. Dip switch setting: turn BUZZER to ON and the rest to OFF 

  7. Test instructions: Listen to the tone change of the buzzer. At the same time, the LED also has corresponding indications 

  8. **********************************************************/  

  9. #include   

  10.   

  11. typedef unsigned char uchar;  

  12.   

  13. uchar step = 0xff;  

  14.   

  15. /****************************Main function****************************/  

  16. void main( void )  

  17. {  

  18.     flying i;  

  19.       

  20.     WDTCTL = WDTPW + WDTHOLD;           //关狗  

  21.      

  22.     /*The following six lines of code close all IO ports*/  

  23.     P1DIR = 0XFF;P1OUT = 0XFF;  

  24.     P2DIR = 0XFF;P2OUT = 0XFF;  

  25.     P3DIR = 0XFF;P3OUT = 0XFF;  

  26.     P4DIR = 0XFF;P4OUT = 0XFF;  

  27.     P5DIR = 0XFF;P5OUT = 0XFF;  

  28.     P6DIR = 0XFF;P6OUT = 0XFF;  

  29.     P6DIR |= BIT2;P6OUT |= BIT2; //Disable level conversion  

  30.       

  31.     /*------Select the system main clock as 8MHz-------*/  

  32.     BCSCTL1 &= ~XT2OFF; //Turn on XT2 high frequency crystal oscillator  

  33.     do  

  34.     {  

  35.         IFG1 &= ~OFIFG; // Clear crystal oscillator failure flag  

  36.         //IFG1 is the interrupt register OFIFG is the crystal oscillator startup failure interrupt flag  

  37.         for (i = 0xFF; i > 0; i--); //Wait for 8MHz crystal to start oscillating  

  38.     }  

  39.     while ((IFG1 & OFIFG)); //Does the crystal oscillator failure flag still exist?  

  40.     //The above step is mainly to wait for the crystal oscillator to work normally  

  41.     BCSCTL2 |= SELM_2 + SELS; //MCLK and SMCLK select high frequency crystal  

  42.       

  43.     TACCTL0 |= CCIE; //Enable compare interrupt  

  44.     TACTL |= TASSEL_2 + ID_3 ; //Count clock selection SMLK=8MHz, 1/8 division is 1MHz  

  45.       

  46.     TBCCR0 = 4096*2 - 1; //cycle two seconds  

  47.     //Time calculation: 32768/8*2+1 Note that a watch crystal is used  

  48.     TBCCTL0 |= CCIE;  

  49.     TBCTL |= TBSSEL_1 + ID_3 + MC_1;    //时钟源ACLK/8,up mode  

  50.       

  51.     P6DIR |= BIT7; //Buzzer corresponds to IO 6.7 and is set as output  

  52.     P2DIR = 0xff; //Indicates the corresponding status  

  53.     P2OUT = 0xff;  

  54.       

  55.     _UNITE();  

  56.       

  57.     LPM1;  

  58. }  

  59. /******************************************* 

  60. Function name: Timer_A 

  61. Function: interrupt service function of timer A, driven here 

  62.           Buzzer sounds 

  63. Parameters: None 

  64. Return value: None 

  65. ********************************************/  

  66. #pragma vector=TIMERA0_VECTOR  

  67. __interrupt void Timer_A (void)  

  68. {  

  69.   P6OUT ^= BIT7;                            // Toggle P6.7  

  70. }  

  71. /******************************************* 

  72. Function name: Timer_B 

  73. Function: Interrupt service function of timer B, change here 

  74.           Buzzer sound frequency 

  75. Parameters: None 

  76. Return value: None 

  77. ********************************************/  

  78. #pragma vector=TIMERB0_VECTOR  

  79. __interrupt void Timer_B (void)  

  80. {  

  81.     if(step == 0xff) //The initial value of step is 0xff,  

  82.       TACTL |= MC_1; //TimerA needs to be set to up-counting mode. This can be set during initialization. I don't understand why it is placed here.     

  83.     step++;  

  84.     switch(step)  

  85.     {    

  86.     case    0:  TACCR0 = 5000;  P2OUT = ~1;   break;      // 100Hz  

  87.     //P2OUT uses LED to display the corresponding value, just for demonstration, no practical significance  

  88.     case    1:  TACCR0 = 2500;  P2OUT = ~2;   break;      // 200Hz  

  89.     case    2:  TACCR0 = 1250;  P2OUT = ~3;   break;      // 400Hz  

  90.     case    3:  TACCR0 = 625;   P2OUT = ~4;   break;      // 800Hz  

  91.     case    4:  TACCR0 = 500;   P2OUT = ~5;   break;      // 1KHz  

  92.     case    5:  TACCR0 = 250;   P2OUT = ~6;   break;      // 2KHz  

  93.     case    6:  TACCR0 = 167;   P2OUT = ~7;   break;      // 3KHz  

  94.     case    7:  TACCR0 = 125;   P2OUT = ~8;   break;      // 4KHz  

  95.     case    8:  TACCR0 = 100;   P2OUT = ~9;   break;      // 5KHz  

  96.     case    9:  TACCR0 = 83;    P2OUT = ~10;   break;      // 6KHz  

  97.     case    10:  TACCR0 = 71;   P2OUT = ~11;   break;      // 7KHz  

  98.     case    11:  TACCR0 = 63;   P2OUT = ~12;   break;      // 8KHz  

  99.     case    12:  TACCR0 = 56;   P2OUT = ~13;   break;      // 9KHz  

  100.     case    13:  TACCR0 = 50;   P2OUT = ~14;   break;      // 10KHz  

  101.     case    14:  TACCR0 = 33;   P2OUT = ~15;   break;      // 15KHz  

  102.     case    15:  TACCR0 = 25;   P2OUT = ~16;   break;      // 20KHz  

  103.     case 16: step = 0xff; // Continue to add, the same effect as clearing, loop playback  

  104.     }  

  105. }  



Keywords:MSP430 Reference address:MSP430 Learning Notes 4-Two timers generate step single frequency tone

Previous article:MSP430 Learning Notes 5- Playing Music with Buzzer
Next article:MSP430 Study Notes 3-PWM Generation

Recommended ReadingLatest update time:2024-11-16 20:49

MPLAB X IDE V4.2-2: How to use PIC10F200's TIMER0 timing
Since I have just come into contact with PIC microcontrollers, I need to study carefully. The delay method I use: The work requires PIC10F200 for timing: 1 Initially I used a simple delay() to implement However, this delay is not accurate, and because the crystal oscillator accuracy of each PIC10F200 chip is d
[Microcontroller]
MPLAB X IDE V4.2-2: How to use PIC10F200's TIMER0 timing
Design of sine wave generator analysis scheme using MSP430F149 microcontroller
The SSD-J-2 vibration comfort meter developed according to the UIC513 international standard "Guidelines for Evaluation of Vibration Comfort of Passengers in Railway Vehicles" is a three-dimensional vibration portable analyzer based on MSP430F149, which contains physiological filters for up and down, left and right,
[Microcontroller]
MSP430F149 clock system
1 Overview   MSP430F149 has three clock sources: external LF (XT1 is generally 32.768K), HF (XT2 is generally 8M), internal DCO. Three clock signals can be obtained from the clock system module: MCLK, SMCLK, ACLK.    By default, the MCLK and SMCLK signals come from the DCO and the ACLK comes from the LF. According t
[Microcontroller]
MSP430 MCU Theory Review Knowledge Points
Looking at the development of microprocessors, one is the development towards high-performance computer systems with complex data operations, high-speed communications, information processing and other functions; the other is the emergence of a computer that integrates the central processing unit, memory, I/O interfa
[Microcontroller]
MSP430 MCU IO Overview
1. Overview of MSP430 MCU ports         P1~P6 each group has 8 I/O ports, P3, P4, P5, P6 have I/O and other on-chip peripheral functions, each group has 4 registers. In addition to the above functions, P1 and P2 also have interrupt capabilities, each group has 7 registers. 2. Px port 1. P1, P2 ports (1) PxDIR in
[Microcontroller]
CC2530 bare metal programming series notes 3--Timer Timer1 free mode program
Timer 1 is a 16-bit timer with timer/counter/pulse width modulation functions. It has 3 individually programmable input capture/output comparison channels, each of which can be used as a PWM output or to capture the edge time of the input signal (for more information on what input capture/output comparison is and how
[Microcontroller]
TIMER0 controls a single LED to flash
The program C language source code is as follows: /* INT1 interrupt 5-bit count */ #include reg51.h typedef unsigned char uint8; typedef unsigned int uint16; sbit LED = P0^0; uint16 T_Count = 0; void main() { TMOD = 0x01; TH0 = 0xFC; TL0 = 0x67; IE = 0x82; TR0 = 1; while(1); } void LED_Flash() interrupt 1
[Microcontroller]
TIMER0 controls a single LED to flash
MSP430 MCU Comparator A Module
First of all, the entire comparator A can only work when the CAON position is set to 1. This bit belongs to the CACTL1 control register. This bit is 0 when the microcontroller is powered on, which means that the comparator is not working. The following is a brief description of the functions of several circuits and so
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号