㈠ 求一份使用MSP4302553單片機做的樣例代碼,是調用光電門測速的,越快越好
//******************************************************************************
// HW UART(J4)!!!!!! 特別注意,板子上J4有2個跳線要豎放,設為HW UART模式
// ADC檢測,並通過串口發送到PC
// MSP430G2533 Demo - USCI_A0, 9600 UART Echo ISR, DCO SMCLK
// http://jiwm.blog.163.com
// Baud rate divider with 1MHz = 1MHz/9600 = ~104.2
// ACLK = n/a, MCLK = SMCLK = CALxxx_1MHZ = 1MHz
//
// MSP430G2xx3
// -----------------
// /|\| XIN|-
// | | |
// --|RST XOUT|-
// | |
// | P1.2/UCA0TXD|------------>
// | | 9600 - 8N1
// | P1.1/UCA0RXD|<------------
//
// IAR Embedded Workbench Version: 5.40
//******************************************************************************
#include "msp430g2553.h"
long temp;
long IntDegF;
long IntDegC;
unsigned int i;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
ADC10CTL1 = INCH_10 + ADC10DIV_3; // Temp Sensor ADC10CLK/4
ADC10CTL0 = SREF_1 + ADC10SHT_3 + REFON + ADC10ON + ADC10IE; //The ADC10OSC, generated internally,
//is in the 5-MHz range, but varies with indivial devices, supply voltage, and temperature. See the device-specific data sheet for the ADC10OSC specification.
DCOCTL = 0x00; // Set DCO 1MHz
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
P1SEL = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
P1SEL2 = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
UCA0CTL1 |= UCSSEL_2; // USCI clock source=SMCLK
UCA0BR0 = 104; // 1MHz 9600 Refer to Page 435 of X2xx Guide
UCA0BR1 = 0; // 1MHz 9600
UCA0MCTL = UCBRS0; // Molation UCBRSx = 1
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
__enable_interrupt(); // Enable interrupts.
IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt
//__bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled
for(;;)
{ ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
}
}
#pragma vector=USCIAB0RX_VECTOR //串口中斷
__interrupt void USCI0RX_ISR(void)
{
if ((UCA0RXBUF == 'c')||(UCA0RXBUF == 'C')) // 'C' or 'c' received?
{
//IE2 |= UCA0TXIE; // Enable USCI_A0 TX interrupt
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
{
UCA0TXBUF=((IntDegC/10240)+48); //10位數
}
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
{
UCA0TXBUF=((IntDegC/1024)%10+48); //個位數
}
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
{
UCA0TXBUF=46; //.
}
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
{
UCA0TXBUF=((IntDegC*10/1024%10)+48); //小數
}
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
{
UCA0TXBUF=13; //換行
}
}
else
{
//IE2 |= UCA0TXIE; // Enable USCI_A0 TX interrupt
UCA0TXBUF = 13; //換行
}
}
// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR (void)
{
temp = ADC10MEM;
//IntDegC = ((temp - 673) * 423) / 1024; //-278~144 ,精度降低了
IntDegC = ((temp - 673) * 423); //改進後的
}
㈡ 單片機如何實現ucb通信功能
有專用的USB轉換晶元,用單片機和它連接,由這個晶元和USB設備通信即可。比如CH374、PDIUSBD12等。
如果電路板太小,建議用自帶USB功能的單片機。51系列中,C8051F的單片機有自帶USB模塊的。PIC、AVR的單片機也有類似功能,另外,網上也有關於直接用PIC或AVR單片機IO口進行軟體模擬USB通信的程序。(用51實現軟體模擬可能復雜點)。
㈢ 如何實現VC與單片機RS-232通訊控制
有c語言基礎開發RS-232,用c++,MFC開發,(開發com使用簡單c++語句就可以了,不難跟你用的c語言很相似)vb更容易實現同樣功能,但是vc是大勢所趨,看一下龔建偉的RS232串口設計,非常簡單,最主要就是電話控制項(串口的控制項),書上面源代碼有,買一本,串口助手實現的源碼也有,也很容易,一個禮拜就看完,書上從最簡單的用PC控制跑馬燈講解,放寒假可以練習,平時上課時間久別去弄了,高考要緊,想研究單片機或者更復雜的嵌入式上面,時間不夠用,在沒人帶你的情況下要走的路也很曲折,在應試教育的情況下,先把高考這關過了。
㈣ MSP430系列單片機跟藍牙串口模塊相連的是哪個介面啊
msp430的15、16系列的 還有2系列的都有I2c介面 程序源代碼 ti網站上有 直接下就可以 每一種都有相應的code library。
貼一個2274的i2c代碼吧 希望對你有用
include "msp430x22x4.h"
unsigned int RxByteCtr;
unsigned int RxWord;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P1DIR |= 0x01; // P1.0 output
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 = 12; // fSCL = SMCLK/12 = ~100kHz
UCB0BR1 = 0;
UCB0I2CSA = 0x4e; // Set slave address
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
IE2 |= UCB0RXIE; // Enable RX interrupt
TACTL = TASSEL_2 + MC_2; // SMCLK, contmode
while (1)
{
RxByteCtr = 2; // Load RX byte counter
UCB0CTL1 |= UCTXSTT; // I2C start condition
__bis_SR_register(CPUOFF + GIE); // Enter LPM0, enable interrupts
// Remain in LPM0 until all data
// is RX'd
if (RxWord < 0x1d00) // >28C?
P1OUT &= ~0x01; // No, P1.0 = 0
else
P1OUT |= 0x01; // Yes, P1.0 = 1
__disable_interrupt();
TACCTL0 |= CCIE; // TACCR0 interrupt enabled
__bis_SR_register(CPUOFF + GIE); // Enter LPM0, enable interrupts
// Remain in LPM0 until TACCR0
// interrupt occurs
TACCTL0 &= ~CCIE; // TACCR0 interrupt disabled
}
}
#pragma vector = TIMERA0_VECTOR
__interrupt void TA0_ISR(void)
{
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0
}
// The USCIAB0TX_ISR is structured such that it can be used to receive any
// 2+ number of bytes by pre-loading RxByteCtr with the byte count.
#pragma vector = USCIAB0TX_VECTOR
__interrupt void USCIAB0TX_ISR(void)
{
RxByteCtr--; // Decrement RX byte counter
if (RxByteCtr)
{
RxWord = (unsigned int)UCB0RXBUF << 8; // Get received byte
if (RxByteCtr == 1) // Only one byte left?
UCB0CTL1 |= UCTXSTP; // Generate I2C stop condition
}
else
{
RxWord |= UCB0RXBUF; // Get final received byte,
// Combine MSB and LSB
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0
}
}