Created
October 23, 2018 10:38
-
-
Save totoroAKI/d178b9adeedd86a88a32fd69be31dea0 to your computer and use it in GitHub Desktop.
Blog2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <msp430.h> //MSP430F2619TPM | |
/** | |
* main.c | |
*/ | |
// 端子設定 ///////////////////////////////////////////////////////// | |
// LED | |
#define test_LED 0b00000001 // P1.0 test_LED | |
#define RED_LED 0b00000010 // P4.1 RED_LED [TB1] | |
#define BLUE_LED 0b00000100 // P4.2 BLUE_LED [TB2] | |
#define GREEN_LED 0b00001000 // P4.3 GREEN_LED[TB3] | |
// ADC | |
#define ADC0 0b00000001 // P6.0 可変抵抗 入力 | |
// Timer B | |
#define Tpwm 8000000/4000 // TimerBの分周用カウンタ | |
#define Tpwm2 4000/2 // TimerBのさらなる分周用カウンタ | |
/////////////////////////////////////////////////////////////////// | |
/* グローバル変数 *///////////////////////////////////////////////////// | |
int INTERRUPT_FLAG = 0; // 割り込み種類フラグ | |
unsigned int TB_CM = 0; // TimerBがコンペアマッチするごとにインクリメント | |
//////////////////////////////////////////////////////////////////// | |
/* 関数定義 *///////////////////////////////////////////////////////// | |
void setup( void ); | |
//////////////////////////////////////////////////////////////////// | |
/* 初期設定 *//////////////////////////////////////////////////////// | |
void setup() { | |
WDTCTL = WDTPW + WDTHOLD; // Stop WathcDog Timer, | |
SVSCTL &= ~SVSFG; // 低電圧フラグを消去 Supply Voltage Supervisor | |
SVSCTL = 0x60 + PORON; // SVS POR enabled @ 2.5V 電圧低下検出.この電圧以下ではマイコンリセット. | |
while(!(SVSCTL & SVSON)); // 電圧監視機能がONになるまで待機 | |
BCSCTL1 = CALBC1_8MHZ; // Set RSELx | |
DCOCTL = CALDCO_8MHZ; // Set DCOx and MODx = MCLK :8MHZ | |
P1DIR = test_LED; // P1.0:OUT | |
P4DIR = RED_LED | BLUE_LED | GREEN_LED; // P4.1,4.2,4.3:OUT, the others;IN | |
P4SEL = RED_LED; // | BLUE_LED | GREEN_LED; | |
P6SEL = ADC0; // P6.0; Primary peripheral module function = A0 | |
// Setup ADC // // The ADC12OSC is generated internally and is in the 5-MHz range | |
ADC12CTL0 = SHT0_2 + SHT1_2 + REFON + ADC12ON; // ADC12OSC(~5MHZ), 16 cycle, Set sampling time:xxus, turn on ADC12 | |
ADC12CTL1 = SHP + ADC12SSEL_0; // Use sampling timer, Single-channel, single-conversion | |
//// 各チャンネルの設定 //// | |
ADC12MCTL0 = SREF_0 + INCH_0; // VRef = AVcc, channel = A0 | |
// SHT0x:Bits 11-8 Sample-and-hold time. for registers ADC12MEM0 to ADC12MEM7. | |
// 0000:4 ADC12CLK cycles 0001:8 ADC12CLK cycles | |
// 0010:16 ADC12CLK cycles 0011:32 ADC12CLK cycles | |
// 0100:64 ADC12CLK cycles 0101:96 ADC12CLK cycles | |
// 0110:128 ADC12CLK cycles 0111:192 ADC12CLK cycles | |
// 1000:256 ADC12CLK cycles 1001:384 ADC12CLK cycles | |
// 1010:512 ADC12CLK cycles 1011:768 ADC12CLK cycles | |
// 1100:1024 ADC12CLK cycles 1101:1024 ADC12CLK cycles | |
// 1110:1024 ADC12CLK cycles 1111:1024 ADC12CLK cycles | |
// setup Timer_B // | |
TBCTL = TBSSEL_2 + ID_0 + MC_1 + TBIE; // Timer_B Control resister (13.3.1 TBCTL, Timer_A Control Register P.391) | |
// 00 TBCLK :外部入力のタイマー用クロック(P1.0 TBCLK) | |
// 01 ACLK :補助クロック(外部32768HZ発振子) /(div) | |
// 10 SMCLK :サブシステムクロック(DCOから) /(div) | |
// 11 INCLK :外部入力クロック(P2.1 TAINCLK) | |
TBCCR0 = Tpwm; // TBCCRx:Bits 15-0 TBCCRx:Timer_B Capture/Compare Resister (13.3.3 TBCCRx, Timer_B Capture/Compare Register x, P.390) | |
TBCCR1 = Tpwm*0.3; // | |
TBCCTL0 = CCIE; // 13.3.4 TBCCTLx, Capture/Compare Control Register, P. | |
TBCCTL1 = OUTMOD_7; // | |
__bis_SR_register(GIE); // 汎用割り込み許可 | |
ADC12CTL0 |= ENC; // ADC go working | |
} | |
//////////////////////////////////////////////////////////////////// | |
//////////////////////////////////////////////////////////////////// | |
/*!!! ************************************************ | |
* タイマー割り込みの処理 | |
******************************************************/ | |
#pragma vector = TIMERB0_VECTOR // Datasheet, Interrupt Vector Addresses P.16 | |
__interrupt void TIMER_B0(void) // Timer interrupt 発生時に割り込む関数の定義 | |
{ | |
TBCTL &= ~TBIFG; // タイマーの割り込みフラグのクリア | |
INTERRUPT_FLAG = 1; | |
TB_CM++; | |
} | |
//////////////////////////////////////////////////////////////////// | |
//////////////////////////////////////////////////////////////////// | |
int main(void) | |
{ | |
setup(); // システム初期設定.これ以前には何も処理をしないこと | |
int AD0 = 0; | |
int Tpwm1 = Tpwm2; | |
P1OUT = test_LED; | |
P4OUT = 0x00; | |
ADC12CTL0 |= ADC12SC; // ADC12 enable, start conversion | |
while(1){ | |
if(INTERRUPT_FLAG == 1){ | |
AD0 = ADC12MEM0; | |
ADC12CTL0 |= ADC12SC; // ADC12 enable, start conversion | |
TBCCR1 = Tpwm*(AD0*0.7 + 4095*0.05)/4095; | |
if(TB_CM > Tpwm1){ | |
TB_CM = 0; | |
P4OUT ^= BLUE_LED; | |
} | |
} | |
P4OUT ^= GREEN_LED; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment