msp430f5529 uart pwm adc
[复制链接]
//msp430f5529的串口使用程序使用示例
//--------------------------------------------- uart 头文件 -------------------------------------------------------------//
#ifndef UART_H_
#define UART_H_
#include <msp430f5529.h>
#include "config.h"
//默认为115200
void USCIA0_Init(void);
void USCIA0_SendChar(u8 c);
u8 USCIA0_ReceiveChar(void);
void USCIA0_SendString(u8 *str);
void USCIA1_Init(void);
void USCIA1_SendChar(u8 c);
u8 USCIA1_ReceiveChar(void);
void USCIA1_SendString(u8 *str);
//void Uart1_Send_AF(void);
#endif /* #ifndef USCI_A0_h */
/*
//使用例程
void main(void)
{
volatile unsigned int i;
u8 data;
dog_Disable();
UCS_Init();
//key_Init();
USCIA0_Init();
while(1)
{
USCIA0_SendChar('A');
USCIA0_SendString(" luoxn28");
data = USCIA0_ReceiveChar();
if(data != 0)
{
USCIA0_SendChar(data);
USCIA0_SendChar('\r');USCIA0_SendChar('\n');
}
delay(10);
}
}
*/
//--------------------------------------------- uart 源文件 -------------------------------------------------------------//
#include "msp430_UART.h"
#if USCIA0_EN >0
/*********************************************************
*名称:USCIA0_Init
*功能:串口初始化
*入口参数:无
*出口参数:无
*说明:设置为P3.3和P3.4为串口通信端口 3.3-UCA0TXD 3.4-UCA0RXD
**********************************************************/
void USCIA0_Init(void)
{
P3SEL |= BIT3+BIT4; // P3.3,4 = USCI_A0 TXD/RXD
UCA0CTL1 |= UCSWRST; // **Put state machine in reset**
UCA0CTL1 |= UCSSEL__ACLK; // ACLK
UCA0BR0 = 34; // 4MHz 115200
UCA0BR1 = 0; // 4MHz 115200
UCA0MCTL |= UCBRS_6 + UCBRF_0; // Modulation UCBRSx=1, UCBRFx=0
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
//IE2 |= UCA0RXIE + UCA0TXIE; // Enable USCI_A0 TX/RX interrupt
//IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt
//__bis_SR_register(GIE); // Enter LPM3 w/ interrupts enabled
}
//串口0发送字符函数
void USCIA0_SendChar(u8 c)
{
UCA0TXBUF=c;
while(!(UCA0IFG & UCTXIFG)); // USCI_A0 TX buffer ready?
UCA0IFG &= ~UCTXIFG;
}
//串口0接收字符函数
u8 USCIA0_ReceiveChar(void)
{
u8 data = 0;
//阻塞式返回值
while(!(UCA0IFG & UCRXIFG)); // USCI_A0 TX buffer ready?
UCA0IFG &= ~UCRXIFG;
data = UCA0RXBUF;
return data;
/*
//非阻塞式返回值
if(UCA0IFG & UCRXIFG)
{
UCA0IFG &= ~UCRXIFG;
data = UCA0RXBUF;
}
return data;
*/
}
//串口0发送字符串函数
void USCIA0_SendString(u8 *str)
{
while(*str != '\0')
{
USCIA0_SendChar(*str);
str++;
}
}
#endif //#if USCIA0_EN >0
#if USCIA1_EN > 0
/*********************************************************
*名称:USCIA0_Init
*功能:串口初始化
*入口参数:无
*出口参数:无
*说明:设置为P4.4和P4.5为串口通信端口 4.4-UCA1TXD 4.5-UCA1RXD
**********************************************************/
//串口1初始化函数
void USCIA1_Init(void)
{
P4SEL |= BIT4+BIT5; // P3.3,4 = USCI_A0 TXD/RXD
UCA1CTL1 |= UCSWRST; // **Put state machine in reset**
UCA1CTL1 |= UCSSEL__ACLK; // ACLK
UCA1BR0 = 34; // 4MHz 115200
UCA1BR1 = 0; // 4MHz 115200
UCA1MCTL |= UCBRS_6 + UCBRF_0; // Modulation UCBRSx=1, UCBRFx=0
UCA1CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
//IE2 |= UCA0RXIE + UCA0TXIE; // Enable USCI_A0 TX/RX interrupt
//IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt
//__bis_SR_register(GIE); // Enter LPM3 w/ interrupts enabled
}
//串口1发送字符函数
void USCIA1_SendChar(u8 c)
{
UCA1TXBUF=c;
while(!(UCA1IFG & UCTXIFG)); // USCI_A0 TX buffer ready?
UCA1IFG &= ~UCTXIFG;
}
//串口1接收字符函数
u8 USCIA1_ReceiveChar(void)
{
u8 data = 0;
//阻塞式返回值
while(!(UCA1IFG & UCRXIFG)); // USCI_A0 TX buffer ready?
UCA1IFG &= ~UCRXIFG;
data = UCA1RXBUF;
return data;
/*
//非阻塞式返回值
if(UCA1IFG & UCRXIFG)
{
UCA1IFG &= ~UCRXIFG;
data = UCA1RXBUF;
}
return data;
*/
}
//串口1发送字符串函数
void USCIA1_SendString(u8 *str)
{
while(*str != '\0')
{
USCIA1_SendChar(*str);
str++;
}
}
#endif //#if USCIA1_EN > 0
/*
#include "UART.h"
void UART0_Init(void)
{
P3SEL = 0x30; // 配置P3.4和P3.5脚为串口的输入输出口
UCA0CTL1 |= UCSWRST; // 复位串口
UCA0CTL1 |= UCSSEL_1; // 选择串口时钟为ACLK(32768HZ)
UCA0BR0 = 0x03; // 设置串口波特率为9600
UCA0BR1 = 0x00;
UCA0MCTL = UCBRS_3+UCBRF_0;
UCA0CTL1 &= ~UCSWRST; // 打开串口
UCA0IE |= UCRXIE; // 打开串口接收中断
}
*/
//PWM的程序示例
//--------------------------------------------- pwm头文件 -------------------------------------------------------------//
#ifndef MSP430_TIMER_H_
#define MSP430_TIMER_H_
#include <msp430f5529.h>
#include "config.h"
//---------------------- the config of timerA0 pwm ----------------------
void TimerA0_1_PWM_Init(int num, int period); //P1.2
void TimerA0_2_PWM_Init(int num, int period); //P1.3
void TimerA0_3_PWM_Init(int num, int period); //P1.4
void TimerA0_4_PWM_Init(int num, int period); //P1.5
//---------------------- the config of timerA1 pwm ----------------------
void TimerA1_1_PWM_Init(int num, int period); //P2.0
#define TimerA1_1_SetPWM(num) (TA1CCR1=(num))
void TimerA1_2_PWM_Init(int num, int period); //P2.1
#define TimerA1_2_SetPWM(num) (TA1CCR2=(num))
//---------------------- the config of timerA2 pwm ----------------------
void TimerA2_1_PWM_Init(int num, int period); //P2.4
#define TimerA2_1_SetPWM(num) (TA2CCR1=(num))
void TimerA2_2_PWM_Init(int num, int period); //P2.5
#define TimerA2_2_SetPWM(num) (TA2CCR2=(num))
//---------------------- the config of timerB0 pwm ----------------------
void TimerB0_1_PWM_Init(int num, int period); //P5.7
void TimerB0_2_PWM_Init(int num, int period); //P7.4
void TimerB0_3_PWM_Init(int num, int period); //P7.5
void TimerB0_4_PWM_Init(int num, int period); //P7.6
void TimerB0_5_PWM_Init(int num, int period); //P3.5
void TimerB0_6_PWM_Init(int num, int period); //P3.6
#endif
//--------------------------------------------- pwm源文件 -------------------------------------------------------------//
#include "msp430_timer.h"
//period 周期值,num百分比值, timerA0的period要一样
void TimerA0_1_PWM_Init(int num, int period) //P1.2
{
P1DIR |= BIT2;
P1SEL |= BIT2;
TA0CCR0 = period;
//TA0CCTL1 = OUTMOD_3;//先低电平后高电平
TA0CCTL1 = OUTMOD_7;//先高电平后低电平
TA0CCR1 = num;
TA0CTL |= TASSEL_2 + MC_1;//以SMCLK为时钟源,工作在up模式 SMCLK 1MHz
TA0CTL |= TACLR; //将计时器A0清零
//TA1CCTL0=CCIE; //定时器A1的捕获比较器0开启CCIFG位中断
//TA1CCTL1=CCIE; //捕获比较器1开启中断
}
void TimerA0_2_PWM_Init(int num, int period) //P1.3
{
P1DIR |= BIT3;
P1SEL |= BIT3;
TA0CCR0 = period;
//TA0CCTL2 = OUTMOD_3;//先低电平后高电平
TA0CCTL2 = OUTMOD_7;//先高电平后低电平
TA0CCR2 = num;
TA0CTL |= TASSEL_2 + MC_1;//以SMCLK为时钟源,工作在up模式 SMCLK 1MHz
TA0CTL |= TACLR; //将计时器A0清零
}
void TimerA0_3_PWM_Init(int num, int period) //P1.4
{
P1DIR |= BIT4;
P1SEL |= BIT4;
TA0CCR0 = period;
//TA0CCTL3 = OUTMOD_3;//先低电平后高电平
TA0CCTL3 = OUTMOD_7;//先高电平后低电平
TA0CCR3 = num;
TA0CTL |= TASSEL_2 + MC_1;//以SMCLK为时钟源,工作在up模式 SMCLK 1MHz
TA0CTL |= TACLR; //将计时器A0清零
}
void TimerA0_4_PWM_Init(int num, int period) //P1.5
{
P1DIR |= BIT5;
P1SEL |= BIT5;
TA0CCR0 = period;
//TA0CCTL4 = OUTMOD_3;//先低电平后高电平
TA0CCTL4 = OUTMOD_7;//先高电平后低电平
TA0CCR4 = num;
TA0CTL |= TASSEL_2 + MC_1;//以SMCLK为时钟源,工作在up模式 SMCLK 1MHz
TA0CTL |= TACLR; //将计时器A0清零
}
//period 周期值,num百分比值, timerA1的period要一样
void TimerA1_1_PWM_Init(int num, int period) //P2.0
{
P2DIR |= BIT0;
P2SEL |= BIT0;
TA1CCR0 = period;
//TA1CCTL1 = OUTMOD_3;//先低电平后高电平
TA1CCTL1 = OUTMOD_7;//先高电平后低电平
TA1CCR1 = num;
TA1CTL |= TASSEL_2 + MC_1;//以SMCLK为时钟源,工作在up模式 SMCLK 1MHz
TA1CTL |= TACLR; //将计时器A0清零
}
void TimerA1_2_PWM_Init(int num, int period) //P2.1
{
P2DIR |= BIT1;
P2SEL |= BIT1;
TA1CCR0 = period;
//TA1CCTL2 = OUTMOD_3;//先低电平后高电平
TA1CCTL2 = OUTMOD_7;//先高电平后低电平
TA1CCR2 = num;
TA1CTL |= TASSEL_2 + MC_1;//以SMCLK为时钟源,工作在up模式 SMCLK 1MHz
TA1CTL |= TACLR; //将计时器A0清零
}
//period 周期值,num百分比值, timerA2的period要一样
void TimerA2_1_PWM_Init(int num, int period) //P2.4
{
P2DIR |= BIT4;
P2SEL |= BIT4;
TA2CCR0 = period;
//TA2CCTL1 = OUTMOD_3;//先低电平后高电平
TA2CCTL1 = OUTMOD_7;//先高电平后低电平
TA2CCR1 = num;
TA2CTL |= TASSEL_2 + MC_1;//以SMCLK为时钟源,工作在up模式 SMCLK 1MHz
TA2CTL |= TACLR; //将计时器A0清零
}
void TimerA2_2_PWM_Init(int num, int period) //P2.5
{
P2DIR |= BIT5;
P2SEL |= BIT5;
TA2CCR0 = period;
//TA2CCTL2 = OUTMOD_3;//先低电平后高电平
TA2CCTL2 = OUTMOD_7;//先高电平后低电平
TA2CCR2 = num;
TA2CTL |= TASSEL_2 + MC_1;//以SMCLK为时钟源,工作在up模式 SMCLK 1MHz
TA2CTL |= TACLR; //将计时器A0清零
}
//period 周期值,num百分比值, timerB0的period要一样
void TimerB0_1_PWM_Init(int num, int period) //P5.7
{
P5DIR |= BIT7;
P5SEL |= BIT7;
TB0CCR0 = period;
//TB0CCTL1 = OUTMOD_3;//先低电平后高电平
TB0CCTL1 = OUTMOD_7;//先高电平后低电平
TB0CCR1 = num;
TB0CTL |= TASSEL_2 + MC_1;//以SMCLK为时钟源,工作在up模式 SMCLK 1MHz
TB0CTL |= TACLR; //将计时器A0清零
}
void TimerB0_2_PWM_Init(int num, int period) //P7.4
{
P7DIR |= BIT4;
P7SEL |= BIT4;
TB0CCR0 = period;
//TB0CCTL2 = OUTMOD_3;//先低电平后高电平
TB0CCTL2 = OUTMOD_7;//先高电平后低电平
TB0CCR2 = num;
TB0CTL |= TASSEL_2 + MC_1;//以SMCLK为时钟源,工作在up模式 SMCLK 1MHz
TB0CTL |= TACLR; //将计时器A0清零
}
void TimerB0_3_PWM_Init(int num, int period) //P7.5
{
P7DIR |= BIT5;
P7SEL |= BIT5;
TB0CCR0 = period;
//TB0CCTL3 = OUTMOD_3;//先低电平后高电平
TB0CCTL3 = OUTMOD_7;//先高电平后低电平
TB0CCR3 = num;
TB0CTL |= TASSEL_2 + MC_1;//以SMCLK为时钟源,工作在up模式 SMCLK 1MHz
TB0CTL |= TACLR; //将计时器A0清零
}
void TimerB0_4_PWM_Init(int num, int period) //P7.6
{
P7DIR |= BIT6;
P7SEL |= BIT6;
TB0CCR0 = period;
//TB0CCTL4 = OUTMOD_3;//先低电平后高电平
TB0CCTL4 = OUTMOD_7;//先高电平后低电平
TB0CCR4 = num;
TB0CTL |= TASSEL_2 + MC_1;//以SMCLK为时钟源,工作在up模式 SMCLK 1MHz
TB0CTL |= TACLR; //将计时器A0清零
}
void TimerB0_5_PWM_Init(int num, int period) //P3.5
{
P3DIR |= BIT5;
P3SEL |= BIT5;
TB0CCR0 = period;
//TB0CCTL5 = OUTMOD_3;//先低电平后高电平
TB0CCTL5 = OUTMOD_7;//先高电平后低电平
TB0CCR5 = num;
TB0CTL |= TASSEL_2 + MC_1;//以SMCLK为时钟源,工作在up模式 SMCLK 1MHz
TB0CTL |= TACLR; //将计时器A0清零
}
void TimerB0_6_PWM_Init(int num, int period) //P3.6
{
P3DIR |= BIT6;
P3SEL |= BIT6;
TB0CCR0 = period;
//TB0CCTL6 = OUTMOD_3;//先低电平后高电平
TB0CCTL6 = OUTMOD_7;//先高电平后低电平
TB0CCR6 = num;
TB0CTL |= TASSEL_2 + MC_1;//以SMCLK为时钟源,工作在up模式 SMCLK 1MHz
TB0CTL |= TACLR; //将计时器A0清零
}
//msp430f5529的ADC使用示例 单通道连续转换模式和多通道连续转化模式
//--------------------------------------------- ADC12头文件 -------------------------------------------------------------//
#ifndef MSP430_ADC_H_
#define MSP430_ADC_H_
#include "msp430f5529.h"
#include "config.h"
#define ADC_CH1_SEL P6SEL
#define ADC_CH2_SEL P6SEL
#define ADC_CH3_SEL P6SEL
#define ADC_CH4_SEL P6SEL
#define ADC12_CH1 BIT4
#define ADC12_CH2 BIT5
#define ADC12_CH3 BIT6
#define ADC12_CH4 BIT7
void ADC12_Init(void);
void ADC12_GetValue(void);
void ADC12_Disable(void);
void ADC12_Enable(void);
#endif
/*
//单个通道的连续转换 A5 P6.5
void ADC12_Init(void)
{
WHEEL_PORT_DIR |= WHEEL_ENABLE;
WHEEL_PORT_OUT |= WHEEL_ENABLE; // Enable wheel
ADC12CTL0 = ADC12SHT02 + ADC12ON; // Sampling time, ADC12 on
ADC12CTL1 = ADC12SHP; // Use sampling timer
ADC12MCTL0 = ADC12INCH_5; // Use A5 (wheel) as input
ADC12CTL0 |= ADC12ENC; // Enable conversions
ADC_CH2_SEL |= ADC12_CH2; // P6.5 ADC option select (A5)
}
*/
//--------------------------------------------- ADC12源文件 -------------------------------------------------------------//
//file: msp430_adc.c
#include "msp430_adc.h"
#define WHEEL_PORT_DIR P8DIR
#define WHEEL_PORT_OUT P8OUT
#define WHEEL_ENABLE BIT0
u16 ADC_Value[4];
u16 ADC_ValueOld = 0;
/*-----------------------------------------------
* 说明:ADC12初始化,4通道扫描,A4,A5,A6,A7
-----------------------------------------------*/
void ADC12_Init(void)
{
WHEEL_PORT_DIR |= WHEEL_ENABLE;
WHEEL_PORT_OUT |= WHEEL_ENABLE; // Enable wheel
ADC_CH2_SEL |= (ADC12_CH1 + ADC12_CH2 + ADC12_CH3 + ADC12_CH4); // P6.5 ADC option select (A5)
ADC12CTL0 = ADC12SHT02 + ADC12ON + ADC12MSC; // Sampling time, ADC12 on
//ADC12MSC 1:当前转换模式不是单通道单次转换
ADC12CTL1 = ADC12SHP + ADC12CONSEQ_3; // Use sampling timer
//ADC12SHP 使用采样定时器输出作采集、转换信号 SAMPCON
//重复采样序列
ADC12CTL1 |= ADC12CSTARTADD3;//起始采样通道
ADC12MCTL4 = ADC12INCH_4;
ADC12MCTL5 = ADC12INCH_5;
ADC12MCTL6 = ADC12INCH_6;
ADC12MCTL7 = ADC12INCH_7 + ADC12EOS; // 结束采样通道
ADC12CTL0 |= ADC12ENC; // Enable conversions
}
/*---------------------------------------------------------
* 说明:得到4个通道的扫描返回值,放到全局变量ADC_Value[4]中
---------------------------------------------------------*/
void ADC12_GetValue(void)
{
//measure ADC value
ADC12IE = ADC12IE7; // Enable interrupt
ADC12CTL0 |= ADC12SC; // Start sampling/conversion
__bis_SR_register(LPM0_bits + GIE); // LPM0, ADC12_ISR will force exit
ADC12IE = 0x00; // Disable interrupt
//return ADC_Value;
}
void ADC12_Disable(void)
{
ADC12CTL0 &= ~ADC12ENC; // Disable conversions
ADC12CTL0 &= ~ADC12ON; // ADC12 off
}
void ADC12_Enable(void)
{
ADC12CTL0 |= ADC12ON; // ADC12 on
ADC12CTL0 |= ADC12ENC; // Enable conversions
}
/*---------------------------------------------------------
* 说明:4个通道的扫描返回值,更新全局变量ADC_Value[4]中
---------------------------------------------------------*/
#pragma vector = ADC12_VECTOR
__interrupt void ADC12_ISR(void)
{
switch (__even_in_range(ADC12IV, ADC12IV_ADC12IFG15))
{
// Vector ADC12IV_NONE: No interrupt
case ADC12IV_NONE:
break;
// Vector ADC12IV_ADC12OVIFG: ADC overflow
case ADC12IV_ADC12OVIFG:
break;
// Vector ADC12IV_ADC12TOVIFG: ADC timing overflow
case ADC12IV_ADC12TOVIFG:
break;
// Vector ADC12IV_ADC12IFG0: ADC12IFG0:
case ADC12IV_ADC12IFG0:
break;
// Vector ADC12IV_ADC12IFG1: ADC12IFG1
case ADC12IV_ADC12IFG1:
break;
// Vector ADC12IV_ADC12IFG2: ADC12IFG2
case ADC12IV_ADC12IFG2:
break;
// Vector ADC12IV_ADC12IFG3: ADC12IFG3
case ADC12IV_ADC12IFG3:
break;
// Vector ADC12IV_ADC12IFG4: ADC12IFG4
case ADC12IV_ADC12IFG4:
break;
// Vector ADC12IV_ADC12IFG5: ADC12IFG5
case ADC12IV_ADC12IFG5:
break;
// Vector ADC12IV_ADC12IFG6: ADC12IFG6
case ADC12IV_ADC12IFG6:
break;
// Vector ADC12IV_ADC12IFG7: ADC12IFG7
case ADC12IV_ADC12IFG7:
ADC_Value[0] = ADC12MEM4; // ADC12MEM = A0 > 0.5AVcc?
ADC_Value[1] = ADC12MEM5;
ADC_Value[2] = ADC12MEM6;
ADC_Value[3] = ADC12MEM7;
//LED_On(BIT2);
__bic_SR_register_on_exit(LPM0_bits); // Exit active CPU
break;
// Vector ADC12IV_ADC12IFG8: ADC12IFG8
case ADC12IV_ADC12IFG8:
break;
// Vector ADC12IV_ADC12IFG9: ADC12IFG9
case ADC12IV_ADC12IFG9:
break;
// Vector ADC12IV_ADC12IFG10: ADC12IFG10
case ADC12IV_ADC12IFG10:
break;
// Vector ADC12IV_ADC12IFG11: ADC12IFG11
case ADC12IV_ADC12IFG11:
break;
// Vector ADC12IV_ADC12IFG12: ADC12IFG12
case ADC12IV_ADC12IFG12:
break;
// Vector ADC12IV_ADC12IFG13: ADC12IFG13
case ADC12IV_ADC12IFG13:
break;
// Vector ADC12IV_ADC12IFG14: ADC12IFG14
case ADC12IV_ADC12IFG14:
break;
// Vector ADC12IV_ADC12IFG15: ADC12IFG15
case ADC12IV_ADC12IFG15:
break;
default:
break;
}
}
|