这是由TI公司推出的一款比較单片机, 相对stm32来说简单些, 由于它是16位的, 所以我们在学习中可能也会像51一样, 直接操纵寄存器.
TI设计这款单片机的初衷是, 让它用于低功耗的嵌入式设备中.
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjE5NzA4NTc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
Reduced instruction set computing, or RISC (pronounced 'risk'), is aCPU design strategy based on
the insight that a simplified instruction set (as opposed to a complex set) provides higher performance when combined with amicroprocessor architecture capable of executing
those instructions using fewermicroprocessor cycles per instruction
以上是MSP430使用的精简混合指令集 wiki解释, 也是为了减少功率
MSP430也使用了集成模拟外设, 可以减少功耗。提高启动速度
单片机的自己主动执行与时钟系统分不开。始终系统通常须要满足4个条件:
1.高频率 用于高速响应
2.低频性 用于减少电流消耗
3. 稳定的频率 满足定时需求 如 RTC real-time clock
4.多时钟源 同意单片机在不同的频率下工作,能够同一时候使用外部晶振和内部振荡器,比如 DCO
MSP430内部有3个时钟, 用于应对不同系统最小化能量消耗
MCLK 主时钟
SMCLK副时钟
ACLK辅助时钟
The MSP430 features 3 clocks which can run at maximum of 16MHz depending upon the conditions. The reason we have three clocks instead of just one or even two is to compromise between systems that need speed and the ability to minimize power consumption,
which is what the msp430 was designed for. These three clocks are:-
- MCLK: This stands for Master Clock, and is the one that drives the processor most of the time.
- 由DCO产生 。也可由外部振荡器产生。 启动须要6us, 一般用于CPU 和快速外设
- SMCLK: The Sub-Main Clock is a secondary clock which is used by other peripherals particularly the internal peripherals.
- 外设选择时钟源的时候使用
- ACLK: The Auxiliary Clock is usually timed outside the MSP430 and is typically used for peripherals.
- 一般用于低俗外设,能够设置
開始一个project的开头必定是配置时钟了
DCO是digital controled Oscillator, 数字晶振, 它没有外部竞争准确, 可是控制的范围已经非常广了, 一定够用.
DCO能够控制时钟, 它由两个寄存器操作
Apart from a versatile clock system, we have 4 different sources to control these three clocks. We will have a look at them at the correct time, however for now one of is theDCO. It’s the digitally controlled oscillator which might not be as accurate
or stable while compared to external crystals, but still is quite good and can operate over a wide range of frequencies.
Lets’ move onto the DCO. The advantage with the DCO is that it requires no external parts and can be fully controlled by the software. There are only two registers should be configured to change yourDCO frequency and that areDCOCTL
and BCSCTL1.
这两句话用寄存器把DCO设置为1MHZ;
basic clock select controller
BCSCTL1=CALBC1—1MHZ;
DCOCTL=CALDCO—1MHZ。
设置MCLK
BCSCTL2 |= SELM——1+DIVM_3;
讲一讲引脚Pin的寄存器
每个Pin都有几个寄存器
PxDIR 数据方向, 假设选择复用功能这个不用管
P1DIR=0xff 将P1作为输出
P1OUT=0x20 P1输出20
P1DIR=0x00
data=P1IN 读取输入值
PxIES 0为上升沿中段,1 为下降沿中断
PxREN 上拉下拉电阻使能寄存器
PxIn Px Out 输入输出的数据
PxSEL(2) 复用选择
PxIFG interrupt Flag register 中断标志位, 使用后须要清理
要想打开中断呢, 一般就是 _EINT();
关闭就是 _DINT();
void main(void)
{ WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P1DIR |= BIT0; // Set P1.0 to output direction
P1IES |= BIT3; // P1.3 下降沿
P1IFG &= ~BIT3; // P1.3 中断标志清除
P1IE |= BIT3; // P1.3 中断使能
_BIS_SR(LPM4_bits + GIE); // 进入低功耗模式,开启全局中断
}
// Port 1 interrupt service routine
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void) //中断服务程序
{
if (P1IFG & BIT3)
{
P1OUT ^= BIT0; // P1.0 led灯闪烁
P1IFG &= ~BIT3; // P1.3 清除中断标志
}
}
再说说FLL+
frequency-locked loop (FLL+) clock
这个独特的模块就是 时钟了... 包括我前面说的几个
Timer
增计数模式下。
CCR0 代表周期 范围65535
TAR代表counter
void zengjishu()
{
TACTL=TASSEL1+TACLR; //选择计数时钟为ACLK。将计数器TAR清零
CCTL0=CCIE; //使能中断
CCR0=200; //计数终值。方波频率为:32768/200/2
TACTL|=MC_1; //选择Timer_A为增计数模式
P1DIR|=BIT0; //P1.0作为输出
_EINT(); //使能总中断
}
#pragma vertor =TIMERA0_VECTOR
__interrupt void Timer_A(void)
{
P1OUT^=0X01; //P1.0取反
}
capture compare
当CCTLx中的CAP=1时进入capture模式。 检測到设定的脉冲沿。TAxR会被写入TAxCCRx 而且CCIFG置位
CM1 CM0 能够选择捕获条件。 有 禁止 捕获 上升沿 下降沿 上升下降沿
可是中断请求 必需要 GIE CCIE都同意才干够
当CCTLx中的CAP=0时进入compare模式,
TAxR记数到TAxCCRn时 CCIFG置位 EQUx =1
??? EQUx依据输出模式影响输出
输入信号TAxCCI 被*到 SCCI中
OUTMODx
|
模式
|
说明
|
000
|
输出模式0:
输出
|
输出信号取决与寄存器 TACCTLx 中的 OUT位。当 OUT位更新时。输出信号马上更新。
|
001
|
输出模式1:
置位
|
输出信号在TAxR等于TAxCCRn时置位。并保持置位到定时器复位或选择还有一种输出模式为止。
|
010
|
输出模式2:
翻转/复位
|
输出在TAxR的值等于TAxCCRn时翻转,当TAxR的值等于TAxCCR0时复位。
|
011
|
输出模式3:
置位/复位
|
输出在TAxR的值等于TAxCCRn时置位,当TAxR的值等于TAxCCR0时复位。
|
100
|
输出模式4:
翻转
|
输出电平在TAxR的值等于TAxCCRn时翻转,输出周期是定时器周期的2倍。
|
101
|
输出模式5:
复位
|
输出在TAxR的值等于TAxCCRn时复位。并保持低电平直到选择还有一种输出模式。
|
110
|
输出模式6:
翻转/置位
|
输出电平在TAxR的值等于TAxCCRn时翻转。当TAxR值等于TAxCCR0时置位。
|
111
|
输出模式7:
复位/置位
|
输出电平在TAxR的值等于TAxCCRn时复位,当TAxR的值等于TAxCCR0时置位。
|
PWM实例
void timerA1_init(){
TA1CTL = TASSEL_2 + TACLR + MC_1; //SMCLK as clock, up mode
TA1CCTL1 = OUTMOD_7; //Compare module 0 is set to Output Mode 7(PWM)
TA1CCR0 = 200;
TA1CCR1 = 100;
}
void PWM_update(float dew){
uchar buffer;
TA1CCR1 = (uchar)((dew+60)/75*180); // dew+15
}
串口设置一般步骤
SPI
void Configure_SPI(){
//--- Initialization of SPI Commuinication (USCI_B) -------------------------
P1SEL |= BIT5 + BIT6 + BIT7; // P1.5 6 7
P1SEL2 |= BIT5 + BIT6 + BIT7; // P1.5 6 7
// IE2 |= UCB0TXIE;
UCB0CTL0 |= UCCKPH + UCMSB+ UCMST+ UCMODE_0 + UCSYNC; // 3-pin, 8-bit SPI mstr, MSb 1st
UCB0CTL1 |= UCSSEL_2; // SMCLK
UCB0BR0 = 0x01;
UCB0BR1 = 0;
UCB0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
}
<div><div>•void TFT_SendData(uint8_t bdata)
</div><div>•{
</div><div>•// uint8_t mask;
</div><div>• ST7735_NCS_CLR();
</div><div>• ST7735_DC_SET();
</div><div><span style="color:#BF0000;">•</span><span style="color:#BF0000;"> </span>/* for(mask=0x80;mask;mask>>=1){ // IO <span style="color:#BF0000;">模拟</span><span style="color:#BF0000;">SPI</span><span style="color:#BF0000;">。
</span></div><div><span style="color:#BF0000;">•</span><span style="color:#BF0000;"> </span>ST7735_SCL_CLR();
</div><div><span style="color:#BF0000;">•</span><span style="color:#BF0000;"> </span>if(bdata&mask)ST7735_SDA_SET();
</div><div><span style="color:#BF0000;">•</span><span style="color:#BF0000;"> </span>else ST7735_SDA_CLR();
</div><div><span style="color:#BF0000;">•</span><span style="color:#BF0000;"> </span>ST7735_SCL_SET();
</div><div><span style="color:#BF0000;">•</span><span style="color:#BF0000;"> </span>}*/
</div><div>• UCB0TXBUF = bdata;
</div><div>• while((UCB0STAT & UCBUSY)==0x01);
</div><div>• ST7735_NCS_SET();
</div><div>•}
</div></div>
ADC
P1SEL&=0x20; //使能A/D通道A5; ADC10CTL0 = ADC10SHT_1+ ADC10ON+SREF_1+REF2_5V+REFON+MSC;
// 打开ADC10内核。确定採样周期为8*ADC10OSC/2,选择内部參考电压为2.5v 选择时钟; ADC10CTL1 = INCH_5+ADC10DIV_1+CONSEQ_2;
// input A5模拟信号输入选择通道A5即P1.5,设置为单通道多次转换模式,分频因子为2 ADC10AE0 |= BIT5; // 使P1.5同意AD模拟输入信号 ADC10CTL0|=ENC; //使能转换。 ADC10CTL0|=ADC10SC; //開始转换。
LowPowerMode
"msp430g2553.h"
#define LPM0 _BIS_SR(LPM0_bits) /* Enter Low Power Mode 0 */
#define LPM0_EXIT _BIC_SR_IRQ(LPM0_bits) /* Exit Low Power Mode 0 */
#define LPM1 _BIS_SR(LPM1_bits) /* Enter Low Power Mode 1 */
#define LPM1_EXIT _BIC_SR_IRQ(LPM1_bits) /* Exit Low Power Mode 1 */
#define LPM2 _BIS_SR(LPM2_bits) /* Enter Low Power Mode 2 */
#define LPM2_EXIT _BIC_SR_IRQ(LPM2_bits) /* Exit Low Power Mode 2 */
#define LPM3 _BIS_SR(LPM3_bits) /* Enter Low Power Mode 3 */
#define LPM3_EXIT _BIC_SR_IRQ(LPM3_bits) /* Exit Low Power Mode 3 */
#define LPM4 _BIS_SR(LPM4_bits) /* Enter Low Power Mode 4 */
#define LPM4_EXIT _BIC_SR_IRQ(LPM4_bits) /* Exit Low Power Mode 4 */
void main(void)
{
WDT_init(); // initialize Watchdog Timer
while(1)
{
__bis_SR_register(LPM3_bits + GIE); // Enter LPM3, enable interrupts
activeMode(); // in active mode. Do stuff!
}
} #pragma vector=WDT_VECTOR
__interrupt void watchdog_timer (void)
{
__bic_SR_register_on_exit(LPM3_bits); // Clear LPM3 bits from 0(SR), Leave LPM3, enter active mode
}