本帖最后由 zoomone 于 2012-6-24 22:01 编辑
主文件代码如下:
#include <msp430.h>
#include "init.h"
#include "lcd.h"
/***************主函数****************/
void main(void)
{
/*关闭所有的IO口*/
P1DIR = 0XFF;P1OUT = 0XFF;
P2DIR = 0XFF;P2OUT = 0XFF;
WDTCTL = WDTPW + WDTHOLD; //关闭看门狗
/*------选择系统主时钟为1MHz-------*/
BCSCTL1 = CALBC1_1MHZ; // Set DCO
DCOCTL = CALDCO_1MHZ;
/*各种初始化*/
music_init();
uart_init();
temp_init();
LCD_init();
//打开全局中断
_BIS_SR( GIE);
send_string("请选择功能: 1,播放歌曲 2,歌曲停止 3,测温度"); //开机菜单
while(1)
{ Play_Song(); }
}
/*******************************************
定时器A的中断服务函数
********************************************/
#pragma vector = TIMER0_A0_VECTOR
__interrupt void TimerA_ISR(void)
{
counter++;
if(new) {
if(new==1)
{send_string(" 歌曲开 曲名:《祝你平安》"); P1OUT|=BIT0;
LCD_clear(); LCD_write_str(1,0,"music on ! ");
delay_ms(250);
}
else if(new==2)
{send_string(" 歌曲关 ")1OUT&=~BIT0;
LCD_clear();
LCD_write_str(1,0,"music off ! ");
delay_ms(250);}
if(new==3) temp_get();;
new=0;
}
}
void Delay_Nms(uchar n)
{
uchar i,j;
for( i = 0;i < n; i++ )
{
for( j = 0;j < 3;j++ ) _NOP();
}
}
/*******************************************
播放《祝你平安》的乐曲
********************************************/
void Play_Song(void)
{
uchar Temp1,Temp2;
uchar addr = 0;
counter = 0; //中断计数器清0
while(music_on==1)
{ if(music_on==2)
break;
Temp1 = SONG[addr++];
if ( Temp1 == 0xFF ) //休止符
{ TACTL &=~MC_1; //停止计数
Delay_Nms(100);
}
else if ( Temp1 == 0x00 ) //歌曲结束符
{ return; }
else
{ Temp2 = SONG[addr++];
TACTL |=MC_1; //开始计数
while(music_on==1)
{
Buzzer_Port ^= Buzzer;
Delay_Nms(Temp1);
if ( Temp2 == counter ) {
counter = 0;
break;
}
}
}
}
}
void send_string(char *str)
{
volatile char i=0;
while(str)
{while (!(IFG2 & UCA0TXIFG));
UCA0TXBUF =str[i++];
} while (!(IFG2 & UCA0TXIFG));
UCA0TXBUF ='\n';
while (!(IFG2 & UCA0TXIFG));
UCA0TXBUF ='\r';
}
// Echo back RXed character, confirm TX buffer is ready first
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF =UCA0RXBUF;// UCA0RXBUF; // TX -> RXed character
if(UCA0RXBUF=='1')
{music_on=1;new=music_on;}
else if(UCA0RXBUF=='2')
{music_on=2;new=music_on;}
else if(UCA0RXBUF=='3')
{music_on=3;new=music_on;}
}
void temp_get()
{
if(music_on==3)
{ music_on=0;
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
}
}
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR (void)
{
// oC = ((A10/1024)*1500mV)-986mV)*1/3.55mV = A10*423/1024 - 278
temp = ADC10MEM;
IntDegC = ((temp - 673) * 423) / 1024; //转化好的摄氏温度
send_string(" 当前温度为:");
UCA0TXBUF=ch[IntDegC/10];while (!(IFG2&UCA0TXIFG)); //串口终端显示
UCA0TXBUF=ch[IntDegC%10];while (!(IFG2&UCA0TXIFG));
while (!(IFG2 & UCA0TXIFG));UCA0TXBUF ='\n';
while (!(IFG2 & UCA0TXIFG));UCA0TXBUF ='\r';
LCD_clear();
LCD_write_str(1,0,"temperature is "); //LCD1602显示
delay_ms(250);
LCD_write_char(6,1,ch[IntDegC/10]);
delay_ms(250);
LCD_write_char(7,1,ch[IntDegC%10]);
delay_ms(250);
LCD_write_char(9,1,'C');
delay_ms(250);
}
|