/* * DMOD: real(kind=16) - pass by address * 128-bit float * Remainder function - use X - INT(X/Y) * Y if Y > zero. * else... */ _f_real16 _DMOD_( _f_real16 *x, _f_real16 *y) { _f_real16 _DINT(_f_real16 x); _f_real16 a = *x; _f_real16 b = *y; _f_real16 c = a/b; return (a - b * ((_f_real16) _DINT(c))); }
/* * DMOD: real(kind=16) - pass by value * 128-bit float * Remainder function - use X - INT(X/Y) * Y if Y > zero. * else... */ _f_real16 _DMOD( _f_real16 x, _f_real16 y) { _f_real16 _DINT(_f_real16 x); _f_real16 a = x; _f_real16 b = y; _f_real16 c = a/b; return (a - b * ((_f_real16) _DINT(c))); }
/* * DNINT: Nearest whole number in real(kind=16) * 128-bit float * - pass by value */ _f_real16 _DNINT(_f_real16 x) { _f_real16 _DINT(_f_real16 x); _f_real16 y = x; if (y < 0.0) y -= 0.5; else y += 0.5; return ((_f_real16) _DINT(y)); }
void switch_to_normal_mode(void) { /* Switch to full speed, full power mode */ meter_status |= STATUS_PHASE_VOLTAGE_OK; set_normal_indicator(); #if defined(__MSP430_HAS_TA3__) /* Disable the TIMER_A0 interrupt */ TACTL = 0; TACCTL0 = 0; #endif _DINT(); init_analog_front_end_normal(); samples_per_second = SAMPLES_PER_10_SECONDS/10; _EINT(); enable_ir_receiver(); operating_mode = OPERATING_MODE_NORMAL; }
void init_hardware() { _DINT(); WDTCTL = WDTPW + WDTHOLD; // + WDTNMI + WDTNMIES; // WDT off NMI hi/lo //IE1 |= NMIIE; // Enable NMI //IFG1 |= NMIIFG; init_timer(); init_uarts(); P1OUT = 0x10; P1DIR = 0x01|0x10; #ifndef GOODFET VJTAGDIR |= VJTAG; VJTAGOUT |= VJTAG; #endif /* P1OUT |= 10;*/ /* P1DIR |= 0x10;*/ /* P1OUT |= 10;*/ _EINT(); }
TIMERID TakeTimer(TIMERID RequiredTimer,unsigned int Interval,unsigned char mode,void (*f)()) //if RequiredTimer = 0, timer is selected by ID { unsigned int i,*pTBCCR,*pTBCCTL; if (RequiredTimer == RANDOM){ for (i=TB2 ; i<TIMERB_AMOUNT ; i++){ //TB0 and TB1 is usd for ADC; RequiredTimer = i; if (TimerB_Array[i].Timer_Function_Pointer == 0) break; } TimerB_Error |= ERROR_CREAT_TIMER; }else{ if (TimerB_Array[i].Timer_Function_Pointer != 0){ TimerB_Error |= ERROR_CREAT_TIMER; return NO_SERV; } } _DINT(); TimerB_Array[RequiredTimer].Period = Interval; TimerB_Array[RequiredTimer].Operated_Mode = mode; TimerB_Array[RequiredTimer].Timer_Function_Pointer = f; _EINT(); pTBCCR = (unsigned int *)(&TBCCR0 + RequiredTimer); pTBCCTL = (unsigned int *)(&TBCCTL0 + RequiredTimer); *pTBCCR = TBR + Interval; *pTBCCTL = CCIE; return RequiredTimer; }
/**************************************************************************** * 名 称:AD7708Init * 功 能:初始化AD7708 * 入口参数:chop:斩波允许标志 为1,斩波允许,0则不允许 * 出口参数:无 * 说 明: 完成AD7708的初始化工作 ****************************************************************************/ void AD7708Init(char chop) { P3DIR|=BIT0; P3OUT&=~BIT0; //CS选中 //主机模式,115200,8位数据位,三线模式,时钟模式1(具体见spi.c) SpiMasterInit(115200,8,3,1); //时钟不是准确的115200(具体见spi.c) _EINT(); //开中断,spi读写程序要需要中断 char filter; adccon = 0x0f; if(chop == 0) { filter = 0x03; //滤波寄存器设为最小值,可以更改 mode = 0x91; //斩波禁止,10通道,无缓冲,空闲模式 } else { filter = 0x0D; //滤波寄存器设为最小值,可以更改 mode = 0x11; //斩波启用,10通道,无缓冲,空闲模式 } AD7708WriteRegister(0x07,0x00); //IO寄存器,不用== AD7708WriteRegister(0x03,filter); //滤波寄存器 AD7708WriteRegister(0x02,0x0F); //ADC控制寄存器,0通道,单极性 AD7708WriteRegister(0x01,mode); //模式寄存器 if(chop == 0) for(int i = 0; i<5;i++) { //校准,因只有5个失调寄存器,多的就会覆盖之前的,只校准5个即可 AD7708Cal(5); } _DINT(); }
void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT P3SEL |= 0x30; // P3.4,5 = USART0 TXD/RXD BCSCTL1 |= DIVA_3; // ACLK= LFXT1CLK/8 Set_DCO(); // Set DCO ME1 |= UTXE0 + URXE0; // Enabled USART0 TXD/RXD UCTL0 |= CHAR; // 8-bit character, SWRST = 1 UTCTL0 |= SSEL1 + SSEL0 + URXSE; // UCLK = SMCLK, start edge detect UBR00 = 0xD0; // 9600 from 2Mhz UBR10 = 0x00; UMCTL0 = 0x00; // No modulation UCTL0 &= ~SWRST; // Initialize USART state machine IE1 |= URXIE0; // Enable USART0 RX interrupt for (;;) { while (!(UTCTL0 & TXEPT)); // Confirm no TXing before --> LPM3 _DINT(); // Disable interrupts for flag test _NOP(); if (!(UTCTL0 & SSEL0)) _BIS_SR(LPM0_bits + GIE); // RX'ing char, enter LPM0, int's active else _BIS_SR(LPM3_bits + GIE); // Enter LPM3, int's active } }
void uart_report(char *line) { to_print = line; #if 0 char *buf = report; unsigned int cnt1_A; _DINT(); cnt1_A = cnt1_a; cnt1_a = 0; _EINT(); buf = cat_ul(buf, jiffies); buf = cat_str(buf, "\t"); buf = cat_ul(buf, cnt1_A); buf = cat_str(buf, "\t"); buf = cat_ul(buf, cnt1_b); # if 0 buf = cat_ul(buf, curr_state); buf = cat_str(buf, "\t"); buf = cat_ul(buf, temp_up); buf = cat_str(buf, "\t"); buf = cat_ul(buf, temp_bottom); buf = cat_str(buf, "\t"); buf = cat_ul(buf, temp_ctl); #endif buf = cat_str(buf, "\r\n"); *buf = 0; #endif i = 0; IE2 |= UCA0TXIE; }
void switch_to_limp_mode(void) { /* Switch to minimum consumption, current measurement only mode */ meter_status &= ~(STATUS_REVERSED | STATUS_PHASE_VOLTAGE_OK); clr_normal_indicator(); clr_reverse_current_indicator(); #if defined(__MSP430_HAS_TA3__) /* Enable the TIMER_A0 interrupt */ TACTL = TACLR | MC_1 | TASSEL_1; TACCTL0 = CCIE; #endif _DINT(); init_analog_front_end_limp(); samples_per_second = LIMP_SAMPLES_PER_10_SECONDS/10; disable_ir_receiver(); _EINT(); operating_mode = OPERATING_MODE_LIMP; }
/*---------------------------------------------------------------------------*/ void flash_setup(void) { /* Disable all interrupts. */ /* Clear interrupt flag1. */ IFG1 = 0; /* Stop watchdog. */ WDTCTL = 0x5A80; /* DCO(SMCLK) is 2,4576MHz, /6 = 409600 Hz select SMCLK for flash timing, divider 5+1 */ FCTL2 = 0xA5C5; /* disable all interrupts to protect CPU during programming from system crash */ _DINT(); /* disable all NMI-Interrupt sources */ ie1 = IE1; ie2 = IE2; IE1 = 0x00; IE2 = 0x00; }
void EEPROM_AckPolling(void) // Description: // Acknowledge Polling. The EEPROM will not acknowledge if a write cycle is // in progress. It can be used to determine when a write cycle is completed. { unsigned int count; while (I2CDCTL&I2CBB); // wait until I2C module has finished all operations U0CTL &= ~I2CEN; // clear I2CEN bit => necessary to re-configure I2C module I2CTCTL |= I2CRM; // transmission is software controlled U0CTL |= I2CEN; // enable I2C module I2CIFG = NACKIFG; // set NACKIFG while (NACKIFG & I2CIFG) { I2CIFG=0x00; // clear I2C interrupt flags U0CTL |= MST; // define Master Mode I2CTCTL |= I2CTRX; // I2CTRX=1 => Transmit Mode (R/W bit = 0) _DINT(); // **** disable interrupts. following code lines should not be interrupted (time critical!!!) I2CTCTL |= I2CSTT; // start condition is generated while (I2CTCTL&I2CSTT); // wait till I2CSTT bit was cleared I2CTCTL |= I2CSTP; // stop condition is generated after slave address was sent // => I2C communication is started _EINT(); //**** enable interrupts again while (I2CDCTL&I2CBB); // wait till stop bit is reset } U0CTL &= ~I2CEN; // clear I2CEN bit => necessary to re-configure I2C module I2CTCTL &= ~I2CRM; // transmission is by the I2C module U0CTL |= I2CEN; // enable I2C module return; }
/*---------------------------------------------------------------------------*/ void flash_setup(void) { /* disable all interrupts to protect CPU during programming from system crash */ _DINT(); /* Clear interrupt flag1. */ /* IFG1 = 0; */ /* The IFG1 = 0; statement locks up contikimac - not sure if this statement needs to be here at all. I've removed it for now, since it seems to work, but leave this little note here in case someone stumbles over this code at some point. */ /* Stop watchdog. */ watchdog_stop(); /* DCO(SMCLK) is 2,4576MHz, /6 = 409600 Hz select SMCLK for flash timing, divider 5+1 */ FCTL2 = 0xA5C5; /* disable all NMI-Interrupt sources */ ie1 = IE1; ie2 = IE2; IE1 = 0x00; IE2 = 0x00; }
void I2CrequestFrom(char address, int numOfBytes) { Bytecount = 0; RXByteCtr = numOfBytes; numRXbytesSent = 0; // Use as counter in @I2CRead() // Setup RX _DINT(); RX = 1; IE2 &= ~UCB0TXIE; UCB0CTL1 |= UCSWRST; // Enable SW reset UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset UCB0BR0 = 12; // fSCL = SMCLK/12 = ~100kHz UCB0BR1 = 0; UCB0I2CSA = SLV_Addr; // Slave Address is set with I2CBeginTransmission UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation IE2 |= UCB0RXIE; // Enable RX interrupt // End Setup RX while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent UCB0CTL1 |= UCTXSTT; // I2C start condition __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts }
//*************************************************************************************** // * // void MenuMessage(): 菜单消息管理 * // * //*************************************************************************************** void MenuMessage(unsigned char Message) { _DINT() ; switch(Message) { case Update: Redraw_Menu(First_Index,Active_Index) ; break ; case Up: if(Active_Index==0) break ; else Active_Index-- ; if(Active_Index<First_Index) if(First_Index>0) First_Index -- ; UpDate_Menu(First_Index,Active_Index) ; break ; case Down: if(Active_Index==Menu.MenuConfig[0]-1) break ; else { Active_Index++ ; if(Active_Index>First_Index+6) First_Index++ ; UpDate_Menu(First_Index,Active_Index) ; } break ; case Left: if(First_Index-7>0) { First_Index -= 7 ; Active_Index = First_Index + 6 ; Redraw_Menu(First_Index,Active_Index) ; } else if(First_Index!=0) { First_Index = 0 ; Active_Index = First_Index ; Redraw_Menu(First_Index,Active_Index) ; } break ; case Right: if(First_Index+Dis_Menu_Num<Menu.MenuConfig[0]) { First_Index += Dis_Menu_Num ; Active_Index = First_Index ; if(First_Index+7>Menu.MenuConfig[0]) { Active_Index = Menu.MenuConfig[0]-1 ; First_Index = Active_Index-6 ; } Redraw_Menu(First_Index,Active_Index) ; } break ; } MenuUpdated = 0x00 ; _EINT() ; }
unsigned char rcv_usart(void) { _DINT(); while(!IFG1&URXIFG0); return U0RXBUF; // while(!IFG2&UTXIFG1); // return U1RXBUF; }
void enable_nmi() { _DINT(); WDTCTL = WDTPW + WDTHOLD + WDTNMI + WDTNMIES; // WDT off NMI hi/lo IE1 |= NMIIE; // Enable NMI IFG1 |= NMIIFG; _EINT(); }
void timer_set_alarm(char hours, int secs ) { _DINT(); timer.stat = TIMER_RUN; timer.hours = hours; timer.secs = secs; _EINT(); }
static void fifo_wr(fifo_t * fp, char c) { fp->b[fp->i] = c; _DINT(); // only need to disable tx irq for this stream fifo_advance(&fp->i); fp->count++; _EINT(); }
static uint8_t fifo_rd(fifo_t * fp) // only called if count>0 { uint8_t c = fp->b[fp->o]; _DINT(); // only need to disable tx irq for this stream fifo_advance(&fp->o); fp->count--; _EINT(); return c; }
//----------------------------------------------------------------------------- // Record audio data and store in Flash using the ADC12, DMA and MPY //----------------------------------------------------------------------------- void Record(void) { // power-up external hardware P1OUT |= 0x09; // LED#1 & audio input stage on P6OUT |= 0x04; // Disable charge pump snooze mode // setup modules ADC12IFG = 0x00; // ADC12CTL1 = SHS_3 + CONSEQ_2; // S&H TB.OUT1, rep. single chan ADC12CTL0 = ADC12ON + ENC; // ADC12 on, enabled TBCCR0 = SamplePrd; // Initialize TBCCR0 w/ sample prd TBCCR1 = SamplePrd - 20; // Trigger for ADC12 SC TBCCR2 = SamplePrd - 5; // Trigger for DMA1 TBCCTL1 = OUTMOD_7; // Reset OUT1 on EQU1, set on EQU0 DMA0SA = ADC12MEM0_; // Src address = ADC12 module DMA0DA = OP2_; // Dst address = multiplier DMA0SZ = 1; // Size in words DMACTL0 = DMA1TSEL_2 + DMA0TSEL_6; // DMA1=TBCCR2_IFG, DMA0=ADC12IFGx DMA0CTL = DMADT_4 + DMAEN; // Sng rpt, config DMA1SA = RESHI_; // Src address = multiplier DMA1DA = Memstart; // Dst address = Flash memory DMA1SZ = (Memend - Memstart); // Size in bytes DMA1CTL = DMADSTINCR_3 + DMASBDB + DMAIE + DMAEN; // Sng, config MPY = 0x0FFF; // MPY first operand // unlock and erase Flash memory FCTL3 = FWKEY; // Unlock Flash memory for write Erase(); // Call Flash erase subroutine FCTL1 = FWKEY + WRT; // Enable Flash write for recording // start recording and enter LPM P1OUT |= 0x01; // LED#1 on TBCTL = TBSSEL_2+ MC_1 + TBCLR ; // SMCLK, clear TBR, up mode _BIS_SR(LPM0_bits + GIE); // Enter LPM0 w/ interrups _DINT(); // Disable interrupts // deactivate Flash memory write access FCTL1 = FWKEY; // Disable Flash write FCTL3 = FWKEY + LOCK; // Lock Flash memory // power-down MSP430 modules DMA0CTL &= ~DMAEN; // Disable ADC12CTL1 &= ~CONSEQ_2; // Stop conversion immidiately ADC12CTL0 &= ~ENC; // Disable ADC12 conversion ADC12CTL0 = 0; // Switch off ADC12 & ref voltage TBCTL = 0; // Disable Timer_B // power-down external hardware P1OUT &= ~0x09; // Disable LED#1 & audio input stage P6OUT &= ~0x04; // Enable charge pump snooze mode }
void send_usart(unsigned char d) { _DINT(); U0TXBUF=d; while((IFG1&UTXIFG0)==0); IFG1&=~UTXIFG0; _EINT(); // U1TXBUF=d; // while(!IFG2&UTXIFG1); // IFG2&=~UTXIFG1; }
void Setup_UART() { _DINT(); IE2 &= ~UCB0RXIE; IE2 &= ~UCB0TXIE; UCA0CTL1 |= UCSSEL_2; // Use SMCLK UCA0BR0 = 104; // Set baud rate to 9600 with 1MHz clock (Data Sheet 15.3.13) UCA0BR1 = 0; // Set baud rate to 9600 with 1MHz clock UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1 UCA0CTL1 &= ~UCSWRST; // Initialize USCI state machine IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt }
//setup i2c void i2cActivate() { _DINT(); P3SEL |= 0x06; // Assign I2C pins to USCI_B0 UCB0CTL1 |= UCSWRST; // Enable SW reset UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset UCB0BR0 = 50; // fSCL = SMCLK/50 = ~80kHz for 4MHz DCO UCB0BR1 = 0; UCB0I2CSA = i2caddress; // set slave address UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation _BIS_SR(GIE); // General Interrupt Enable }
ComManager::ComManager() { // initialize route table routeTable[GCS] = GCS_ID; routeTable[AIRSHIP] = AIRSHIP_ID; routeTable[ROTORCRAFT] = ROTORCRAFT_ID; _DINT(); // disable interrupts until we go into the main loop InitTimer(); InitUsci(); }
void Setup_RX(void){ _DINT(); RX = 1; IE2 &= ~UCB0TXIE; // Disable TX interrupt UCB0CTL1 |= UCSWRST; // Enable SW reset UCB0CTL0 = UCMODE_3 + UCSYNC; // I2C Slave, synchronous mode UCB0I2COA = 0x48; // Own Address is 048h UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation UCB0I2CIE |= UCSTPIE + UCSTTIE; // Enable STT and STP interrupt IE2 |= UCB0RXIE; // Enable RX interrupt }
void AddISRMessage(ISRMessageQueue *IM,unsigned char PirorityValue,void (*F)(),unsigned char MSGType){ if (IM->Index>=MSGQueueSize){ AddErrorCode(ISRQueueFull); //OVERFLOW ISR QUEUE FULL //Reset(); } _DINT(); IM->MSGNode[IM->Index].PirorityValue=PirorityValue; IM->MSGNode[IM->Index].FunctionAddr=F; IM->MSGNode[IM->Index].MSGType=MSGType; IM->Index++; _EINT(); }
void main() { char addr[5]; char buf[32]; char status; WDTCTL = WDTHOLD | WDTPW; DCOCTL = CALDCO_1MHZ; BCSCTL1 = CALBC1_1MHZ; BCSCTL2 = DIVS_0; // SMCLK = DCOCLK/1 // SPI (USI) uses SMCLK, prefer SMCLK=DCO (no clock division) /* Initial values for nRF24L01+ library config variables */ rf_crc = RF24_EN_CRC | RF24_CRCO; // CRC enabled, 16-bit rf_addr_width = 5; rf_speed_power = RF24_SPEED_MIN | RF24_POWER_MAX; rf_channel = 0x4c; msprf24_init(); // All RX pipes closed by default msprf24_open_pipe(0, 1); // Open pipe#0 with Enhanced ShockBurst enabled for receiving Auto-ACKs msprf24_set_pipe_packetsize(0, 0); // Dynamic payload length enabled (size=0) // Transmit to 'rad01' (0x72 0x61 0x64 0x30 0x31) msprf24_standby(); addr[0] = 'r'; addr[1] = 'a'; addr[2] = 'd'; addr[3] = '0'; addr[4] = '1'; w_tx_addr(addr); w_rx_addr(0, addr); // Pipe 0 receives auto-ack's, autoacks are sent back to the TX addr so the PTX node // needs to listen to the TX addr on pipe#0 to receive them. buf[0] = '1'; buf[1] = '2'; buf[2] = '3'; buf[3] = '\0'; w_tx_payload(4, buf); msprf24_activate_tx(); LPM4; if (rf_irq & RF24_IRQ_FLAGGED) { msprf24_get_irq_reason(); // this updates rf_irq if (rf_irq & RF24_IRQ_TX) status = 1; if (rf_irq & RF24_IRQ_TXFAILED) status = 0; msprf24_irq_clear(RF24_IRQ_MASK); // Clear any/all of them } status += 1; // Do something cool with the 'status' variable // ??? // Profit! // Go to sleep forever: _DINT(); LPM4; }
void Setup_RX(unsigned char Add) { _DINT(); RX = 1; IE2 &= ~UCB0TXIE; // Disable TX interrupt UCB0CTL1 |= UCSWRST; // Enable SW reset UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset UCB0BR0 = 12; // fSCL = SMCLK/12 = ~100kHz UCB0BR1 = 0; UCB0I2CSA = Add; // Slave Address UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation IE2 |= UCB0RXIE; // Enable RX interrupt }
void Setup_i2c_RX(unsigned char addr){ _DINT(); RX = 1; IE2 &= ~UCB0TXIE; UCB0CTL1 |= UCSWRST; // Enable SW reset UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset UCB0BR0 = 20; // fSCL = DCO/2/5 = ~100Hz UCB0BR1 = 0; UCB0I2CSA = addr; // Slave Address is 048h UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation IE2 |= UCB0RXIE|UCB0TXIE; // Enable RX interrupt }