?? davinci_evm.c
字號:
str[2] = 0;
for(x = 0; x < 2; x++)
{
if( str[x] > 9 )
{
str[x] += 0x37;
}
else
{
str[x] += 0x30;
}
}
txStr(str);
}
//
// UART Function - transmit word data as ASCII characters
//
void txWord(unsigned int word)
{
txByte(word >> 8);
txByte(word & 0x00ff);
}
#endif /* FET_UART_DEBUG */
//
// Main Program Loop
//
void main(void)
{
unsigned char P2State;
unsigned char P3State;
unsigned char stateChange;
// Initialize System
sysInit();
#ifdef FET_UART_DEBUG
txStr("\x1b[2JDaVinci ");
#endif
prevP2State = (P2IN & P2_MASK); // Get current P2 state
prevP3State = (P3IN & P3_MASK); // Get current P3 state
// Main Program Loop
// Enable global interrupts
_BIS_SR(GIE); // MW
for(;;)
{
if( !Event )
{
// Enter low-power mode 0 with interrupts enabled
// MW _BIS_SR(LPM0_bits + GIE);
_NOP();
}
if( Event & EVENT_TIMEOUT ) // Check for timeout event
{
Event &= ~EVENT_TIMEOUT; // Clear event flag
// MW P1OUT &= ~0x02; // Set P1.1 to low
// MW FLAG P1OUT |= 0x02; // Set P1.1 high (deassert DaVinci interrupt)
// MW FLAG i2cIndex = 0; // Reset buffer index
// MW FLAG resetSWI2C(); // Reset I2C
resetIR(); // Reset IR
// MW FLAG IE2 |= URXIE0; // Enable USART0 RX interrupt
// MW RTC IE1 |= WDTIE; // Enable WDT interrupt
}
if( Event & EVENT_RTC ) // Check for RTC event
{
Event &= ~EVENT_RTC; // Clear event flag
incrementSeconds(); // Increment seconds
}
if( Event & EVENT_CHK_PINS ) // Check I/O pin state event
{
Event &= ~EVENT_CHK_PINS; // Clear event flag
stateChange = 0; // Clear flag
P2State = (P2IN & P2_MASK); // Read P2
P3State = (P3IN & P3_MASK); // Read P3
if( P2State != prevP2State ) // Check for P2 input state change
{
stateChange = 1; // Set state change flag
}
if( P3State != prevP3State ) // Check for P3 input state change
{
stateChange = 1; // Set state change flag
}
prevP2State = (P2State & P2_MASK); // Save current P2 input state
prevP3State = (P3State & P3_MASK); // Save current P3 input state
if( stateChange )
{
EventFlag = EVENT_STATE_CHG; // Set event flag
interruptMaster(); // Interrupt master I2C device
}
}
if( Event & EVENT_IR ) // Check for IR event flag
{
Event &= ~EVENT_IR; // Clear event flag
#ifdef IR_LED_DEBUG
P2OUT ^= 0x18; // Toggle LEDs
#endif
#ifdef FET_UART_DEBUG
txStr("\r\n");
txStr("Raw: 0x");
txWord(IRData & 0x3FFF);
txStr(", Addr: 0x");
txWord((IRData & 0x07C0) >> 6);
txStr(", Data: 0x");
txWord(IRData & 0x003F);
#endif /* FET_UART_DEBUG */
// MW i2cIndex = 0; // Reset buffer index
// MW resetSWI2C(); // Reset I2C
EventFlag = EVENT_IR_DATA; // Set event flag
interruptMaster(); // Interrupt master I2C device
}
}
}
//
// UART0 RX Interrupt Service Routine
//
#pragma vector=UART0RX_VECTOR
__interrupt void usart0_Rx_ISR(void)
{
while( !(IFG2 & UTXIFG0) ){} // Is USART0 TX buffer ready?
TXBUF0 = RXBUF0 + 1; // Increment RXBUF0, copy to TXBUF0
}
//
// Watchdog Timer (WDT) Interrupt Service Routine
//
// Interrupts once every 250 msec
//
#pragma vector=WDT_VECTOR
__interrupt void wdt_ISR(void)
{
static unsigned char cnt;
if( (cnt++ & 0x3) == 0 )
{
Event |= EVENT_RTC; // Set RTC event flag
}
Event |= EVENT_CHK_PINS; // Set check pins event
// MW LPM0_EXIT; // Exit LPM0
}
//
// Timer_A0 Interrupt Service Routine
//
#pragma vector=TIMERA0_VECTOR
__interrupt void timerA0_ISR(void)
{
// MW LPM0_EXIT; // Exit LPM0
}
//
// Timer_A Interrupt Vector (TAIV) Handler
//
#pragma vector=TIMERA1_VECTOR
__interrupt void timerA_ISR(void)
{
switch( TAIV )
{
case 2: // TA1 CCIFG
if( TACCTL1 & CAP ) // Is TA1 in capture mode?
{ // Yes, capture mode
TACCTL2 = 0; // Disable TA2
if( IRBit == 14 )
{
// MW FLAG SDA_IE &= ~SDA_PIN; // Disable SDA interrupt
// MW FLAG SCL_IE &= ~SCL_PIN; // Disable SCL interrupt
// MW FLAG IE2 = 0; // Disable interrupts
// MW FLAG IE1 = 0; // Disable interrupts
}
// Configure TA1 to measure 3/4 IR bit time
TACCR1 += BIT_75; // Add 3/4 IR bit time
TACCTL1 = CCIE; // Compare mode, enable interrupt
}
else // No, compare mode
{
if( --IRBit == 0 ) // Dec IR bit cnt, is 0?
{ // Yes, IR bit cnt = 0
TACCTL1 = 0; // Stop TA1
TACCTL2 = 0; // Stop TA2
IRBuff[irWriteIndex++] = IRData; // Save IR data
irWriteIndex &= (IRBUFFLEN-1); // Reset index
Event |= EVENT_IR; // Set IR event flag
// MW LPM0_EXIT; // Exit LPM0
}
else // No, IR bit cnt > 0
{
IRData <<= 1; // Left shift IRData
if( TACCTL1 & SCCI ) // Is SCCI bit set?
{ // Yes, SCCI is set
IRData |= 1; // Set LSB to 1
}
// Set TA2 to generate a timeout if next IR packet edge
// is NOT captured within 1/2 I/R bit time
TACCR2 = TACCR1 + BIT_50; // Add 1/2 IR bit time to TACCR1,
// load into TACCR2
TACCTL2 = CCIE; // Compare mode, enable interrupt
// Configure TA1 to capture IR data packet edge
TACCTL1 = CAP + CM1 + CM0 + SCS + CCIE;
// Capture mode, CCI1A input,
// both edges, synchronous,
// enable interrupt
}
}
break;
case 4: // TA2 CCIFG
TACCTL1 = 0; // Timeout, stop TA1
TACCTL2 = 0; // Timeout, stop TA2
resetIR(); // MW FLAG
// MW FLAG Event |= EVENT_TIMEOUT; // Set timeout event flag
// MW P3OUT ^= 0x08; // DEBUG
// MW LPM0_EXIT; // Exit LPM0
break;
case 10: // TA IFG
break;
}
}
//
// Read message from I2C Master
//
// I2C Message Format:
// Required Byte 1 = Msg Length (includes length and msg ID bytes)
// Required Byte 2 = Msg Identifier
// Optional Byte 3 = Data byte 0
// .
// .
// .
// Optional Byte N = Data byte N-1
//
// I2C message length = number of data bytes + 2
//
// +--------+--------+--------+-----+----------+
// | Length | Msg Id | Data 0 | ... | Data N-1 |
// +--------+--------+--------+-----+----------+
//
void i2cReadMsg(unsigned char i2cData)
{
static unsigned char i2cLen;
I2CRxBuff[i2cIndex++] = i2cData; // Save I2C data
if( i2cIndex == 1 )
{
i2cLen = i2cData; // First byte contains msg length
}
else if( i2cLen == i2cIndex ) // Msg length includes Length and
{ // Msg Id bytes along with data
// Process I2C message
(i2cMsgTbl[I2CRxBuff[1]])();
return;
}
i2cIndex &= (I2CMSGLEN-1); // Prevent buffer overflow
}
//
// Get Real-Time Clock (RTC) Parameters
//
void getRTC(void)
{
I2CTxBuff[0] = 9; // Set length
I2CTxBuff[1] = GET_RTC_PARAMS; // Set msg ID
I2CTxBuff[2] = (year & 0x00ff); // Set year LSB
I2CTxBuff[3] = (year >> 8); // Set year MSB
I2CTxBuff[4] = month; // Set month
I2CTxBuff[5] = day; // Set day
I2CTxBuff[6] = hour; // Set hour
I2CTxBuff[7] = minute; // Set minute
I2CTxBuff[8] = second; // Set second
}
//
// Set RTC Parameters
//
void setRTC(void)
{
year = I2CRxBuff[3]; // Get year MSB
year <<= 8;
year |= I2CRxBuff[2]; // Get year LSB
month = I2CRxBuff[4]; // Get month
day = I2CRxBuff[5]; // Get day
hour = I2CRxBuff[6]; // Get hour
minute = I2CRxBuff[7]; // Get minute
second = I2CRxBuff[8]; // Get second
}
//
// Get IR Data
//
void getIRData(void)
{
unsigned char index = 2;
while( index < (I2CTXBUFFLEN-2) )
{
if( irReadIndex == irWriteIndex )
{
break;
}
I2CTxBuff[index++] = (IRBuff[irReadIndex] & 0x00ff);
I2CTxBuff[index++] = (IRBuff[irReadIndex++] >> 8);
irReadIndex &= (IRBUFFLEN-1); // Reset index
}
I2CTxBuff[0] = index; // Set msg length
I2CTxBuff[1] = SEND_IR_DATA; // Set msg ID
}
//
// Get Last IR Value
//
void getIRVal(void)
{
unsigned char index;
if( irWriteIndex == 0 )
{
index = IRBUFFLEN-1;
}
else
{
index = irWriteIndex - 1;
}
I2CTxBuff[0] = 4; // Set msg length
I2CTxBuff[1] = SEND_IR_DATA; // Set msg ID
I2CTxBuff[2] = (IRBuff[index] & 0x00ff);
I2CTxBuff[3] = (IRBuff[index] >> 8);
}
//
// Get port pin input signal state
//
void getInputState(void)
{
I2CTxBuff[0] = 4; // Set length
I2CTxBuff[1] = GET_INPUT_STATE; // Set msg ID
I2CTxBuff[2] = (P2IN & P2_MASK); // Set P2 input state
I2CTxBuff[3] = (P3IN & P3_MASK); // Set P3 input state
}
//
// Get event
//
void getEvent(void)
{
I2CTxBuff[0] = 3; // Set length
I2CTxBuff[1] = GET_EVENT; // Set msg ID
I2CTxBuff[2] = EventFlag; // Get event
EventFlag = 0; // Reset event flag
}
//
// Set port pin output signal state
//
void setOutputState(void)
{
// P2OUT = I2CRxBuff[2]; // Set P2 output state
P3OUT = (I2CRxBuff[3] & 0x9) | (P3OUT & ~0x9); // Set P3 output state (masked for P3.0 and P3.3)
}
//
// Interrupt Master I2C Device
//
void interruptMaster(void)
{
int i;
TACCR2 = TAR + INTR_TIMEOUT; // Set timeout
TACCTL2 = CCIE; // Compare mode, enable interrupt
// MW P1OUT |= 0x02; // Set P1.1 output high
P1OUT &= ~0x02; // Pull DaVinci interrupt low
for (i = 0; i < 2; i++);
P1OUT |= 0x02; // Pull DaVinci interrupt high
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -